Search in sources :

Example 16 with GeneType

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

the class VDJCAlignmentsFormatter method getTargetAsMultiAlignment.

public static MultiAlignmentHelper getTargetAsMultiAlignment(VDJCObject vdjcObject, int targetId, boolean addHitScore, boolean addReads) {
    if (addReads && !(vdjcObject instanceof VDJCAlignments))
        throw new IllegalArgumentException("Read alignments supported only for VDJCAlignments.");
    NSequenceWithQuality target = vdjcObject.getTarget(targetId);
    NucleotideSequence targetSeq = target.getSequence();
    SequencePartitioning partitioning = vdjcObject.getPartitionedTarget(targetId).getPartitioning();
    List<Alignment<NucleotideSequence>> alignments = new ArrayList<>();
    List<String> alignmentLeftComments = new ArrayList<>();
    List<String> alignmentRightComments = new ArrayList<>();
    for (GeneType gt : GeneType.values()) for (VDJCHit hit : vdjcObject.getHits(gt)) {
        Alignment<NucleotideSequence> alignment = hit.getAlignment(targetId);
        if (alignment == null)
            continue;
        alignment = alignment.invert(targetSeq);
        alignments.add(alignment);
        alignmentLeftComments.add(hit.getGene().getName());
        alignmentRightComments.add(" " + (int) (hit.getAlignment(targetId).getScore()) + (addHitScore ? " (" + (int) (hit.getScore()) + ")" : ""));
    }
    // Adding read information
    if (addReads) {
        VDJCAlignments vdjcAlignments = (VDJCAlignments) vdjcObject;
        SequenceHistory history = vdjcAlignments.getHistory(targetId);
        List<SequenceHistory.RawSequence> reads = history.rawReads();
        for (SequenceHistory.RawSequence read : reads) {
            NucleotideSequence seq = vdjcAlignments.getOriginalSequence(read.index).getSequence();
            int offset = history.offset(read.index);
            Alignment<NucleotideSequence> alignment = Aligner.alignOnlySubstitutions(targetSeq, seq, offset, seq.size(), 0, seq.size(), AffineGapAlignmentScoring.IGBLAST_NUCLEOTIDE_SCORING);
            alignments.add(alignment);
            alignmentLeftComments.add(read.index.toString());
            alignmentRightComments.add("");
        }
    }
    MultiAlignmentHelper helper = MultiAlignmentHelper.build(MultiAlignmentHelper.DEFAULT_SETTINGS, new Range(0, target.size()), targetSeq, alignments.toArray(new Alignment[alignments.size()]));
    if (!alignments.isEmpty())
        drawPoints(helper, partitioning, POINTS_FOR_REARRANGED);
    drawAASequence(helper, partitioning, targetSeq);
    helper.addSubjectQuality("Quality", target.getQuality());
    helper.setSubjectLeftTitle("Target" + targetId);
    helper.setSubjectRightTitle(" Score" + (addHitScore ? " (hit score)" : ""));
    for (int i = 0; i < alignmentLeftComments.size(); i++) {
        helper.setQueryLeftTitle(i, alignmentLeftComments.get(i));
        helper.setQueryRightTitle(i, alignmentRightComments.get(i));
    }
    return helper;
}
Also used : MultiAlignmentHelper(com.milaboratory.core.alignment.MultiAlignmentHelper) ArrayList(java.util.ArrayList) SequencePartitioning(io.repseq.core.SequencePartitioning) Range(com.milaboratory.core.Range) ReferencePoint(io.repseq.core.ReferencePoint) Alignment(com.milaboratory.core.alignment.Alignment) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) GeneType(io.repseq.core.GeneType)

Example 17 with GeneType

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

the class VDJCAlignmentsWriter method header.

@Override
public void header(VDJCAlignerParameters parameters, List<VDJCGene> genes) {
    if (parameters == null || genes == null)
        throw new IllegalArgumentException();
    if (header)
        throw new IllegalStateException();
    // Writing magic bytes
    assert MAGIC_BYTES.length == MAGIC_LENGTH;
    output.write(MAGIC_BYTES);
    // Writing version information
    output.writeUTF(MiXCRVersionInfo.get().getVersionString(MiXCRVersionInfo.OutputType.ToFile));
    // Writing parameters
    output.writeObject(parameters);
    IOUtil.writeAndRegisterGeneReferences(output, genes, parameters);
    // Registering links to features to align
    for (GeneType gt : GeneType.VDJC_REFERENCE) {
        GeneFeature feature = parameters.getFeatureToAlign(gt);
        output.writeObject(feature);
        if (feature != null)
            output.putKnownReference(feature);
    }
    header = true;
}
Also used : GeneFeature(io.repseq.core.GeneFeature) GeneType(io.repseq.core.GeneType)

