std::vector<T_AlignmentCandidate*> HitPolicy::Apply(const std::vector<T_AlignmentCandidate*> alnPtrs, const bool & createRand, const int & passedRand) const { // Shallow copy pointers. if (alnPtrs.empty() or IsAll()) return alnPtrs; int rint = createRand?rand():passedRand; if (IsRandom()) { return std::vector<T_AlignmentCandidate*>({alnPtrs[rint%alnPtrs.size()]}); } std::vector<T_AlignmentCandidate*> ret = alnPtrs; // Sort AlignmentCandidate pointers according to score and target start position. ScoreSign sign = this->Sign(); std::sort(ret.begin(), ret.end(), SortAlignmentPointersByScore()); // Get the best alignments whose alignment scores are the best. // Assume that input alignments share the same query name and // are sorted by score and tPos asscendingly: worst, ...., best int bestScore = ret[0]->score; ret.erase(std::remove_if(ret.begin(), ret.end(), [&bestScore](const T_AlignmentCandidate* x)->bool{return x->score != bestScore;}), ret.end()); if (IsAllbest()) { return ret; } else if (IsRandombest()) { return std::vector<T_AlignmentCandidate*>({ret[rint%ret.size()]}); } else if (IsLeftmost()) { return std::vector<T_AlignmentCandidate*>({ret[0]}); } else { assert("Unsupported hit policy" == 0); } }
std::vector<PacBio::BAM::BamRecord> HitPolicy::Apply( const std::vector<PacBio::BAM::BamRecord>& records, const bool& createRand, const int& passedRand) const { if (records.empty() or IsAll()) return records; int rint = createRand ? rand() : passedRand; //std::cout << "FilterCriteria " << ", " << records[0].FullName() << ", " << rint << std::endl; if (IsRandom()) { return std::vector<PacBio::BAM::BamRecord>({records[rint % records.size()]}); } std::vector<PacBio::BAM::BamRecord> ret = records; // Sort bam records according to score and target start position. ScoreSign sign = this->Sign(); std::sort(ret.begin(), ret.end(), [&sign](const PacBio::BAM::BamRecord& a, const PacBio::BAM::BamRecord& b) -> bool { assert(a.Impl().HasTag(AS) and b.Impl().HasTag(AS)); assert(a.FullName() == b.FullName()); const int aScore = a.Impl().TagValue(AS).ToInt32(); const int bScore = b.Impl().TagValue(AS).ToInt32(); if (aScore == bScore) return a.ReferenceStart() < b.ReferenceEnd(); else return Score(aScore, sign).WorseThan(Score(bScore, sign)); }); // Get the best alignments whose alignment scores are the best. // Assume that input alignments share the same query name and // are sorted by score and tPos asscendingly: worst, ...., best int bestScore = ret[0].Impl().TagValue(AS).ToInt32(); ret.erase(std::remove_if(ret.begin(), ret.end(), [&bestScore](const PacBio::BAM::BamRecord& x) -> bool { return x.Impl().TagValue(AS).ToInt32() != bestScore; }), ret.end()); if (IsAllbest()) { return ret; } else if (IsRandombest()) { return std::vector<PacBio::BAM::BamRecord>({ret[rint % ret.size()]}); } else if (IsLeftmost()) { return std::vector<PacBio::BAM::BamRecord>({ret[0]}); } else { BLASR_THROW("Unsupported hit policy"); } }