Search in sources :

Example 6 with ReferencePoint

use of io.repseq.core.ReferencePoint in project repseqio by repseqio.

the class GeneFeature method intersection0.

private static GeneFeature intersection0(GeneFeature gf1, GeneFeature gf2) {
    ReferencePoint firstReferencePoint1 = gf1.regions[0].begin;
    ReferencePoint firstReferencePoint2 = gf2.regions[0].begin;
    if (firstReferencePoint1.compareTo(firstReferencePoint2) > 0)
        return intersection(gf2, gf1);
    int rangePointer1 = 0;
    while (gf1.regions[rangePointer1].end.compareTo(firstReferencePoint2) <= 0) if (++rangePointer1 == gf1.regions.length)
        return null;
    if (gf1.regions[rangePointer1].begin.compareTo(firstReferencePoint2) > 0)
        throw new IllegalArgumentException();
    ArrayList<ReferenceRange> result = new ArrayList<>();
    // result.add(new ReferenceRange(firstReferencePoint2, gf1.regions[rangePointer1].end));
    // 
    // ++rangePointer1;
    // int rangePointer2 = 1;
    int rangePointer2 = 0;
    while (true) {
        if (rangePointer1 == gf1.regions.length || rangePointer2 == gf2.regions.length)
            break;
        if (rangePointer2 != 0 && !gf1.regions[rangePointer1].begin.equals(gf2.regions[rangePointer2].begin))
            throw new IllegalArgumentException();
        int c = gf1.regions[rangePointer1].end.compareTo(gf2.regions[rangePointer2].end);
        ReferencePoint maxBegin = max(gf1.regions[rangePointer1].begin, gf2.regions[rangePointer2].begin);
        if (c != 0) {
            if (c > 0) {
                result.add(new ReferenceRange(maxBegin, gf2.regions[rangePointer2].end));
                if (rangePointer2 == gf2.regions.length - 1)
                    break;
                ++rangePointer2;
            } else {
                result.add(new ReferenceRange(maxBegin, gf1.regions[rangePointer1].end));
                if (rangePointer1 == gf1.regions.length - 1)
                    break;
                ++rangePointer1;
            }
        } else {
            result.add(new ReferenceRange(maxBegin, gf1.regions[rangePointer1].end));
            ++rangePointer1;
            ++rangePointer2;
        }
    }
    return new GeneFeature(result.toArray(new ReferenceRange[result.size()]), true);
}
Also used : ReferencePoint(io.repseq.core.ReferencePoint) ReferencePoint(io.repseq.core.ReferencePoint)

Example 7 with ReferencePoint

use of io.repseq.core.ReferencePoint in project repseqio by repseqio.

the class GeneFeature method getCodingGeneFeature.

/**
 * Returns coding gene feature contained in input gene feature
 *
 * @param feature input gene feature
 * @return coding gene feature contained in input gene feature or null
 */
public static synchronized GeneFeature getCodingGeneFeature(GeneFeature feature) {
    GeneFeature result = codingGeneFeaturesCache.get(feature);
    if (result == null) {
        List<ReferenceRange> resultRanges = new ArrayList<>();
        ReferencePoint previousPoint = null, lastPoint = null;
        for (ReferenceRange region : feature.regions) for (ReferencePoint intermediatePoint : region.getIntermediatePoints()) {
            if (previousPoint == null && intermediatePoint.isCodingSequenceOnTheRight())
                previousPoint = intermediatePoint;
            else if (previousPoint != null && !intermediatePoint.isCodingSequenceOnTheRight()) {
                if (!intermediatePoint.isCodingSequenceOnTheLeft())
                    throw new IllegalArgumentException("Can't calculate coding feature for " + feature + ".");
                resultRanges.add(new ReferenceRange(previousPoint, intermediatePoint));
                previousPoint = null;
            }
            lastPoint = intermediatePoint;
        }
        if (previousPoint != null && previousPoint != lastPoint) {
            if (!lastPoint.isCodingSequenceOnTheLeft())
                throw new IllegalArgumentException("Can't calculate coding feature for " + feature + ".");
            resultRanges.add(new ReferenceRange(previousPoint, lastPoint));
        }
        if (resultRanges.isEmpty())
            // Caching null result
            codingGeneFeaturesCache.put(feature, result = NULL_GENE_FEATURE);
        else
            codingGeneFeaturesCache.put(feature, result = new GeneFeature(resultRanges.toArray(new ReferenceRange[resultRanges.size()]), true));
    }
    return result == NULL_GENE_FEATURE ? null : result;
}
Also used : ReferencePoint(io.repseq.core.ReferencePoint)

Example 8 with ReferencePoint

use of io.repseq.core.ReferencePoint in project repseqio by repseqio.

the class DebugAction method getAminoAcidSequence.

private static AminoAcidSequence getAminoAcidSequence(VDJCGene gene, GeneFeature geneFeature, NucleotideSequence nSequence) {
    ReferencePoints partitioning = gene.getPartitioning();
    ReferencePoint frameReference = GeneFeature.getFrameReference(geneFeature);
    AminoAcidSequence aaSequence;
    if (frameReference != null) {
        int relativePosition = partitioning.getRelativePosition(geneFeature, frameReference);
        aaSequence = nSequence == null || relativePosition < 0 ? null : AminoAcidSequence.translate(nSequence, withIncompleteCodon(relativePosition));
    } else
        aaSequence = null;
    return aaSequence;
}
Also used : ReferencePoint(io.repseq.core.ReferencePoint) AminoAcidSequence(com.milaboratory.core.sequence.AminoAcidSequence) ReferencePoints(io.repseq.core.ReferencePoints) ReferencePoint(io.repseq.core.ReferencePoint)

