Bsoft  2.1.0
Bernard's software package
bseq.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "rwgencode.h"
11 #include "seq_util.h"
12 #include "utilities.h"
13 #include "options.h"
14 #include "timer.h"
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 // Usage assistance
20 const char* use[] = {
21 " ",
22 "Usage: bseq [options] input.seq output.seq",
23 "------------------------------------------",
24 "Manipulates DNA and protein sequences.",
25 " ",
26 "Actions:",
27 "-show Show all DNA and protein sequences after other operations.",
28 "-Mass Show the calculated mass for each sequence.",
29 "-complement Complement all DNA sequences before other operations.",
30 "-translate 1 Translate all DNA sequences with this frame (0,1,2).",
31 "-finddna ggattcga DNA sequence to look for in DNA sequences.",
32 "-findprotein fghwereaas Protein sequence to look for in protein sequences.",
33 "-findcoding aklsdrtv Coding sequence to look for in DNA sequences.",
34 " Can be a file name with a protein sequence.",
35 " ",
36 "Parameters:",
37 "-verbose 7 Verbosity of output.",
38 "-nogaps Strip gaps from input sequences.",
39 "-length 1100,1500 Sequence length range to select for.",
40 "-number 10,14 Number of residues on either side to include in output.",
41 "-threshold 50 Threshold percentage to report hits (default only best hit).",
42 " ",
43 "Input:",
44 "-geneticcode file.star Genetic code file (default = internal values).",
45 "-elements prop.star Calculate elemental composition.",
46 " ",
47 NULL
48 };
49 
50 int main(int argc, char **argv)
51 {
52  // Initialize variables
53  int setshow(0); // Show flag
54  int setmass(0); // Show flag
55  int setfind(0); // Search flag
56  int setcomplement(0); // Complement flag
57  int settranslate(0); // Translation flag
58  int frame(0); // Frame for translation
59  int side1(0), side2(0); // Additional residues
60  double threshold(0); // Only best match
61  int seqlenmin(0); // Minimum sequence length
62  int seqlenmax(100000); // Minimum sequence length
63  Bstring atom_select("all"); // Selection
64 
65  // Genetic code parameter file and template sequence file
66  Bstring gcfile;
67  Bstring seqfile; // Sequence to search for in a file
68  Bstring rpfile; // Residue properties file
69  Bstring paramfile;
70 
71  int i, optind;
72  Boption* option = get_option_list(use, argc, argv, optind);
73  Boption* curropt;
74  for ( curropt = option; curropt; curropt = curropt->next ) {
75  if ( curropt->tag == "nogaps" )
76  atom_select = "nogap";
77  if ( curropt->tag == "show" )
78  setshow = 1;
79  if ( curropt->tag == "Mass" )
80  setmass = 1;
81  if ( curropt->tag == "complement" )
82  setcomplement = 1;
83  if ( curropt->tag == "translate" ) {
84  if ( ( frame = curropt->value.integer() ) < 0 )
85  cerr << "-translate: A frame must be specified!" << endl;
86  while ( frame < 0 ) frame += 3;
87  while ( frame > 2 ) frame -= 3;
88  settranslate = 1;
89  }
90  if ( curropt->tag == "length" ) {
91  if ( curropt->values(seqlenmin, seqlenmax) < 1 )
92  cerr << "-length: Minimum and maximum lengths must be specified!" << endl;
93  else {
94  if ( seqlenmin < 0 ) seqlenmin = 0;
95  if ( seqlenmax < seqlenmin ) {
96  i = seqlenmin;
97  seqlenmin = seqlenmax;
98  seqlenmax = i;
99  }
100  if ( seqlenmax == seqlenmin ) seqlenmax = seqlenmin + 1;
101  }
102  }
103  if ( curropt->tag == "finddna" ) {
104  seqfile = curropt->filename();
105  setfind = 1;
106  }
107  if ( curropt->tag == "findprotein" ) {
108  seqfile = curropt->filename();
109  setfind = 2;
110  }
111  if ( curropt->tag == "findcoding" ) {
112  seqfile = curropt->filename();
113  setfind = 3;
114  }
115  if ( curropt->tag == "Sequence" ) {
116  seqfile = curropt->filename();
117  if ( seqfile.length() ) setfind = 3;
118  }
119  if ( curropt->tag == "number" )
120  if ( curropt->values(side1, side2) < 1 )
121  cerr << "-number: At least one number must be specified!" << endl;
122  if ( curropt->tag == "threshold" )
123  if ( ( threshold = curropt->value.real() ) < 1e-30 )
124  cerr << "-threshold: A threshold must be specified!" << endl;
125  if ( curropt->tag == "geneticcode" )
126  gcfile = curropt->filename();
127  if ( curropt->tag == "elements" )
128  rpfile = curropt->filename();
129  }
130  option_kill(option);
131 
132  double ti = timer_start();
133 
134  // Read the sequence request file if given
135  Bmolgroup* seq_request = NULL;
136  Bstring seq; // Sequence to search for
137  if ( seqfile.length() ) {
138  if ( seqfile.contains(".") ) {
139  seq_request = read_molecule(seqfile, atom_select, paramfile);
140  if ( seq_request->mol->naseq.length() ) {
141  seq = seq_request->mol->naseq;
142  seq_request->mol->naseq = NULL;
143  } else {
144  seq = seq_request->mol->seq;
145  seq_request->mol->seq = NULL;
146  }
147  molgroup_kill(seq_request);
148  } else {
149  seq = seqfile;
150  }
151  // Convert threshold to number of residues
152  threshold = floor(threshold*seq.length()/100.0);
153  }
154 
155  if ( optind >= argc ) {
156  cerr << "Error: No input file given!" << endl;
157  bexit(-1);
158  }
159 
160  if ( verbose && seq.length() ) cout << "Search string: " << seq << endl;
161 
162  // Read the sequence file
163  seqfile = argv[optind++];
164  Bmolgroup* molgroup = read_molecule(seqfile, atom_select, paramfile);
165  if ( !molgroup ) {
166  cerr << "Error: No input file given!" << endl;
167  bexit(-1);
168  }
169 
170  if ( setcomplement )
171  seq_complement_all(molgroup);
172 
173  if ( settranslate )
174  seq_translate_all(molgroup, frame, gcfile);
175 
176  switch ( setfind ) {
177  case 1: seq_find_dna(molgroup, seq); break;
178  case 2: seq_find_protein(molgroup, seq); break;
179  case 3: seq_find_protein_in_dna(molgroup, seq, seqlenmin, seqlenmax, side1, side2, threshold, gcfile); break;
180  default: break;
181  }
182 
183  if ( setshow )
184  seq_show(molgroup);
185 
186  if ( setmass )
187  seq_mass(molgroup);
188 
189  if ( rpfile.length() )
190  seq_elements(molgroup, rpfile);
191 
192  molecule_update_comment(molgroup, argc, argv);
193 
194  if ( optind < argc )
195  write_molecule(argv[optind], molgroup);
196 
197  molgroup_kill(molgroup);
198 
199  if ( verbose & VERB_TIME )
200  timer_report(ti);
201 
202  bexit(0);
203 }
204 
Boption
A class for a command line input option.
Definition: options.h:26
seq_elements
vector< double > seq_elements(Bmolgroup *molgroup, Bstring &paramfile)
Shows the elemental composition of all molecular sequences.
Definition: seq_util.cpp:176
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
molecule_update_comment
int molecule_update_comment(Bmolgroup *molgroup, int n, char **strings)
Puts a set of strings and time in the main comment of a molecule group.
Definition: rwmolecule.cpp:1467
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
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
Boption::next
Boption * next
Definition: options.h:28
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
main
int main(int argc, char **argv)
Definition: bseq.cpp:50
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
seq_find_dna
long seq_find_dna(Bmolgroup *molgroup, Bstring &seq)
Finds a nucleotide sequence.
Definition: seq_util.cpp:314
Bstring::real
double real()
Definition: Bstring.cpp:553
read_molecule
Bmolgroup * read_molecule(const char *filename, const char *select, const char *paramfile)
The generalized function for reading molecular files.
Definition: rwmolecule.cpp:818
options.h
Header file for option handlers.
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
seq_translate_all
int seq_translate_all(Bmolgroup *molgroup, int frame, Bstring &gcname)
Translates all nucleotide sequences to protein sequences.
Definition: seq_util.cpp:289
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
seq_mass
int seq_mass(Bmolgroup *molgroup)
Shows the masses of all molecular sequences.
Definition: seq_util.cpp:131
rwmolecule.h
Header file for reading reflection files.
rwgencode.h
Header file for reading a genetic code from a file.
Boption::value
Bstring value
Definition: options.h:30
seq_complement_all
int seq_complement_all(Bmolgroup *molgroup)
Complements all nucleotide sequences.
Definition: seq_util.cpp:265
timer.h
Utilities for timing functions.
seq_find_protein
long seq_find_protein(Bmolgroup *molgroup, Bstring &seq)
Finds an amino acid sequence.
Definition: seq_util.cpp:356
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
seq_util.h
Header file for sequence utilities.
write_molecule
int write_molecule(char *filename, Bmolgroup *molgroup)
Writes a molecule group.
Definition: rwmolecule.cpp:975
seq_show
int seq_show(Bmolgroup *molgroup)
Shows all molecular sequences.
Definition: seq_util.cpp:81
Bmolecule::naseq
Bstring naseq
Definition: rwmolecule.h:115
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bmolgroup
Definition: rwmolecule.h:170
verbose
int verbose
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
use
const char * use[]
Definition: bseq.cpp:20