Bsoft  2.1.0
Bernard's software package
rwmol_text.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.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 
22 int read_mol_text(Bstring& filename, Bmolgroup *molgroup)
23 {
24  if ( verbose & VERB_DEBUG )
25  cout << "DEBUG read_mol_text: filename=" << filename << endl;
26 
27  ifstream fseq(filename.c_str());
28  if ( fseq.fail() ) {
29  cout << "File not opened!" << endl;
30  return -1;
31  }
32 
33  long nres(0), nna(0);
34  char aline[MAXLINELEN];
35  char molname[MAXLINELEN] = "A";
36  char na[8] = "ACGTU";
37  char* seq = new char[MAXSEQLEN];
38  char* aptr;
39  memset(seq, 0, MAXSEQLEN);
40 
41  Bmolecule* mol = molecule_add(&molgroup->mol, molname);
42 
43  while ( fseq.getline(aline, MAXLINELEN) ) {
44  for ( aptr = aline; *aptr; aptr++ ) {
45  if ( isalpha(*aptr) ) {
46  seq[nres++] = toupper(*aptr);
47  if ( strchr(na, *aptr) ) nna++;
48  } else if ( ( *aptr == '-' ) || ( *aptr == '.' ) ) {
49  seq[nres++] = '-';
50  }
51  }
52  }
53 
54  fseq.close();
55 
56  if ( 1.5*nna > nres ) {
57  mol->naseq = seq;
58  mol->nbase = nres;
59  } else {
60  mol->seq = seq;
61  mol->nres = nres;
62  }
63 
64  delete[] seq;
65 
66  return 1;
67 }
68 
75 int write_mol_text(Bstring& filename, Bmolgroup *molgroup)
76 {
77  if ( verbose & VERB_DEBUG )
78  cout << "DEBUG write_mol_text: filename=" << filename << endl;
79 
80  ofstream fseq(filename.c_str());
81 
82  if ( fseq.fail() ) return -1;
83 
84  int i(0), j, k, n;
85  char aline[MAXLINELEN] = "";
86  Bmolecule* mol;
87 
88  for ( mol = molgroup->mol; mol; mol = mol->next ) {
89  n = mol->naseq.length();
90  for ( j=0; j<n; j+=60 ) {
91  snprintf(aline, MAXLINELEN, "%9d", j+1);
92  for ( k=j; k<j+60 && k<n; k+=10 ) {
93  strcat(aline, " ");
94  strncat(aline, &mol->naseq[k], 10);
95  }
96  strcat(aline, "\n");
97  fseq << aline;
98  }
99  n = mol->seq.length();
100  for ( j=0; j<n; j+=60 ) {
101  snprintf(aline, MAXLINELEN, "%9d", j+1);
102  for ( k=j; k<j+60 && k<n; k+=10 ) {
103  strcat(aline, " ");
104  strncat(aline, &mol->seq[k], 10);
105  }
106  strcat(aline, "\n");
107  fseq << aline;
108  }
109  fseq << endl;
110  i++;
111  }
112 
113  fseq.close();
114 
115  return i;
116 }
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
read_mol_text
int read_mol_text(Bstring &filename, Bmolgroup *molgroup)
Reads a molecule group from a text file.
Definition: rwmol_text.cpp:22
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
Bmolecule::nbase
long nbase
Definition: rwmolecule.h:112
rwmolecule.h
Header file for reading reflection files.
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
verbose
int verbose
write_mol_text
int write_mol_text(Bstring &filename, Bmolgroup *molgroup)
Writes a molecule group to a text file.
Definition: rwmol_text.cpp:75
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