use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.
the class CloneAssemblerRunnerTest method runFullPipeline.
private static CloneSet runFullPipeline(String... fastqFiles) throws IOException, InterruptedException {
// building alignments
VDJCAlignerParameters alignerParameters = VDJCParametersPresets.getByName("default");
VDJCAligner aligner = fastqFiles.length == 1 ? new VDJCAlignerS(alignerParameters) : new VDJCAlignerWithMerge(alignerParameters);
for (VDJCGene gene : VDJCLibraryRegistry.getDefault().getLibrary("mi", "hs").getGenes(Chains.IGH)) if (alignerParameters.containsRequiredFeature(gene))
aligner.addGene(gene);
SequenceReader reader;
if (fastqFiles.length == 1)
reader = new SingleFastqReader(CloneAssemblerRunnerTest.class.getClassLoader().getResourceAsStream(fastqFiles[0]), true);
else
reader = new PairedFastqReader(CloneAssemblerRunnerTest.class.getClassLoader().getResourceAsStream(fastqFiles[0]), CloneAssemblerRunnerTest.class.getClassLoader().getResourceAsStream(fastqFiles[1]), true);
// write alignments to byte array
ByteArrayOutputStream alignmentsSerialized = new ByteArrayOutputStream();
try (VDJCAlignmentsWriter writer = new VDJCAlignmentsWriter(alignmentsSerialized)) {
writer.header(aligner);
for (Object read : CUtils.it(reader)) {
VDJCAlignmentResult result = (VDJCAlignmentResult) aligner.process((SequenceRead) read);
if (result.alignment != null)
writer.write(result.alignment);
}
}
AlignmentsProvider alignmentsProvider = AlignmentsProvider.Util.createProvider(alignmentsSerialized.toByteArray(), VDJCLibraryRegistry.getDefault());
LinearGapAlignmentScoring<NucleotideSequence> scoring = new LinearGapAlignmentScoring<>(NucleotideSequence.ALPHABET, 5, -9, -12);
CloneFactoryParameters factoryParameters = new CloneFactoryParameters(new VJCClonalAlignerParameters(0.8f, scoring, 5), new VJCClonalAlignerParameters(0.8f, scoring, 5), null, new DAlignerParameters(GeneFeature.DRegion, 0.85f, 30.0f, 3, scoring));
CloneAssemblerParameters assemblerParameters = new CloneAssemblerParameters(new GeneFeature[] { GeneFeature.CDR3 }, 12, QualityAggregationType.Average, new CloneClusteringParameters(2, 1, TreeSearchParameters.ONE_MISMATCH, new RelativeConcentrationFilter(1.0E-6)), factoryParameters, true, true, false, 0.4, true, (byte) 20, .8, "2 of 6", (byte) 15);
System.out.println(GlobalObjectMappers.toOneLine(assemblerParameters));
CloneAssemblerRunner assemblerRunner = new CloneAssemblerRunner(alignmentsProvider, new CloneAssembler(assemblerParameters, true, aligner.getUsedGenes(), alignerParameters), 2);
SmartProgressReporter.startProgressReport(assemblerRunner);
assemblerRunner.run();
CloneSet cloneSet = assemblerRunner.getCloneSet(null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CloneSetIO.write(cloneSet, bos);
CloneSet cloneSetDeserialized = CloneSetIO.readClns(new ByteArrayInputStream(bos.toByteArray()));
assertCSEquals(cloneSet, cloneSetDeserialized);
OutputPortCloseable<ReadToCloneMapping> rrr = assemblerRunner.assembler.getAssembledReadsPort();
ReadToCloneMapping take;
while ((take = rrr.take()) != null) System.out.println(take);
return cloneSet;
}
use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.
the class ClonalSequenceTest method testIsCompatible2.
@Test
public void testIsCompatible2() throws Exception {
RandomGenerator generator = new Well1024a();
Mutations<NucleotideSequence> muts = Mutations.decode("", NucleotideSequence.ALPHABET);
for (int i = 0; i < 100; i++) {
ClonalSequence c1 = createRandom(5 + generator.nextInt(10), generator);
Assert.assertTrue(c1.isCompatible(c1, muts));
}
}
use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.
the class ClonalSequenceTest method createRandomTestDara.
private TestData createRandomTestDara(int size, RandomGenerator random) {
MutationsBuilder<NucleotideSequence> builder = new MutationsBuilder<>(NucleotideSequence.ALPHABET);
NSequenceWithQuality[] c1 = new NSequenceWithQuality[size];
NSequenceWithQuality[] c2 = new NSequenceWithQuality[size];
int offset = 0;
for (int i = 0; i < size; ++i) {
NucleotideSequence s1 = TestUtil.randomSequence(NucleotideSequence.ALPHABET, random, 5, 15);
Mutations<NucleotideSequence> muts = MutationsGenerator.generateMutations(s1, MutationModels.getEmpiricalNucleotideMutationModel(), 1, s1.size() - 1);
NucleotideSequence s2 = muts.mutate(s1);
c1[i] = new NSequenceWithQuality(s1, SequenceQuality.getUniformQuality((byte) 0, s1.size()));
c2[i] = new NSequenceWithQuality(s2, SequenceQuality.getUniformQuality((byte) 0, s2.size()));
builder.append(muts.move(offset));
offset += s1.size();
}
return new TestData(new ClonalSequence(c1), new ClonalSequence(c2), builder.createAndDestroy());
}
use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.
the class ClonalSequenceTest method create.
private ClonalSequence create(String... strings) {
NSequenceWithQuality[] data = new NSequenceWithQuality[strings.length];
for (int i = 0; i < strings.length; ++i) {
NucleotideSequence s = new NucleotideSequence(strings[i]);
SequenceQuality q = SequenceQuality.getUniformQuality((byte) 0, s.size());
data[i] = new NSequenceWithQuality(s, q);
}
return new ClonalSequence(data);
}
use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.
the class TargetMerger method merge.
@SuppressWarnings("unchecked")
public AlignedTarget merge(AlignedTarget targetLeft, AlignedTarget targetRight, int offset, OverlapType overlapType, int nMismatches) {
if (offset < 0)
return merge(targetRight, targetLeft, -offset, overlapType, nMismatches);
final NSequenceWithQuality mergedTarget = merger.overlap(targetLeft.getTarget(), targetRight.getTarget(), offset);
EnumMap<GeneType, VDJCHit[]> result = new EnumMap<>(GeneType.class);
for (GeneType geneType : GeneType.VJC_REFERENCE) {
final BatchAlignerWithBaseParameters bp = ((KGeneAlignmentParameters) alignerParameters.getGeneAlignerParameters(geneType)).getParameters();
final VDJCHit[] leftHits = targetLeft.getAlignments().getHits(geneType);
final VDJCHit[] rightHits = targetRight.getAlignments().getHits(geneType);
GeneFeature alignedFeature = leftHits.length == 0 ? rightHits.length == 0 ? null : rightHits[0].getAlignedFeature() : leftHits[0].getAlignedFeature();
Map<VDJCGeneId, HitMappingRecord> map = extractHitsMapping(targetLeft, targetRight, geneType);
ArrayList<VDJCHit> resultingHits = new ArrayList<>();
for (Map.Entry<VDJCGeneId, HitMappingRecord> mE : map.entrySet()) {
final VDJCGene gene = mE.getValue().gene;
Alignment<NucleotideSequence> mergedAl = merge(bp.getScoring(), extractBandedWidth(bp), mergedTarget.getSequence(), offset, mE.getValue().alignments[0], mE.getValue().alignments[1]);
resultingHits.add(new VDJCHit(gene, mergedAl, alignedFeature));
}
Collections.sort(resultingHits);
// final float relativeMinScore = extractRelativeMinScore(bp);
// int threshold = (int) (resultingHits.size() > 0 ? resultingHits.get(0).getScore() * relativeMinScore : 0);
// for (int i = resultingHits.size() - 1; i > 0; --i)
// if (resultingHits.get(i).getScore() < threshold)
// resultingHits.remove(i);
result.put(geneType, resultingHits.toArray(new VDJCHit[resultingHits.size()]));
}
VDJCAlignments alignments = new VDJCAlignments(result, new NSequenceWithQuality[] { mergedTarget }, new SequenceHistory[] { new SequenceHistory.Merge(overlapType, targetLeft.getHistory(), targetRight.getHistory(), offset, nMismatches) }, VDJCAlignments.mergeOriginalReads(targetLeft.getAlignments(), targetRight.getAlignments()));
AlignedTarget resultTarget = new AlignedTarget(alignments, 0);
for (BPoint bPoint : BPoint.values()) {
int leftPoint = targetLeft.getBPoint(bPoint);
int rightPoint = targetRight.getBPoint(bPoint);
if (leftPoint != -1 && rightPoint != -1)
throw new IllegalArgumentException("Same bPoint defined in both input targets.");
else if (leftPoint != -1)
resultTarget = resultTarget.setBPoint(bPoint, leftPoint);
else if (rightPoint != -1)
resultTarget = resultTarget.setBPoint(bPoint, offset + rightPoint);
}
return resultTarget;
}
Aggregations