Search in sources :

Example 21 with Range

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

the class ConcatenatedLazySequence method getRegion.

@Override
public S getRegion(Range range) {
    if (range.getUpper() > size())
        throw new IllegalArgumentException("Can't get sequence outside defined region.");
    Range direct = range.isReverse() ? new Range(range.getLower(), range.getUpper()) : range;
    SequenceBuilder<S> seq = null;
    for (SequenceProvider<S> provider : providers) {
        if (provider.size() <= direct.getLower())
            direct = direct.move(-provider.size());
        else {
            Range targetRange;
            if (direct.getUpper() <= provider.size()) {
                targetRange = direct;
                direct = null;
            } else {
                Range r = new Range(0, provider.size());
                targetRange = direct.intersection(r);
                assert targetRange != null;
                direct = new Range(0, direct.getUpper() - provider.size());
            }
            S currentSeq = provider.getRegion(targetRange);
            if (seq == null)
                seq = currentSeq.getBuilder().ensureCapacity(range.length()).append(currentSeq);
            else
                seq.append(currentSeq);
            if (direct == null)
                break;
        }
    }
    assert seq != null;
    S result = seq.createAndDestroy();
    return range.isReverse() ? result.getRange(result.size(), 0) : result;
}
Also used : Range(com.milaboratory.core.Range)

Example 22 with Range

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

the class SequencePartitioning method getRelativePosition.

/**
 * Returns a relative position of specified {@code referencePoint} in specified {@code feature} or -1 if this
 * position is not available.
 *
 * @param feature        gene feature
 * @param referencePoint reference point
 * @return a relative position of specified {@code referencePoint} in specified {@code feature} or -1 if this
 * position is not available
 */
public int getRelativePosition(GeneFeature feature, ReferencePoint referencePoint) {
    int absolutePosition = getPosition(referencePoint);
    if (absolutePosition == -1)
        return -1;
    Range[] ranges = getRanges(feature);
    if (ranges == null)
        return -1;
    int relativePosition = 0;
    for (int i = 0; i < ranges.length; i++) {
        Range range = ranges[i];
        if (!feature.getReferenceRange(i).isReversed() && range.containsBoundary(absolutePosition))
            return relativePosition + range.convertBoundaryToRelativePosition(absolutePosition);
        else
            relativePosition += range.length();
    }
    return -1;
}
Also used : Range(com.milaboratory.core.Range)

Example 23 with Range

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

the class RangeSet method add.

public RangeSet add(Range range) {
    if (range.isEmpty())
        return this;
    if (range.isReverse())
        throw new IllegalArgumentException();
    List<Range> result = new ArrayList<>(ranges.size() + 1);
    for (Range r : ranges) if (r.intersectsWithOrTouches(range)) {
        range = r.tryMerge(range);
        assert range != null;
    } else
        result.add(r);
    result.add(range);
    Collections.sort(result);
    return new RangeSet(result);
}
Also used : Range(com.milaboratory.core.Range)

Example 24 with Range

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

the class MergerTest method mAssert.

public static void mAssert(String originalSeq, String seq1, String seq2, String expectedSequence, String expectedQuality) {
    NucleotideSequence originalSequence = new NucleotideSequence(originalSeq);
    NSequenceWithQuality target1 = new NSequenceWithQuality(seq1, lets('A', seq1.length()));
    NSequenceWithQuality target2 = new NSequenceWithQuality(seq2, lets('B', seq2.length()));
    LinearGapAlignmentScoring<NucleotideSequence> scoring = new LinearGapAlignmentScoring<>(NucleotideSequence.ALPHABET, 4, -5, -9);
    Alignment<NucleotideSequence> alignment1 = Aligner.alignLocalLinear(scoring, originalSequence, target1.getSequence());
    Alignment<NucleotideSequence> alignment2 = Aligner.alignLocalLinear(scoring, originalSequence, target2.getSequence());
    NSequenceWithQuality result = Merger.merge(new Range(0, originalSequence.size()), new Alignment[] { alignment1, alignment2 }, new NSequenceWithQuality[] { target1, target2 });
    Assert.assertEquals(expectedSequence, result.getSequence().toString());
    Assert.assertEquals(expectedQuality, result.getQuality().toString());
}
Also used : LinearGapAlignmentScoring(com.milaboratory.core.alignment.LinearGapAlignmentScoring) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) Range(com.milaboratory.core.Range)

Example 25 with Range

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

the class CompileAction method compile.

public static void compile(Path source, Path destination, int surroundings) throws IOException {
    VDJCLibraryRegistry.resetDefaultRegistry();
    VDJCLibraryRegistry reg = VDJCLibraryRegistry.getDefault();
    reg.registerLibraries(source, "lib");
    List<VDJCLibraryData> result = new ArrayList<>();
    for (VDJCLibrary lib : reg.getLoadedLibraries()) {
        VDJCDataUtils.FragmentsBuilder fragmentsBuilder = new VDJCDataUtils.FragmentsBuilder();
        for (KnownSequenceFragmentData fragment : lib.getData().getSequenceFragments()) fragmentsBuilder.addRegion(fragment);
        for (VDJCGene gene : lib.getGenes()) {
            if (!gene.getData().getBaseSequence().isPureOriginalSequence())
                throw new IllegalArgumentException("Don't support mutated sequences yet.");
            URI uri = gene.getData().getBaseSequence().getOrigin();
            Range region = gene.getPartitioning().getContainingRegion();
            region = region.expand(surroundings);
            NucleotideSequence seq;
            try {
                seq = gene.getSequenceProvider().getRegion(region);
            } catch (SequenceProviderIndexOutOfBoundsException e) {
                region = e.getAvailableRange();
                if (region == null)
                    throw new IllegalArgumentException("Wrong anchor points for " + gene.getName() + " ?");
                seq = gene.getSequenceProvider().getRegion(region);
            }
            fragmentsBuilder.addRegion(uri, region, seq);
        }
        result.add(new VDJCLibraryData(lib.getTaxonId(), lib.getData().getSpeciesNames(), lib.getData().getGenes(), lib.getData().getMeta(), fragmentsBuilder.getFragments()));
    }
    VDJCDataUtils.writeToFile(result, destination, true);
    log.info("{} compiled successfully.", source);
}
Also used : VDJCDataUtils(io.repseq.dto.VDJCDataUtils) ArrayList(java.util.ArrayList) Range(com.milaboratory.core.Range) URI(java.net.URI) KnownSequenceFragmentData(io.repseq.dto.KnownSequenceFragmentData) SequenceProviderIndexOutOfBoundsException(com.milaboratory.core.sequence.provider.SequenceProviderIndexOutOfBoundsException) VDJCLibraryData(io.repseq.dto.VDJCLibraryData) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCLibrary(io.repseq.core.VDJCLibrary) VDJCGene(io.repseq.core.VDJCGene) VDJCLibraryRegistry(io.repseq.core.VDJCLibraryRegistry)

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