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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
// ==========================================================================
// Mason - A Read Simulator
// ==========================================================================
// 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: Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>
// ==========================================================================
// Compute transcripts from a genome and a GFF/GTF file. Optionally, you
// can apply a VCF file to the genome before splicing.
//
// Transcripts must not span structural variants.
// ==========================================================================
// Note: We treat all given variants as phased.
#include <seqan/arg_parse.h>
#include <seqan/basic.h>
#include <seqan/seq_io.h>
#include <seqan/sequence.h>
#include <seqan/vcf_io.h>
#include <seqan/gff_io.h>
#include "vcf_materialization.h"
#include "mason_options.h"
#include "mason_types.h"
// ==========================================================================
// Classes
// ==========================================================================
// --------------------------------------------------------------------------
// Class MyGffRecord
// --------------------------------------------------------------------------
// Subclass of GffRecord that has an rID member.
class MyGffRecord : public seqan2::GffRecord
{
public:
int rID;
static const int INVALID_IDX;
MyGffRecord() : seqan2::GffRecord(), rID(std::numeric_limits<int>::max())
{}
};
const int MyGffRecord::INVALID_IDX = std::numeric_limits<int>::max();
// --------------------------------------------------------------------------
// Class SplicingInstruction
// --------------------------------------------------------------------------
// Represents one exon.
struct SplicingInstruction
{
// ID of the transcript that this instruction belongs to.
int transcriptID;
// Begin and end position of the exon.
int beginPos, endPos;
// The strand of the instruction '-' or '+'.
char strand;
SplicingInstruction() : transcriptID(-1), beginPos(-1), endPos(-1), strand('.')
{}
SplicingInstruction(int transcriptID, int beginPos, int endPos, char strand) :
transcriptID(transcriptID), beginPos(beginPos), endPos(endPos), strand(strand)
{}
bool operator<(SplicingInstruction const & other) const
{
return std::make_pair(transcriptID, std::make_pair(beginPos, std::make_pair(endPos, strand))) <
std::make_pair(other.transcriptID,
std::make_pair(other.beginPos, std::make_pair(other.endPos, other.strand)));
}
};
bool differentTranscript(SplicingInstruction const & lhs, SplicingInstruction const & rhs)
{
return lhs.transcriptID != rhs.transcriptID;
}
// --------------------------------------------------------------------------
// Class MasonSplicingApp
// --------------------------------------------------------------------------
class MasonSplicingApp
{
public:
// The configuration to use.
MasonSplicingOptions const & options;
// The random number generation.
TRng rng;
// Materialization of VCF.
VcfMaterializer vcfMat;
// Input GFF/GTF stream.
seqan2::GffFileIn gffFileIn;
// Output sequence stream.
seqan2::SeqFileOut seqFileOut;
MasonSplicingApp(MasonSplicingOptions const & _options) :
options(_options), rng(options.seed),
vcfMat(rng, toCString(options.matOptions.fastaFileName), toCString(options.matOptions.vcfFileName))
{}
int run()
{
// Intialization
std::cerr << "__INITIALIZATION_____________________________________________________________\n"
<< "\n";
std::cerr << "Opening files...";
try
{
vcfMat.init();
if (!open(seqFileOut, toCString(options.outputFileName)))
throw MasonIOException("Could not open output file.");
if (!open(gffFileIn, toCString(options.inputGffFile)))
throw MasonIOException("Could not open GFF/GTF file.");
}
catch (MasonIOException & e)
{
std::cerr << "\nERROR: " << e.what() << "\n";
return 1;
}
std::cerr << " OK\n";
// Perform genome simulation.
std::cerr << "\n__COMPUTING TRANSCRIPTS______________________________________________________\n"
<< "\n";
// Read first GFF record.
MyGffRecord record;
_readFirstRecord(record);
if (record.rID == std::numeric_limits<int>::max())
return 0; // at end, could not read any, done
// Transcript names.
typedef seqan2::StringSet<seqan2::CharString> TNameStore;
typedef seqan2::NameStoreCache<TNameStore> TNameStoreCache;
TNameStore transcriptNames;
TNameStoreCache transcriptNamesCache(transcriptNames);
// The splicing instructions for the current contig.
std::vector<SplicingInstruction> splicingInstructions;
// Materialized sequence.
seqan2::Dna5String seq;
// Tanscript ids, used as a buffer below.
seqan2::String<unsigned> transcriptIDs;
// Read GFF/GTF file contig by contig (must be sorted by reference name). For each contig, we all recors,
// create simulation instructions and then build the transcripts for each haplotype.
while (record.rID != std::numeric_limits<int>::max()) // sentinel, at end
{
seqan2::CharString refName = record.ref;
std::cerr << "Splicing for " << refName << " ...";
// Read GFF records for this contig.
MyGffRecord firstGffRecord = record;
while (record.rID == firstGffRecord.rID)
{
if (empty(options.gffType) || (record.type == options.gffType))
{
// Make transcript names known to the record.
_appendTranscriptNames(transcriptIDs, transcriptNames, transcriptNamesCache, record);
// Add the splicing instructions for this record to the list for this contig.
for (unsigned i = 0; i < length(transcriptIDs); ++i)
splicingInstructions.push_back(SplicingInstruction(transcriptIDs[i], record.beginPos,
record.endPos, record.strand));
}
if (atEnd(gffFileIn))
{
record.rID = std::numeric_limits<int>::max();
break;
}
readRecord(record, gffFileIn);
// Translate ref to idx from VCF.
unsigned idx = 0;
if (!getIdByName(idx, vcfMat.faiIndex, record.ref))
throw MasonIOException("Reference name from GFF/GTF not in VCF!");
record.rID = idx;
}
// ---------------------------------------------------------------
// Process the splicing instructions.
// ---------------------------------------------------------------
// First, sort them.
std::sort(splicingInstructions.begin(), splicingInstructions.end());
// Materialize all haplotypes of this contig
int rID = 0, hID = 0; // reference and haplotype id
// Get index of the gff record's reference in the VCF file.
unsigned idx = 0;
if (!getIdByName(idx, vcfMat.faiIndex, refName))
{
std::stringstream ss;
ss << "Reference from GFF file " << refName << " unknown in FASTA/FAI file.";
throw MasonIOException(ss.str());
}
rID = idx;
vcfMat.currRID = rID - 1;
std::vector<SmallVarInfo> varInfos; // small variants for counting in read alignments
std::vector<std::pair<int, int> > breakpoints; // unused/ignored
while (vcfMat.materializeNext(seq, varInfos, breakpoints, rID, hID))
{
std::cerr << " (allele " << (hID + 1) << ")";
if (rID != (int)idx)
break; // no more haplotypes for this reference
_performSplicing(splicingInstructions, seq, transcriptNames, hID, vcfMat);
}
std::cerr << " DONE.\n";
// ---------------------------------------------------------------
// Handle contig switching.
// ---------------------------------------------------------------
// Check that the input GFF file is clustered (weaker than sorted) by reference name.
if (record.rID < firstGffRecord.rID)
throw MasonIOException("GFF file not sorted or clustered by reference.");
// Reset transcript names and cache.
clear(transcriptNames);
refresh(transcriptNamesCache);
// Flush splicing instructions.
splicingInstructions.clear();
}
std::cerr << "\nDone splicing FASTA.\n";
return 0;
}
// Perform splicing of transcripts.
void _performSplicing(std::vector<SplicingInstruction> const & instructions,
seqan2::Dna5String const & seq,
seqan2::StringSet<seqan2::CharString> const & tNames,
int hID, // -1 in case of no variants
VcfMaterializer const & vcfMat)
{
typedef std::vector<SplicingInstruction>::const_iterator TIter;
TIter it = instructions.begin();
TIter itEnd = std::adjacent_find(it, instructions.end(), differentTranscript);
if (itEnd != instructions.end())
++itEnd;
seqan2::Dna5String transcript, buffer;
do
{
clear(transcript);
bool onBreakpoint = false;
int tID = it->transcriptID;
for (; it != itEnd; ++it)
{
// Convert from original coordinate system to coordinate system with SVs.
std::pair<int, int> smallVarInt = vcfMat.posMap.originalToSmallVarInterval(
it->beginPos, it->endPos);
GenomicInterval gi = vcfMat.posMap.getGenomicIntervalSmallVarPos(smallVarInt.first);
SEQAN_ASSERT_GT(smallVarInt.second, 0);
GenomicInterval giR = vcfMat.posMap.getGenomicIntervalSmallVarPos(smallVarInt.second - 1);
bool overlapsWithBreakpoint = (gi != giR);
std::pair<int, int> largeVarInt = vcfMat.posMap.smallVarToLargeVarInterval(
smallVarInt.first, smallVarInt.second);
// Transcripts with exons overlapping breakpoints are not written out.
if (overlapsWithBreakpoint)
{
onBreakpoint = true;
break;
}
// Append buffer to transcript in original state or reverse-complemented.
buffer = infix(seq, largeVarInt.first, largeVarInt.second);
if (it->strand != gi.strand)
reverseComplement(buffer);
append(transcript, buffer);
}
if (onBreakpoint)
{
std::cerr << "\nWARNING: Exon lies on breakpoint!\n";
while (it != instructions.end() && it->transcriptID == tID)
++it;
}
else
{
std::stringstream ss;
ss << tNames[tID];
if (!empty(options.matOptions.vcfFileName))
ss << options.haplotypeNameSep << (hID + 1);
writeRecord(seqFileOut, ss.str(), transcript);
}
// Search next range.
itEnd = std::adjacent_find(it, instructions.end(), differentTranscript);
if (itEnd != instructions.end())
++itEnd;
}
while (it != instructions.end());
}
// Append the transcript names for the given record.
void _appendTranscriptNames(seqan2::String<unsigned> & tIDs, // transcript ids to write out
seqan2::StringSet<seqan2::CharString> & contigNames,
seqan2::NameStoreCache<seqan2::StringSet<seqan2::CharString> > & cache,
MyGffRecord const & record)
{
clear(tIDs);
seqan2::CharString groupNames;
for (unsigned i = 0; i < length(record.tagNames); ++i)
if (record.tagNames[i] == options.gffGroupBy)
groupNames = record.tagValues[i];
if (empty(groupNames))
return; // Record has no group names.
// Write out the ids of the transcripts that the record belongs to as indices in contigNames.
unsigned idx = 0;
seqan2::StringSet<seqan2::CharString> ss;
strSplit(ss, groupNames, seqan2::EqualsChar<','>());
for (unsigned i = 0; i < length(ss); ++i)
{
if (empty(ss[i]))
continue;
if (!getIdByName(idx, cache, ss[i]))
{
appendValue(tIDs, length(contigNames));
appendName(cache, ss[i]);
}
else
{
appendValue(tIDs, idx);
}
}
}
void _readFirstRecord(MyGffRecord & record)
{
record.rID = record.INVALID_IDX; // uninitialized
bool found = false;
while (!found && !atEnd(gffFileIn))
{
readRecord(record, gffFileIn);
// Translate ref to idx from VCF.
unsigned idx = 0;
if (!getIdByName(idx, vcfMat.faiIndex, record.ref))
throw MasonIOException("Reference name from GFF/GTF not in VCF!");
record.rID = idx;
if (empty(options.gffType) || (options.gffType == record.type))
{
found = true;
break;
}
}
if (!found)
record.rID = std::numeric_limits<int>::max();
}
};
// ==========================================================================
// Functions
// ==========================================================================
// --------------------------------------------------------------------------
// Function parseCommandLine()
// --------------------------------------------------------------------------
seqan2::ArgumentParser::ParseResult
parseCommandLine(MasonSplicingOptions & options, int argc, char const ** argv)
{
// Setup ArgumentParser.
seqan2::ArgumentParser parser("mason_splicing");
// Set short description, version, and date.
setShortDescription(parser, "Generating Transcripts");
setDateAndVersion(parser);
setCategory(parser, "Simulators");
// Define usage line and long description.
addUsageLine(parser,
"[OPTIONS] \\fB-ir\\fP \\fIIN.fa\\fP \\fB-ig\\fP \\fIIN.gff\\fP [\\fB-iv\\fP \\fIIN.vcf\\fP] \\fB-o\\fP \\fIOUT.fa\\fP");
addDescription(parser,
"Create transcripts from \\fIIN.fa\\fP using the annotations from \\fIIN.gff\\fP. The resulting "
"transcripts are written to \\fIOUT.fa\\fP.");
addDescription(parser,
"You can pass an optional VCF file \\fIIN.vcf\\fP and the transcripts will be created from the "
"haplotypes stored in the VCF file.");
// Add option and text sections.
options.addOptions(parser);
options.addTextSections(parser);
// Parse command line.
seqan2::ArgumentParser::ParseResult res = seqan2::parse(parser, argc, argv);
// Only extract options if the program will continue after parseCommandLine()
if (res != seqan2::ArgumentParser::PARSE_OK)
return res;
options.getOptionValues(parser);
return seqan2::ArgumentParser::PARSE_OK;
}
// --------------------------------------------------------------------------
// Function main()
// --------------------------------------------------------------------------
// Program entry point.
int main(int argc, char const ** argv)
{
// Parse the command line.
MasonSplicingOptions 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)
return res == seqan2::ArgumentParser::PARSE_ERROR;
std::cerr << "MASON SPLICING\n"
<< "==============\n\n";
// Print the command line arguments back to the user.
if (options.verbosity > 0)
{
std::cerr << "__OPTIONS____________________________________________________________________\n"
<< "\n";
options.print(std::cerr);
}
MasonSplicingApp app(options);
return app.run();
}
|