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
|
#include <iostream>
#include <string>
#include <vector>
#include <alignment/files/ReaderAgglomerate.hpp>
#include <alignment/utils/FileOfFileNames.hpp>
#include <alignment/utils/RegionUtils.hpp>
#include <hdf/HDFPlsReader.hpp>
#include <hdf/HDFRegionTableReader.hpp>
#include <hdf/HDFUtils.hpp>
#include <pbdata/CommandLineParser.hpp>
#include <pbdata/SMRTSequence.hpp>
#include <pbdata/reads/ReadInterval.hpp>
#include <pbdata/reads/RegionTable.hpp>
#include <pbdata/utils.hpp>
#include <pbdata/utils/TimeUtils.hpp>
char VERSION[] = "v1.0.0";
char PERFORCE_VERSION_STRING[] = "$Change: 126414 $";
int main(int argc, char* argv[])
{
std::string program = "pls2fasta";
std::string versionString = VERSION;
AppendPerforceChangelist(PERFORCE_VERSION_STRING, versionString);
std::string plsFileName, fastaOutName;
std::vector<std::string> plsFileNames;
bool trimByRegion, maskByRegion;
trimByRegion = false;
maskByRegion = false;
RegionTable regionTable;
std::string regionsFOFNName = "";
std::vector<std::string> regionFileNames;
bool splitSubreads = true;
int minSubreadLength = 0;
bool addSimulatedData = false;
bool printFastq = false;
bool printCcs = false;
int lineLength = 50;
int minReadScore = 0;
std::vector<int> holeNumbers;
CommandLineParser clp;
bool printOnlyBest = false;
clp.SetProgramName(program);
clp.SetVersion(versionString);
clp.RegisterStringOption("in.bax.h5", &plsFileName, "Input plx.h5/bax.h5/fofn file.", true);
clp.RegisterStringOption("out.fasta", &fastaOutName, "Output fasta/fastq file.", true);
clp.RegisterPreviousFlagsAsHidden();
clp.RegisterFlagOption("trimByRegion", &trimByRegion, "Trim away low quality regions.");
clp.RegisterFlagOption("maskByRegion", &maskByRegion, "Mask low quality regions with 'N'.");
clp.RegisterStringOption("regionTable", ®ionsFOFNName,
"Optional HDF file with a /PulseData/Regions dataset.");
clp.RegisterIntOption("minSubreadLength", &minSubreadLength,
"Do not write subreads less than the specified length.",
CommandLineParser::PositiveInteger);
clp.RegisterFlagOption("noSplitSubreads", &splitSubreads,
"Do not split reads on adapter sequences.");
clp.RegisterIntListOption("holeNumber", &holeNumbers,
"Only print this hole number (or list of numbers).");
clp.RegisterFlagOption("fastq", &printFastq, "Print in FASTQ format with quality.");
clp.RegisterFlagOption("ccs", &printCcs, "Print de novo CCS sequences");
clp.RegisterIntOption("lineLength", &lineLength, "Specify fasta/fastq line length",
CommandLineParser::PositiveInteger);
clp.RegisterIntOption(
"minReadScore", &minReadScore,
"Minimum read score to print a read. The score is "
"a number between 0 and 1000 and represents the expected accuracy percentage * 10. "
"A typical value would be between 750 and 800. This does not apply to ccs reads.",
CommandLineParser::NonNegativeInteger);
clp.RegisterFlagOption("best", &printOnlyBest,
"If a CCS sequence exists, print this. Otherwise, print the longest"
"subread. This does not support fastq.");
std::string description =
("Converts plx.h5/bax.h5/fofn files to fasta or fastq files. Although fasta files are "
"provided"
" with every run, they are not trimmed nor split into subreads. This program takes "
"additional annotation information, such as the subread coordinates and high quality "
"regions "
"and uses them to create fasta sequences that are substrings of all bases called. Most of "
"the time "
"you will want to trim low quality reads, so you should specify -trimByRegion.");
clp.SetProgramSummary(description);
clp.ParseCommandLine(argc, argv);
std::cerr << "[INFO] " << GetTimestamp() << " [" << program << "] started." << std::endl;
if (trimByRegion and maskByRegion) {
std::cout << "ERROR! You cannot both trim and mask regions. Use one or the other."
<< std::endl;
std::exit(EXIT_FAILURE);
}
if (printFastq) {
// Setting lineLength to 0 flags to print on one line.
lineLength = 0;
}
FileOfFileNames::StoreFileOrFileList(plsFileName, plsFileNames);
if (regionsFOFNName == "") {
regionFileNames = plsFileNames;
} else {
FileOfFileNames::StoreFileOrFileList(regionsFOFNName, regionFileNames);
}
std::ofstream fastaOut;
CrucialOpen(fastaOutName, fastaOut);
HDFRegionTableReader hdfRegionReader;
sort(holeNumbers.begin(), holeNumbers.end());
std::vector<int> pls2rgn = MapPls2Rgn(plsFileNames, regionFileNames);
for (size_t plsFileIndex = 0; plsFileIndex < plsFileNames.size(); plsFileIndex++) {
if (trimByRegion or maskByRegion or splitSubreads) {
hdfRegionReader.Initialize(regionFileNames[pls2rgn[plsFileIndex]]);
hdfRegionReader.ReadTable(regionTable);
}
ReaderAgglomerate reader;
HDFBasReader ccsReader;
if (printOnlyBest) {
ccsReader.SetReadBasesFromCCS();
ccsReader.Initialize(plsFileNames[plsFileIndex]);
}
if (printCcs == false) {
reader.IgnoreCCS();
} else {
reader.hdfBasReader.SetReadBasesFromCCS();
}
if (addSimulatedData) {
reader.hdfBasReader.IncludeField("SimulatedCoordinate");
reader.hdfBasReader.IncludeField("SimulatedSequenceIndex");
}
if (reader.SetReadFileName(plsFileNames[plsFileIndex]) == 0) {
std::cout << "ERROR, could not determine file type." << plsFileNames[plsFileIndex]
<< std::endl;
std::exit(EXIT_FAILURE);
}
if (reader.Initialize() == 0) {
std::cout << "ERROR, could not initialize file " << plsFileNames[plsFileIndex]
<< std::endl;
std::exit(EXIT_FAILURE);
}
DNALength simulatedCoordinate;
DNALength simulatedSequenceIndex;
reader.SkipReadQuality();
SMRTSequence seq;
std::vector<ReadInterval> subreadIntervals;
;
SMRTSequence ccsSeq;
while (reader.GetNextBases(seq, printFastq)) {
if (printOnlyBest) {
ccsReader.GetNext(ccsSeq);
}
if (holeNumbers.size() != 0 and
binary_search(holeNumbers.begin(), holeNumbers.end(), seq.zmwData.holeNumber) ==
false) {
continue;
}
if (seq.length == 0) {
continue;
}
if (addSimulatedData) {
reader.hdfBasReader.simulatedCoordinateArray.Read(reader.hdfBasReader.curRead - 1,
reader.hdfBasReader.curRead,
&simulatedCoordinate);
reader.hdfBasReader.simulatedSequenceIndexArray.Read(
reader.hdfBasReader.curRead - 1, reader.hdfBasReader.curRead,
&simulatedSequenceIndex);
}
if (printCcs == true) {
if (printFastq == false) {
seq.PrintSeq(fastaOut);
} else {
seq.PrintFastq(fastaOut, lineLength);
}
continue;
}
//
// Determine the high quality boundaries of the read. This is
// the full read is no hq regions exist, or it is stated to
// ignore regions.
//
DNALength hqReadStart, hqReadEnd;
int hqRegionScore;
if (GetReadTrimCoordinates(seq, seq.zmwData, regionTable, hqReadStart, hqReadEnd,
hqRegionScore) == false or
(trimByRegion == false and maskByRegion == false)) {
hqReadStart = 0;
hqReadEnd = seq.length;
}
//
// Mask off the low quality portions of the reads.
//
if (maskByRegion) {
if (hqReadStart > 0) {
std::fill(&seq.seq[0], &seq.seq[hqReadStart], 'N');
}
if (hqReadEnd != seq.length) {
std::fill(&seq.seq[hqReadEnd], &seq.seq[seq.length], 'N');
}
}
//
// Now possibly print the full read with masking. This could be handled by making a
//
if (splitSubreads == false) {
ReadInterval wholeRead(0, seq.length);
// The set of subread intervals is just the entire read.
subreadIntervals.clear();
subreadIntervals.push_back(wholeRead);
} else {
//
// Print subread coordinates no matter whether or not reads have subreads.
//
if (regionTable.HasHoleNumber(seq.HoleNumber())) {
subreadIntervals =
regionTable[seq.HoleNumber()].SubreadIntervals(seq.length, false, true);
} else {
subreadIntervals = {};
}
}
//
// Output all subreads as separate sequences.
//
SMRTSequence bestSubreadSequence;
int bestSubreadScore = -1;
int bestSubreadIndex = 0;
SMRTSequence bestSubread;
for (size_t intvIndex = 0; intvIndex < subreadIntervals.size(); intvIndex++) {
SMRTSequence subreadSequence, subreadSequenceRC;
subreadSequence.SubreadStart(subreadIntervals[intvIndex].start);
subreadSequence.SubreadEnd(subreadIntervals[intvIndex].end);
//
// When trimming by region, only output the parts of the
// subread that overlap the hq region.
//
if (trimByRegion == true) {
subreadSequence.SubreadStart(
std::max((DNALength)subreadIntervals[intvIndex].start, hqReadStart));
subreadSequence.SubreadEnd(
std::min((DNALength)subreadIntervals[intvIndex].end, hqReadEnd));
}
if (subreadSequence.SubreadStart() >= subreadSequence.SubreadEnd() or
subreadSequence.SubreadEnd() - subreadSequence.SubreadStart() <=
DNALength(minSubreadLength)) {
//
// There is no high quality portion of this subread. Skip it.
//
continue;
}
if (hqRegionScore < minReadScore) {
continue;
}
//
// Print the subread, adding the coordinates as part of the title.
//
subreadSequence.ReferenceSubstring(seq, subreadSequence.SubreadStart(),
subreadSequence.SubreadLength());
std::stringstream titleStream;
titleStream << seq.title;
if (splitSubreads) {
//
// Add the subread coordinates if splitting on subread.
//
titleStream << "/" << subreadSequence.SubreadStart() << "_"
<< subreadSequence.SubreadEnd();
}
//
// If running on simulated data, add where the values were simulated from.
//
if (addSimulatedData) {
titleStream << ((FASTASequence*)&seq)->title << "/chrIndex_"
<< simulatedSequenceIndex << "/position_" << simulatedCoordinate;
((FASTASequence*)&seq)->CopyTitle(titleStream.str());
}
subreadSequence.CopyTitle(titleStream.str());
//
// Eventually replace with WriterAgglomerate.
//
if (printOnlyBest == false) {
if (subreadSequence.length > 0) {
if (printFastq == false) {
((FASTASequence*)&subreadSequence)->PrintSeq(fastaOut);
} else {
subreadSequence.PrintFastq(fastaOut, lineLength);
}
}
} else {
int subreadWeightedScore = subreadSequence.length * hqRegionScore;
if (subreadWeightedScore > bestSubreadScore) {
bestSubreadIndex = intvIndex;
(void)(bestSubreadIndex);
bestSubread = subreadSequence;
bestSubreadScore = subreadWeightedScore;
}
}
}
if (printOnlyBest) {
if (ccsSeq.length > 0) {
if (printFastq == false) {
ccsSeq.PrintSeq(fastaOut);
} else {
ccsSeq.PrintFastq(fastaOut, ccsSeq.length);
}
} else {
if (bestSubreadScore >= 0) {
if (printFastq == false) {
bestSubread.PrintSeq(fastaOut);
} else {
bestSubread.PrintFastq(fastaOut, bestSubread.length);
}
bestSubread.Free();
}
}
ccsSeq.Free();
}
seq.Free();
}
reader.Close();
hdfRegionReader.Close();
}
std::cerr << "[INFO] " << GetTimestamp() << " [" << program << "] ended." << std::endl;
}
|