Bsoft  2.1.0
Bernard's software package
rwFASTA.cpp
Go to the documentation of this file.
1 
9 #include "rwFASTA.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 
28 int readFASTA(Bstring& filename, Bmolgroup* molgroup)
29 {
30  if ( verbose & VERB_DEBUG )
31  cout << "DEBUG readFASTA: filename=" << filename << endl;
32 
33  // Open seq file read only
34  ifstream fseq(filename.c_str());
35  if ( fseq.fail() ) {
36  cerr << "Error: File " << filename << " not opened!" << endl;
37  return -1;
38  }
39 
40  char aline[MAXLINELEN];
41  long i, sl, nres, n(0);
42  long m(0), seq_flag(0);
43  Bmolecule* mol = NULL;
44 
45  char* seq = new char[MAXSEQLEN];
46 
47  nres = 0;
48  while ( !fseq.eof() ) {
49  fseq.getline(aline, MAXLINELEN);
50  sl = strlen(aline);
51  if ( sl > 0 && aline[sl-1] == '\n' ) { // Remove trailing newline
52  sl--;
53  aline[sl] = 0;
54  }
55  if ( seq_flag ) {
56  if ( aline[0] == '>' || sl < 1 ) {
57  mol->seq = seq;
58  mol->nres = nres;
59  seq_flag = 0;
60  } else {
61  for ( i=0; i<sl; i++ ) {
62  if ( isalpha(aline[i]) ) {
63  seq[m] = toupper(aline[i]);
64  nres++;
65  m++;
66  }
67  if ( ( aline[i] == '-' ) || ( aline[i] == '.' ) ) {
68  seq[m] = '-';
69  m++;
70  }
71  }
72  }
73  }
74  if ( aline[0] == '>' ) {
75  seq_flag = 1;
76  mol = molecule_add(&molgroup->mol, &aline[1]);
77  n++;
78  nres = m = 0;
79  memset(seq, 0, MAXSEQLEN);
80  }
81  }
82  if ( mol->nres < 1 ) { // Clean up last sequence
83  mol->seq = seq;
84  mol->nres = nres;
85  }
86 
87  fseq.close();
88 
89  delete[] seq;
90 
91  return n;
92 }
93 
100 int writeFASTA(Bstring& filename, Bmolgroup* molgroup)
101 {
102  if ( verbose & VERB_DEBUG )
103  cout << "DEBUG writeFASTA: filename=" << filename << endl;
104 
105  ofstream fseq(filename.c_str());
106  if ( fseq.fail() ) {
107  cerr << "Error: File " << filename << " not opened!" << endl;
108  return -1;
109  }
110 
111  long i = 0;
112  long j, k, n;
113  char aline[MAXLINELEN] = "";
114  Bmolecule* mol;
115  Bstring seq;
116 
117  for ( mol = molgroup->mol; mol; mol = mol->next ) {
118  if ( ( n = mol->seq.length() ) ) seq = mol->seq;
119  else if ( ( n = mol->naseq.length() ) ) seq = mol->naseq;
120  else n = 0;
121  fseq << ">" << mol->id << endl;
122  for ( j=0; j<n; j+=80 ) {
123  k = 80;
124  if ( k > seq.length() - j ) k = seq.length() - j;
125  strncpy(aline, &seq[j], k);
126  aline[k] = '\n';
127  aline[k+1] = 0;
128  fseq << aline;
129  }
130  i++;
131  }
132 
133  fseq.close();
134 
135  return i;
136 }
writeFASTA
int writeFASTA(Bstring &filename, Bmolgroup *molgroup)
Writes FASTA format sequence files.
Definition: rwFASTA.cpp:100
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
verbose
int verbose
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
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
readFASTA
int readFASTA(Bstring &filename, Bmolgroup *molgroup)
Reads FASTA format sequence files.
Definition: rwFASTA.cpp:28
rwFASTA.h
Header file for reading and writing FASTA sequence files.
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmolecule::naseq
Bstring naseq
Definition: rwmolecule.h:115
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