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
|
// ==========================================================================
// 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>
// ==========================================================================
#include "sequencing.h"
// ===========================================================================
// Class IlluminaSequencingSimulator
// ===========================================================================
// ---------------------------------------------------------------------------
// Constructor IlluminaSequencingSimulator::IlluminaSequencingSimulator
// ---------------------------------------------------------------------------
IlluminaSequencingSimulator::IlluminaSequencingSimulator(TRng & rng,
TRng & methRng,
SequencingOptions const & seqOptions,
IlluminaSequencingOptions const & illuminaOptions) :
SequencingSimulator(rng, methRng, seqOptions), illuminaOptions(illuminaOptions),
model(new IlluminaModel())
{
this->_initModel();
}
// ---------------------------------------------------------------------------
// Function IlluminaSequencingSimulator::_initModel()
// ---------------------------------------------------------------------------
void IlluminaSequencingSimulator::_initModel()
{
// Compute mismatch probabilities, piecewise linear function.
resize(model->mismatchProbabilities, illuminaOptions.readLength);
// Compute probability at raise point.
double y_r = 2 * illuminaOptions.probabilityMismatch - illuminaOptions.positionRaise * illuminaOptions.probabilityMismatchBegin - illuminaOptions.probabilityMismatchEnd + illuminaOptions.probabilityMismatchEnd * illuminaOptions.positionRaise;
if (illuminaOptions.verbosity >= 2)
{
std::cerr << "Illumina error curve:\n"
<< " (0, " << illuminaOptions.probabilityMismatchBegin << ") -- (" << illuminaOptions.positionRaise << ", " << y_r << ") -- (1, " << illuminaOptions.probabilityMismatchEnd << ")\n";
}
// std::cout << "y_r = " << y_r << std::endl;
// Compute mismatch probability at each base.
if (!empty(illuminaOptions.probabilityMismatchFile))
{
// Open file.
std::fstream file;
file.open(toCString(illuminaOptions.probabilityMismatchFile), std::ios_base::in);
if (!file.is_open())
{
std::cerr << "Failed to load mismatch probabilities from " << illuminaOptions.probabilityMismatchFile << std::endl;
// return 1;
}
// Load probabilities.
double x;
file >> x;
unsigned i;
for (i = 0; i < illuminaOptions.readLength && !file.eof(); ++i) {
model->mismatchProbabilities[i] = x;
file >> x;
}
if (i != illuminaOptions.readLength)
{
std::cerr << "Not enough mismatch probabilites in " << illuminaOptions.probabilityMismatchFile << " (" << i << " < " << illuminaOptions.readLength << ")!" << std::endl;
// return 1;
}
} else {
// Use piecewise linear function for mismatch probability simulation.
for (unsigned i = 0; i < illuminaOptions.readLength; ++i) {
double x = static_cast<double>(i) / (illuminaOptions.readLength - 1);
if (x < illuminaOptions.positionRaise) {
double b = illuminaOptions.probabilityMismatchBegin;
double m = (y_r - illuminaOptions.probabilityMismatchBegin) / illuminaOptions.positionRaise;
model->mismatchProbabilities[i] = m * x + b;
// std::cout << "model->mismatchProbabilities[" << i << "] = " << model->mismatchProbabilities[i] << std::endl;
} else {
double b = y_r;
double m = (illuminaOptions.probabilityMismatchEnd - y_r) / (1 - illuminaOptions.positionRaise);
x -= illuminaOptions.positionRaise;
model->mismatchProbabilities[i] = m * x + b;
// std::cout << "model->mismatchProbabilities[" << i << "] = " << model->mismatchProbabilities[i] << std::endl;
}
}
}
if (illuminaOptions.probabilityMismatchScale != 1.0) {
for (unsigned i = 0; i < illuminaOptions.readLength; ++i)
model->mismatchProbabilities[i] *= illuminaOptions.probabilityMismatchScale;
}
// Compute match/mismatch means and standard deviations.
resize(model->mismatchQualityMeans, illuminaOptions.readLength);
for (unsigned i = 0; i < illuminaOptions.readLength; ++i) {
double b = illuminaOptions.meanMismatchQualityBegin;
double x = static_cast<double>(i) / (illuminaOptions.readLength - 1);
double m = (illuminaOptions.meanMismatchQualityEnd - illuminaOptions.meanMismatchQualityBegin);
model->mismatchQualityMeans[i] = m * x + b;
// std::cout << "model->mismatchQualityMeans[" << i << "] = " << model->mismatchQualityMeans[i] << std::endl;
}
resize(model->mismatchQualityStdDevs, illuminaOptions.readLength);
for (unsigned i = 0; i < illuminaOptions.readLength; ++i) {
double b = illuminaOptions.stdDevMismatchQualityBegin;
double x = static_cast<double>(i) / (illuminaOptions.readLength - 1);
double m = (illuminaOptions.stdDevMismatchQualityEnd - illuminaOptions.stdDevMismatchQualityBegin);
model->mismatchQualityStdDevs[i] = m * x + b;
// std::cout << "model->mismatchQualityStdDevs[" << i << "] = " << model->mismatchQualityStdDevs[i] << std::endl;
}
resize(model->qualityMeans, illuminaOptions.readLength);
for (unsigned i = 0; i < illuminaOptions.readLength; ++i) {
double b = illuminaOptions.meanQualityBegin;
double x = static_cast<double>(i) / (illuminaOptions.readLength - 1);
double m = (illuminaOptions.meanQualityEnd - illuminaOptions.meanQualityBegin);
model->qualityMeans[i] = m * x + b;
// std::cout << "model->qualityMeans[" << i << "] = " << model->qualityMeans[i] << std::endl;
}
resize(model->qualityStdDevs, illuminaOptions.readLength);
for (unsigned i = 0; i < illuminaOptions.readLength; ++i) {
double b = illuminaOptions.stdDevQualityBegin;
double x = static_cast<double>(i) / (illuminaOptions.readLength - 1);
double m = (illuminaOptions.stdDevQualityEnd - illuminaOptions.stdDevQualityBegin);
model->qualityStdDevs[i] = m * x + b;
// std::cout << "model->qualityStdDevs[" << i << "] = " << model->qualityStdDevs[i] << std::endl;
}
}
// ---------------------------------------------------------------------------
// Function IlluminaSequencingSimulator::simulateRead()
// ---------------------------------------------------------------------------
// Actually simulate read and qualities from fragment and direction forward/reverse strand.
void IlluminaSequencingSimulator::simulateRead(TRead & seq, TQualities & quals, SequencingSimulationInfo & info,
TFragment const & frag, Direction dir, Strand strand)
{
// std::cerr << "simulateRead(" << (char const *)(dir == LEFT ? "L" : "R") << ", " << (char const *)(strand == FORWARD ? "-->" : "<--") << ")\n";
// Simulate sequencing operations.
TCigarString cigar;
_simulateCigar(cigar);
unsigned lenInRef = 0;
_getLengthInRef(lenInRef, cigar);
if (lenInRef > length(frag))
{
throw std::runtime_error("Illumina read is too long, increase fragment length");
}
// Simulate sequence (materialize mismatches and insertions).
typedef seqan2::ModifiedString<seqan2::ModifiedString<TFragment, seqan2::ModView<seqan2::FunctorComplement<seqan2::Dna5> > >, seqan2::ModReverse> TRevCompFrag;
if ((dir == LEFT) && (strand == FORWARD))
{
_simulateSequence(seq, rng, prefix(frag, lenInRef), cigar);
}
else if ((dir == LEFT) && (strand == REVERSE))
{
seqan2::Prefix<TFragment>::Type holder(prefix(frag, lenInRef));
_simulateSequence(seq, rng, TRevCompFrag(holder), cigar);
}
else if ((dir == RIGHT) && (strand == FORWARD))
{
_simulateSequence(seq, rng, suffix(frag, length(frag) - lenInRef), cigar);
}
else // ((dir == RIGHT) && (strand == REVERSE))
{
seqan2::Suffix<TFragment>::Type holder(suffix(frag, length(frag) - lenInRef));
_simulateSequence(seq, rng, TRevCompFrag(holder), cigar);
}
// Simulate qualities.
_simulateQualities(quals, cigar);
SEQAN_ASSERT_EQ(length(seq), length(quals));
// // Reverse qualities if necessary.
// if (strand == REVERSE)
// reverse(quals);
// Write out extended sequencing information info if configured to do so. We always write out the sample position
// and alignment information.
info.cigar = cigar;
unsigned len = 0;
_getLengthInRef(len, cigar);
info.beginPos = (dir == LEFT) ? beginPosition(frag) : (beginPosition(frag) + length(frag) - len);
info.isForward = (strand == FORWARD);
// std::cerr << " beginPos=" << info.beginPos - beginPosition(frag) << "\n";
if (seqOptions->embedReadInfo)
{
if (dir == LEFT)
info.sampleSequence = prefix(frag, len);
else
info.sampleSequence = suffix(frag, length(frag) - len);
if (strand == REVERSE)
reverseComplement(info.sampleSequence);
}
// std::cerr << " frag =" << frag << "\n";
// std::cerr << " fragRC=" << TRevCompFrag(frag) << "\n";
// std::cerr << " seq=" << seq << "\tquals=" << quals << "\n";
}
// ---------------------------------------------------------------------------
// Function IlluminaSequencingSimulator::_simulateQualities()
// ---------------------------------------------------------------------------
// Simulate PHRED qualities from the CIGAR string.
void IlluminaSequencingSimulator::_simulateQualities(TQualities & quals, TCigarString const & cigar)
{
clear(quals);
unsigned pos = 0;
for (unsigned i = 0; i < length(cigar); ++i)
{
for (unsigned j = 0; j < cigar[i].count; ++j)
{
int q = 0;
if (cigar[i].operation == 'M')
{
std::normal_distribution<double> dist(model->qualityMeans[pos], model->qualityStdDevs[pos]);
q = static_cast<int>(dist(rng));
++pos;
}
else if (cigar[i].operation == 'I' || cigar[i].operation == 'X')
{
std::normal_distribution<double> dist(model->mismatchQualityMeans[pos], model->mismatchQualityStdDevs[pos]);
q = static_cast<int>(dist(rng));
++pos;
}
else
{
// Deletion/padding, no quality required.
continue;
}
q = std::max(0, std::min(q, 40)); // limit quality to 0..40
appendValue(quals, (char)('!' + q));
}
}
}
// ---------------------------------------------------------------------------
// Function IlluminaSequencingSimulator::_simulateCigar()
// ---------------------------------------------------------------------------
// Simulate CIGAR string. We can do this with position specific parameters only and thus independent of any
// context.
void IlluminaSequencingSimulator::_simulateCigar(TCigarString & cigar)
{
clear(cigar);
unsigned len = this->readLength();
std::uniform_real_distribution<double> dist(0, 1);
for (int i = 0; i < (int)len;)
{
double x = dist(rng);
double pMismatch = model->mismatchProbabilities[i];
double pInsert = illuminaOptions.probabilityInsert;
double pDelete = illuminaOptions.probabilityDelete;
double pMatch = 1.0 - pMismatch - pInsert - pDelete;
// Simulate mutation/insertion/deletion events. If possible we reuse the last CIGAR entry. Adjacent
// insertion/deletion pairs cancel each other out.
// TODO(holtgrew): No indel at beginning or ending! Same for other simulators!
if (x < pMatch) // match
i += appendOperation(cigar, 'M').first;
else if (x < pMatch + pMismatch) // point polymorphism
i += appendOperation(cigar, 'X').first;
else if (x < pMatch + pMismatch + pInsert) // insertion
i += appendOperation(cigar, 'I').first;
else // deletion
i += appendOperation(cigar, 'D').first;
}
}
// ============================================================================
// Class SequencingSimulatorFactory
// ============================================================================
// ----------------------------------------------------------------------------
// Function SequencingSimulatorFactory::make()
// ----------------------------------------------------------------------------
std::unique_ptr<SequencingSimulator> SequencingSimulatorFactory::make()
{
std::unique_ptr<SequencingSimulator> res;
switch (seqOptions.sequencingTechnology)
{
case SequencingOptions::ILLUMINA:
res.reset(new IlluminaSequencingSimulator(rng, methRng, seqOptions, illuminaOptions));
break;
case SequencingOptions::SANGER:
res.reset(new SangerSequencingSimulator(rng, methRng, seqOptions, sangerOptions));
break;
case SequencingOptions::ROCHE_454:
res.reset(new Roche454SequencingSimulator(rng, methRng, seqOptions, roche454Options));
break;
}
return res;
}
|