Bsoft  2.1.0
Bernard's software package
rwPIR.cpp
Go to the documentation of this file.
1 
9 #include "rwPIR.h"
10 #include "utilities.h"
11 #include <fstream>
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
29 int readPIR(Bstring& filename, Bmolgroup* molgroup)
30 {
31  if ( verbose & VERB_DEBUG )
32  cout << "DEBUG readPIR: filename=" << filename << endl;
33 
34  // Open seq file read only
35  ifstream fseq(filename.c_str());
36  if ( fseq.fail() ) {
37  cerr << "Error: File " << filename << " not opened!" << endl;
38  return -1;
39  }
40 
41  char aline[MAXLINELEN];
42  char item[40], seqname[MAXLINELEN];
43  size_t i;
44  int nid(0), nseq(0), nres;
45  int m(0), seq_flag(0);
46  Bmolecule* mol;
47 
48  char* seq = new char[MAXSEQLEN];
49 
50 // int gaps = 1; // Retain gaps
51 // if ( strstr(molgroup->select, "NOGAP") ) gaps = 0; // Omit gaps
52 
53  // Pass to read the data
54  nres = nid = nseq = 0;
55  while ( !fseq.eof() ) {
56  fseq.getline(aline, MAXLINELEN);
57  memcpy(item, aline, 5);
58  if ( seq_flag > 1 ) {
59  for ( i=0; i<strlen(aline); i++ ) {
60  if ( isalpha(aline[i]) ) {
61  seq[m] = toupper(aline[i]);
62  nres++;
63  m++;
64  }
65  if ( ( aline[i] == '-' ) || ( aline[i] == '.' ) ) {
66 // if ( gaps ) {
67  seq[m] = '-';
68  m++;
69 // }
70  }
71  if ( aline[i] == '*' ) {
72  mol = molecule_add(&molgroup->mol, seqname);
73  mol->seq = seq;
74  mol->nres = nres;
75  nres = 0;
76  seq_flag = 0;
77  memset(seq, 0, MAXSEQLEN);
78  m = 0;
79  nseq++;
80  }
81  }
82  }
83  if ( seq_flag == 1 ) seq_flag++;
84  if ( strstr(item, ">P1") != NULL ) {
85  sscanf(aline, ">P1;%s", seqname);
86  seq_flag = 1;
87  nid++;
88  }
89  }
90 
91  fseq.close();
92 
93  if ( nseq != nid ) {
94  cout << "Number of identifiers (" << nid << ") don't agree with number of sequences (" << nseq << ")" << endl;
95  return -1;
96  }
97 
98  delete[] seq;
99 
100  return nid;
101 }
102 
109 int writePIR(Bstring& filename, Bmolgroup* molgroup)
110 {
111  if ( verbose & VERB_DEBUG )
112  cout << "DEBUG writePIR: filename=" << filename << endl;
113 
114  ofstream fseq(filename.c_str());
115  if ( fseq.fail() ) {
116  cerr << "Error: File " << filename << " not opened!" << endl;
117  return -1;
118  }
119 
120  int i(0);
121  long j, k, n;
122  char aline[MAXLINELEN] = "";
123  Bmolecule* mol;
124  Bstring seq;
125 
126  for ( mol = molgroup->mol; mol; mol = mol->next ) {
127  if ( ( n = mol->seq.length() ) ) seq = mol->seq;
128  else if ( ( n = mol->naseq.length() ) ) seq = mol->naseq;
129  else n = 0;
130  fseq << ">P1;" << mol->id << endl;
131  fseq << mol->id << ", " << mol->nres << " bases, 0 checksum" << endl;
132  for ( j=0; j<n; j+=50 ) {
133  aline[0] = 0;
134  for ( k=j; k<j+50 && k<n; k+=10 ) {
135  strcat(aline, " ");
136  strncat(aline, &seq[k], 10);
137  }
138 // if ( k >= n ) strcat(aline, "*");
139  fseq << aline << endl;
140  }
141  fseq << "*" << endl;
142  i++;
143  }
144 
145  fseq.close();
146 
147  return i;
148 }
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstring
Definition: Bstring.h:29
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Bmolecule::nres
long nres
Definition: rwmolecule.h:111
rwPIR.h
Header file for reading and writing PIR sequence files.
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
readPIR
int readPIR(Bstring &filename, Bmolgroup *molgroup)
Reads PIR format sequence files.
Definition: rwPIR.cpp:29
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmolecule::naseq
Bstring naseq
Definition: rwmolecule.h:115
writePIR
int writePIR(Bstring &filename, Bmolgroup *molgroup)
Writes PIR format sequence files.
Definition: rwPIR.cpp:109
Bmolgroup
Definition: rwmolecule.h:170
verbose
int verbose
molecule_add
Bmolecule * molecule_add(Bmolecule **mol, char *name)
Adds a molecule to a linked list.
Definition: rwmolecule.cpp:76
MAXSEQLEN
#define MAXSEQLEN
Definition: rwmolecule.h:14
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110