Bsoft  2.1.0
Bernard's software package
rwEMBL.cpp
Go to the documentation of this file.
1 
9 #include "rwEMBL.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 readEMBL(Bstring& filename, Bmolgroup *molgroup)
30 {
31  if ( verbose & VERB_DEBUG )
32  cout << "DEBUG readEMBL: 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], tag[20], seqname[1000];
43  size_t i;
44  long nid(0), nseq(0), nres;
45  int n(0), m(0), seq_flag(0);
46  Bmolecule* mol;
47 
48  char* seq = new char[MAXSEQLEN];
49 
50  // Pass to read the data
51  nres = 0;
52  while ( !fseq.eof() ) {
53  fseq.getline(aline, MAXLINELEN);
54  if ( strncmp(aline,"ID",2) == 0 ) nid++;
55  if ( strncmp(aline,"SQ",2) == 0 ) nseq++;
56  memcpy(item, aline, 5);
57  if ( seq_flag && strstr(item,"//") != NULL ) {
58  mol = molecule_add(&molgroup->mol, seqname);
59  mol->seq = seq;
60  mol->nres = nres;
61  nres = 0;
62  seq_flag = 0;
63  memset(seq, 0, MAXSEQLEN);
64  m = 0;
65  n++;
66  }
67  if ( seq_flag )
68  for ( i=0; i<strlen(aline); i++ ) {
69  if ( isalpha(aline[i]) ) {
70  seq[m] = toupper(aline[i]);
71  nres++;
72  m++;
73  }
74  if ( ( aline[i] == '-' ) || ( aline[i] == '.' ) ) {
75  seq[m] = '-';
76  m++;
77  }
78  }
79  if ( strstr(item, "ID") != NULL ) {
80  sscanf(aline,"%s %s", tag, seqname);
81  }
82  if ( strstr(item,"SQ") != NULL ) {
83  seq_flag = 1;
84  }
85  }
86 
87  fseq.close();
88 
89  if ( nid != nseq ) {
90  cerr << "Error: Number of identifiers (" << nid << ") don't agree with number of sequences (" << nseq << ")!" << endl;
91  return -1;
92  }
93 
94  delete[] seq;
95 
96  return nseq;
97 }
98 
105 int writeEMBL(Bstring& filename, Bmolgroup* molgroup)
106 {
107  if ( verbose & VERB_DEBUG )
108  cout << "DEBUG writeEMBL: filename=" << filename << endl;
109 
110  ofstream fseq(filename.c_str());
111  if ( fseq.fail() ) {
112  cerr << "Error: File " << filename << " not opened!" << endl;
113  return -1;
114  }
115 
116  long i(0), j, k, n;
117  char aline[MAXLINELEN] = "";
118  Bmolecule* mol;
119  Bstring seq;
120 
121  for ( mol = molgroup->mol; mol; mol = mol->next ) {
122  if ( ( n = mol->seq.length() ) ) seq = mol->seq;
123  else if ( ( n = mol->naseq.length() ) ) seq = mol->naseq;
124  else n = 0;
125  fseq << "ID " << mol->id << endl;
126  fseq << "DE " << mol->id << ", " << n << " bases, 0 checksum" << endl;
127  fseq << "SQ " << n << " BP" << endl;
128  for ( j=0; j<n; j+=60 ) {
129  strcpy(aline, " ");
130  for ( k=j; k<j+60 && k<n; k+=10 ) {
131  strcat(aline, " ");
132  strncat(aline, &seq[k], 10);
133  }
134  fseq << aline << endl;
135  }
136  fseq << "//" << endl;
137  i++;
138  }
139 
140  fseq.close();
141 
142  return i;
143 }
144 
145 
146 
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
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
readEMBL
int readEMBL(Bstring &filename, Bmolgroup *molgroup)
Reads EMBL format sequence files.
Definition: rwEMBL.cpp:29
writeEMBL
int writeEMBL(Bstring &filename, Bmolgroup *molgroup)
Writes EMBL format sequence files.
Definition: rwEMBL.cpp:105
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
verbose
int verbose
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmolecule::naseq
Bstring naseq
Definition: rwmolecule.h:115
rwEMBL.h
Header file for reading and writing EMBL sequence files.
Bmolgroup
Definition: rwmolecule.h:170
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