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
|
#include <cstdlib>
//![include_seq_io]
#include <seqan/seq_io.h>
#include <seqan/stream.h>
//![include_seq_io]
//![include_align]
#include <seqan/align.h>
//![include_align]
//![include_arg_parse]
#include <seqan/arg_parse.h>
//![include_arg_parse]
//![include_index]
#include <seqan/index.h>
//![include_index]
//![include_seeds]
#include <seqan/seeds.h>
//![include_seeds]
using namespace seqan2;
std::stringstream quiet_output;
//![lagan_option]
struct LaganOption
{
std::string seq1Filename;
std::string seq2Filename;
std::string outFilename;
unsigned qGramSize;
unsigned distanceCriteria;
unsigned gapCriteria;
};
//![lagan_option]
/*
//![parse_arguments]
auto parseCommandLine(LaganOption & options, int const argc, char * argv[])
{
ArgumentParser parser("lagan");
// Set short description, version, and date.
setShortDescription(parser, "Glocal alignment computation.");
setVersion(parser, "0.1");
setDate(parser, "September 2017");
// Define usage line and long description.
addUsageLine(parser,
"[\\fIOPTIONS\\fP] \"\\fISEQUENCE_1\\fP\" \"\\fISEQUENCE_2\\fP\"");
addDescription(parser,
"lagan is a large-scale sequence alignment tool "
"using a glocal alignment approach. It first filters for good seeding matches which are "
"chained together using CHAOS chaining and finally a global chain is computed and "
"the alignment around this chain.");
// We require two arguments.
addArgument(parser, seqan2::ArgParseArgument(seqan2::ArgParseArgument::INPUT_FILE, "SEQUENCE_1"));
addArgument(parser, seqan2::ArgParseArgument(seqan2::ArgParseArgument::INPUT_FILE, "SEQUENCE_2"));
addOption(parser, seqan2::ArgParseOption("o", "output", "Output file to write the alignment to.",
ArgParseArgument::OUTPUT_FILE, "FILE"));
setValidValues(parser, "output", ".align");
setDefaultValue(parser, "output", "out.align");
addOption(parser, seqan2::ArgParseOption("q", "qgram", "Size of the qGram.", ArgParseArgument::INTEGER, "INT"));
addDefaultValue(parser, "qgram", "12");
addOption(parser, seqan2::ArgParseOption("d", "distance", "Distance criteria for CHAOS chaining.",
ArgParseArgument::INTEGER, "INT"));
addDefaultValue(parser, "distance", "10");
addOption(parser, seqan2::ArgParseOption("g", "gap", "Gap criteria for CHAOS chaining.",
ArgParseArgument::INTEGER, "INT"));
addDefaultValue(parser, "gap", "15");
auto parseResult = parse(parser, argc, argv);
if (parseResult == ArgumentParser::PARSE_OK)
{
getArgumentValue(options.seq1Filename, parser, 0);
getArgumentValue(options.seq2Filename, parser, 1);
getOptionValue(options.outFilename, parser, "output");
getOptionValue(options.qGramSize, parser, "qgram");
getOptionValue(options.distanceCriteria, parser, "distance");
getOptionValue(options.gapCriteria, parser, "gap");
}
return parseResult;
}
//![parse_arguments]
*/
auto parseCommandLine(LaganOption & options, int const argc, char * argv[])
{
ArgumentParser parser("lagan");
// Set short description, version, and date.
setShortDescription(parser, "Glocal alignment computation.");
setVersion(parser, "0.1");
setDate(parser, "September 2017");
// Define usage line and long description.
addUsageLine(parser,
"[\\fIOPTIONS\\fP] \"\\fISEQUENCE_1\\fP\" \"\\fISEQUENCE_2\\fP\"");
addDescription(parser,
"lagan is a large-scale sequence alignment tool "
"using a glocal alignment approach. It first filters for good seeding matches which are "
"chained together using CHAOS chaining and finally a global chain is computed and "
"the alignment around this chain.");
// We require two arguments.
addArgument(parser, seqan2::ArgParseArgument(seqan2::ArgParseArgument::INPUT_FILE, "SEQUENCE_1"));
addArgument(parser, seqan2::ArgParseArgument(seqan2::ArgParseArgument::INPUT_FILE, "SEQUENCE_2"));
addOption(parser, seqan2::ArgParseOption("o", "output", "Output file to write the alignment to.",
ArgParseArgument::OUTPUT_FILE, "FILE"));
setValidValues(parser, "output", ".align");
setDefaultValue(parser, "output", "out.align");
addOption(parser, seqan2::ArgParseOption("q", "qgram", "Size of the qGram.", ArgParseArgument::INTEGER, "INT"));
addDefaultValue(parser, "qgram", "12");
addOption(parser, seqan2::ArgParseOption("d", "distance", "Distance criteria for CHAOS chaining.",
ArgParseArgument::INTEGER, "INT"));
addDefaultValue(parser, "distance", "10");
addOption(parser, seqan2::ArgParseOption("g", "gap", "Gap criteria for CHAOS chaining.",
ArgParseArgument::INTEGER, "INT"));
addDefaultValue(parser, "gap", "15");
auto parseResult = parse(parser, argc, argv, quiet_output, quiet_output);
if (parseResult == ArgumentParser::PARSE_OK)
{
getArgumentValue(options.seq1Filename, parser, 0);
getArgumentValue(options.seq2Filename, parser, 1);
getOptionValue(options.outFilename, parser, "output");
getOptionValue(options.qGramSize, parser, "qgram");
getOptionValue(options.distanceCriteria, parser, "distance");
getOptionValue(options.gapCriteria, parser, "gap");
}
return parseResult;
}
/* load the sequences */
/*
//![load_sequence_template]
inline std::pair<Dna5String, CharString>
loadSequence(std::string const & fileName)
{
// Write your code here.
}
//![load_sequence_template]
*/
//![load_sequence_solution]
inline std::pair<CharString, Dna5String>
loadSequence(std::string const & fileName)
{
if (empty(fileName))
{
std::cerr << "Invalid file name!\n";
std::exit(EXIT_FAILURE);
}
try {
SeqFileIn seqFile(fileName.c_str());
Dna5String seq;
CharString seqId;
readRecord(seqId, seq, seqFile);
return {std::move(seqId), std::move(seq)};
}
catch (ParseError & error)
{
std::cerr << "Error while parsing " << fileName << "!\n";
std::cerr << error.what() << '\n';
std::exit(EXIT_FAILURE);
}
catch (IOError & error)
{
std::cerr << "Could not read " << fileName << "!\n";
std::cerr << error.what() << '\n';
std::exit(EXIT_FAILURE);
}
}
//![load_sequence_solution]
/*
//![initial_main]
int main(int const argc, char * argv[])
{
LaganOption options;
if (parseCommandLine(options, argc, argv) != ArgumentParser::PARSE_OK)
{
std::exit(EXIT_FAILURE); // terminate the program.
}
}
//![initial_main]
*/
int main(int const argc, char * argv[])
{
LaganOption options;
if (parseCommandLine(options, argc, argv) != ArgumentParser::PARSE_OK)
{
std::exit(EXIT_SUCCESS); // terminate the program.
}
//![load_seq_1]
/* load the sequences */
CharString seq1Id;
Dna5String seq1;
std::tie(seq1Id, seq1) = loadSequence(options.seq1Filename);
std::cout << "Loaded " << seq1Id << " (" << length(seq1) << "bp)\n";
//![load_seq_1]
//![load_seq_2]
CharString seq2Id;
Dna5String seq2;
std::tie(seq2Id, seq2) = loadSequence(options.seq2Filename);
std::cout << "Loaded " << seq2Id << " (" << length(seq2) << "bp)\n";
//![load_seq_2]
//![declare_index]
using TIndex = Index<Dna5String, IndexQGram<Shape<Dna5, SimpleShape>, OpenAddressing>>;
//![declare_index]
//![declare_seed_set]
using TSeed = Seed<ChainedSeed>;
using TSeedSize = typename Size<TSeed>::Type;
using TSeedSet = SeedSet<TSeed>;
//![declare_seed_set]
//![init_seed_set]
TSeedSet seedSet;
Score<int, Simple> scoreScheme{2, -1, -2};
//![init_seed_set]
//![init_index]
TIndex qGramIndex{seq1};
// Initialize the shape.
resize(indexShape(qGramIndex), options.qGramSize);
hashInit(indexShape(qGramIndex), begin(seq2, Standard()));
//![init_index]
/*
//![solution_assignment2]
for (auto seqIter = begin(seq2, Standard());
seqIter != end(seq2, Standard()) - length(indexShape(qGramIndex)) + 1;
++seqIter)
{
hashNext(indexShape(qGramIndex), seqIter);
std::cout << getOccurrences(qGramIndex, indexShape(qGramIndex));
}
//![solution_assignment2]
*/
//![solution_seeding]
for (auto seqIter = begin(seq2, Standard());
seqIter != end(seq2, Standard()) - length(indexShape(qGramIndex)) + 1;
++seqIter)
{
hashNext(indexShape(qGramIndex), seqIter);
for (TSeedSize seq1Pos : getOccurrences(qGramIndex, indexShape(qGramIndex)))
{
/* filter the seeds */
if (!addSeed(seedSet,
TSeed{seq1Pos, static_cast<TSeedSize>(seqIter - begin(seq2, Standard())), options.qGramSize},
options.distanceCriteria, options.gapCriteria, scoreScheme, seq1, seq2, Chaos{}))
{
addSeed(seedSet,
TSeed{seq1Pos, static_cast<TSeedSize>(seqIter - begin(seq2, Standard())), options.qGramSize},
Single{});
}
}
}
std::cout << "Finished seeding: " << length(seedSet) << " seeds!\n";
//![solution_seeding]
//![chain_seeds]
/* chain seeds */
String<TSeed> seedChain;
chainSeedsGlobally(seedChain, seedSet, SparseChaining());
std::cout << "Finished chaining: " << length(seedChain) << " seeds!\n";
//![chain_seeds]
//![build_alignment]
/* build alignment */
Align<Dna5String, ArrayGaps> alignment;
resize(rows(alignment), 2);
assignSource(row(alignment, 0), seq1);
assignSource(row(alignment, 1), seq2);
int score = bandedChainAlignment(alignment, seedChain, Score<int, Simple>{4, -5, -1, -11}, 15);
std::cout << "Finished alignment: Score = " << score << "\n";
//![build_alignment]
//![output_alignment]
/* output alignment */
std::ofstream outStream;
outStream.open(options.outFilename.c_str());
if (!outStream.good())
{
std::cerr << "Could not open " << options.outFilename << "!\n";
std::exit(EXIT_FAILURE);
}
outStream << "#seq1: " << seq1Id << "\n";
outStream << "#seq2: " << seq2Id << "\n";
outStream << "#score: " << score << "\n";
outStream << "#alignment\n";
outStream << alignment;
std::exit(EXIT_SUCCESS);
//![output_alignment]
}
|