Bsoft  2.1.0
Bernard's software package
rwClustal.cpp
Go to the documentation of this file.
1 
9 #include "rwClustal.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 readClustal(Bstring& filename, Bmolgroup *molgroup)
30 {
31  if ( verbose & VERB_DEBUG )
32  cout << "DEBUG readClustal: 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], seqname[MAXLINELEN], aseq[MAXLINELEN];
42  int m, nseq(0);
43  Bmolecule* mol = NULL;
44 
45  // Read the header line
46  fseq.getline(aline, MAXLINELEN);
47 
48  // Read the rest of the data
49  nseq = m = 0;
50  while ( !fseq.eof() ) {
51  fseq.getline(aline, MAXLINELEN);
52  seqname[0] = 0;
53  sscanf(aline, "%s %s", seqname, aseq);
54  if ( isalnum(aline[0]) && seqname[0] ) {
55  if ( m == 0 ) {
56  mol = molecule_add(&molgroup->mol, seqname);
57  nseq++;
58  }
59  mol->seq = mol->seq + aseq;
60  mol = mol->next;
61  } else {
62  if ( !mol && nseq > 0 ) m++; // Test for end of first section
63  mol = molgroup->mol;
64  }
65  }
66 
67  if ( verbose & VERB_DEBUG )
68  cout << "DEBUG readClustal: sequences read = " << nseq << " lines per sequence = " << m << endl;
69 
70  fseq.close();
71 
72  return nseq;
73 }
74 
81 int writeClustal(Bstring& filename, Bmolgroup* molgroup)
82 {
83  if ( verbose & VERB_DEBUG )
84  cout << "DEBUG writeClustal: filename=" << filename << endl;
85 
86  ofstream fseq(filename.c_str());
87  if ( fseq.fail() ) {
88  cerr << "Error: File " << filename << " not opened!" << endl;
89  return -1;
90  }
91 
92  long m, n, nseq;
93  long i, max_name_len(10), max_seq_len(60);
94  char aline[MAXLINELEN];
95  Bmolecule* mol;
96  Bstring molname, seq;
97 
98  for ( nseq = 0, mol = molgroup->mol; mol; mol = mol->next ) {
99  nseq++;
100  molname = mol->id.pre(' ');
101  if ( max_name_len < molname.length() ) max_name_len = molname.length();
102  }
103  if ( max_name_len > 10 ) max_seq_len = 50;
104  max_name_len += 6;
105 
106  time_t ti = time(NULL);
107 
108  fseq << "CLUSTAL alignment file, Written by Bsoft on " << asctime(localtime(&ti)) << endl << endl;
109 
110  for ( n = 0; n < molgroup->maxlen; n += max_seq_len ) {
111  fseq << endl;
112  for ( mol = molgroup->mol; mol; mol = mol->next ) {
113  if ( mol->seq.length() ) seq = mol->seq;
114  else if ( mol->naseq.length() ) seq = mol->naseq;
115  m = max_seq_len;
116  if ( molgroup->maxlen - n < max_seq_len ) m = molgroup->maxlen - n;
117  aline[0] = 0;
118  if ( n < seq.length() ) strncpy(aline, &seq[n], max_seq_len);
119  for ( i = strlen(aline); i < m; i++ ) aline[i] = '-';
120  aline[m] = 0;
121  molname = mol->id.pre(' ');
122  fseq << setw(max_name_len) << molname << aline << endl;
123  }
124  fseq << endl;
125  }
126 
127  fseq.close();
128 
129  return nseq;
130 }
writeClustal
int writeClustal(Bstring &filename, Bmolgroup *molgroup)
Writes Clustal format sequence files.
Definition: rwClustal.cpp:81
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
readClustal
int readClustal(Bstring &filename, Bmolgroup *molgroup)
Reads Clustal format sequence files.
Definition: rwClustal.cpp:29
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
rwClustal.h
Header file for reading and writing Clustal sequence files.
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
verbose
int verbose
Bmolgroup::maxlen
long maxlen
Definition: rwmolecule.h:180
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
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110