use of org.broadinstitute.hellbender.utils.bwa.BwaMemAlignment in project gatk by broadinstitute.
the class ContigAligner method alignContigs.
/**
* Takes a collection of assembled contigs and aligns them to the reference with bwa-mem. Non-canonical
* (secondary) alignments are filtered out, preserving the primary and supplementary alignments.
* Within the output list, alignments are sorted first by contig (based on the order in which
* the contigs were passed in, and then by their start position on the contig).
* @param assemblyId An identifier for the assembly or set of contigs
* @param contigsCollection The set of all canonical (primary or supplementary) alignments for the contigs.*/
AlignedAssembly alignContigs(final int assemblyId, final ContigsCollection contigsCollection) {
final List<AlignedContig> alignedContigs = new ArrayList<>(contigsCollection.getContents().size());
final BwaMemIndex index = BwaMemIndexSingleton.getInstance(indexImageFile);
try (final BwaMemAligner aligner = new BwaMemAligner(index)) {
final List<String> refNames = index.getReferenceContigNames();
final List<Tuple2<ContigsCollection.ContigID, ContigsCollection.ContigSequence>> contents = contigsCollection.getContents();
final List<byte[]> seqs = contents.stream().map(contigInfo -> contigInfo._2.toString().getBytes()).collect(Collectors.toList());
final List<List<BwaMemAlignment>> allAlignments = aligner.alignSeqs(seqs);
for (int contigIdx = 0; contigIdx < seqs.size(); ++contigIdx) {
final int contigLen = seqs.get(contigIdx).length;
// filter out secondary alignments, convert to AlignmentInterval objects and sort by alignment start pos
final List<AlignedAssembly.AlignmentInterval> alignmentIntervals = allAlignments.get(contigIdx).stream().filter(a -> (a.getSamFlag() & SAMFlag.NOT_PRIMARY_ALIGNMENT.intValue()) == 0).filter(a -> (a.getSamFlag() & SAMFlag.READ_UNMAPPED.intValue()) == 0).map(a -> new AlignedAssembly.AlignmentInterval(a, refNames, contigLen)).sorted(Comparator.comparing(a -> a.startInAssembledContig)).collect(Collectors.toList());
final String contigName = AlignedAssemblyOrExcuse.formatContigName(assemblyId, Integer.valueOf(contents.get(contigIdx)._1.toString().split(" ")[0].replace("contig", "").replace("-", "").replace(">", "")));
alignedContigs.add(new AlignedContig(contigName, seqs.get(contigIdx), alignmentIntervals));
}
}
return new AlignedAssembly(assemblyId, alignedContigs);
}
use of org.broadinstitute.hellbender.utils.bwa.BwaMemAlignment in project gatk by broadinstitute.
the class BwaMemTestUtils method assertCorrectChimericContigAlignment.
public static void assertCorrectChimericContigAlignment(final BwaMemIndex index) {
try (final BwaMemAligner aligner = new BwaMemAligner(index)) {
// synthetic contig generated by extracting 300 bases from two different positions on chr20 in the reference
final byte[] seq = "GTGGGAGAGAACTGGAACAAGAACCCAGTGCTCTTTCTGCTCTACCCACTGACCCATCCTCTCACGCATCATACACCCATACTCCCATCCACCCACCTTCCCATTCATGCATTCACCCATTCACCCACCTTCCATCCATCTACCATCCACCACGTACCTACACTCCCATCTACCATCCAACCACATTTCCATTCACCCATCCTCCCATCCATCAACCCTCCAATCCACCACCCACAGACCTTCCCATCCATTCATTTACCCATCCACATATTCACCCACCCTCCCATCCATCCATCTACTTGAAGCAGAATGAGGGAGGAAACCCAAGCCAGCTCGGGCTTGATCATGGCAGGCTTTGTCAGTCACTGTGAGTAACCAGACTTTATTTCAAGTGAGTTGGCCAACAGTGTCTCCCCAGTAGTAGAGTGATTGCTCGTCTGCCAGAAAAGGGGCACAGAGCTCTGGACATCAATACTTGCCGATCTCTCCTTCATCAGCCACCCAACCCTGGCAACAGTTTTCAATTACACCTGTGAAAACTTGTGGGTTAAACAGCTGAGATCCATGCCTCAGCTTCTATAGGTGATAAGCCTGTCCTTC".getBytes();
final List<List<BwaMemAlignment>> allAlignments = aligner.alignSeqs(Collections.singletonList(seq));
Assert.assertEquals(allAlignments.size(), 1);
final List<BwaMemAlignment> alignments = allAlignments.get(0);
Assert.assertEquals(alignments.size(), 2);
final BwaMemAlignment alignment1;
final BwaMemAlignment alignment2;
if (alignments.get(0).getRefStart() < alignments.get(1).getRefStart()) {
alignment1 = alignments.get(0);
alignment2 = alignments.get(1);
} else {
alignment1 = alignments.get(1);
alignment2 = alignments.get(0);
}
Assert.assertEquals(index.getReferenceContigNames().get(alignment1.getRefId()), "20");
Assert.assertEquals(alignment1.getCigar(), "300M300S");
Assert.assertEquals(alignment1.getMapQual(), 60);
Assert.assertEquals(alignment1.getRefStart(), 999999);
Assert.assertEquals(index.getReferenceContigNames().get(alignment2.getRefId()), "20");
Assert.assertEquals(alignment2.getCigar(), "300S300M");
Assert.assertEquals(alignment2.getMapQual(), 60);
Assert.assertEquals(alignment2.getRefStart(), 1000999);
}
}
Aggregations