Search in sources :

Example 26 with Range

use of com.milaboratory.core.Range in project repseqio by repseqio.

the class BaseSequence method resolve.

/**
 * Returns SequenceProvider to access underlying sequence
 *
 * @param context  resolution context
 * @param resolver sequence resolver
 * @return SequenceProvider to access underlying sequence
 */
public SequenceProvider<NucleotideSequence> resolve(Path context, SequenceResolver resolver) {
    CachedSequenceProvider<NucleotideSequence> originalProvider = resolver.resolve(new SequenceAddress(context, origin));
    if (isPureOriginalSequence()) {
        return originalProvider;
    } else {
        // TODO implement more lazy algorithm
        int length = 0;
        for (Range region : regions) length += region.length();
        SequenceBuilder<NucleotideSequence> builder = NucleotideSequence.ALPHABET.createBuilder().ensureCapacity(length);
        for (Range region : regions) builder.append(originalProvider.getRegion(region));
        NucleotideSequence seq = builder.createAndDestroy();
        seq = mutations == null ? seq : mutations.mutate(seq);
        return SequenceProviderUtils.fromSequence(seq);
    }
}
Also used : SequenceAddress(io.repseq.seqbase.SequenceAddress) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) Range(com.milaboratory.core.Range)

Example 27 with Range

use of com.milaboratory.core.Range in project repseqio by repseqio.

the class SequencePartitioning method getTranslationParameters.

/**
 * Returns RangeTranslationParameters for all ranges in current partitioning that can be transcribed
 *
 * @param length length of original sequence (Integer.MAX_VALUE can be used if value is not known in advance, this
 *               value will be set for final range and must be processed accordingly)
 */
