Bsoft  2.1.0
Bernard's software package
bseqaln.cpp
Go to the documentation of this file.
1 
9 #include "rwresprop.h"
10 #include "seq_align.h"
11 #include "seq_analysis.h"
12 #include "rwimg.h"
13 #include "utilities.h"
14 #include "options.h"
15 #include "timer.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 // Usage assistance
21 const char* use[] = {
22 " ",
23 "Usage: bseqaln [options] input.seq output.seq",
24 "---------------------------------------------",
25 "Analyzes aligned protein sequences.",
26 "All sequence numbers and position numbers start at 1.",
27 "Numbering is according to the input, including gaps.",
28 " ",
29 "Actions:",
30 "-pair Align a pair of sequences (first two in input file).",
31 "-nogaps Strip gaps from input sequences.",
32 "-consolidategaps Remove positions in alignment with only gaps.",
33 "-identity Pairwise identity.",
34 "-similarity 0.5 Pairwise similarity: threshold for counting similar residues (default 0.5).",
35 "-profile Generate a profile from an alignment.",
36 "-information Information analysis.",
37 "-hydrophobicity 0.8 Hydrophobicity analysis: representation threshold (fraction of # of seqs).",
38 " ",
39 "Selections:",
40 "-select 3,0.85 Select relative to a reference sequence and cutoff.",
41 "-length 128,435 Select sequences within a length range.",
42 "-delete Delete non-selected sequences.",
43 " ",
44 "Parameters:",
45 "-verbose 7 Verbosity of output.",
46 "-gap 10,0.2 Gap opening and extending penalties for alignment (default 20,0.2).",
47 "-segments 1-33,36-69 Selection of sequence segments (default all).",
48 " Every pair of numbers indicate a segment to select.",
49 "-sequences 2,18 Subset of sequences to compare (default all).",
50 "-window 17 Window for moving averages (default 20).",
51 " ",
52 "Input:",
53 "-properties file.star Property file (default = internal values).",
54 "-Similarity sim.star Residue similarity matrix (default blosum62.star).",
55 " ",
56 "Output:",
57 "-Matrix file.mat Matrix output filename.",
58 "-Image file.img Image output filename.",
59 "-Postscript file.ps Postscript output filename.",
60 " ",
61 NULL
62 };
63 
64 int main(int argc, char **argv)
65 {
66  int i, j;
67 // int n, flag;
68 // char *pnt;
69 
70  // Initialize variables
71  int sub1(0), sub2(0); // Subset to compare with
72  int consolidate_gaps(0);
73  int do_pair(0);
74  int do_id(0);
75  int do_sim(0);
76  int do_profile(0);
77  int do_hp(0);
78  int do_info(0);
79  int delseq(0); // Flag to delete non-selected sequences
80  long ref(0); // Reference sequence for selection
81  double cutoff(-100); // Selection threshold
82  long minlen(0), maxlen(0); // Length range to select for
83  double gapopen(20), gapextend(0.2);
84 // int sel[20]; // Residue range selection
85  int window(20); // Window for moving average
86  double sim_threshold(0.5); // Similarity threshold
87  double rep_threshold(0); // Representation threshold
88  Bstring segments; // Position selector
89  Bstring atom_select("all"); // Selection
90 
91  // Initialize the file names and image structure
92  Bstring matfile;
93  Bstring imgfile;
94  Bstring psfile;
95  Bstring propfile;
96  Bstring simfile; // Use default similarity matrix file
97 
98  int optind;
99  Boption* option = get_option_list(use, argc, argv, optind);
100  Boption* curropt;
101  for ( curropt = option; curropt; curropt = curropt->next ) {
102  if ( curropt->tag == "pair" )
103  do_pair = 1;
104  if ( curropt->tag == "consolidategaps" )
105  consolidate_gaps = 1;
106  if ( curropt->tag == "identity" )
107  do_id = 1;
108  if ( curropt->tag == "similarity" ) {
109  if ( ( sim_threshold = curropt->value.real() ) < 0.01 )
110  cerr << "-similarity: A threshold must be specified!" << endl;
111  else
112  do_sim = 1;
113  }
114  if ( curropt->tag == "profile" )
115  do_profile = 1;
116  if ( curropt->tag == "information" )
117  do_info = 1;
118  if ( curropt->tag == "hydrophobicity" ) {
119  if ( ( rep_threshold = curropt->value.real() ) < 0.001 )
120  cerr << "-hydrophobicity: A threshold must be specified!" << endl;
121  else
122  do_hp = 1;
123  }
124  if ( curropt->tag == "window" )
125  if ( ( window = curropt->value.integer() ) < 1 )
126  cerr << "-window: A window size must be specified!" << endl;
127  if ( curropt->tag == "gap" ) {
128  if ( ( i = curropt->values(gapopen, gapextend) ) < 1 )
129  cerr << "-gap: At least one gap penalty must be specified!" << endl;
130  else if ( i == 1 )
131  gapextend = gapopen;
132  }
133  if ( curropt->tag == "nogaps" )
134  atom_select = "NOGAP";
135  if ( curropt->tag == "segments" ) {
136  segments = curropt->value;
137  if ( segments.length() < 1 )
138  cerr << "-segments: Numbers must be specified!" << endl;
139  }
140  if ( curropt->tag == "sequences" ) {
141  if ( curropt->values(sub1, sub2) < 2 )
142  cerr << "-sequences: Two numbers must be specified!" << endl;
143  else {
144  if ( sub1 > 0 ) sub1--;
145  if ( sub2 > sub1 ) sub2--;
146  else sub2 = sub1;
147  }
148  }
149  if ( curropt->tag == "delete" ) delseq = 1;
150  if ( curropt->tag == "select" )
151  if ( curropt->values(ref, cutoff) < 2 )
152  cerr << "-select: Two numbers must be specified!" << endl;
153  if ( curropt->tag == "length" )
154  if ( curropt->values(minlen, maxlen) < 2 )
155  cerr << "-length: Two lengths must be specified!" << endl;
156  if ( curropt->tag == "properties" )
157  propfile = curropt->filename();
158  if ( curropt->tag == "Similarity" )
159  simfile = curropt->filename();
160  if ( curropt->tag == "Matrix" )
161  matfile = curropt->filename();
162  if ( curropt->tag == "Image" )
163  imgfile = curropt->filename();
164  if ( curropt->tag == "Postscript" )
165  psfile = curropt->filename();
166  }
167  option_kill(option);
168 
169  double ti = timer_start();
170 
171  // Read the molecule file
172  Bstring filename(argv[optind++]);
173  Bmolgroup* molgroup = read_molecule(filename, atom_select, propfile);
174  if ( !molgroup ) {
175  cerr << "Error: No molecules read!" << endl;
176  bexit(-1);
177  }
178 
179  molecule_update_comment(molgroup, argc, argv);
180 
181  Bmolecule* mol = NULL;
182  char* profile = NULL;
183 
184  // Check the moving average window size
185  if ( window < 2 ) window = 2;
186  if ( window > 1000 ) window = 1000;
187 
188  // Set the tags according to the sequences selected
189  if ( sub2 > 0 ) {
190  for ( j=0; j<molgroup->maxlen; j++ )
191  molgroup->seqflag[j] = 0;
192  for ( i=0, mol = molgroup->mol; mol; i++, mol = mol->next ) {
193  if ( mol->seq.length() && i>=sub1 && i<=sub2 ) {
194  for ( j=0; j<mol->seq.length(); j++ )
195  if ( mol->seq[j] != '-' )
196  molgroup->seqflag[j] = 1;
197  }
198  }
199  }
200 
201  // Get the selection
202 /* if ( segments.length() ) {
203  pnt = segments;
204  n = 0;
205  while ( pnt && sscanf(pnt, "%d", &sel[n]) ) {
206  if ( ( pnt = strchr(pnt, ',') ) ) pnt++;
207  n++;
208  }
209  if ( 2*(n/2) != n ) {
210  sel[n] = molgroup->maxlen - 1;
211  n++;
212  }
213  i = flag = 0;
214  for ( j=0; j<molgroup->maxlen; j++ ) {
215  if ( j == sel[i] + flag - 1 ) {
216  i++;
217  flag = 1 - flag;
218  }
219  molgroup->seqflag[j] *= flag;
220  }
221  }
222 */
223  if ( segments.length() )
224  select_numbers(segments, molgroup->maxlen, molgroup->seqflag);
225 
226  j = 0;
227  for ( i=0; i<molgroup->maxlen; i++ )
228  j += molgroup->seqflag[i];
229 
230  if ( verbose & VERB_PROCESS )
231  cout << "Alignment positions flagged: " << j << endl << endl;
232 
233  Bresidue_matrix* simat = get_residue_matrix(simfile);
234 
235  if ( do_pair )
236  molgroup->maxlen = seq_pair_align(molgroup->mol, molgroup->mol->next, gapopen, gapextend, simat);
237 
238  if ( consolidate_gaps ) molgroup_consolidate_gaps(molgroup);
239 
240  Matrix mat;
241  if ( do_id ) mat = seq_aligned_identity(molgroup);
242 
243  if ( do_sim ) mat = seq_aligned_similarity(molgroup, sim_threshold, simat);
244 
245  if ( maxlen ) seq_select(molgroup, minlen, maxlen);
246 
247  if ( ref ) seq_select(molgroup, mat, ref, cutoff);
248 
249  if ( delseq ) seq_delete(molgroup, mat);
250 
251  if ( do_profile ) profile = seq_aligned_profile(molgroup);
252 
253  if ( do_hp ) seq_aligned_hydrophobicity(molgroup, window, rep_threshold, propfile, psfile);
254 
255  if ( do_info ) seq_aligned_information(molgroup, window, psfile);
256 
257  if ( optind < argc )
258  write_molecule(argv[optind], molgroup);
259 
260  if ( matfile.length() && mat.rows() )
261  mat.write(matfile);
262 
263  if ( imgfile.length() && mat.rows() ) {
264  Bimage* pimg = new Bimage(mat, 1);
265  pimg->statistics();
266  write_img(imgfile, pimg, 0);
267  delete pimg;
268  }
269 
270  molgroup_kill(molgroup);
271 
272  residue_matrix_kill(simat);
273 
274  if ( profile ) delete[] profile;
275 
276  if ( verbose & VERB_TIME )
277  timer_report(ti);
278 
279  bexit(0);
280 }
281 
Boption
A class for a command line input option.
Definition: options.h:26
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
Bmolgroup::seqflag
int * seqflag
Definition: rwmolecule.h:188
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
seq_aligned_profile
char * seq_aligned_profile(Bmolgroup *molgroup)
Generates a PROSITE format profile from an aligned set of sequences.
Definition: seq_analysis.cpp:422
Bstring::integer
long integer()
Definition: Bstring.cpp:547
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
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_delete
long seq_delete(Bmolgroup *molgroup, Matrix mat)
Deletes non-selected sequences and corresponding elelments of a comparison matrix.
Definition: seq_analysis.cpp:362
use
const char * use[]
Definition: bseqaln.cpp:21
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.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
rwresprop.h
Header file for reading residue property files.
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Matrix::rows
long rows()
Definition: Matrix.h:153
Boption::value
Bstring value
Definition: options.h:30
seq_align.h
Header file for functions to generate and analyze dot plots.
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
seq_aligned_similarity
Matrix seq_aligned_similarity(Bmolgroup *molgroup, double threshold, Bresidue_matrix *simat)
Calculates the pairwise similarities between aligned sequences.
Definition: seq_analysis.cpp:152
seq_aligned_hydrophobicity
int seq_aligned_hydrophobicity(Bmolgroup *molgroup, int window, double threshold, Bstring &hphobfile, Bstring &psfile)
Calculates the average hydrophobicity at every position in an alignment.
Definition: seq_analysis.cpp:681
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
main
int main(int argc, char **argv)
Definition: bseqaln.cpp:64
Bmolgroup::maxlen
long maxlen
Definition: rwmolecule.h:180
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
verbose
int verbose
utilities.h
Header file for general utilities.
seq_analysis.h
Header file for sequence analysis functions.
Bstring::length
long length() const
Definition: Bstring.cpp:263
seq_aligned_information
int seq_aligned_information(Bmolgroup *molgroup, int window, Bstring &psfile)
Calculates the sequence logo representation for an alignment.
Definition: seq_analysis.cpp:527
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
write_molecule
int write_molecule(char *filename, Bmolgroup *molgroup)
Writes a molecule group.
Definition: rwmolecule.cpp:975
rwimg.h
Header file for 2D and 3D image I/O.
molgroup_consolidate_gaps
int molgroup_consolidate_gaps(Bmolgroup *molgroup)
Removes redundant gaps from an alignment.
Definition: rwmolecule.cpp:1249
Boption::filename
Bstring filename()
Definition: options.h:85
residue_matrix_kill
int residue_matrix_kill(Bresidue_matrix *resmat)
Deallocating a residue matrix.
Definition: rwresprop.cpp:208
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Matrix::write
void write(Bstring &filename)
Definition: Matrix.h:95
Bmolgroup
Definition: rwmolecule.h:170
seq_select
long seq_select(Bmolgroup *molgroup, long minlen, long maxlen)
Selects sequences within a range of lengths.
Definition: seq_analysis.cpp:233
Matrix
Definition: Matrix.h:22
seq_pair_align
long seq_pair_align(Bmolecule *mol1, Bmolecule *mol2, double gapopen, double gapextend, Bresidue_matrix *simat)
Aligns two sequences.
Definition: seq_align.cpp:34
Bresidue_matrix
Definition: rwresprop.h:44
seq_aligned_identity
Matrix seq_aligned_identity(Bmolgroup *molgroup)
Calculates the pairwise identities between aligned sequences.
Definition: seq_analysis.cpp:73
select_numbers
int select_numbers(Bstring &string, int n, int *numsel)
Converts a string with a selection specification into an integer array.
Definition: utilities.cpp:335
get_residue_matrix
Bresidue_matrix * get_residue_matrix(Bstring &filename)
Reading a residue matrix from a file.
Definition: rwresprop.cpp:164
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22