| Bsoft 2.0.5 Bernard's software package

seq_util.cpp
Go to the documentation of this file.
1
9 #include "rwgencode.h"
10 #include "seq_analysis.h"
11 #include "seq_util.h"
12 #include "linked_list.h"
13 #include "utilities.h"
14
15 #include <map>
16
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19
20 /* One-letter to 3-letter mapping */
21 /*const char* res_code[] = {
22 "-", "GAP",
23 "A", "ALA",
24 "B", "ASX",
25 "C", "CYS",
26 "D", "ASP",
27 "E", "GLU",
28 "F", "PHE",
29 "G", "GLY",
30 "H", "HIS",
31 "I", "ILE",
32 "K", "LYS",
33 "L", "LEU",
34 "M", "MET",
35 "N", "ASN",
36 "P", "PRO",
37 "Q", "GLN",
38 "R", "ARG",
39 "S", "SER",
40 "T", "THR",
41 "V", "VAL",
42 "W", "TRP",
43 "Y", "TYR",
44 "Z", "GLX",
45 "X", "UNK",
46 } ;
47 */
48 const map<char, string> res_code = {
49 {'-', "GAP"},
50 {'*', "UNK"},
51 {'A', "ALA"},
52 {'B', "ASX"},
53 {'C', "CYS"},
54 {'D', "ASP"},
55 {'E', "GLU"},
56 {'F', "PHE"},
57 {'G', "GLY"},
58 {'H', "HIS"},
59 {'I', "ILE"},
60 {'K', "LYS"},
61 {'L', "LEU"},
62 {'M', "MET"},
63 {'N', "ASN"},
64 {'P', "PRO"},
65 {'Q', "GLN"},
66 {'R', "ARG"},
67 {'S', "SER"},
68 {'T', "THR"},
69 {'V', "VAL"},
70 {'W', "TRP"},
71 {'Y', "TYR"},
72 {'Z', "GLX"},
73 {'X', "UNK"}
74 } ;
75
81 int seq_show(Bmolgroup* molgroup)
82 {
83 long i, maxnum;
84 Bstring seq;
85 Bmolecule* mol;
86 Bresidue* res;
87 Bsecondary* sec;
88
89 if ( verbose & VERB_LABEL )
90 cout << endl << "Sequences:" << endl << endl;
91
92 for ( mol = molgroup->mol; mol; mol = mol->next ) {
93 cout << "Molecule " << mol->id << ": " << mol->nbase << " nucleotides, "
94 << mol->nres << " residues" << endl;
95 if ( mol->naseq.length() ) cout << mol->naseq << endl;
96 if ( mol->res ) {
97 for ( maxnum=mol->nres, res = mol->res; res; res = res->next )
98 if ( maxnum < res->num ) maxnum = res->num;
99 // seq = Bstring(maxnum, '-');
100 seq = Bstring('-', maxnum);
101 for ( res = mol->res; res; res = res->next )
102 seq[res->num-1] = getcode1(res->type);
103 cout << seq << endl;
104 seq = 0;
105 } else if ( mol->seq.length() ) cout << mol->seq << endl;
106 if ( mol->sec ) {
107 for ( maxnum=0, sec=mol->sec; sec; sec=sec->next )
108 if ( maxnum < sec->last->num ) maxnum = sec->last->num;
109 // seq = Bstring(maxnum, ' ');
110 seq = Bstring('-', maxnum);
111 for ( sec=mol->sec; sec; sec=sec->next ) {
112 if ( sec->type < Strand )
113 for ( i=sec->first->num-1; i<sec->last->num; i++ ) seq[i] = 'H';
114 if ( sec->type == Strand )
115 for ( i=sec->first->num-1; i<sec->last->num; i++ ) seq[i] = 'E';
116 }
117 cout << seq << endl;
118 seq = 0;
119 }
120 cout << endl;
121 }
122
123 return 0;
124 }
125
131 int seq_mass(Bmolgroup* molgroup)
132 {
133 long i, n;
134 double mass;
135 Bstring temp;
136 Bmolecule* mol;
137 Bresidue* res;
138 Bresidue_type* rt_list = get_residue_properties(temp);
139 Bresidue_type* rt = NULL;
140
141 if ( verbose & VERB_LABEL )
142 cout << "Calculating molecular weights:" << endl << endl;
143
144 cout << "Molecule\tMass" << endl;
145 for ( mol = molgroup->mol; mol; mol = mol->next ) {
146 mass = 0;
147 n = mol->seq.length();
148 if ( n ) {
149 for ( i=0; i<n; i++ ) {
150 for ( rt = rt_list; rt && rt->c != mol->seq[i]; rt = rt->next ) ;
151 if ( rt ) mass += rt->mass;
152 }
153 } else if ( mol->res ) {
154 for ( res = mol->res; res; res = res->next ) {
155 for ( rt = rt_list; rt && rt->cod != res->type; rt = rt->next ) ;
156 if ( rt ) mass += rt->mass;
157 }
158 }
159 mass -= 18*(n-1);
160 cout << mol->id << tab << mass << endl;
161 }
162
163 cout << endl;
164
165 kill_list((char *) rt_list, sizeof(Bresidue_type));
166
167 return 0;
168 }
169
176 vector<double> seq_elements(Bmolgroup* molgroup, Bstring& paramfile)
177 {
178 long i, j, n, nat, natr;
179 Bmolecule* mol;
180 Bresidue* res;
181 Batom* atom;
182 Bresidue_type* rt_list = get_residue_properties(paramfile);
183 Bresidue_type* rt = NULL;
184 vector<double> el(5,0), elr(5,0), elall(5,0), el1(5,0);
185
186 if ( verbose & VERB_LABEL )
187 cout << "Calculating the elemental composition:" << endl << endl;
188
189 cout << "Molecule\tH\tC\tN\tO\tS\tTotal" << endl;
190 for ( mol = molgroup->mol; mol; mol = mol->next ) {
191 for ( j=0; j<5; ++j ) el[j] = elr[j] = 0;
192 n = mol->seq.length();
193 if ( n ) {
194 for ( i=0; i<n; i++ ) {
195 for ( rt = rt_list; rt && rt->c != mol->seq[i]; rt = rt->next ) ;
196 if ( rt ) for ( j=0; j<5; ++j ) el[j] += rt->comp[j];
197 }
198 }
199 // } else if ( mol->res ) {
200 /* if ( mol->res ) {
201 for ( res = mol->res; res; res = res->next ) {
202 char c1 = getcode1(res->type);
203 for ( rt = rt_list; rt && rt->c != c1; rt = rt->next ) ;
204 if ( rt ) for ( j=0; j<5; ++j ) elr[j] += rt->comp[j];
205 }
206 }*/
207 if ( mol->res ) {
208 for ( res = mol->res; res; res = res->next ) {
209 char c1 = getcode1(res->type);
210 for ( rt = rt_list; rt && rt->c != c1; rt = rt->next ) ;
211 for ( j=0; j<5; ++j ) el1[j] = 0;
212 for ( atom = res->atom; atom; atom = atom->next ) if ( atom->el[1] == 0 ) {
213 if ( atom->el[0] == 'H' ) elr[0]++;
214 if ( atom->el[0] == 'C' ) elr[1]++;
215 if ( atom->el[0] == 'N' ) elr[2]++;
216 if ( atom->el[0] == 'O' ) elr[3]++;
217 if ( atom->el[0] == 'S' ) elr[4]++;
218 if ( atom->el[0] == 'H' ) el1[0]++;
219 if ( atom->el[0] == 'C' ) el1[1]++;
220 if ( atom->el[0] == 'N' ) el1[2]++;
221 if ( atom->el[0] == 'O' ) el1[3]++;
222 if ( atom->el[0] == 'S' ) el1[4]++;
223 }
224 /* cout << c1;
225 for ( j=0; j<5; ++j ) cout << tab << el1[j];
226 cout << endl;
227 cout << c1;
228 for ( j=0; j<5; ++j ) cout << tab << rt->comp[j];
229 cout << endl;*/
230 }
231 }
232 cout << mol->id;
233 for ( j=nat=natr=0; j<5; ++j ) {
234 nat += el[j];
235 natr += elr[j];
236 elall[j] += el[j];
237 cout << tab << el[j];
238 }
239 cout << tab << nat << endl;
240 // cout << mol->id;
241 // for ( j=0; j<5; ++j ) cout << tab << elr[j];
242 // cout << tab << natr << endl;
243 }
244
245 cout << "Total";
246 for ( j=n=0; j<5; n+=elall[j], ++j ) cout << tab << elall[j];
247 cout << tab << n << endl << "Percentage:";
248 for ( j=0; j<5; ++j ) cout << tab << elall[j]*100.0/n;
249 cout << endl << endl;
250
251 kill_list((char *) rt_list, sizeof(Bresidue_type));
252
253 return elall;
254 }
255
256
265 int seq_complement_all(Bmolgroup* molgroup)
266 {
267 Bmolecule* mol;
268
269 if ( verbose )
270 cout << endl << "Complementing all nucleotide sequences" << endl << endl;
271
272 for ( mol = molgroup->mol; mol; mol = mol->next ) // Complement all molecules
273 if ( mol->naseq.length() ) complement_sequence(mol->naseq);
274
275 return 0;
276 }
277
289 int seq_translate_all(Bmolgroup* molgroup, int frame, Bstring& gcfile)
290 {
291 Bmolecule* mol;
292
293 Bstring gencode = get_genetic_code(gcfile);
294
295 if ( verbose & VERB_LABEL )
296 cout << endl << "Translating all nucleotide sequences to amino acid sequences in frame " << frame << endl << endl;
297
298 molgroup->maxlen = 0;
299 for ( mol = molgroup->mol; mol; mol = mol->next ) if ( mol->naseq.length() ) {
300 mol->seq = sequence_translate(mol->naseq, frame, gencode);
301 mol->nres = mol->seq.length();
302 if ( molgroup->maxlen < mol->nres ) molgroup->maxlen = mol->nres;
303 }
304
305 return 0;
306 }
307
314 long seq_find_dna(Bmolgroup* molgroup, Bstring& seq)
315 {
316 Bmolecule* mol;
317 Bstring sseq = seq.upper();
318 Bstring mol_naseq;
319 long i, offset(0);
320
321 if ( verbose )
322 cout << "Searching for: " << sseq << endl << endl;
323
324 for ( mol = molgroup->mol; mol; mol = mol->next ) {
325 mol_naseq = mol->naseq.upper();
326 if ( mol_naseq.length() ) {
327 offset = mol_naseq.find(sseq, 0);
328 if ( offset > -1 ) {
329 cout << mol->id << ": " << offset << endl;
330 for ( i=0; i<offset; i++ ) cout << " ";
331 cout << sseq << endl << mol_naseq << endl;
332 } else {
333 complement_sequence(mol_naseq);
334 offset = mol_naseq.find(sseq, 0);
335 if ( offset > -1 ) {
336 cout << mol->id << ": " << offset << " (complement)" << endl;
337 for ( i=0; i<offset; i++ ) cout << " ";
338 cout << sseq << endl << mol_naseq << endl;
339 } else {
340 cout << mol->id << ": not found" << endl;
341 }
342 }
343 } else
344 cout << "Molecule " << mol->id << ": Nucleic acid sequence not found" << endl;
345 }
346
347 return offset;
348 }
349
356 long seq_find_protein(Bmolgroup* molgroup, Bstring& seq)
357 {
358 Bmolecule* mol;
359 Bstring sseq = seq.upper();
360 Bstring mol_seq;
361 long offset(0);
362
363 if ( verbose )
364 cout << "Searching for: " << sseq << endl << endl;
365
366 for ( mol = molgroup->mol; mol; mol = mol->next ) {
367 mol_seq = mol->seq.upper();
368 if ( mol_seq.length() ) {
369 offset = mol_seq.find(sseq, 0);
370 if ( offset > -1 ) cout << mol->id << ": " << offset << endl;
371 else cout << mol->id << ": not found" << endl;
372 } else
373 cout << "Molecule " << mol->id << ": Protein sequence not found" << endl;
374 }
375
376 return offset;
377 }
378
394 Bstring seq_find_protein_in_dna(Bmolgroup* molgroup, Bstring& seq, int seqlenmin, int seqlenmax,
395 int side1, int side2, double threshold, Bstring& gcfile)
396 {
397 long n = seq.length();
398 if ( n < 1 ) return "";
399
400 Bmolecule* mol;
401 Bmolecule* bestmol = NULL;
402 if ( seqlenmin < n/2 ) seqlenmin = n/2;
403 if ( seqlenmax <= n ) seqlenmin = 2*n;
404
405 int linelength = 80;
406 int f, i, j, nres = 0, start, length, frame, term, seqlen = 0;
407 int bestscore = 0, bestframe = 0;
408 int bestindex = 0, bestseqlen = 0;
409 int* score = (int *) malloc(sizeof(int));
410 Bstring bestseq;
411 char format[100];
412 snprintf(format, 100, "%%.%ds%c", linelength, '\n');
413
414 Bstring gencode = get_genetic_code(gcfile);
415
416 if ( verbose ) {
417 cout << endl << "Finding an amino acid sequence in translated nucleotide sequences:" << endl;
418 cout << "Sequence length range: " << seqlenmin << " - " << seqlenmax << endl;
419 cout << "Score threshold (residues): " << threshold << endl;
420 }
421
422 for ( mol = molgroup->mol; mol; mol = mol->next ) { // Search all molecules
423 for ( f=0; f<6; f++ ) { // Search all translational frames
424 Bstring nucseq = mol->naseq;
425 frame = f;
426 if ( f > 2 ) {
427 frame = f - 3;
428 complement_sequence(nucseq);
429 }
430 Bstring aaseq = sequence_translate(nucseq, frame, gencode);
431 nres = aaseq.length();
432 score = (int *) realloc(score, nres*sizeof(int));
433 memset(score, 0, nres*sizeof(int));
434
435 for ( i=0; i<=nres - n; i++ ) { // Loop over starting positions
436 if ( i == 0 || aaseq[i] == '*' ) {
437 seqlen = 0;
438 while ( ( i+seqlen+1 < nres ) && ( aaseq[i+seqlen+1] != '*' ) )
439 seqlen++;
440 }
441 if ( seqlen >= seqlenmin && seqlen <= seqlenmax ) {
442 term = 0;
443 for ( j=0; j<n && j<nres && !term; j++ ) {
444 if ( aaseq[i+j] == seq[j] ) score[i]++;
445 if ( aaseq[i+j] == '*' ) term = 1;
446 }
447 if ( !term && threshold > 0 && score[i] >= threshold ) {
448 if ( verbose )
449 cout << "Frame: " << f << " Position: " << 3*i + frame <<
450 " Length: " << seqlen << " Score: " << score[i] <<
451 " (" << score[i]*100.0/seq.length() << " %)" << endl;
452 for ( j=0; j<n; j+= linelength )
453 printf(format, &aaseq[i + j]);
454 }
455 if ( !term && bestscore < score[i] ) {
456 bestscore = score[i];
457 bestseqlen = seqlen;
458 bestmol = mol;
459 bestframe = f;
460 bestindex = 3*i + frame;
461 start = (i - side1)*3 + frame;
462 while ( start < 0 ) start += 3;
463 length = (n + side1 + side2)*3;
464 if ( length + start > nucseq.length() )
465 length = nucseq.length() - start;
466 bestseq = nucseq.substr(start, length);
467 }
468 }
469 }
470 }
471 }
472
473 free(score);
474
475 cout << "Best sequence =" << bestseq << endl;
476
477 if ( bestmol && verbose ) {
478 cout << endl << "Best sequence score: " << bestscore <<
479 " (" << bestscore*100.0/seq.length() << "%)" << endl;
480 cout << "Molecule: " << bestmol->id << endl;
481 cout << "Length: " << bestseqlen << endl;
482 if ( bestframe < 3 ) cout << "Frame: " << bestframe << endl;
483 else cout << "Frame: " << bestframe - 3 << " (complement)" << endl;
484 cout << "Index: " << bestindex << endl;
485 cout << "Nucleotide sequence:" << endl;
486 for ( i=0; i<bestseq.length(); i+= linelength )
487 printf(format, bestseq.c_str() + i);
488 Bstring aaseq = sequence_translate(bestseq, 0, gencode);
489 cout << "Amino acid sequence:" << endl;
490 for ( i=0; i<aaseq.length(); i+= linelength )
491 printf(format, aaseq.c_str() + i);
492 cout << "Requested sequence:" << endl;
493 for ( i=0; i<seq.length(); i+= linelength )
494 printf(format, seq.c_str() + i);
495 }
496 cout << endl;
497
498 return bestseq;
499 }
500
510 int getcode3(char c, char* cod)
511 {
512 c = toupper(c);
513
514 string rc(res_code.at(c));
515 for ( int i=0; i<3; ++i ) cod[i] = rc[i];
516
517 if ( verbose & VERB_DEBUG )
518 cout << "DEBUG getcode3: c=" << c << " cod=" << cod << endl;
519
520 return 0;
521 }
522
531 char getcode1(char* acode)
532 {
533 size_t i;
534 char c(' ');
535
536 for ( i=0; i<3 && i<strlen(acode); i++ ) acode[i] = toupper(acode[i]);
537 acode[i] = 0;
538
539 for ( auto it=res_code.begin(); it!=res_code.end(); ++it )
540 if ( it->second == acode ) c = it->first;
541
542 return c;
543 }
544
550 int complement_sequence(Bstring& nucseq)
551 {
552 int i, j;
553 char nuc;
554
555 j = nucseq.length();
556 for ( i=0; i<(j+1)/2; i++ ) {
557 nuc = get_complement(nucseq[j-i-1]);
558 nucseq[j-i-1] = get_complement(nucseq[i]);
559 nucseq[i] = nuc;
560 }
561
562 return 0;
563 }
564
570 char get_complement(char nuc)
571 {
572 char nunuc = 'X';
573
574 switch ( nuc ) {
575 case 'A': nunuc = 'T'; break;
576 case 'G': nunuc = 'C'; break;
577 case 'C': nunuc = 'G'; break;
578 case 'T': nunuc = 'A'; break;
579 case 'U': nunuc = 'A'; break;
580 default: break;
581 }
582
583 return nunuc;
584 }
585
593 Bstring sequence_translate(Bstring& nucseq, long frame, Bstring& gencode)
594 {
595 long i, j;
596 long nnuc = nucseq.length();
597 long nres = (nnuc - frame)/3;
598 Bstring aaseq(nres, ' ');
599
600 for ( i=0, j=frame; i<nres && j<nnuc; i++, j+=3 )
601 aaseq[i] = gencode[index_from_codon(&nucseq[j])];
602
603 if ( i < nres ) aaseq[i] = 0;
604
605 return aaseq;
606 }
607
608
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
sequence_translate
Bstring sequence_translate(Bstring &nucseq, long frame, Bstring &gencode)
Translates a nucleotide sequence to a protein sequence.
Definition: seq_util.cpp:593
seq_util.h
Header file for sequence utilities.
Bresidue_type::comp
float comp[5]
Definition: rwresprop.h:32
Bstring::substr
Bstring substr(long start, long len)
Definition: Bstring.cpp:324
Bmolecule::nres
long nres
Definition: rwmolecule.h:111
complement_sequence
int complement_sequence(Bstring &nucseq)
Complements a nucleotide sequence in place.
Definition: seq_util.cpp:550
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bstring::find
long find(const Bstring &s)
Definition: Bstring.h:103
rwgencode.h
Header file for reading a genetic code from a file.
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bresidue_type::c
char c
Definition: rwresprop.h:24
verbose
int verbose
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bresidue::type
char type[6]
Definition: rwmolecule.h:53
Bresidue_type::mass
float mass
Definition: rwresprop.h:26
res_code
const map< char, string > res_code
Definition: seq_util.cpp:48
getcode3
int getcode3(char c, char *cod)
Converts a one-letter amino acid designation to the three-letter equivalent.
Definition: seq_util.cpp:510
Bsecondary::next
Bsecondary * next
Definition: rwmolecule.h:88
Bmolecule::sec
Bsecondary * sec
Definition: rwmolecule.h:123
seq_translate_all
int seq_translate_all(Bmolgroup *molgroup, int frame, Bstring &gcfile)
Translates all nucleotide sequences to protein sequences.
Definition: seq_util.cpp:289
seq_show
int seq_show(Bmolgroup *molgroup)
Shows all molecular sequences.
Definition: seq_util.cpp:81
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
linked_list.h
Header file for generalized linked list functions.
Bsecondary::last
Bresidue * last
Definition: rwmolecule.h:95
Bmolgroup
Definition: rwmolecule.h:170
Bmolecule::nbase
long nbase
Definition: rwmolecule.h:112
getcode1
char getcode1(char *acode)
Converts a three-letter amino acid designation to the one-letter equivalent.
Definition: seq_util.cpp:531
seq_elements
vector< double > seq_elements(Bmolgroup *molgroup, Bstring &paramfile)
Shows the elemental composition of all molecular sequences.
Definition: seq_util.cpp:176
length
double length(vector< T > v)
Definition: Vector3.h:528
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule::naseq
Bstring naseq
Definition: rwmolecule.h:115
Bmolgroup::maxlen
long maxlen
Definition: rwmolecule.h:180
Bresidue_type
Definition: rwresprop.h:22
seq_mass
int seq_mass(Bmolgroup *molgroup)
Shows the masses of all molecular sequences.
Definition: seq_util.cpp:131
seq_find_dna
long seq_find_dna(Bmolgroup *molgroup, Bstring &seq)
Finds a nucleotide sequence.
Definition: seq_util.cpp:314
seq_analysis.h
Header file for sequence analysis functions.
Bstring::upper
Bstring upper()
Definition: Bstring.cpp:316
seq_find_protein_in_dna
Bstring seq_find_protein_in_dna(Bmolgroup *molgroup, Bstring &seq, int seqlenmin, int seqlenmax, int side1, int side2, double threshold, Bstring &gcfile)
Finds the coding region for an amino acid sequence.
Definition: seq_util.cpp:394
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule
Definition: rwmolecule.h:107
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Bresidue
Definition: rwmolecule.h:49
Bstring
Definition: Bstring.h:29
Batom
Definition: rwmolecule.h:24
Bsecondary::first
Bresidue * first
Definition: rwmolecule.h:94
get_residue_properties
Bresidue_type * get_residue_properties(Bstring &filename)
Reading residue properties from parameter files.
Definition: rwresprop.cpp:99
get_complement
char get_complement(char nuc)
Get the Watson-Crick complement of a nucleotide base.
Definition: seq_util.cpp:570
seq_find_protein
long seq_find_protein(Bmolgroup *molgroup, Bstring &seq)
Finds an amino acid sequence.
Definition: seq_util.cpp:356
Bresidue::num
int num
Definition: rwmolecule.h:51
seq_complement_all
int seq_complement_all(Bmolgroup *molgroup)
Complements all nucleotide sequences.
Definition: seq_util.cpp:265
Batom::el
char el[4]
Definition: rwmolecule.h:27
Strand
Definition: rwmolecule.h:75
Bsecondary::type
SecondaryType type
Definition: rwmolecule.h:89
index_from_codon
int index_from_codon(const char codon[3])
Gets the index associated with a specific codon.
Definition: rwgencode.cpp:112
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
Batom::next
Batom * next
Definition: rwmolecule.h:25
get_genetic_code
Bstring get_genetic_code(Bstring &filename)
Gets the genetic code from a parameter file.
Definition: rwgencode.cpp:26
Bresidue_type::cod
char cod[3]
Definition: rwresprop.h:25
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bsecondary
Definition: rwmolecule.h:87
Bresidue_type::next
Bresidue_type * next
Definition: rwresprop.h:23