void LodExtract::writeUnsampledSequence(const Sequence* outSequence, SegmentIteratorPtr outSegment) { outSegment->setCoordinates(outSequence->getStartPosition(), outSequence->getSequenceLength()); if (outSegment->isTop()) { assert(outSequence->getNumTopSegments() == 1); TopSegmentIteratorPtr top = outSegment.downCast<TopSegmentIteratorPtr>(); top->setParentIndex(NULL_INDEX); top->setParentReversed(false); top->setNextParalogyIndex(NULL_INDEX); top->setBottomParseIndex(NULL_INDEX); } else { assert(outSequence->getNumBottomSegments() == 1); BottomSegmentIteratorPtr bottom = outSegment.downCast<BottomSegmentIteratorPtr>(); hal_size_t numChildren = bottom->getNumChildren(); for (hal_size_t childNum = 0; childNum < numChildren; ++childNum) { bottom->setChildIndex(childNum, NULL_INDEX); bottom->setChildReversed(childNum, false); } bottom->setTopParseIndex(NULL_INDEX); } }
int MappedSegment::boundComp(const SegmentIteratorPtr &s1, const SegmentIteratorPtr &s2) { int res = 0; bool flip = s2->getReversed(); if (flip) { s2->toReverse(); } if (s1->isTop() && !s2->isTop()) { BottomSegmentIteratorPtr bot = std::dynamic_pointer_cast<BottomSegmentIterator>(s2); hal_index_t lb = bot->bseg()->getTopParseIndex(); hal_index_t ub = lb; if ((hal_size_t)bot->getArrayIndex() < bot->getGenome()->getNumBottomSegments() - 1) { bot = bot->clone(); bot->slice(0, 0); bot->toRight(); ub = bot->bseg()->getTopParseIndex(); } if (s1->getArrayIndex() < lb) { res = -1; } else if (s1->getArrayIndex() > ub) { res = 1; } } else if (!s1->isTop() && s2->isTop()) { TopSegmentIteratorPtr top = std::dynamic_pointer_cast<TopSegmentIterator>(s2); hal_index_t lb = top->tseg()->getBottomParseIndex(); hal_index_t ub = lb; if ((hal_size_t)top->getArrayIndex() < top->getGenome()->getNumTopSegments() - 1) { top = top->clone(); top->slice(0, 0); top->toRight(); ub = top->tseg()->getBottomParseIndex(); } if (s1->getArrayIndex() < lb) { res = -1; } else if (s1->getArrayIndex() > ub) { res = 1; } } if (flip) { s2->toReverse(); } return res; }
int MappedSegment::fastComp(const SegmentIteratorPtr &s1, const SegmentIteratorPtr &s2) { // compare without accessing anything from disk (ie using only index // and offset) int res = 0; assert(s1->getGenome() == s2->getGenome()); if (s1->isTop() != s2->isTop()) { res = boundComp(s1, s2); if (res == 0) { res = slowComp(s1, s2); } } else { if (s1->getArrayIndex() < s2->getArrayIndex()) { res = -1; } else if (s1->getArrayIndex() > s2->getArrayIndex()) { res = 1; } else { hal_offset_t so1 = s1->getStartOffset(); hal_offset_t eo1 = s1->getEndOffset(); if (s1->getReversed()) { swap(so1, eo1); } hal_offset_t so2 = s2->getStartOffset(); hal_offset_t eo2 = s2->getEndOffset(); if (s2->getReversed()) { swap(so2, eo2); } if (so1 < so2) { res = -1; } else if (so1 > so2) { res = 1; } else if (eo1 > eo2) { res = -1; } else if (eo1 < eo2) { res = 1; } } } assert(res == slowComp(s1, s2)); return res; }
// note: takes smart pointer as it maybe added to the results static hal_size_t mapSelf(MappedSegmentPtr mappedSeg, list<MappedSegmentPtr> &results, hal_size_t minLength) { hal_size_t added = 0; if (mappedSeg->isTop() == true) { SegmentIteratorPtr target = mappedSeg->getTargetIteratorPtr(); SegmentIteratorPtr source = mappedSeg->getSourceIteratorPtr(); TopSegmentIteratorPtr top = std::dynamic_pointer_cast<TopSegmentIterator>(target); TopSegmentIteratorPtr topCopy = top->clone(); do { // FIXME: why isn't clone() polymorphic? SegmentIteratorPtr newSource; if (source->isTop()) { newSource = std::dynamic_pointer_cast<TopSegmentIterator>(source)->clone(); } else { newSource = std::dynamic_pointer_cast<BottomSegmentIterator>(source)->clone(); } TopSegmentIteratorPtr newTop = topCopy->clone(); MappedSegmentPtr newMappedSeg(new MappedSegment(newSource, newTop)); assert(newMappedSeg->getGenome() == mappedSeg->getGenome()); assert(newMappedSeg->getSource()->getGenome() == mappedSeg->getSource()->getGenome()); results.push_back(newMappedSeg); ++added; if (topCopy->tseg()->hasNextParalogy()) { topCopy->toNextParalogy(); } } while (topCopy->tseg()->hasNextParalogy() == true && topCopy->getLength() >= minLength && topCopy->getArrayIndex() != top->getArrayIndex()); } else if (mappedSeg->getGenome()->getParent() != NULL) { hal_index_t rightCutoff = mappedSeg->getEndPosition(); BottomSegmentIteratorPtr bottom = mappedSeg->targetAsBottom(); hal_index_t startOffset = (hal_index_t)bottom->getStartOffset(); hal_index_t endOffset = (hal_index_t)bottom->getEndOffset(); TopSegmentIteratorPtr top = mappedSeg->getGenome()->getTopSegmentIterator(); top->toParseUp(bottom); do { TopSegmentIteratorPtr topNew = top->clone(); // we map the new target back to see how the offsets have // changed. these changes are then applied to the source segment // as deltas BottomSegmentIteratorPtr bottomBack = bottom->clone(); bottomBack->toParseDown(topNew); hal_index_t startBack = (hal_index_t)bottomBack->getStartOffset(); hal_index_t endBack = (hal_index_t)bottomBack->getEndOffset(); assert(startBack >= startOffset); assert(endBack >= endOffset); SegmentIteratorPtr newSource = mappedSeg->sourceClone(); hal_index_t startDelta = startBack - startOffset; hal_index_t endDelta = endBack - endOffset; assert((hal_index_t)newSource->getLength() > startDelta + endDelta); newSource->slice(newSource->getStartOffset() + startDelta, newSource->getEndOffset() + endDelta); MappedSegmentPtr newMappedSeg(new MappedSegment(newSource, topNew)); assert(newMappedSeg->isTop() == true); assert(newMappedSeg->getSource()->getGenome() == mappedSeg->getSource()->getGenome()); added += mapSelf(newMappedSeg, results, minLength); // stupid that we have to make this check but odn't want to // make fundamental api change now if (top->getEndPosition() != rightCutoff) { top->toRight(rightCutoff); } else { break; } } while (true); } return added; }