| Bsoft 2.0.5 Bernard's software package

rwGenBank.cpp
Go to the documentation of this file.
1
9 #include "rwGenBank.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
33 int readGenBank(Bstring& filename, Bmolgroup *molgroup)
34 {
35 if ( verbose & VERB_DEBUG )
36 cout << "DEBUG readGenBank: filename=" << filename << endl;
37
38 // Open seq file read only
39 ifstream fseq(filename.c_str());
40 if ( fseq.fail() ) {
41 cerr << "Error: File " << filename << " not opened!" << endl;
42 return -1;
43 }
44
45 char aline[MAXLINELEN];
46 char item[40], seqname[MAXLINELEN];
47 size_t i;
48 int type(0); // Type of sequence: DNA=1, protein=2
49 int nid(0), nseq(0), nres(0);
50 int m(0), seq_flag(0);
51 Bmolecule* mol;
52
53 char* seq = new char[MAXSEQLEN];
54
55 // int gaps = 1; // Retain gaps
56 // if ( strstr(molgroup->select, "NOGAP") ) gaps = 0; // Omit gaps
57
58 // Pass to read the data
59 while ( !fseq.eof() ) {
60 fseq.getline(aline, MAXLINELEN);
61 if ( strncmp(aline, "LOCUS", 5) == 0 ) {
62 if ( strstr(aline, "bp") || strstr(aline, "DNA") ) type = 1;
63 else type = 2;
64 if ( verbose & VERB_DEBUG )
65 cout << "DEBUG readGenBank: type=" << type << endl;
66 }
67 memcpy(item, aline, 12);
68 if ( seq_flag > 1 ) {
69 if ( strncmp(item, "//", 2) == 0 ) {
70 mol = molecule_add(&molgroup->mol, seqname);
71 if ( type == 1 ) {
72 mol->naseq = seq;
73 mol->nbase = nres;
74 } else {
75 mol->seq = seq;
76 mol->nres = nres;
77 }
78 nres = 0;
79 seq_flag = 0;
80 memset(seq, 0, MAXSEQLEN);
81 m = 0;
82 nseq++;
83 } else for ( i=0; i<strlen(aline); i++ ) {
84 if ( isalpha(aline[i]) ) {
85 seq[m] = toupper(aline[i]);
86 nres++;
87 m++;
88 }
89 if ( ( aline[i] == '-' ) || ( aline[i] == '.' ) ) {
90 // if ( gaps ) {
91 seq[m] = '-';
92 m++;
93 // }
94 }
95 }
96 }
97 if ( strstr(item, "LOCUS") != NULL ) {
98 sscanf(aline, "%s %s", item, seqname);
99 seq_flag = 1;
100 nid++;
101 }
102 if ( strstr(item, "ORIGIN") != NULL ) seq_flag++;
103 }
104
105 fseq.close();
106
107 if ( nseq != nid ) {
108 cout << "Number of identifiers (" << nid << ") don't agree with number of sequences (" << nseq << ")" << endl;
109 return -1;
110 }
111
112 delete[] seq;
113
114 return nid;
115 }
116
123 int writeGenBank(Bstring& filename, Bmolgroup* molgroup)
124 {
125 if ( verbose & VERB_DEBUG )
126 cout << "DEBUG writeGenBank: filename=" << filename << endl;
127
128 ofstream fseq(filename.c_str());
129 if ( fseq.fail() ) {
130 cerr << "Error: File " << filename << " not opened!" << endl;
131 return -1;
132 }
133
134 int i(0);
135 long j, k, n;
136 char aline[MAXLINELEN] = "";
137 Bmolecule* mol;
138 Bstring seq;
139
140 for ( mol = molgroup->mol; mol; mol = mol->next ) {
141 if ( ( n = mol->seq.length() ) ) seq = mol->seq;
142 else if ( ( n = mol->naseq.length() ) ) seq = mol->naseq;
143 else n = 0;
144 fseq << "LOCUS " << mol->id << endl;
145 // fseq << mol->id << "," << mol->nres << " bases, 0 checksum" << endl;
146 fseq << "ORIGIN" << endl;
147 for ( j=0; j<n; j+=60 ) {
148 snprintf(aline, MAXLINELEN, "%9ld", j+1);
149 for ( k=j; k<j+60 && k<n; k+=10 ) {
150 strcat(aline, " ");
151 strncat(aline, &seq[k], 10);
152 }
153 fseq << aline << endl;
154 }
155 fseq << "//" << endl << endl;
156 i++;
157 }
158
159 fseq.close();
160
161 return i;
162 }
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Bmolecule::nres
long nres
Definition: rwmolecule.h:111
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bstring::length
long length() const
Definition: Bstring.cpp:263
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
writeGenBank
int writeGenBank(Bstring &filename, Bmolgroup *molgroup)
Writes GenBank format sequence files.
Definition: rwGenBank.cpp:123
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
Bmolgroup
Definition: rwmolecule.h:170
Bmolecule::nbase
long nbase
Definition: rwmolecule.h:112
MAXSEQLEN
#define MAXSEQLEN
Definition: rwmolecule.h:14
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
molecule_add
Bmolecule * molecule_add(Bmolecule **mol, char *name)
Adds a molecule to a linked list.
Definition: rwmolecule.cpp:76
Bmolecule::naseq
Bstring naseq
Definition: rwmolecule.h:115
verbose
int verbose
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule
Definition: rwmolecule.h:107
Bstring
Definition: Bstring.h:29
rwGenBank.h
Header file for reading and writing GenBank sequence files.
readGenBank
int readGenBank(Bstring &filename, Bmolgroup *molgroup)
Reads GenBank format sequence files.
Definition: rwGenBank.cpp:33