public List<RangeTranslationParameters> getTranslationParameters(int length) {
    final boolean reversed = isReversed();
    // Creating list of points
    List<PointPosition> points = new ArrayList<>();
    PointPosition previousPoint = new PointPosition(null, 0);
    for (ReferencePoint currentPoint : ReferencePoint.DefaultReferencePoints) {
        int position = getPosition(currentPoint);
        if (position == -1 || (position < previousPoint.position && !reversed) || (position > previousPoint.position && reversed))
            continue;
        if (currentPoint.isTripletBoundary() || currentPoint.isCodingSequenceBoundary()) {
            if (previousPoint.point != null && previousPoint.position == position) {
                if (previousPoint.point.isCodingOnBothSides() && currentPoint.isCodingOnBothSides())
                    if (previousPoint.point.isTripletBoundary())
                        // Skipping this point, it changes nothing (zero-length coding sequence)
                        continue;
                    else
                        // Current point supersedes previous one
                        points.set(points.size() - 1, previousPoint = new PointPosition(currentPoint, position));
                else if (!previousPoint.point.isTripletBoundary() && !currentPoint.isTripletBoundary() && previousPoint.point.isCodingSequenceOnTheLeft() == currentPoint.isCodingSequenceOnTheRight()) {
                    // Points annihilation
                    points.remove(points.size() - 1);
                    previousPoint = points.isEmpty() ? new PointPosition(null, 0) : points.get(points.size() - 1);
                } else
                    // Both points must be considered in the same position
                    points.add(previousPoint = new PointPosition(currentPoint, position));
            } else
                // Adding point
                points.add(previousPoint = new PointPosition(currentPoint, position));
        }
    }
    if (points.isEmpty())
        return Collections.EMPTY_LIST;
    RangeTranslationParameters.Accumulator acc = new RangeTranslationParameters.Accumulator();
    // Processing left edge
    if (points.get(0).point.isTripletBoundary() && points.get(0).point.isCodingSequenceOnTheLeft() && points.get(0).position != (reversed ? length : 0))
        acc.put(new RangeTranslationParameters(null, points.get(0).point, new Range(reversed ? length : 0, points.get(0).position)));
    // Processing intermediate ranges
    for (int i = 1; i < points.size(); i++) if (points.get(i - 1).point.isCodingSequenceOnTheRight() && points.get(i).point.isCodingSequenceOnTheLeft() && points.get(i - 1).position != points.get(i).position)
        acc.put(new RangeTranslationParameters(points.get(i - 1).point, points.get(i).point, new Range(points.get(i - 1).position, points.get(i).position)));
    // Processing right edge
    if (points.get(points.size() - 1).point.isCodingSequenceOnTheRight() && points.get(points.size() - 1).point.isTripletBoundary() && points.get(points.size() - 1).position != (reversed ? 0 : length))
        acc.put(new RangeTranslationParameters(points.get(points.size() - 1).point, null, new Range(points.get(points.size() - 1).position, reversed ? 0 : length)));
    List<RangeTranslationParameters> result = acc.getResult();
    // Adding codon leftovers
    for (int i = 1; i < result.size(); i++) {
        if (result.get(i - 1).acceptCodonLeftover() && result.get(i).leftIncompleteCodonRange() != null)
            result.set(i - 1, result.get(i - 1).withCodonLeftover(result.get(i).leftIncompleteCodonRange()));
        if (result.get(i).acceptCodonLeftover() && result.get(i - 1).rightIncompleteCodonRange() != null)
            result.set(i, result.get(i).withCodonLeftover(result.get(i - 1).rightIncompleteCodonRange()));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Range(com.milaboratory.core.Range)

Example 28 with Range

use of com.milaboratory.core.Range in project repseqio by repseqio.

the class SequencePartitioning method getRelativeRange.

/**
 * Returns a relative range of specified {@code subFeature} in specified {@code feature} or null if this is not
 * available.
 *
 * @param feature    gene feature
 * @param subFeature a part of feature
 * @return relative range of specified {@code subFeature} in specified {@code feature} or null if this is not
 * available
 */
public Range getRelativeRange(GeneFeature feature, GeneFeature subFeature) {
    // Get target ranges of
    Range[] featureRanges = getRanges(feature);
    if (featureRanges == null)
        return null;
    Range[] subFeatureRanges = getRanges(subFeature);
    if (subFeatureRanges == null)
        return null;
    int offset = 0, begin = -1, end = -1;
    int subFeaturePointer = 0;
    // 0 - before; 1 - rightOnBegin; 2 - inside
    int state = 0;
    for (Range range : featureRanges) {
        int from = subFeatureRanges[subFeaturePointer].getFrom();
        if (state == 0 && range.containsBoundary(from) && subFeatureRanges[subFeaturePointer].hasSameDirection(range)) {
            state = 1;
            begin = offset + range.convertBoundaryToRelativePosition(from);
        }
        int to = subFeatureRanges[subFeaturePointer].getTo();
        if (state > 0 && subFeaturePointer == subFeatureRanges.length - 1) {
            if (!range.containsBoundary(to))
                return null;
            end = offset + range.convertBoundaryToRelativePosition(to);
            break;
        }
        if (state == 1) {
            if (to != range.getTo())
                return null;
            state = 2;
            ++subFeaturePointer;
        } else if (state == 2) {
            if (!subFeatureRanges[subFeaturePointer].equals(range))
                return null;
            ++subFeaturePointer;
        }
        offset += range.length();
    }
    if (begin == -1 || end == -1)
        return null;
    return new Range(begin, end);
}
Also used : Range(com.milaboratory.core.Range)

Example 29 with Range

use of com.milaboratory.core.Range in project repseqio by repseqio.

the class PartitionedSequence method getFeature.

public S getFeature(GeneFeature feature) {
    if (!feature.isComposite()) {
        Range range = getPartitioning().getRange(feature);
        if (range == null)
            return null;
        return getSequence(range);
    }
    Range[] ranges = getPartitioning().getRanges(feature);
    if (ranges == null)
        return null;
    if (ranges.length == 1)
        return getSequence(ranges[0]);
    int size = 0;
    for (Range range : ranges) size += range.length();
    S seq0 = getSequence(ranges[0]);
    SeqBuilder<S> builder = seq0.getBuilder().ensureCapacity(size).append(seq0);
    for (int i = 1; i < ranges.length; ++i) builder.append(getSequence(ranges[i]));
    return builder.createAndDestroy();
}
Also used : Range(com.milaboratory.core.Range)

Example 30 with Range

use of com.milaboratory.core.Range in project repseqio by repseqio.

the class ReferencePointsTest method test4.

@Test
public void test4() throws Exception {
    ReferencePoints rp = new ReferencePoints(3, new int[] { 1, 2, 4, -1, 5, -1, 7 });
    Assert.assertEquals(new Range(2, 4), rp.getRange(GeneFeature.FR1));
    Assert.assertNull(rp.getRange(GeneFeature.CDR1));
    Assert.assertNull(rp.getRange(GeneFeature.FR2));
}
Also used : Range(com.milaboratory.core.Range) Test(org.junit.Test)

Aggregations

Range (com.milaboratory.core.Range)45 Test (org.junit.Test)23 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)19 VDJCHit (com.milaboratory.mixcr.basictypes.VDJCHit)4 ArrayList (java.util.ArrayList)4 SingleFastqReaderTest (com.milaboratory.core.io.sequence.fastq.SingleFastqReaderTest)3 MutationsBuilder (com.milaboratory.core.mutations.MutationsBuilder)3 ReferencePoint (io.repseq.core.ReferencePoint)3 Path (java.nio.file.Path)3 NSequenceWithQuality (com.milaboratory.core.sequence.NSequenceWithQuality)2 Clone (com.milaboratory.mixcr.basictypes.Clone)2 VDJCPartitionedSequence (com.milaboratory.mixcr.basictypes.VDJCPartitionedSequence)2 VDJCAlignerParameters (com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters)2 CUtils (cc.redberry.pipe.CUtils)1 OutputPort (cc.redberry.pipe.OutputPort)1 com.milaboratory.core.alignment (com.milaboratory.core.alignment)1 Alignment (com.milaboratory.core.alignment.Alignment)1 LinearGapAlignmentScoring (com.milaboratory.core.alignment.LinearGapAlignmentScoring)1 MultiAlignmentHelper (com.milaboratory.core.alignment.MultiAlignmentHelper)1 com.milaboratory.core.sequence (com.milaboratory.core.sequence)1