Example 18 with GeneType

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

the class VDJCAlignmentsReader method init.

void init(Map<GeneFeature, GeneFeature> geneFeatureRefs) {
    if (usedGenes != null)
        return;
    assert MAGIC_BYTES.length == MAGIC_LENGTH;
    byte[] magic = new byte[MAGIC_LENGTH];
    input.readFully(magic);
    String magicString = new String(magic);
    this.magic = magicString;
    SerializersManager serializersManager = input.getSerializersManager();
    switch(magicString) {
        case MAGIC_V9:
            serializersManager.registerCustomSerializer(VDJCAlignments.class, new IO.VDJCAlignmentsSerializer21());
        case MAGIC:
            break;
        default:
            throw new RuntimeException("Unsupported file format; .vdjca file of version " + new String(magic) + " while you are running MiXCR " + MAGIC);
    }
    versionInfo = input.readUTF();
    parameters = input.readObject(VDJCAlignerParameters.class);
    this.usedGenes = IOUtil.readAndRegisterGeneReferences(input, vdjcRegistry, parameters);
    // Registering links to features to align
    for (GeneType gt : GeneType.VDJC_REFERENCE) {
        GeneFeature featureParams = parameters.getFeatureToAlign(gt);
        GeneFeature featureDeserialized = input.readObject(GeneFeature.class);
        if (!Objects.equals(featureDeserialized, featureParams))
            throw new RuntimeException("Wrong format.");
        // Find corresponding reference
        if (geneFeatureRefs != null) {
            featureParams = geneFeatureRefs.get(featureParams);
            if (featureParams == null)
                throw new RuntimeException("Absent record for " + featureDeserialized + " in geneFeatureRefs map.");
        }
        if (featureDeserialized != null)
            input.putKnownReference(featureParams);
    }
}
Also used : GeneFeature(io.repseq.core.GeneFeature) VDJCAlignerParameters(com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters) SerializersManager(com.milaboratory.primitivio.SerializersManager) GeneType(io.repseq.core.GeneType)

Example 19 with GeneType

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

the class ActionAlignmentsDiff method go.

