-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofileToAlignmentProfile.cpp
More file actions
285 lines (251 loc) · 11.2 KB
/
profileToAlignmentProfile.cpp
File metadata and controls
285 lines (251 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @file profileToAlignmentProfile.cpp
*
* @author Ted Holzman <tholzman@scharp.org>
* modified from the Align.cpp file by Paul Edlefsen <pedlefsen@gmail.com>
* @version 0.1
* @date 02/01/2012
* @section LICENSE
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* \brief Create one (or more) "Alignment Profiles" from a fasta file and a
* a profile
*
* @section DESCRIPTION
* Part of the \a profuse library.
*
* Converts a "standard" galosh profile to an alignment profile containing
* positional information about insertions and deletions. It gleans this
* information from a fasta file containing several sequences. profileToAlignmentProfile
* aligns these sequences, determines the indels, and combines the information
* with the given profile to produce an Alignment Profile.
*
* The following options are understood:
*<p><pre>
* Usage: profileToAlignmentProfile [options] <profile-file-name> <fasta-file-name>
*
* profileToAlignmentProfile options:
*
* -h [ --help ] output this help message
* --verbosity arg (=0) set verbosity level (0 - 3)
* -i [ --individual ] output individual alignment profiles instead of
* combined
* -n [ --nseq ] arg number of sequences to use (default is ALL)
* -v [ --viterbi ] use viterbi algorithm
* </pre>
*
*/
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
#include "GenAlignmentProfiles.hpp"
#ifdef __HAVE_MUSCLE
int g_argc;
char **g_argv;
#endif // __HAVE_MUSCLE
using namespace galosh;
// forward decls
string escape_path ( const string & unescaped_path );
/**
* \fn int main(int const argc, char ** argv)
* \brief main driver. Parses command line and calls gen_alignment_profiles
* prints alignment profile output.
*/
int
main ( int const argc, char ** argv )
{
typedef doublerealspace ProbabilityType;
typedef bfloat ScoreType;
typedef bfloat MatrixValueType;
#ifdef __PROFUSE_USE_AMINOS
typedef seqan::AminoAcid20 ResidueType;
typedef seqan::AminoAcid SequenceResidueType;
#else // __PROFUSE_USE_AMINOS .. else
typedef seqan::Dna ResidueType;
typedef seqan::Iupac SequenceResidueType;
#endif // __PROFUSE_USE_AMINOS .. else ..
try {
string config_file;
// Declare a group of options that will be
// allowed only on command line
po::options_description generic( "Generic options" );
generic.add_options()
( "version,v", "print version string" )
( "help,h", "produce help message" )
( "config,c", po::value<string>( &config_file )->default_value( "profuse.cfg" ),
"name of a file of a configuration." )
;
// Declare a group of options that will be
// allowed both on command line and in
// config file
po::options_description config( "Configuration" );
config.add_options()
( "profile,p",
po::value<string>(),
"filename: where to find the input profile" )
( "alignment_profiles_prefix,o",
po::value<string>()->default_value( "profillic_profileToAlignmentProfile" ),
"filename prefix: where to put the output alignment profiles" )
( "fasta,f",
po::value<string>(),
"input sequences, in (unaligned) Fasta format" )
("individual,i",
"output individual alignment profiles instead of average")
("individual-filename-suffix-pattern,s",
po::value<string>()->default_value( "_$n.aprof" ),
"pattern for filenames by which to differentiate output individual alignment profiles, in which $n will be replaced by the sequence name (possibly modified to be a valid filename) and $d with be replaced by the sequence number (default: '_$n.aprof'")
("nseq,n",
po::value<int>(),
"number of sequences to use (default is ALL)")
("viterbi,v", // todo: remove this. it's just for debugging.
"use viterbi algorithm")
;
// Hidden options, will be allowed both on command line and
// in config file, but will not be shown to the user.
// TODO: CHANGE THESE
//po::options_description lengthadjust_opts( "Lengthadjust options" );
//lengthadjust_opts.add_options()
// ( "proposeInsertingOccupancyThreshold,dms.occupancy_threshold",
// po::value<double>(),
// "DMS threshold for fraction of sequences inserting/deleting to trigger a model edit of a given position" )
// ;
typename DynamicProgramming<ResidueType, ProbabilityType, ScoreType, MatrixValueType>::Parameters params;
po::options_description cmdline_options;
cmdline_options.add( generic ).add( params.m_galosh_options_description ).add( config );//.add( lengthadjust_opts );
po::options_description config_file_options;
config_file_options.add( params.m_galosh_options_description ).add( config ); //.add( lengthadjust_opts );
po::options_description visible( "Basic options" );
visible.add( generic ).add( config );
po::positional_options_description p;
p.add( "profile", 1 );
p.add( "fasta", 1 );
p.add( "alignment_profiles_prefix", 1 );
store( po::command_line_parser( argc, argv ).options( cmdline_options ).positional( p ).run(), params.m_galosh_options_map );
notify( params.m_galosh_options_map );
// TODO: REMOVE
//cout << params << endl;
//cout << endl;
#define USAGE() " " << argv[ 0 ] << " [options] <profile file> <gapless fasta sequences file> [<output alignment profiles file prefix>]"
// Read in the config file.
if( config_file.length() > 0 ) {
ifstream ifs( config_file.c_str() );
if( !ifs ) {
if(!params.m_galosh_options_map["config"].defaulted()) { //TAH 3/13 don't choke if config file was defaulted and is missing
cout << "Can't open the config file named \"" << config_file << "\"\n";
return 0;
}
} else {
store( parse_config_file( ifs, config_file_options ), params.m_galosh_options_map );
notify( params.m_galosh_options_map );
}
}
if( params.m_galosh_options_map.count( "help" ) > 0 ) {
cout << "Usage: " << USAGE() << endl;
cout << visible << "\n";
return 0;
}
if( params.m_galosh_options_map.count( "version" ) ) {
cout << "profileToAlignmentProfile, version 1.0\n";
return 0;
}
//if( params.m_galosh_options_map.count( "debug" ) ) {
// cout << "[DEBUGGING]\n";
// return 0;
//}
// Required options
if( ( params.m_galosh_options_map.count( "profile" ) == 0 ) || ( params.m_galosh_options_map.count( "fasta" ) == 0 ) ) {
cout << "Usage: " << USAGE() << endl;
return 1;
}
//if( params.m_galosh_options_map.count( "include-path" ) ) {
// cout << "Include paths are: "
// << params.m_galosh_options_map["include-path"].as< vector<string> >() << "\n";
//}
//
// if (params.m_galosh_options_map.count("input-file"))
// {
// cout << "Input files are: "
// << params.m_galosh_options_map["input-file"].as< vector<string> >() << "\n";
// }
/// Do the work
const bool indiv_profiles = ( params.m_galosh_options_map ).count( "individual" ) > 0;
GenAlignmentProfiles<ProbabilityType, ScoreType, MatrixValueType, ResidueType, SequenceResidueType> genAlignProf;
std::vector<DynamicProgramming<ResidueType, ProbabilityType, ScoreType, MatrixValueType>::AlignmentProfile> alignment_profiles;
alignment_profiles = genAlignProf.gen_alignment_profiles( params );
const std::string output_filename_prefix = ( params.m_galosh_options_map )[ "alignment_profiles_prefix" ].template as<string>();
const std::string individual_filename_suffix_pattern = ( params.m_galosh_options_map )[ "individual-filename-suffix-pattern" ].template as<string>();
std::string individual_output_filename = output_filename_prefix;
if( indiv_profiles ) {
individual_output_filename += individual_filename_suffix_pattern;
}
string const * output_filename_ptr = &individual_output_filename;
/// Output the results
if( ( output_filename_ptr == NULL ) ) { //|| ( output_filename.compare( "" ) == 0 ) || ( output_filename.compare( "-" ) == 0 ) ) {
for( int i = 0; i < alignment_profiles.size(); i++ )
{
if( alignment_profiles.size() > 1 ) {
std:cout << "#" << alignment_profiles[ i ].m_comment << std::endl;
}
std::cout << alignment_profiles[ i ];
}
} else {
for( int i = 0; i < alignment_profiles.size(); i++ )
{
if( indiv_profiles ) {
individual_output_filename = output_filename_prefix + individual_filename_suffix_pattern;
if( alignment_profiles[ i ].m_comment.length() > 0 ) {
boost::replace_all( individual_output_filename, "$n", escape_path( alignment_profiles[ i ].m_comment ) );
} else {
boost::replace_all( individual_output_filename, "$n", boost::lexical_cast<std::string>( i ) );
}
boost::replace_all( individual_output_filename, "$d", boost::lexical_cast<std::string>( i ) );
output_filename_ptr = &individual_output_filename;
}
std::ofstream fs ( output_filename_ptr->c_str() );
if( !fs.is_open() ) {
// TODO: ?
cerr << "The alignment profiles output file '" << *output_filename_ptr << "' could not be opened." << endl;
} else {
fs << alignment_profiles[ i ];
fs.close();
// We print out the output files as a side effect
cout << *output_filename_ptr << endl;
}
} // End foreach alignment_profile i
} // End if profile_output_filename_ptr != NULL
return 0; // success
} catch( std::exception& e ) { /// exceptions thrown by boost stuff
cerr << "error: " << e.what() << endl;
return 1;
} catch( string &err ) { /// exceptions thrown by GenAlignmentProfiles, etc.
cerr << "error: " << err << endl;
return 1;
} catch( ... ) { /// anything else
cerr << "Strange unknown exception" << endl;
return 1;
}
} // main (..)
string escape_path ( const string & unescaped_path )
{
std::string escaped_path_str( unescaped_path );
boost::replace_all( escaped_path_str, "|", "-x-BAR-x-" );
boost::replace_all( escaped_path_str, "/", "-x-SLASH-x-" );
boost::replace_all( escaped_path_str, "\\", "-x-BACKSLASH-x-" );
boost::replace_all( escaped_path_str, "..", "-x-DOTDOT-x-" );
boost::replace_all( escaped_path_str, ".", "-x-DOT-x-" );
boost::filesystem::path escaped_path( escaped_path_str );
// TODO: REMOVE
//cout << "escape_path( " << unescaped_path << " ): " << escaped_path.native() << endl;
return( escaped_path.native() );
} // escape_path ( const string & )