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
|
// ==========================================================================
// gustaf_mate_joining
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Kathrin Trappe <kathrin.trappe@fu-berlin.de>
// ==========================================================================
#include <sstream>
#include <iostream>
#include <fstream>
#include <seqan/basic.h>
#include <seqan/sequence.h>
#include <seqan/seq_io.h>
#include <seqan/modifier.h>
#include <seqan/file.h>
#include <seqan/stream.h>
#include <seqan/arg_parse.h>
// This struct stores the options from the command line.
struct JoinMatesOptions
{
// Verbosity level. 0 -- quiet, 1 -- normal, 2 -- verbose, 3 -- very verbose.
int verbosity;
// Path to Fasta/Fastq input files
seqan2::String<seqan2::CharString> inPaths;
// Path to Fasta output file
seqan2::String<seqan2::CharString> outPaths;
// CharString outPathPos;
// Whether or not to rev-compl the second input file
bool revCompl;
JoinMatesOptions() :
verbosity(1),
revCompl(true)
{}
};
// ==========================================================================
// Functions
// ==========================================================================
// --------------------------------------------------------------------------
// Function parseCommandLine()
// --------------------------------------------------------------------------
seqan2::ArgumentParser::ParseResult
parseCommandLine(JoinMatesOptions & options, int argc, char const ** argv)
{
// Setup ArgumentParser.
seqan2::ArgumentParser parser("gustaf_mate_joining");
// Set short description, version, and date.
setShortDescription(parser, "Joining paired-end files.");
setVersion(parser, SEQAN_APP_VERSION " [" SEQAN_REVISION "]");
setDate(parser, SEQAN_DATE);
// Define usage line and long description.
addUsageLine(parser, "[\\fIOPTIONS\\fP] \"\\fIMATES1 FASTA/FASTQ FILE\\fP\" \"\\fIMATES2 FASTA/FASTQ FILE\\fP\"");
addUsageLine(parser, "[\\fIOPTIONS\\fP] \"\\fIMATES FASTA/FASTQ FILE\\fP\" \"\\fI-o MATES1 FASTA/FASTQ FILE\\fP\" \"\\fI-o MATES2 FASTA/FASTQ FILE\\fP\"");
addDescription(parser, "Joining two paired-end files into one file with joined (single-end) reads. Automatically reverse-complements reads of the second input file.");
addDescription(parser, "This simple program takes as input two mate pair or paired-end files and outputs a file "
"where both mate sequences have been joined together. The FASTA file with joined mates "
"is an required input file for the paired-end mode of Gustaf. "
"The tool assumes the mates in the second file to be reverse complemented compared to the "
"first file. This behaviour can be turned off using the command line argument \"-rc\".");
addDescription(parser, "Given only one input file and two output files, the program will split the reads from "
"the input files at half length, and write the first half of each sequence as mates1 into "
"the first output file and the reversed complemented second half of each sequence as "
"mates2 into the second output file. Reverse complementing the sequences can again be "
"turned off using \"-rc\".");
addDescription(parser, "To prepare the joined mate file for the Gustaf paired-end example, call \n "
"./gustaf_mate_joining adeno_modified_reads_mates1.fa adeno_modified_reads_mates2.fa "
"-rc -o adeno_modified_reads_joinedMates.fa");
// We require two arguments.
addArgument(parser, seqan2::ArgParseArgument(seqan2::ArgParseArgument::INPUT_FILE, "FASTA/FASTQ FILE(S)", true));
setValidValues(parser, 0, "fa fasta fq fastq");
/*
addArgument(parser, seqan2::ArgParseArgument(seqan2::ArgParseArgument::INPUT_FILE, "FASTA/FASTQ FILE 2"));
setValidValues(parser, 1, "fasta fa fastq fq");
*/
addOption(parser, seqan2::ArgParseOption("o", "outPath", "Set name of output FASTA/FASTQ file(s).", seqan2::ArgParseOption::OUTPUT_FILE, "FASTA/FASTQ", true));
setValidValues(parser, "o", "fasta fa fq fastq");
setDefaultValue(parser, "o", "joined_mates.fa");
addOption(parser, seqan2::ArgParseOption("rc", "revcompl", "Disable reverse complementing second input file."));
addOption(parser, seqan2::ArgParseOption("q", "quiet", "Set verbosity to a minimum."));
addOption(parser, seqan2::ArgParseOption("v", "verbose", "Enable verbose output."));
addOption(parser, seqan2::ArgParseOption("vv", "very-verbose", "Enable very verbose output."));
// Parse command line.
seqan2::ArgumentParser::ParseResult res = parse(parser, argc, argv);
// Only extract options if the program will continue after parseCommandLine()
if (res != seqan2::ArgumentParser::PARSE_OK)
return res;
resize(options.inPaths, getArgumentValueCount(parser, 0), seqan2::Exact());
for (unsigned i = 0; i < length(options.inPaths); ++i)
getArgumentValue(options.inPaths[i], parser, 0, i);
/*
getArgumentValue(options.inPath1, parser, 0);
getArgumentValue(options.inPath2, parser, 1);
*/
resize(options.outPaths, getOptionValueCount(parser, "o"), seqan2::Exact());
for (unsigned i = 0; i < length(options.outPaths); ++i)
getOptionValue(options.outPaths[i], parser, "o", i);
//getOptionValue(options.outPath, parser, "o");
if (isSet(parser, "rc"))
options.revCompl = false;
// Extract option values.
if (isSet(parser, "quiet"))
options.verbosity = 0;
if (isSet(parser, "verbose"))
options.verbosity = 2;
if (isSet(parser, "very-verbose"))
options.verbosity = 3;
if (length(options.inPaths) == 1 && length(options.outPaths) == 1)
return seqan2::ArgumentParser::PARSE_ERROR;
return seqan2::ArgumentParser::PARSE_OK;
}
// ----------------------------------------------------------------------------
// Function _getShortId()
// ----------------------------------------------------------------------------
// Creates a short Id out of a long one (i.e. it takes the prefix til the first white space)
template <typename TId>
void _getShortId(TId & shortId, TId const & longId)
{
clear(shortId);
for (typename seqan2::Position<TId>::Type i = 0; i < length(longId) && isgraph(value(longId, i)); ++i)
{
appendValue(shortId, value(longId, i));
}
}
// --------------------------------------------------------------------------
// Function _importSequences()
// --------------------------------------------------------------------------
// Imports mate pairs from two files, joins them, stores the joining position in
// String readJoinPositions, and stores the sequences in the StringSet seqs and
// their identifiers in the StringSet ids
template <typename TSequence, typename TId>
inline bool
_importSequences(seqan2::CharString const & fileNameL,
seqan2::CharString const & fileNameR,
bool revCompl,
seqan2::StringSet<TSequence> & seqs,
seqan2::StringSet<TId> & ids,
seqan2::StringSet<TId> & sIds,
seqan2::StringSet<seqan2::CharString> & quals,
seqan2::String<unsigned> & readJoinPositions)
{
try
{
seqan2::SeqFileIn l(toCString(fileNameL));
seqan2::SeqFileIn r(toCString(fileNameR));
TSequence seq;
TSequence seqL;
TSequence seqR;
TId id;
TId sId;
seqan2::CharString qual;
seqan2::CharString qualL;
seqan2::CharString qualR;
while (!atEnd(l) || !atEnd(r))
{
readRecord(id, seqL, qualL, l);
readRecord(id, seqR, qualR, r);
appendValue(readJoinPositions, length(seqL));
if (revCompl)
{
reverseComplement(seqR);
reverse(qualR);
}
append(seq, seqL);
append(seq, seqR);
append(qual, qualL);
append(qual, qualR);
appendValue(seqs, seq, seqan2::Generous());
appendValue(quals, qual, seqan2::Generous());
appendValue(ids, id, seqan2::Generous());
_getShortId(sId, id);
appendValue(sIds, sId);
clear(seq);
clear(qual);
}
}
catch (seqan2::FileOpenError const & openErr)
{
std::cerr << "Error: Problem opening the file (" << openErr.what() << ")\n";
return false;
}
catch (seqan2::IOError const & ioErr)
{
std::cerr << "Error: Problem reading from the file (" << ioErr.what() << ")\n";
return false;
}
return true;
}
// Imports mate pairs from one file, separates them, and stores the sequences in the StringSets seqs and and mateSeqs
// and their identifiers in the StringSet ids
// Note: Assumes equally long mates, i.e. splits in the middle of the read
template <typename TSequence, typename TId>
inline bool
_importSequences(seqan2::CharString const & fileName,
seqan2::StringSet<TSequence> & seqs,
seqan2::StringSet<TSequence> & mateSeqs,
seqan2::StringSet<TId> & ids,
seqan2::StringSet<TId> & sIds,
seqan2::StringSet<seqan2::CharString> & quals,
seqan2::StringSet<seqan2::CharString> & mateQuals)
{
typedef typename seqan2::Position<TSequence>::Type TPos;
seqan2::SeqFileIn f;
if (!open(f, toCString(fileName)))
{
std::cerr << "Failed to open file.\n";
return false;
}
TSequence seq;
TSequence seqL;
TSequence seqR;
TId id;
TId sId;
seqan2::CharString qual;
seqan2::CharString qualL;
seqan2::CharString qualR;
TPos splitPos;
while (!atEnd(f))
{
try
{
readRecord(id, seq, qual, f);
}
catch (seqan2::IOError const & ioErr)
{
std::cerr << "Problem reading from first input file (" << ioErr.what() << ")\n";
return false;
}
splitPos = static_cast<TPos>(length(seq)/2);
append(seqL, prefix(seq, splitPos));
append(seqR, suffix(seq, splitPos));
appendValue(seqs, seqL, seqan2::Generous());
appendValue(mateSeqs, seqR, seqan2::Generous());
if (length(qual) > splitPos)
{
append(qualL, prefix(qual, splitPos));
append(qualR, suffix(qual, splitPos));
}
else
{
append(qualL, qual);
append(qualR, qual);
}
appendValue(quals, qualL, seqan2::Generous());
appendValue(mateQuals, qualR, seqan2::Generous());
appendValue(ids, id, seqan2::Generous());
_getShortId(sId, id);
appendValue(sIds, sId);
clear(seqL);
clear(seqR);
clear(qualL);
clear(qualR);
}
return true;
}
// --------------------------------------------------------------------------
// Function _writeSequences()
// --------------------------------------------------------------------------
// Writes out sequences and ids in FASTA format
template <typename TSequence>
int _writeSequences(seqan2::CharString & outPath,
seqan2::StringSet<TSequence> const & seqs,
seqan2::StringSet<seqan2::CharString> const & sIds,
seqan2::StringSet<seqan2::CharString> const & quals)
{
try
{
seqan2::SeqFileOut seqFile(toCString(outPath));
writeRecords(seqFile, sIds, seqs, quals);
}
catch (seqan2::FileOpenError const & openErr)
{
std::cerr << "Error: Could not open output file (" << openErr.what() << ")\n";
return 1;
}
catch (seqan2::IOError const & ioErr)
{
std::cerr << "Error: Could not write to file (" << ioErr.what() << ")\n";
return 1;
}
return 0;
}
// Writes out sequences and ids in FASTA format
template <typename TSequence>
int _writeSequences(seqan2::CharString & outPath1,
seqan2::CharString & outPath2,
bool revCompl,
seqan2::StringSet<TSequence> const & seqs,
seqan2::StringSet<TSequence> const & mateSeqs,
seqan2::StringSet<seqan2::CharString> const & sIds,
seqan2::StringSet<seqan2::CharString> const & quals,
seqan2::StringSet<seqan2::CharString> const & mateQuals
)
{
try
{
seqan2::SeqFileOut f1(toCString(outPath1));
seqan2::SeqFileOut f2(toCString(outPath2));
for (unsigned i = 0; i < length(seqs); ++i)
{
TSequence mateSeq = mateSeqs[i];
seqan2::CharString mateQual = mateQuals[i];
if (revCompl)
{
reverseComplement(mateSeq);
reverse(mateQual);
}
writeRecord(f1, sIds[i], seqs[i], quals[i]);
writeRecord(f2, sIds[i], mateSeq, mateQual);
}
}
catch (seqan2::FileOpenError const & openErr)
{
std::cerr << "Error: Could not open file (" << openErr.what() << ")\n";
return 1;
}
catch (seqan2::IOError const & ioErr)
{
std::cerr << "Error: Could not write to file (" << ioErr.what() << ")\n";
return 1;
}
return 0;
}
// --------------------------------------------------------------------------
// Function main()
// --------------------------------------------------------------------------
// Program entry point.
int main(int argc, char const ** argv)
{
// Parse the command line.
seqan2::ArgumentParser parser;
JoinMatesOptions options;
seqan2::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 != seqan2::ArgumentParser::PARSE_OK)
{
std::cout << "Error parsing command line, please check correct number and values of input parameters!" << std::endl;
return res == seqan2::ArgumentParser::PARSE_ERROR;
}
// Print the command line arguments back to the user.
if (options.verbosity > 0)
{
std::cout << "__OPTIONS____________________________________________________________________\n"
<< '\n'
<< "VERBOSITY \t\t" << options.verbosity << '\n'
<< "INPUT FILE 1 \t" << options.inPaths[0] << '\n';
if (length(options.inPaths) > 1)
std::cout << "INPUT FILE 2 \t" << options.inPaths[1] << '\n';
std::cout << "OUTPUT FILE \t" << options.outPaths[0] << '\n';
if (length(options.outPaths) > 1)
std::cout << "OUTPUT FILE 2 \t" << options.outPaths[1] << '\n';
std::cout << "OPTION REV COMPL \t" << options.revCompl << "\n\n";
}
typedef seqan2::String<seqan2::Dna5> TSequence;
seqan2::StringSet<TSequence> seqs;
seqan2::StringSet<TSequence> mateSeqs;
seqan2::StringSet<seqan2::CharString> ids;
seqan2::StringSet<seqan2::CharString> sIds;
seqan2::StringSet<seqan2::CharString> quals;
seqan2::StringSet<seqan2::CharString> mateQuals;
seqan2::String<unsigned> joinPos;
if (length(options.inPaths) > 1)
{
// Read in paired-end reads
_importSequences(options.inPaths[0], options.inPaths[1], options.revCompl, seqs, ids, sIds, quals, joinPos);
// Write out one file with joined sequences in FASTA format
_writeSequences(options.outPaths[0], seqs, sIds, quals);
} else
{
// Read in joined reads and output two FASTA files
_importSequences(options.inPaths[0], seqs, mateSeqs, ids, sIds, quals, mateQuals);
// Write out one file with joined sequences in FASTA format
_writeSequences(options.outPaths[0], options.outPaths[1], options.revCompl, seqs, mateSeqs, sIds, quals, mateQuals);
}
return 0;
}
|