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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/*==========================================================================
SeqAn - The Library for Sequence Analysis
http://www.seqan.de
============================================================================
Copyright (C) 2007-2012
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
==========================================================================*/
#include <seqan/basic.h>
#include <seqan/align.h>
#include <seqan/modifier.h>
#include <seqan/arg_parse.h>
#include <iostream>
#include <fstream>
using namespace seqan;
// --------------------------------------------------------------------------
// Class Options
// --------------------------------------------------------------------------
struct Options
{
static int const INVALID_DIAGONAL;
seqan::CharString inputFile;
seqan::CharString outputFile;
seqan::CharString alphabet;
seqan::CharString method;
int outputFormat;
int gop;
int gex;
seqan::CharString matrix;
int msc;
int mmsc;
int low;
int high;
seqan::CharString config;
Options() : gop(0), gex(0), msc(0), mmsc(0), low(INVALID_DIAGONAL), high(INVALID_DIAGONAL)
{}
};
int const Options::INVALID_DIAGONAL = seqan::MaxValue<int>::VALUE;
//////////////////////////////////////////////////////////////////////////////////
template <typename TSeqSet, typename TNameSet>
bool _loadSequences(TSeqSet& sequences,
TNameSet& fastaIDs,
const char *fileName)
{
MultiFasta multiFasta;
if (!open(multiFasta.concat, fileName, OPEN_RDONLY)) return false;
AutoSeqFormat format;
guessFormat(multiFasta.concat, format);
split(multiFasta, format);
unsigned seqCount = length(multiFasta);
resize(sequences, seqCount, Exact());
resize(fastaIDs, seqCount, Exact());
for(unsigned i = 0; i < seqCount; ++i)
{
assignSeqId(fastaIDs[i], multiFasta[i], format);
assignSeq(sequences[i], multiFasta[i], format);
}
return (seqCount > 0);
}
// TODO(holtgrew): Make publically available.
template<typename TStringSet, typename TCargo, typename TSpec>
inline int
globalAlignment(Graph<Alignment<TStringSet, TCargo, TSpec> >& g,
Lcs)
{
return globalAlignment(g, stringSet(g), Lcs());
}
//////////////////////////////////////////////////////////////////////////////////
template<typename TAlphabet, typename TAlignConfig, typename TScore, typename TSeqFile, typename TMethod, typename TDiag, typename TOutputFormat, typename TOutfile>
inline void
pairwise_align(TScore const& sc,
TSeqFile& seqfile,
TMethod method,
TDiag low,
TDiag high,
bool banded,
TOutputFormat outputFormat,
TOutfile& outfile)
{
// Load the 2 sequences
typedef String<TAlphabet> TSequence;
StringSet<TSequence, Owner<> > sequenceSet;
StringSet<String<char> > sequenceNames;
_loadSequences(sequenceSet, sequenceNames, seqfile.c_str());
// Fix low and high diagonal.
low = _max(low, -1 * (int) length(sequenceSet[1]));
high = _min(high, (int) length(sequenceSet[0]));
// Align the sequences
Graph<Alignment<StringSet<TSequence, Dependent<> >, void, WithoutEdgeId> > gAlign(sequenceSet);
int aliScore = 0;
// Banded alignment?
if (!banded) {
if (method == 0) aliScore = globalAlignment(gAlign, sc, TAlignConfig(), NeedlemanWunsch());
else if (method == 1) aliScore = globalAlignment(gAlign, sc, TAlignConfig(), Gotoh());
else if (method == 2) aliScore = localAlignment(gAlign, sc);
else if (method == 3) aliScore = globalAlignment(gAlign, Lcs());
} else {
if (method == 0) aliScore = globalAlignment(gAlign, sc, TAlignConfig(), low, high, NeedlemanWunsch());
else if (method == 1) aliScore = globalAlignment(gAlign, sc, TAlignConfig(), low, high, Gotoh());
}
// Alignment output
std::cout << "Alignment score: " << aliScore << std::endl;
if (outputFormat == 0) {
FILE* strmWrite = fopen(outfile.c_str(), "w");
write(strmWrite, gAlign, sequenceNames, FastaFormat());
fclose(strmWrite);
} else if (outputFormat == 1) {
FILE* strmWrite = fopen(outfile.c_str(), "w");
write(strmWrite, gAlign, sequenceNames, MsfFormat());
fclose(strmWrite);
}
}
//////////////////////////////////////////////////////////////////////////////////
template<typename TScore, typename TSc>
inline void
_setMatchScore(TScore&, TSc) {
// No operation
}
//////////////////////////////////////////////////////////////////////////////////
template<typename TScore, typename TSc>
inline void
_setMismatchScore(TScore&, TSc) {
// No operation
}
//////////////////////////////////////////////////////////////////////////////////
template<typename TSc>
inline void
_setMatchScore(Score<int, Simple>& sc, TSc msc) {
sc.data_match = msc;
}
//////////////////////////////////////////////////////////////////////////////////
template<typename TSc>
inline void
_setMismatchScore(Score<int, Simple>& sc, TSc mmsc) {
sc.data_mismatch = mmsc;
}
//////////////////////////////////////////////////////////////////////////////////
template<typename TAlphabet, typename TScore>
inline void
_initAlignParams(Options const & options, TScore& sc) {
// Set options
sc.data_gap_open = options.gop;
sc.data_gap_extend = options.gex;
int msc = options.msc;
_setMatchScore(sc, msc);
int mmsc = options.mmsc;
_setMismatchScore(sc, mmsc);
::std::string seqfile = toCString(options.inputFile);
::std::string outfile = toCString(options.outputFile);
unsigned int method = 0;
String<char> meth = options.method;
if (meth == "nw") method = 0;
else if (meth == "gotoh") method = 1;
else if (meth == "sw") method = 2;
else if (meth == "lcs") method = 3;
int low = 0;
int high = 0;
bool banded = false;
if (options.low != Options::INVALID_DIAGONAL)
{
low = options.low;
banded = true;
}
if (options.high != Options::INVALID_DIAGONAL)
{
high = options.high;
banded = true;
}
// Check options
if (low > high) banded = false;
// Do pairwise alignment
String<char> config = options.config;
if (!empty(config))
{
if (config == "tttt")
pairwise_align<TAlphabet, AlignConfig<true, true, true, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "tttf")
pairwise_align<TAlphabet, AlignConfig<true, true, true, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "ttft")
pairwise_align<TAlphabet, AlignConfig<true, true, false, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "ttff")
pairwise_align<TAlphabet, AlignConfig<true, true, false, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "tftt")
pairwise_align<TAlphabet, AlignConfig<true, false, true, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "tftf")
pairwise_align<TAlphabet, AlignConfig<true, false, true, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "tfft")
pairwise_align<TAlphabet, AlignConfig<true, false, false, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "tfff")
pairwise_align<TAlphabet, AlignConfig<true, false, false, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "fttt")
pairwise_align<TAlphabet, AlignConfig<false, true, true, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "fttf")
pairwise_align<TAlphabet, AlignConfig<false, true, true, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "ftft")
pairwise_align<TAlphabet, AlignConfig<false, true, false, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "ftff")
pairwise_align<TAlphabet, AlignConfig<false, true, false, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "fftt")
pairwise_align<TAlphabet, AlignConfig<false, false, true, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "fftf")
pairwise_align<TAlphabet, AlignConfig<false, false, true, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "ffft")
pairwise_align<TAlphabet, AlignConfig<false, false, false, true> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
else if (config == "ffff")
pairwise_align<TAlphabet, AlignConfig<false, false, false, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
}
else
{
pairwise_align<TAlphabet, AlignConfig<false, false, false, false> >(sc, seqfile, method, low, high, banded, options.outputFormat, outfile);
}
}
//////////////////////////////////////////////////////////////////////////////////
inline void
_initScoreMatrix(Options const & options, Dna5 const) {
String<char> matrix = options.matrix;
if (!empty(options.matrix))
{
Score<int, ScoreMatrix<> > sc;
loadScoreMatrix(sc, options.matrix);
_initAlignParams<Dna5>(options, sc);
}
else
{
Score<int> sc;
_initAlignParams<Dna5>(options, sc);
}
}
//////////////////////////////////////////////////////////////////////////////////
inline void
_initScoreMatrix(Options const & options, char const) {
String<char> matrix = options.matrix;
if (!empty(options.matrix))
{
Score<int, ScoreMatrix<> > sc;
loadScoreMatrix(sc, options.matrix);
_initAlignParams<char>(options, sc);
}
else
{
Score<int> sc;
_initAlignParams<char>(options, sc);
}
}
//////////////////////////////////////////////////////////////////////////////////
inline void
_initScoreMatrix(Options const & options, Rna5 const) {
String<char> matrix = options.matrix;
if (!empty(options.matrix))
{
Score<int, ScoreMatrix<> > sc;
loadScoreMatrix(sc, options.matrix);
_initAlignParams<Rna5>(options, sc);
}
else
{
Score<int> sc;
_initAlignParams<Rna5>(options, sc);
}
}
//////////////////////////////////////////////////////////////////////////////////
inline void
_initScoreMatrix(Options const & options, AminoAcid const) {
String<char> matrix = options.matrix;
if (!empty(options.matrix))
{
Score<int, ScoreMatrix<> > sc;
loadScoreMatrix(sc, options.matrix);
_initAlignParams<AminoAcid>(options, sc);
}
else
{
Blosum62 sc;
_initAlignParams<AminoAcid>(options, sc);
}
}
// --------------------------------------------------------------------------
// Function parseCommandLine()
// --------------------------------------------------------------------------
seqan::ArgumentParser::ParseResult
parseCommandLine(Options & options, int argc, char const ** argv)
{
// Setup ArgumentParser.
seqan::ArgumentParser parser("pair_align");
// Set short description, version, and date.
setShortDescription(parser, "Pairwise alignment");
setVersion(parser, "1.1");
setDate(parser, "November 2012");
// Define usage line and long description.
addUsageLine(parser, "[\\fIOPTIONS\\fP] \\fB-s\\fP \\fIIN.fa\\fP");
setCategory(parser, "Sequence Alignment");
addDescription(parser,
"The program allows to align two sequences using dyamic programming alignment algorithms while "
"tweaking various parameters.");
addSection(parser, "Main Options");
addOption(parser, seqan::ArgParseOption("s", "seq", "FASTA file with two sequences.", seqan::ArgParseOption::INPUTFILE, "IN.fa"));
setRequired(parser, "seq");
setValidValues(parser, "seq", "fasta fa");
addOption(parser, seqan::ArgParseOption("a", "alphabet", "Sequence alphabet.", seqan::ArgParseOption::STRING, "ALPHABET"));
setValidValues(parser, "alphabet", "protein dna rna text");
setDefaultValue(parser, "alphabet", "protein");
addOption(parser, seqan::ArgParseOption("m", "method",
"DP alignment method: Needleman-Wunsch, Gotoh, Smith-Waterman, "
"Longest Common Subsequence",
seqan::ArgParseOption::STRING, "METHOD"));
setValidValues(parser, "method", "nw gotoh sw lcs");
setDefaultValue(parser, "method", "gotoh");
addOption(parser, seqan::ArgParseOption("o", "outfile", "Output filename.", seqan::ArgParseOption::OUTPUTFILE, "OUT"));
setDefaultValue(parser, "outfile", "out.fasta");
setValidValues(parser, "outfile", "fa fasta msf");
//TODO(rmaerker): We removed this option. The file format is derived from the outfile format.
//addOption(parser, seqan::ArgParseOption("f", "format", "Output format.", seqan::ArgParseOption::STRING));
//setValidValues(parser, "format", "fa fasta msf");
//setDefaultValue(parser, "format", "fasta");
addSection(parser, "Scoring Options");
addOption(parser, seqan::ArgParseOption("g", "gop", "Gap open penalty.", seqan::ArgParseOption::INTEGER, "INT"));
setDefaultValue(parser, "gop", "-11");
addOption(parser, seqan::ArgParseOption("e", "gex", "Gap extension penalty.", seqan::ArgParseOption::INTEGER, "INT"));
setDefaultValue(parser, "gex", "-1");
addOption(parser, seqan::ArgParseOption("ma", "matrix", "Score matrix.", seqan::ArgParseOption::STRING, "MATRIX_FILE"));
addOption(parser, seqan::ArgParseOption("ms", "msc", "Match score.", seqan::ArgParseOption::INTEGER, "INT"));
setDefaultValue(parser, "msc", "5");
addOption(parser, seqan::ArgParseOption("mm", "mmsc", "Mismatch penalty.", seqan::ArgParseOption::INTEGER, "INT"));
setDefaultValue(parser, "mmsc", "-4");
addSection(parser, "Banded Alignment Options");
addOption(parser, seqan::ArgParseOption("lo", "low", "Lower diagonal.", seqan::ArgParseOption::INTEGER, "INT"));
addOption(parser, seqan::ArgParseOption("hi", "high", "Upper diagonal.", seqan::ArgParseOption::INTEGER, "INT"));
addSection(parser, "DP Matrix Configuration Options");
addOption(parser, seqan::ArgParseOption("c", "config", "Alignment configuration.", seqan::ArgParseOption::STRING, "CONF"));
setValidValues(parser, "config", "ffff ffft fftf fftt ftff ftft fttf fttt tfff tfft tftf tftt ttff ttft tttf tttt");
addTextSection(parser, "Alignment configuration");
addText(parser,
"The alignment configuration is a string of four characters, each being either t or f. All "
"combinations are allowed. The meaning is as follows.");
addListItem(parser, "tfff", "First row initialized with 0s.");
addListItem(parser, "ftff", "First column initialized with 0s.");
addListItem(parser, "fftf", "Search last column for maximum.");
addListItem(parser, "ffft", "Search last row for maximum.");
// Parse command line.
seqan::ArgumentParser::ParseResult res = seqan::parse(parser, argc, argv);
// Only extract options if the program will continue after parseCommandLine()
if (res != seqan::ArgumentParser::PARSE_OK)
return res;
getOptionValue(options.inputFile, parser, "seq");
getOptionValue(options.outputFile, parser, "outfile");
// Guess file format based on extension of file.
CharString tmp = options.outputFile;
if (endsWith(tmp, ".fa") || endsWith(tmp, "fasta"))
options.outputFormat = 0;
else if (endsWith(tmp, ".msf"))
options.outputFormat = 1;
getOptionValue(options.alphabet, parser, "alphabet");
getOptionValue(options.method, parser, "method");
//getOptionValue(options.format, parser, "format");
getOptionValue(options.gop, parser, "gop");
getOptionValue(options.gex, parser, "gex");
getOptionValue(options.matrix, parser, "matrix");
getOptionValue(options.msc, parser, "msc");
getOptionValue(options.mmsc, parser, "mmsc");
getOptionValue(options.low, parser, "low");
getOptionValue(options.high, parser, "high");
getOptionValue(options.config, parser, "config");
return seqan::ArgumentParser::PARSE_OK;
}
// --------------------------------------------------------------------------
// Function main()
// --------------------------------------------------------------------------
int main(int argc, const char *argv[])
{
// Parse the command line.
Options options;
seqan::ArgumentParser::ParseResult res = parseCommandLine(options, argc, argv);
// If there was an error parsing or built-in argument parser functionality
// was triggered then we exit the program. The return code is 1 if there
// were errors and 0 if there were none.
if (res != seqan::ArgumentParser::PARSE_OK)
return res == seqan::ArgumentParser::PARSE_ERROR;
// Initialize scoring matrices
if (options.alphabet == "dna") _initScoreMatrix(options, Dna5());
else if (options.alphabet == "rna") _initScoreMatrix(options, Rna5());
else if (options.alphabet == "protein") _initScoreMatrix(options, AminoAcid());
else _initScoreMatrix(options, char());
return 0;
}
|