@Override
public void go(ActionHelper actionHelper) throws Exception {
    try (VDJCAlignmentsReader reader1 = new VDJCAlignmentsReader(parameters.get1());
        VDJCAlignmentsReader reader2 = new VDJCAlignmentsReader(parameters.get2());
        VDJCAlignmentsWriterI only1 = parameters.onlyFirst == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.onlyFirst);
        VDJCAlignmentsWriterI only2 = parameters.onlySecond == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.onlySecond);
        VDJCAlignmentsWriterI diff1 = parameters.diff1 == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.diff1);
        VDJCAlignmentsWriterI diff2 = parameters.diff1 == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.diff2);
        PrintStream report = parameters.report().equals(".") ? System.out : new PrintStream(new FileOutputStream(parameters.report()))) {
        if (reader1.getNumberOfReads() > reader2.getNumberOfReads())
            SmartProgressReporter.startProgressReport("Analyzing diff", reader1);
        else
            SmartProgressReporter.startProgressReport("Analyzing diff", reader2);
        long same = 0, onlyIn1 = 0, onlyIn2 = 0, diffFeature = 0, justDiff = 0;
        long[] diffHits = new long[GeneType.NUMBER_OF_TYPES];
        only1.header(reader1.getParameters(), reader1.getUsedGenes());
        diff1.header(reader1.getParameters(), reader1.getUsedGenes());
        only2.header(reader2.getParameters(), reader2.getUsedGenes());
        diff2.header(reader2.getParameters(), reader2.getUsedGenes());
        VDJCAlignmentsDifferenceReader diffReader = new VDJCAlignmentsDifferenceReader(reader1, reader2, parameters.getFeature(), parameters.hitsCompareLevel);
        for (VDJCAlignmentsDifferenceReader.Diff diff : CUtils.it(diffReader)) {
            switch(diff.status) {
                case AlignmentsAreSame:
                    ++same;
                    break;
                case AlignmentPresentOnlyInFirst:
                    ++onlyIn1;
                    only1.write(diff.first);
                    break;
                case AlignmentPresentOnlyInSecond:
                    ++onlyIn2;
                    only2.write(diff.second);
                    break;
                case AlignmentsAreDifferent:
                    ++justDiff;
                    diff1.write(diff.first);
                    diff2.write(diff.second);
                    if (diff.reason.diffGeneFeature)
                        ++diffFeature;
                    for (Map.Entry<GeneType, Boolean> e : diff.reason.diffHits.entrySet()) if (e.getValue())
                        ++diffHits[e.getKey().ordinal()];
            }
        }
        only1.setNumberOfProcessedReads(onlyIn1);
        only2.setNumberOfProcessedReads(onlyIn2);
        diff1.setNumberOfProcessedReads(justDiff);
        diff2.setNumberOfProcessedReads(justDiff);
        report.println("First  file: " + parameters.get1());
        report.println("Second file: " + parameters.get2());
        report.println("Completely same reads: " + same);
        report.println("Aligned reads present only in the FIRST  file: " + onlyIn1 + " (" + Util.PERCENT_FORMAT.format(100. * onlyIn1 / reader1.getNumberOfReads()) + ")%");
        report.println("Aligned reads present only in the SECOND file: " + onlyIn2 + " (" + Util.PERCENT_FORMAT.format(100. * onlyIn2 / reader2.getNumberOfReads()) + ")%");
        report.println("Total number of different reads: " + justDiff);
        report.println("Reads with not same " + parameters.geneFeatureToMatch + ": " + diffFeature);
        for (GeneType geneType : GeneType.VDJC_REFERENCE) report.println("Reads with not same " + geneType.name() + " hits: " + diffHits[geneType.ordinal()]);
    }
}
Also used : PrintStream(java.io.PrintStream) VDJCAlignmentsReader(com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader) VDJCAlignmentsWriterI(com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriterI) VDJCAlignmentsWriter(com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriter) VDJCAlignmentsDifferenceReader(com.milaboratory.mixcr.util.VDJCAlignmentsDifferenceReader) FileOutputStream(java.io.FileOutputStream) GeneType(io.repseq.core.GeneType) Map(java.util.Map)

Example 20 with GeneType

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

the class IO method readGF2GTMap.

public static EnumMap<GeneType, GeneFeature> readGF2GTMap(PrimitivI input) {
    int count = input.readInt();
    EnumMap<GeneType, GeneFeature> map = new EnumMap<GeneType, GeneFeature>(GeneType.class);
    for (int i = 0; i < count; i++) {
        GeneType gt = input.readObject(GeneType.class);
        GeneFeature gf = input.readObject(GeneFeature.class);
        map.put(gt, gf);
    }
    return map;
}
Also used : GeneFeature(io.repseq.core.GeneFeature) GeneType(io.repseq.core.GeneType) EnumMap(java.util.EnumMap)

Aggregations

GeneType (io.repseq.core.GeneType)25 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)14 GeneFeature (io.repseq.core.GeneFeature)8 VDJCHit (com.milaboratory.mixcr.basictypes.VDJCHit)7 VDJCGene (io.repseq.core.VDJCGene)5 PairedRead (com.milaboratory.core.io.sequence.PairedRead)4 NSequenceWithQuality (com.milaboratory.core.sequence.NSequenceWithQuality)4 EnumMap (java.util.EnumMap)4 Test (org.junit.Test)4 Alignment (com.milaboratory.core.alignment.Alignment)3 VDJCAlignments (com.milaboratory.mixcr.basictypes.VDJCAlignments)3 Chains (io.repseq.core.Chains)3 ReferencePoint (io.repseq.core.ReferencePoint)3 VDJCGeneId (io.repseq.core.VDJCGeneId)3 ArrayList (java.util.ArrayList)3 MultiAlignmentHelper (com.milaboratory.core.alignment.MultiAlignmentHelper)2 AlignmentHit (com.milaboratory.core.alignment.batch.AlignmentHit)2 VDJCAlignerParameters (com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2