| Bsoft 2.0.5 Bernard's software package

rwPhylip.cpp
Go to the documentation of this file.
1
9 #include "rwPhylip.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 readPhylip(Bstring& filename, Bmolgroup *molgroup)
30 {
31 if ( verbose & VERB_DEBUG )
32 cout << "DEBUG readPhylip: 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 Bstring seqname;
43 long i(0), j, k(0), n(0), nseq(0), maxlen;
44 Bmolecule* mol;
45
46 // int gaps = 1; // Retain gaps
47 // if ( strstr(molgroup->select, "NOGAP") ) gaps = 0; // Omit gaps
48
49 fseq.getline(aline, 1024);
50 sscanf(aline, "%ld %ld", &nseq, &maxlen);
51
52 if ( verbose & VERB_DEBUG )
53 cout << "DEBUG readPhylip: nseq=" << nseq << " maxlen=" << maxlen << endl;
54
55 // Set up molecule structures and read the first parts of the sequences
56 maxlen++;
57 char* seq = new char[maxlen];
58
59 while ( !fseq.eof() ) {
60 fseq.getline(aline, MAXLINELEN);
61 if ( i < nseq ) {
62 seqname = aline;
63 seqname = seqname.pre(' ');
64 mol = molecule_add(&molgroup->mol, seqname);
65 } else {
66 for ( j=0, mol = molgroup->mol; mol && j<n; mol = mol->next ) j++;
67 }
68 if ( n == nseq ) { // Empty line and reset sequence counter
69 n = 0;
70 } else {
71 for ( size_t j=10, k=0; j<strlen(aline); j++ ) {
72 if ( isalpha(aline[j]) ) seq[k++] = aline[j];
73 if ( ( aline[j] == '-' ) || ( aline[j] == '.' ) ) seq[k++] = '-';
74 }
75 seq[k] = 0;
76 mol->seq += seq;
77 i++; n++;
78 }
79 }
80
81 fseq.close();
82
83 delete[] seq;
84
85 return nseq;
86 }
87
94 int writePhylip(Bstring& filename, Bmolgroup* molgroup)
95 {
96 if ( verbose & VERB_DEBUG )
97 cout << "DEBUG writePhylip: filename=" << filename << endl;
98
99 ofstream fseq(filename.c_str());
100 if ( fseq.fail() ) {
101 cerr << "Error: File " << filename << " not opened!" << endl;
102 return -1;
103 }
104
105 long i, m, n;
106 int nseq;
107 char aline[MAXLINELEN];
108 Bmolecule* mol;
109 Bstring molname, seq;
110
111 for ( nseq = 0, mol = molgroup->mol; mol; mol = mol->next ) nseq++;
112
113 fseq << setw(6) << nseq << setw(7) << molgroup->maxlen << endl;
114
115 for ( n = 0; n < molgroup->maxlen; n += 50 ) {
116 for ( mol = molgroup->mol; mol; mol = mol->next ) {
117 molname = mol->id.substr(0,10);
118 if ( mol->seq.length() ) seq = mol->seq;
119 else if ( mol->naseq.length() ) seq = mol->naseq;
120 m = 50;
121 if ( molgroup->maxlen - n < 50 ) m = molgroup->maxlen - n;
122 aline[0] = 0;
123 if ( n < seq.length() ) strncpy(aline, &seq[n], 50);
124 for ( i = strlen(aline); i < m; i++ ) aline[i] = '-';
125 aline[m] = 0;
126 if ( n == 0 ) fseq << setw(10) << molname << " " << aline << endl;
127 else fseq << setw(11) << " " << aline << endl;
128 }
129 fseq << endl;
130 }
131
132 fseq.close();
133
134 return nseq;
135 }
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Bstring::substr
Bstring substr(long start, long len)
Definition: Bstring.cpp:324
writePhylip
int writePhylip(Bstring &filename, Bmolgroup *molgroup)
Writes Phylip format sequence files.
Definition: rwPhylip.cpp:94
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
readPhylip
int readPhylip(Bstring &filename, Bmolgroup *molgroup)
Reads Phylip format sequence files.
Definition: rwPhylip.cpp:29
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
Bmolgroup
Definition: rwmolecule.h:170
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:381
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
Bmolgroup::maxlen
long maxlen
Definition: rwmolecule.h:180
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule
Definition: rwmolecule.h:107
rwPhylip.h
Header file for reading and writing Phylip sequence files.
Bstring
Definition: Bstring.h:29
verbose
int verbose