Search in sources :

Example 81 with NucleotideSequence

use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.

the class TargetBuilder method generateSequence.

public static NucleotideSequence generateSequence(VDJCGenes genes, String model, RandomGenerator rg) {
    // Pre-processing
    model = preProcessModel(model);
    // Building sequence
    SequenceBuilder<NucleotideSequence> builder = NucleotideSequence.ALPHABET.createBuilder();
    // builder.ensureCapacity(model.length());
    Matcher matcher = modelParserPattern.matcher(model);
    ReferencePoint leftPoint = null;
    ReferencePoint rightPoint;
    int prevPosition = 0;
    while (matcher.find()) {
        if (matcher.start() != prevPosition)
            throw new IllegalArgumentException("Can't parse " + model.substring(prevPosition, matcher.start()));
        prevPosition = matcher.end();
        String rpGroup = matcher.group(refPointGroupId);
        String gf1Group = matcher.group(geneFeaturePoint1GroupId);
        String gf2Group = matcher.group(geneFeaturePoint2GroupId);
        String nGroup = matcher.group(nGroupId);
        String atgcGroup = matcher.group(atgcGroupId);
        if (rpGroup != null)
            leftPoint = ReferencePoint.parse(rpGroup);
        else if (gf1Group != null) {
            leftPoint = ReferencePoint.parse(gf1Group);
            rightPoint = ReferencePoint.parse(gf2Group);
            GeneFeature geneFeature = new GeneFeature(leftPoint, rightPoint);
            VDJCGene gene = genes.geneByGeneType(geneFeature.getGeneType());
            NucleotideSequence seqGF = gene.getFeature(geneFeature);
            if (seqGF == null)
                throw new RuntimeException("No sequence for feature " + geneFeature);
            builder.append(seqGF);
            leftPoint = rightPoint;
        } else if (nGroup != null)
            for (int j = 0; j < nGroup.length(); j++) builder.append((byte) rg.nextInt(4));
        else if (atgcGroup != null)
            builder.append(new NucleotideSequence(atgcGroup));
        else {
            for (int i = 0; i < 4; i++) {
                GeneType geneType = GeneType.VDJC_REFERENCE[i];
                String group = matcher.group(i + vGroupId);
                if (group != null) {
                    if (leftPoint == null || leftPoint.getGeneType() != geneType)
                        throw new IllegalArgumentException("No reference point for " + group);
                    rightPoint = leftPoint.move(group.length());
                    GeneFeature geneFeature = new GeneFeature(leftPoint, rightPoint);
                    VDJCGene gene = genes.geneByGeneType(geneType);
                    NucleotideSequence seqGF = gene.getFeature(geneFeature);
                    if (seqGF == null)
                        throw new RuntimeException("No sequence for feature " + geneFeature);
                    for (int j = 0; j < seqGF.size(); j++) {
                        char ch = group.charAt(j);
                        byte n = seqGF.codeAt(j);
                        if (Character.isLowerCase(ch))
                            builder.append((byte) ((n + 1 + rg.nextInt(3)) % 3));
                        else
                            builder.append(n);
                    }
                }
            }
        }
    }
    if (model.length() != prevPosition)
        throw new IllegalArgumentException("Can't parse " + model.substring(prevPosition, model.length()));
    return builder.createAndDestroy();
}
Also used : Matcher(java.util.regex.Matcher) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence)

Aggregations

NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)81 Test (org.junit.Test)32 Range (com.milaboratory.core.Range)19 VDJCGene (io.repseq.core.VDJCGene)15 GeneType (io.repseq.core.GeneType)14 NSequenceWithQuality (com.milaboratory.core.sequence.NSequenceWithQuality)13 GeneFeature (io.repseq.core.GeneFeature)9 VDJCAlignments (com.milaboratory.mixcr.basictypes.VDJCAlignments)8 VDJCHit (com.milaboratory.mixcr.basictypes.VDJCHit)8 ReferencePoint (io.repseq.core.ReferencePoint)7 VDJCLibrary (io.repseq.core.VDJCLibrary)7 ArrayList (java.util.ArrayList)7 VDJCAlignerParameters (com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters)6 AlignmentHit (com.milaboratory.core.alignment.batch.AlignmentHit)5 PairedRead (com.milaboratory.core.io.sequence.PairedRead)5 AminoAcidSequence (com.milaboratory.core.sequence.AminoAcidSequence)5 Path (java.nio.file.Path)5 Alignment (com.milaboratory.core.alignment.Alignment)4 Pattern (java.util.regex.Pattern)4 AlignmentHelper (com.milaboratory.core.alignment.AlignmentHelper)3