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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
#include <LibBlasrConfig.h>
#ifdef USE_PBBAM
#include <hdf/HDFPulseCallsWriter.hpp>
#include <pbdata/utils/TimeUtils.hpp>
#include <pbcopper/data/QualityValues.h>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <sstream>
const std::vector<PacBio::BAM::BaseFeature> HDFPulseCallsWriter::ValidQVEnums = {
PacBio::BAM::BaseFeature::PULSE_CALL, PacBio::BAM::BaseFeature::LABEL_QV,
PacBio::BAM::BaseFeature::PKMID, PacBio::BAM::BaseFeature::PKMEAN,
PacBio::BAM::BaseFeature::PULSE_MERGE_QV, PacBio::BAM::BaseFeature::ALT_LABEL,
PacBio::BAM::BaseFeature::ALT_LABEL_QV, PacBio::BAM::BaseFeature::START_FRAME,
PacBio::BAM::BaseFeature::PULSE_CALL_WIDTH};
std::vector<PacBio::BAM::BaseFeature> HDFPulseCallsWriter::WritableQVs(
const std::vector<PacBio::BAM::BaseFeature>& qvsToWrite)
{
std::vector<PacBio::BAM::BaseFeature> ret;
for (auto qv : qvsToWrite) {
// Filter qvs which are not in format specification.
if (std::find(ValidQVEnums.begin(), ValidQVEnums.end(), qv) != ValidQVEnums.end()) {
if (std::find(ret.begin(), ret.end(), qv) == ret.end()) ret.push_back(qv);
}
}
return ret;
}
HDFPulseCallsWriter::HDFPulseCallsWriter(const std::string& filename, HDFGroup& parentGroup,
const std::map<char, size_t>& baseMap,
const std::string& basecallerVersion,
const std::vector<PacBio::BAM::BaseFeature>& qvsToWrite)
: HDFWriterBase(filename)
, parentGroup_(parentGroup)
, baseMap_(baseMap)
, qvsToWrite_({}) // Input qvsToWrite must be checked.
, zmwWriter_(nullptr)
, arrayLength_(0)
, basecallerVersion_(basecallerVersion)
{
// Add PulseCalls as a child group to the parent group.
AddChildGroup(parentGroup_, pulsecallsGroup_, PacBio::GroupNames::pulsecalls);
if (basecallerVersion_.empty()) {
AddErrorMessage("BaseCallerVersion must not be empty!");
return;
}
this->qvsToWrite_ = WritableQVs(qvsToWrite);
// Any QVs to write?
if (qvsToWrite_.size() == 0) {
AddErrorMessage("No QVs to write.");
return;
}
// Reject if it does not have StartFrame
if (not _HasQV(PacBio::BAM::BaseFeature::START_FRAME)) {
AddErrorMessage("Bam input must contain tag 'StartFrame' using PPA 2.0.0 or later.");
return;
}
// Initialize QV groups
if (not InitializeQVGroups()) {
AddErrorMessage("Failed to initialize QV Groups.");
return;
}
// Create a zmwWriter.
zmwWriter_.reset(new HDFZMWWriter(Filename(), pulsecallsGroup_, true, baseMap));
inverseGain_ = 1.0f;
// Note: ignore /PulseCalls/ZMWMetrics none of its metrics exist in BAM.
}
uint32_t HDFPulseCallsWriter::NumZMWs(void) const
{
if (zmwWriter_)
return zmwWriter_->NumZMWs();
else
return 0;
}
void HDFPulseCallsWriter::Content(std::vector<std::string>& names,
std::vector<std::string>& types) const
{
// Print order matters;
const bool fakeChi2 = true, fakeMaxSignal = true, fakeMidStdDev = true;
const std::string uint8_t_str = "uint8_t";
const std::string uint16_t_str = "uint16_t";
const std::string uint32_t_str = "uint32_t";
const std::string channeltype = uint8_t_str;
const std::string ispulsetype = uint8_t_str;
const std::string labelqvtype = uint8_t_str;
const std::string mergeqvtype = uint8_t_str;
const std::string altlabeltype = uint8_t_str;
const std::string altlabelqvtype = uint8_t_str;
const std::string chi2type = uint16_t_str;
const std::string maxsignaltype = uint16_t_str;
const std::string meansignaltype = uint16_t_str;
const std::string midsignaltype = uint16_t_str;
const std::string midstddevtype = uint16_t_str;
const std::string widthinframestype = uint16_t_str;
const std::string startframetype = uint32_t_str;
names.clear();
types.clear();
if (HasAltLabel()) {
names.push_back(PacBio::GroupNames::altlabel);
types.push_back(altlabeltype);
}
if (HasAltLabelQV()) {
names.push_back(PacBio::GroupNames::altlabelqv);
types.push_back(altlabelqvtype);
}
if (HasPulseCall()) {
names.push_back(PacBio::GroupNames::channel);
types.push_back(channeltype);
}
if (fakeChi2) { // fake chi2
names.push_back(PacBio::GroupNames::chi2);
types.push_back(chi2type);
}
if (HasIsPulse()) {
names.push_back(PacBio::GroupNames::ispulse);
types.push_back(ispulsetype);
}
if (HasLabelQV()) {
names.push_back(PacBio::GroupNames::labelqv);
types.push_back(labelqvtype);
}
if (fakeMaxSignal) { // fake maxsignal
names.push_back(PacBio::GroupNames::maxsignal);
types.push_back(maxsignaltype);
}
if (HasPkmean()) {
names.push_back(PacBio::GroupNames::meansignal);
types.push_back(meansignaltype);
}
if (HasPulseMergeQV()) {
names.push_back(PacBio::GroupNames::mergeqv);
types.push_back(mergeqvtype);
}
if (HasPkmid()) {
names.push_back(PacBio::GroupNames::midsignal);
types.push_back(midsignaltype);
}
if (fakeMidStdDev) { // fake MidStdDev
names.push_back(PacBio::GroupNames::midstddev);
types.push_back(midstddevtype);
}
if (HasStartFrame()) {
names.push_back(PacBio::GroupNames::startframe);
types.push_back(startframetype);
}
if (HasPulseCallWidth()) {
names.push_back(PacBio::GroupNames::widthinframes);
types.push_back(widthinframestype);
}
}
void HDFPulseCallsWriter::SetInverseGain(const float igain) { inverseGain_ = igain; }
bool HDFPulseCallsWriter::_WriteAttributes(void)
{
std::vector<std::string> content_names, content_types;
Content(content_names, content_types);
// ChangeListID
bool OK =
AddAttribute<std::string>(pulsecallsGroup_, PacBio::AttributeNames::Common::changelistid,
basecallerVersion_) and
// Content
AddAttribute<std::vector<std::string>>(
pulsecallsGroup_, PacBio::AttributeNames::Common::content, content_names) and
// ContentStored
AddAttribute<uint32_t>(pulsecallsGroup_, PacBio::AttributeNames::Common::contentstored,
NumZMWs()) and
// DateCreated
AddAttribute<std::string>(pulsecallsGroup_, PacBio::AttributeNames::Common::datacreated,
GetTimestamp()) and
// SchemaRevision
AddAttribute<std::string>(pulsecallsGroup_, PacBio::AttributeNames::Common::schemarevision,
PacBio::AttributeValues::Common::schemarevision);
return OK;
}
std::vector<std::string> HDFPulseCallsWriter::Errors(void) const
{
std::vector<std::string> retErrors = this->errors_;
if (zmwWriter_) {
const std::vector<std::string>& zmwErrors = zmwWriter_->Errors();
retErrors.insert(retErrors.end(), zmwErrors.begin(), zmwErrors.end());
}
return retErrors;
}
HDFPulseCallsWriter::~HDFPulseCallsWriter(void) { this->Close(); }
bool HDFPulseCallsWriter::InitializeQVGroups(void)
{
int ret = 1;
if (_HasQV(PacBio::BAM::BaseFeature::PULSE_CALL))
ret *= pulseCallArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::channel);
ret *= isPulseArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::ispulse);
if (_HasQV(PacBio::BAM::BaseFeature::LABEL_QV))
ret *= labelQVArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::labelqv);
if (_HasQV(PacBio::BAM::BaseFeature::PKMEAN))
ret *= pkmeanArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::meansignal, 4);
if (_HasQV(PacBio::BAM::BaseFeature::PULSE_MERGE_QV))
ret *= pulseMergeQVArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::mergeqv);
if (_HasQV(PacBio::BAM::BaseFeature::PKMID))
ret *= pkmidArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::midsignal);
if (_HasQV(PacBio::BAM::BaseFeature::START_FRAME))
ret *= startFrameArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::startframe);
if (_HasQV(PacBio::BAM::BaseFeature::PULSE_CALL_WIDTH))
ret *= pulseCallWidthArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::widthinframes);
if (_HasQV(PacBio::BAM::BaseFeature::ALT_LABEL))
ret *= altLabelArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::altlabel);
if (_HasQV(PacBio::BAM::BaseFeature::ALT_LABEL_QV))
ret *= altLabelQVArray_.Initialize(pulsecallsGroup_, PacBio::GroupNames::altlabelqv);
return (ret != 0);
}
bool HDFPulseCallsWriter::WriteOneZmw(const SMRTSequence& read)
{
const PacBio::BAM::BamRecord& record = read.bamRecord;
if (zmwWriter_) zmwWriter_->WriteOneZmw(record);
_WritePulseCall(record); // Write PulseCall and IsPulse
_WriteLabelQV(record);
_WritePkmean(record);
_WritePulseMergeQV(record);
_WritePkmid(record);
_WriteStartFrame(record);
_WritePulseCallWidth(record);
_WriteAltLabel(record);
_WriteAltLabelQV(record);
arrayLength_ += record.PulseCall().size();
return Errors().empty();
}
bool HDFPulseCallsWriter::WriteFakeDataSets()
{
uint32_t block_sz = 65536; // This is a data buffer.
std::vector<uint16_t> buffer_uint16_5M_0(block_sz, 0);
// Write 2D Array: Chi2, 1D Arrays: MaxSignal, MidStdDev
bool OK = __WriteFake2DDataSet<uint16_t>(pulsecallsGroup_, PacBio::GroupNames::chi2,
arrayLength_, 4, 0) and
__WriteFakeDataSet<uint16_t>(pulsecallsGroup_, PacBio::GroupNames::maxsignal,
arrayLength_, buffer_uint16_5M_0) and
__WriteFakeDataSet<uint16_t>(pulsecallsGroup_, PacBio::GroupNames::midstddev,
arrayLength_, buffer_uint16_5M_0);
if (zmwWriter_) return OK and zmwWriter_->WriteFakeDataSets();
return OK;
}
bool HDFPulseCallsWriter::_CheckRead(const PacBio::BAM::BamRecord& read, const uint32_t qvLength,
const std::string& qvName)
{
// Tag 'pb' will not be available until 3.0.1, bug 29486
(void)(read);
(void)(qvLength);
(void)(qvName);
return true;
/*
// FIXME: pbbam should provide HasPulseBlockSize() and PulseBlockSize().
if (read.Impl().HasTag("pb")) {
if (qvLength != read.Impl().TagValue("pb").ToUInt32()) {
AddErrorMessage(std::string(qvName) + "'s size does not match PulseBlockSize in read " + read.FullName());
return false;
} else {
return true;
}
} else {
AddErrorMessage(std::string("Tag PulseBlockSize is absent in read ") + read.FullName());
return false;
}
*/
}
bool HDFPulseCallsWriter::_WritePulseCall(const PacBio::BAM::BamRecord& read)
{
if (HasPulseCall()) {
if (read.HasPulseCall()) {
// Write both PulseCall (i.e., Channel) and IsPulse
const std::string& pulsecall = read.PulseCall();
const unsigned int length = pulsecall.size();
_CheckRead(read, length, "PulseCall");
std::vector<uint8_t> channel;
channel.resize(length);
uint32_t num_bases = 0;
for (size_t i = 0; i < length; i++) {
const char base = pulsecall[i];
if (base == 'A' or base == 'C' or base == 'G' or base == 'T') {
channel[i] = static_cast<uint8_t>(baseMap_[std::toupper(base)]);
num_bases += 1;
} else if (base == 'a' or base == 'c' or base == 'g' or base == 't') {
channel[i] = static_cast<uint8_t>(baseMap_[std::toupper(base)]);
} else {
AddErrorMessage(
std::string("Unrecognizable base in PulseCall of " + read.FullName()));
}
}
if (num_bases != read.Sequence().size()) {
AddErrorMessage(
std::string("Number of bases in PulseCall does not match BaseCall in read ") +
read.FullName());
}
pulseCallArray_.Write(&channel[0], length);
std::vector<uint8_t> ispulse(length, 1); // all pulses in BAM are real pulses
isPulseArray_.Write(&ispulse[0], length);
} else {
AddErrorMessage(std::string("PulseCall is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WriteLabelQV(const PacBio::BAM::BamRecord& read)
{
if (HasLabelQV()) {
if (read.HasLabelQV()) {
const PacBio::Data::QualityValues& qvs = read.LabelQV();
std::vector<uint8_t> data(qvs.cbegin(), qvs.cend());
_CheckRead(read, data.size(), "LabelQV");
labelQVArray_.Write(&data[0], data.size());
} else {
AddErrorMessage(std::string("LabelQV is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WritePkmean(const PacBio::BAM::BamRecord& read)
{
if (HasPkmean()) {
if (read.HasPkmean()) {
std::vector<float> pkmids = read.Pkmean();
// convert from photoE to counts
std::for_each(pkmids.begin(), pkmids.end(),
[&](float& x) { x = std::round(x / inverseGain_); });
// down-convert to ushorts (required by pulse file specification)
std::vector<uint16_t> data(pkmids.begin(), pkmids.end());
const std::string& pulsecall = read.PulseCall();
for (size_t i = 0; i < pulsecall.size(); i++) {
uint16_t pkm[4] = {0, 0, 0, 0};
pkm[baseMap_[std::toupper(pulsecall[i])]] = data[i];
pkmeanArray_.WriteRow(pkm, 4);
}
} else {
AddErrorMessage(std::string("Pkmean is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WritePulseMergeQV(const PacBio::BAM::BamRecord& read)
{
if (HasPulseMergeQV()) {
if (read.HasPulseMergeQV()) {
const PacBio::Data::QualityValues& qvs = read.PulseMergeQV();
std::vector<uint8_t> data(qvs.cbegin(), qvs.cend());
_CheckRead(read, data.size(), "PulseMergeQV");
pulseMergeQVArray_.Write(&data[0], data.size());
} else {
AddErrorMessage(std::string("PulseMergeQV is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WritePkmid(const PacBio::BAM::BamRecord& read)
{
if (HasPkmid()) {
if (read.HasPkmid()) {
std::vector<float> pkmids = read.Pkmid();
// convert from photoE to counts
std::for_each(pkmids.begin(), pkmids.end(),
[&](float& x) { x = std::round(x / inverseGain_); });
// down-convert to ushorts (required by pulse file specification)
std::vector<uint16_t> data(pkmids.begin(), pkmids.end());
pkmidArray_.Write(&data[0], data.size());
} else {
AddErrorMessage(std::string("Pkmid is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WriteStartFrame(const PacBio::BAM::BamRecord& read)
{
if (HasStartFrame()) {
if (read.HasStartFrame()) {
const std::vector<uint32_t> data = read.StartFrame();
_CheckRead(read, data.size(), "StartFrame");
startFrameArray_.Write(&data[0], data.size());
} else {
AddErrorMessage(std::string("StartFrame is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WritePulseCallWidth(const PacBio::BAM::BamRecord& read)
{
if (HasPulseCallWidth()) {
if (read.HasPulseCallWidth()) {
//FIXME: pbbam PulseCallWidth().Data() returns incorrect vector (e.g., size)
const PacBio::BAM::Tag& tag = read.Impl().TagValue("px");
std::vector<uint16_t> data = tag.ToUInt16Array();
_CheckRead(read, data.size(), "PulseCallWidth");
pulseCallWidthArray_.Write(&data[0], data.size());
} else {
AddErrorMessage(std::string("PulseCallWidth is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WriteAltLabel(const PacBio::BAM::BamRecord& read)
{
if (HasAltLabel()) {
if (read.HasAltLabelTag()) {
const std::string& tags = read.AltLabelTag();
std::vector<unsigned char> data(tags.begin(), tags.end());
_CheckRead(read, data.size(), "AltLabel");
altLabelArray_.Write(&data[0], tags.size());
} else {
AddErrorMessage(std::string("AltLabel is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
bool HDFPulseCallsWriter::_WriteAltLabelQV(const PacBio::BAM::BamRecord& read)
{
if (HasAltLabelQV()) {
if (read.HasAltLabelQV()) {
const PacBio::Data::QualityValues& qvs = read.AltLabelQV();
std::vector<uint8_t> data(qvs.begin(), qvs.end());
_CheckRead(read, data.size(), "AltLabelQV");
altLabelQVArray_.Write(&data[0], data.size());
} else {
AddErrorMessage(std::string("AltLabelQV is absent in read " + read.FullName()));
}
}
return Errors().empty();
}
void HDFPulseCallsWriter::Flush(void)
{
if (HasPulseCall()) pulseCallArray_.Flush();
if (HasIsPulse()) isPulseArray_.Flush();
if (HasLabelQV()) labelQVArray_.Flush();
if (HasPkmean()) pkmeanArray_.Flush();
if (HasPulseMergeQV()) pulseMergeQVArray_.Flush();
if (HasPkmid()) pkmidArray_.Flush();
if (HasStartFrame()) startFrameArray_.Flush();
if (HasPulseCallWidth()) pulseCallWidthArray_.Flush();
if (HasAltLabel()) altLabelArray_.Flush();
if (HasAltLabelQV()) altLabelQVArray_.Flush();
if (zmwWriter_) zmwWriter_->Flush();
}
void HDFPulseCallsWriter::Close(void)
{
this->Flush();
// Write attributes to pulsecallsGroup
try {
_WriteAttributes();
} catch (const H5::Exception& e) {
AddErrorMessage("Failed to write attributes to " + PacBio::GroupNames::pulsecalls);
}
if (HasPulseCall()) pulseCallArray_.Close();
if (HasIsPulse()) isPulseArray_.Close();
if (HasLabelQV()) labelQVArray_.Close();
if (HasPkmean()) pkmeanArray_.Close();
if (HasPulseMergeQV()) pulseMergeQVArray_.Close();
if (HasPkmid()) pkmidArray_.Close();
if (HasStartFrame()) startFrameArray_.Close();
if (HasPulseCallWidth()) pulseCallWidthArray_.Close();
if (HasAltLabel()) altLabelArray_.Close();
if (HasAltLabelQV()) altLabelQVArray_.Close();
}
#endif
|