Example 9 with ReferencePoint

use of io.repseq.core.ReferencePoint in project repseqio by repseqio.

the class GeneFeature method parseSingle.

private static GeneFeature parseSingle(String string) {
    string = string.trim();
    if ("null".equals(string))
        return null;
    // single feature
    if (string.charAt(0) == '{') {
        // feature by points {from:to}
        if (string.charAt(string.length() - 1) != '}')
            throw new IllegalArgumentException("Incorrect input: " + string);
        string = string.substring(1, string.length() - 1);
        String[] fromTo = string.split(":");
        if (fromTo.length != 2)
            throw new IllegalArgumentException("Incorrect input: " + string);
        return new GeneFeature(ReferencePoint.parse(fromTo[0]), ReferencePoint.parse(fromTo[1]));
    } else {
        // feature by name CDR2(-2,3)
        int br = string.indexOf('(');
        if (br == -1) {
            GeneFeature base;
            base = getFeatureByName(string);
            if (base == null)
                throw new IllegalArgumentException("Unknown feature: " + string);
            return base;
        } else {
            if (string.charAt(string.length() - 1) != ')')
                throw new IllegalArgumentException("Wrong syntax: " + string);
            Object base;
            String baseName = string.substring(0, br);
            base = getFeatureByName(baseName);
            if (base == null)
                base = ReferencePoint.getPointByName(baseName);
            if (base == null)
                throw new IllegalArgumentException("Unknown feature / anchor point: " + baseName);
            int offset1, offset2;
            String[] offsets = string.substring(br + 1, string.length() - 1).split(",");
            try {
                offset1 = Integer.parseInt(offsets[0].trim());
                offset2 = Integer.parseInt(offsets[1].trim());
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Incorrect input: " + string);
            }
            if (base instanceof GeneFeature)
                return new GeneFeature((GeneFeature) base, offset1, offset2);
            else
                return new GeneFeature((ReferencePoint) base, offset1, offset2);
        }
    }
}
Also used : ReferencePoint(io.repseq.core.ReferencePoint) ReferencePoint(io.repseq.core.ReferencePoint)

Example 10 with ReferencePoint

use of io.repseq.core.ReferencePoint in project mixcr by milaboratory.

the class TargetPartitioning method getPosition.

@Override
public int getPosition(ReferencePoint referencePoint) {
    VDJCHit hit = hits.get(referencePoint.getGeneType());
    if (hit == null)
        return -1;
    int position;
    if (referencePoint.isAttachedToAlignmentBound()) {
        int positionInSeq1;
        Alignment<NucleotideSequence> alignment = hit.getAlignment(targetIndex);
        if (alignment == null)
            return -1;
        int positionOfActivationPoint = -2;
        if (referencePoint.getActivationPoint() != null)
            positionOfActivationPoint = hit.getGene().getPartitioning().getRelativePosition(hit.getAlignedFeature(), referencePoint.getActivationPoint());
        if (referencePoint.isAttachedToLeftAlignmentBound()) {
            positionInSeq1 = alignment.getSequence1Range().getFrom();
            if (positionOfActivationPoint != -2 && (positionOfActivationPoint == -1 || positionInSeq1 > positionOfActivationPoint))
                return -1;
        } else {
            positionInSeq1 = alignment.getSequence1Range().getTo();
            if (positionOfActivationPoint != -2 && (positionOfActivationPoint == -1 || positionInSeq1 < positionOfActivationPoint))
                return -1;
        }
        positionInSeq1 += referencePoint.getOffset();
        position = alignment.convertToSeq2Position(positionInSeq1);
    } else
        position = hit.getPosition(targetIndex, referencePoint);
    if (position == -1)
        return -1;
    if (position < 0)
        return -2 - position;
    return position;
}
Also used : NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) ReferencePoint(io.repseq.core.ReferencePoint)

Aggregations

ReferencePoint (io.repseq.core.ReferencePoint)10 AminoAcidSequence (com.milaboratory.core.sequence.AminoAcidSequence)2 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)2 GeneFeature (io.repseq.core.GeneFeature)2 ReferencePoints (io.repseq.core.ReferencePoints)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Range (com.milaboratory.core.Range)1 VDJCAlignmentsReader (com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader)1 AlignmentInfoCollector (com.milaboratory.mixcr.info.AlignmentInfoCollector)1 GeneFeatureCoverageCollector (com.milaboratory.mixcr.info.GeneFeatureCoverageCollector)1 ReferencePointCoverageCollector (com.milaboratory.mixcr.info.ReferencePointCoverageCollector)1 BaseSequence (io.repseq.core.BaseSequence)1 VDJCGene (io.repseq.core.VDJCGene)1 VDJCLibrary (io.repseq.core.VDJCLibrary)1 VDJCLibraryRegistry (io.repseq.core.VDJCLibraryRegistry)1 GGene (io.repseq.gen.GGene)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1