Java Code Examples for htsjdk.samtools.SAMRecord#setCigarString()
The following examples show how to use
htsjdk.samtools.SAMRecord#setCigarString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MergeBamAlignmentTest.java From picard with MIT License | 6 votes |
private void addAlignmentsForBestFragmentMapqStrategy( final SAMFileWriter writer, final SAMRecord unmappedRecord, final String sequence, final int[] mapqs) { boolean reverse = false; int alignmentStart = 1; for (final int mapq : mapqs) { final SAMRecord alignedRecord = new SAMRecord(writer.getFileHeader()); alignedRecord.setReadName(unmappedRecord.getReadName()); alignedRecord.setReadBases(unmappedRecord.getReadBases()); alignedRecord.setBaseQualities(unmappedRecord.getBaseQualities()); alignedRecord.setReferenceName(sequence); alignedRecord.setAlignmentStart(alignmentStart); alignmentStart += 10; // Any old position will do alignedRecord.setReadNegativeStrandFlag(reverse); reverse = !reverse; alignedRecord.setCigarString(unmappedRecord.getReadBases().length + "M"); alignedRecord.setMappingQuality(mapq); alignedRecord.setReadPairedFlag(unmappedRecord.getReadPairedFlag()); alignedRecord.setFirstOfPairFlag(unmappedRecord.getFirstOfPairFlag()); alignedRecord.setSecondOfPairFlag(unmappedRecord.getSecondOfPairFlag()); alignedRecord.setMateUnmappedFlag(true); writer.addAlignment(alignedRecord); } }
Example 2
Source File: PalindromeArtifactClipReadTransformerUnitTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static GATKRead makeRead(final SAMFileHeader header, final String contig, final int readStart, final int fragmentLength, final byte[] bases, final byte[] qual, final String cigar) { final SAMRecord read = new SAMRecord(header); read.setReferenceName(contig); read.setAlignmentStart(readStart); read.setReadPairedFlag(true); read.setReadUnmappedFlag(false); read.setMateUnmappedFlag(false); read.setMateReferenceName("Mate"); read.setReadNegativeStrandFlag(false); read.setMateNegativeStrandFlag(true); read.setReadBases(bases); read.setBaseQualities(qual); final int mateStart = readStart + fragmentLength - bases.length; read.setMateAlignmentStart(mateStart); read.setInferredInsertSize(fragmentLength); read.setProperPairFlag(true); read.setCigarString(cigar); return new SAMRecordToGATKReadAdapter(read); }
Example 3
Source File: ReadContextCounterTest.java From hmftools with GNU General Public License v3.0 | 6 votes |
@NotNull public static SAMRecord buildSamRecord(final int alignmentStart, @NotNull final String cigar, @NotNull final String readString, @NotNull final String qualities) { final SAMRecord record = new SAMRecord(null); record.setAlignmentStart(alignmentStart); record.setCigarString(cigar); record.setReadString(readString); record.setReadNegativeStrandFlag(false); record.setBaseQualityString(qualities); record.setMappingQuality(20); record.setDuplicateReadFlag(false); record.setReadUnmappedFlag(false); record.setProperPairFlag(true); record.setReadPairedFlag(true); return record; }
Example 4
Source File: SAMRecordUtilsTest.java From abra2 with MIT License | 5 votes |
@Test (groups = "unit" ) public void testRemoveSoftClips_withDeletionAndSoftClipAtStart() { SAMRecord read = new SAMRecord(null); read.setReadName("TEST1"); read.setCigarString("2S5M2D3M"); read.setReadString("CCCCCCAGCC"); SAMRecordUtils.removeSoftClips(read); Assert.assertEquals(read.getCigarString(), "5M2D3M"); Assert.assertEquals(read.getReadString(), "CCCCAGCC"); }
Example 5
Source File: SamOutputTest.java From rtg-tools with BSD 2-Clause "Simplified" License | 5 votes |
public void test() throws IOException { final SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); header.getSequenceDictionary().addSequence(new SAMSequenceRecord("seq", 1000000)); final SAMRecord rec = new SAMRecord(header); rec.setAlignmentStart(500000); rec.setReferenceName("seq"); rec.setReadName("read"); rec.setReadString("acgt"); rec.setBaseQualityString("####"); rec.setCigarString("4="); try (TestDirectory dir = new TestDirectory()) { final MemoryPrintStream mps = new MemoryPrintStream(); try (SamOutput out = SamOutput.getSamOutput(new File(dir, "foo"), mps.outputStream(), header, true, true, null)) { out.getWriter().addAlignment(rec); } mNano.check("samoutput_expected_1.sam", MainResult.run(new ExtractCli(), "--header", new File(dir, "foo.bam").getPath(), "seq:500000+1").out()); assertTrue(new File(dir, "foo.bam.bai").exists()); assertEquals("", mps.toString()); try (SamOutput out = SamOutput.getSamOutput(new File(dir, "foo.sam"), mps.outputStream(), header, true, true, null)) { out.getWriter().addAlignment(rec); } mNano.check("samoutput_expected_1.sam", MainResult.run(new ExtractCli(), "--header", new File(dir, "foo.sam.gz").getPath(), "seq:500000+1").out()); assertTrue(new File(dir, "foo.sam.gz.tbi").exists()); assertEquals("", mps.toString()); try (SamOutput out = SamOutput.getSamOutput(new File(dir, "foo.sam"), mps.outputStream(), header, false, true, null)) { out.getWriter().addAlignment(rec); } mNano.check("samoutput_expected_1.sam", FileHelper.fileToString(new File(dir, "foo.sam"))); assertFalse(new File(dir, "foo.sam.tbi").exists()); assertEquals("", mps.toString()); try (SamOutput out = SamOutput.getSamOutput(new File("-"), mps.outputStream(), header, true, true, null)) { out.getWriter().addAlignment(rec); } mNano.check("samoutput_expected_1.sam", mps.toString()); } }
Example 6
Source File: TagReadWithGeneFunctionTest.java From Drop-seq with MIT License | 5 votes |
private SAMRecord getFakeRecord (final File testBAMFile, final int start, final int end, final boolean negativeStrand) { SamReader inputSam = SamReaderFactory.makeDefault().open(testBAMFile); SAMRecord r = inputSam.iterator().next(); r.setAlignmentStart(start); int length = (end -start) +1; r.setCigarString(length+"M"); r.setMappingQuality(255); r.setReadNegativeStrandFlag(negativeStrand); r.setReadBases(Arrays.copyOf(r.getReadBases(), length)); r.setBaseQualities(Arrays.copyOf(r.getBaseQualities(), length)); return (r); }
Example 7
Source File: SAMRecordUtilsTest.java From abra2 with MIT License | 5 votes |
@Test (groups = "unit" ) public void testRemoveSoftClips_withDeletionAndSoftClipAtEnd() { SAMRecord read = new SAMRecord(null); read.setReadName("TEST1"); read.setCigarString("5M2D3M2S"); read.setReadString("CCCCCCAGCC"); SAMRecordUtils.removeSoftClips(read); Assert.assertEquals(read.getCigarString(), "5M2D3M"); Assert.assertEquals(read.getReadString(), "CCCCCCAG"); }
Example 8
Source File: AbstractAlignmentMergerTest.java From picard with MIT License | 5 votes |
@Test(dataProvider = "readPositionIgnoringSoftClips") public void testGetReadPositionIgnoringSoftClips(final String cigarString, final int startPosition, final int queryPosition, final int expectedReadPosititon) { final SAMFileHeader newHeader = SAMRecordSetBuilder.makeDefaultHeader(SAMFileHeader.SortOrder.queryname, 100000,false); final SAMRecord rec = new SAMRecord(newHeader); rec.setCigarString(cigarString); rec.setAlignmentStart(startPosition); final int readPosition = AbstractAlignmentMerger.getReadPositionAtReferencePositionIgnoreSoftClips(rec, queryPosition); Assert.assertEquals(readPosition, expectedReadPosititon); }
Example 9
Source File: ReadContextFactoryTest.java From hmftools with GNU General Public License v3.0 | 5 votes |
@NotNull static SAMRecord buildSamRecord(@NotNull final String cigar, @NotNull final String readString, @NotNull final String qualities) { final SAMRecord record = new SAMRecord(null); record.setAlignmentStart(1000); record.setCigarString(cigar); record.setReadString(readString); record.setReadNegativeStrandFlag(false); record.setBaseQualityString(qualities); record.setMappingQuality(20); record.setDuplicateReadFlag(false); record.setReadUnmappedFlag(false); return record; }
Example 10
Source File: RefSequenceTest.java From hmftools with GNU General Public License v3.0 | 5 votes |
@NotNull public static SAMRecord buildSamRecord(final int alignmentStart, @NotNull final String cigar, @NotNull final String readString, @NotNull final String qualities) { final SAMRecord record = new SAMRecord(null); record.setAlignmentStart(alignmentStart); record.setCigarString(cigar); record.setReadString(readString); record.setReadNegativeStrandFlag(false); record.setBaseQualityString(qualities); record.setMappingQuality(20); record.setDuplicateReadFlag(false); record.setReadUnmappedFlag(false); return record; }
Example 11
Source File: SAMRecordsTest.java From hmftools with GNU General Public License v3.0 | 5 votes |
@NotNull public static SAMRecord buildSamRecord(final int alignmentStart, @NotNull final String cigar, @NotNull final String readString, @NotNull final String qualities) { final SAMRecord record = new SAMRecord(null); record.setAlignmentStart(alignmentStart); record.setCigarString(cigar); record.setReadString(readString); record.setReadNegativeStrandFlag(false); record.setBaseQualityString(qualities); record.setMappingQuality(20); record.setDuplicateReadFlag(false); record.setReadUnmappedFlag(false); return record; }
Example 12
Source File: TagReadWithGeneExonFunctionTest.java From Drop-seq with MIT License | 5 votes |
private SAMRecord getFakeRecord (final File testBAMFile, final int start, final int end, final boolean negativeStrand) { SamReader inputSam = SamReaderFactory.makeDefault().open(testBAMFile); SAMRecord r = inputSam.iterator().next(); r.setAlignmentStart(start); int length = (end -start) +1; r.setCigarString(length+"M"); r.setMappingQuality(255); r.setReadNegativeStrandFlag(negativeStrand); r.setReadBases(Arrays.copyOf(r.getReadBases(), length)); r.setBaseQualities(Arrays.copyOf(r.getBaseQualities(), length)); return (r); }
Example 13
Source File: TagReadWithGeneFunctionTest.java From Drop-seq with MIT License | 5 votes |
private SAMRecord getFakeSplitRecord (final File testBAMFile, final int start1, final int end1, final int start2, final int end2, final boolean negativeStrand) { SamReader inputSam = SamReaderFactory.makeDefault().open(testBAMFile); SAMRecord r = inputSam.iterator().next(); r.setAlignmentStart(start1); int length = (end1 -start1) +1; int gap = ((start2-1)-(end1+1)) +1; int length2 = (end2 -start2) +1; r.setCigarString(length+"M"+gap+"N"+length2+"M"); r.setMappingQuality(255); r.setReadNegativeStrandFlag(negativeStrand); r.setReadBases(Arrays.copyOf(r.getReadBases(), length+length2)); r.setBaseQualities(Arrays.copyOf(r.getBaseQualities(), length+length2)); return (r); }
Example 14
Source File: SAMRecordUtilsTest.java From abra2 with MIT License | 4 votes |
private Cigar getCigar(String str) { SAMRecord read = new SAMRecord(null); read.setCigarString(str); return read.getCigar(); }
Example 15
Source File: AlignmentsTagsTest.java From cramtools with Apache License 2.0 | 4 votes |
private void doTest(byte[] ref, int alignmentStart, byte[] readBases) throws IOException, CloneNotSupportedException { SAMSequenceRecord sequenceRecord = new SAMSequenceRecord("1", ref.length); SAMSequenceDictionary sequenceDictionary = new SAMSequenceDictionary(); sequenceDictionary.addSequence(sequenceRecord); SAMFileHeader header = new SAMFileHeader(); header.setSequenceDictionary(sequenceDictionary); SAMRecord samRecord = new SAMRecord(header); samRecord.setReadUnmappedFlag(false); samRecord.setAlignmentStart(alignmentStart); samRecord.setReferenceIndex(0); samRecord.setReadBases(readBases); samRecord.setCigarString(samRecord.getReadLength() + "M"); ReferenceSource referenceSource = new ReferenceSource() { @Override public synchronized ReferenceRegion getRegion(SAMSequenceRecord record, int start_1based, int endInclusive_1based) throws IOException { int zbInclusiveStart = start_1based - 1; int zbExlcusiveEnd = endInclusive_1based; return new ReferenceRegion(Arrays.copyOfRange(ref, zbInclusiveStart, zbExlcusiveEnd), sequenceRecord.getSequenceIndex(), sequenceRecord.getSequenceName(), start_1based); } }; AlignmentsTags.calculateMdAndNmTags(samRecord, referenceSource, sequenceDictionary, true, true); SAMRecord checkRecord = (SAMRecord) samRecord.clone(); SequenceUtil.calculateMdAndNmTags(checkRecord, ref, true, true); // System.out.printf("TEST: ref %s, start %d, read bases %s\n", new // String(ref), alignmentStart, new String( // readBases)); // System.out // .println(referenceSource.getRegion(sequenceRecord, alignmentStart, // alignmentStart + readBases.length)); // System.out.printf("NM: %s x %s\n", samRecord.getAttribute("NM"), // checkRecord.getAttribute("NM")); // System.out.printf("MD: %s x %s\n", samRecord.getAttribute("MD"), // checkRecord.getAttribute("MD")); Assert.assertEquals(checkRecord.getAttribute("NM"), samRecord.getAttribute("NM")); Assert.assertEquals(checkRecord.getAttribute("MD"), samRecord.getAttribute("MD")); }
Example 16
Source File: TestBAMRecordView.java From cramtools with Apache License 2.0 | 4 votes |
@Test public void test() throws IOException { byte[] buf = new byte[1024]; BAMRecordView view = new BAMRecordView(buf); view.setRefID(0); view.setAlignmentStart(77); view.setMappingScore(44); view.setIndexBin(99); view.setFlags(555); view.setMateRefID(0); view.setMateAlStart(78); view.setInsertSize(133); view.setReadName("name1"); view.setCigar(TextCigarCodec.decode("10M")); view.setBases("AAAAAAAAAA".getBytes()); view.setQualityScores("BBBBBBBBBB".getBytes()); int id = 'A' << 16 | 'M' << 8 | 'A'; view.addTag(id, "Q".getBytes(), 0, 1); int len = view.finish(); System.out.println(Arrays.toString(Arrays.copyOf(buf, len))); ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAMFileHeader header = new SAMFileHeader(); header.addSequence(new SAMSequenceRecord("14", 14)); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); SAMFileWriter writer = new SAMFileWriterFactory().makeBAMWriter(header, true, baos2); SAMRecord record = new SAMRecord(header); record.setReferenceIndex(0); record.setAlignmentStart(1); record.setCigarString("10M"); record.setFlags(555); record.setMappingQuality(44); record.setMateReferenceIndex(0); record.setMateAlignmentStart(0); record.setInferredInsertSize(133); record.setReadName("name1"); record.setReadBases("AAAAAAAAAA".getBytes()); record.setBaseQualities("BBBBBBBBBB".getBytes()); record.setAttribute("AM", 'Q'); System.out.println("BAMFileWriter.addAlignment():"); writer.addAlignment(record); System.out.println("."); writer.close(); System.out.println("------------------------------------------"); System.out.println(); System.out.println(new String(baos2.toByteArray())); System.out.println(); SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); SAMFileReader reader2 = new SAMFileReader(new ByteArrayInputStream(baos2.toByteArray())); SAMRecordIterator iterator = reader2.iterator(); while (iterator.hasNext()) { record = iterator.next(); System.out.println(record.getSAMString()); } System.out.println("------------------------------------------"); BlockCompressedOutputStream bcos = new BlockCompressedOutputStream(baos, null); bcos.write("BAM\1".getBytes()); bcos.write(toByteArray(header)); CramInt.writeInt32(header.getSequenceDictionary().size(), bcos); for (final SAMSequenceRecord sequenceRecord : header.getSequenceDictionary().getSequences()) { byte[] bytes = sequenceRecord.getSequenceName().getBytes(); CramInt.writeInt32(bytes.length + 1, bcos); bcos.write(sequenceRecord.getSequenceName().getBytes()); bcos.write(0); CramInt.writeInt32(sequenceRecord.getSequenceLength(), bcos); } bcos.write(buf, 0, len); bcos.close(); System.out.println(new String(baos.toByteArray())); SAMFileReader reader = new SAMFileReader(new ByteArrayInputStream(baos.toByteArray())); iterator = reader.iterator(); while (iterator.hasNext()) { record = iterator.next(); System.out.println(record.getSAMString()); } reader.close(); }
Example 17
Source File: RevertSam.java From picard with MIT License | 4 votes |
/** * Takes an individual SAMRecord and applies the set of changes/reversions to it that * have been requested by program level options. */ public void revertSamRecord(final SAMRecord rec) { if (RESTORE_ORIGINAL_QUALITIES) { final byte[] oq = rec.getOriginalBaseQualities(); if (oq != null) { rec.setBaseQualities(oq); rec.setOriginalBaseQualities(null); } } if (REMOVE_DUPLICATE_INFORMATION) { rec.setDuplicateReadFlag(false); } if (REMOVE_ALIGNMENT_INFORMATION) { if (rec.getReadNegativeStrandFlag()) { rec.reverseComplement(true); rec.setReadNegativeStrandFlag(false); } // Remove all alignment based information about the read itself rec.setReferenceIndex(SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX); rec.setAlignmentStart(SAMRecord.NO_ALIGNMENT_START); rec.setCigarString(SAMRecord.NO_ALIGNMENT_CIGAR); rec.setMappingQuality(SAMRecord.NO_MAPPING_QUALITY); rec.setInferredInsertSize(0); rec.setNotPrimaryAlignmentFlag(false); rec.setProperPairFlag(false); rec.setReadUnmappedFlag(true); // Then remove any mate flags and info related to alignment rec.setMateAlignmentStart(SAMRecord.NO_ALIGNMENT_START); rec.setMateNegativeStrandFlag(false); rec.setMateReferenceIndex(SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX); rec.setMateUnmappedFlag(rec.getReadPairedFlag()); if (RESTORE_HARDCLIPS) { String hardClippedBases = rec.getStringAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG); String hardClippedQualities = rec.getStringAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG); if (hardClippedBases != null && hardClippedQualities != null) { // Record has already been reverse complemented if this was on the negative strand rec.setReadString(rec.getReadString() + hardClippedBases); rec.setBaseQualities(SAMUtils.fastqToPhred(SAMUtils.phredToFastq(rec.getBaseQualities()) + hardClippedQualities)); // Remove hard clipping storage tags rec.setAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG, null); rec.setAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG, null); } } // And then remove any tags that are calculated from the alignment ATTRIBUTE_TO_CLEAR.forEach(tag -> rec.setAttribute(tag, null)); } }
Example 18
Source File: ReorderSam.java From picard with MIT License | 4 votes |
/** * Helper function that writes reads from iterator it into writer out, updating each SAMRecord along the way * according to the newOrder mapping from dictionary index -> index. Name is used for printing only. */ private void writeReads(final SAMFileWriter out, final SAMRecordIterator it, final Map<Integer, Integer> newOrder, final String name) { long counter = 0; log.info(" Processing " + name); while (it.hasNext()) { counter++; final SAMRecord read = it.next(); final int oldRefIndex = read.getReferenceIndex(); final int oldMateIndex = read.getMateReferenceIndex(); final int newRefIndex = newOrderIndex(read, oldRefIndex, newOrder); read.setHeader(out.getFileHeader()); read.setReferenceIndex(newRefIndex); // read becoming unmapped if (oldRefIndex != NO_ALIGNMENT_REFERENCE_INDEX && newRefIndex == NO_ALIGNMENT_REFERENCE_INDEX) { read.setAlignmentStart(NO_ALIGNMENT_START); read.setReadUnmappedFlag(true); read.setCigarString(SAMRecord.NO_ALIGNMENT_CIGAR); read.setMappingQuality(SAMRecord.NO_MAPPING_QUALITY); } final int newMateIndex = newOrderIndex(read, oldMateIndex, newOrder); if (oldMateIndex != NO_ALIGNMENT_REFERENCE_INDEX && newMateIndex == NO_ALIGNMENT_REFERENCE_INDEX) { // mate becoming unmapped read.setMateAlignmentStart(NO_ALIGNMENT_START); read.setMateUnmappedFlag(true); read.setAttribute(SAMTag.MC.name(), null); // Set the Mate Cigar String to null } read.setMateReferenceIndex(newMateIndex); out.addAlignment(read); } it.close(); log.info("Wrote " + counter + " reads"); }
Example 19
Source File: MergeBamAlignmentTest.java From picard with MIT License | 4 votes |
/** * Confirm that paired reads are rejected by PrimaryAlignmentStrategy.EarliestFragment. */ @Test(expectedExceptions = UnsupportedOperationException.class) public void testEarliestFragmentStrategyPaired() throws Exception { final File output = File.createTempFile("mergeTest", ".sam"); output.deleteOnExit(); final File unmappedSam = File.createTempFile("unmapped.", ".sam"); unmappedSam.deleteOnExit(); final SAMFileWriterFactory factory = new SAMFileWriterFactory(); final SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.queryname); final String cigar = "16M"; final SAMRecord firstOfPair = new SAMRecord(header); firstOfPair.setReadName("theRead"); firstOfPair.setReadString("ACGTACGTACGTACGT"); firstOfPair.setBaseQualityString("5555555555555555"); firstOfPair.setReadUnmappedFlag(true); firstOfPair.setReadPairedFlag(true); firstOfPair.setFirstOfPairFlag(true); final SAMRecord secondOfPair = new SAMRecord(header); secondOfPair.setReadName("theRead"); secondOfPair.setReadString("ACGTACGTACGTACGT"); secondOfPair.setBaseQualityString("5555555555555555"); secondOfPair.setReadUnmappedFlag(true); secondOfPair.setReadPairedFlag(true); secondOfPair.setSecondOfPairFlag(true); SamPairUtil.setMateInfo(firstOfPair, secondOfPair); final SAMFileWriter unmappedWriter = factory.makeSAMWriter(header, false, unmappedSam); unmappedWriter.addAlignment(firstOfPair); unmappedWriter.addAlignment(secondOfPair); unmappedWriter.close(); final File alignedSam = File.createTempFile("aligned.", ".sam"); alignedSam.deleteOnExit(); // Populate the header with SAMSequenceRecords header.setSequenceDictionary(SAMSequenceDictionaryExtractor.extractDictionary(sequenceDict2.toPath())); // Create 2 alignments for each end of pair final SAMFileWriter alignedWriter = factory.makeSAMWriter(header, false, alignedSam); for (int i = 1; i <= 2; ++i) { final SAMRecord firstOfPairAligned = new SAMRecord(header); firstOfPairAligned.setReadName(firstOfPair.getReadName()); firstOfPairAligned.setReadBases(firstOfPair.getReadBases()); firstOfPairAligned.setBaseQualities(firstOfPair.getBaseQualities()); firstOfPairAligned.setReferenceName("chr1"); firstOfPairAligned.setAlignmentStart(i); firstOfPairAligned.setCigarString(cigar); firstOfPairAligned.setMappingQuality(100); firstOfPairAligned.setReadPairedFlag(true); firstOfPairAligned.setFirstOfPairFlag(true); firstOfPairAligned.setAttribute(SAMTag.HI.name(), i); final SAMRecord secondOfPairAligned = new SAMRecord(header); secondOfPairAligned.setReadName(secondOfPair.getReadName()); secondOfPairAligned.setReadBases(secondOfPair.getReadBases()); secondOfPairAligned.setBaseQualities(secondOfPair.getBaseQualities()); secondOfPairAligned.setReferenceName("chr1"); secondOfPairAligned.setAlignmentStart(i + 10); secondOfPairAligned.setCigarString(cigar); secondOfPairAligned.setMappingQuality(100); secondOfPairAligned.setReadPairedFlag(true); secondOfPairAligned.setSecondOfPairFlag(true); secondOfPairAligned.setAttribute(SAMTag.HI.name(), i); SamPairUtil.setMateInfo(firstOfPairAligned, secondOfPairAligned); alignedWriter.addAlignment(firstOfPairAligned); alignedWriter.addAlignment(secondOfPairAligned); } alignedWriter.close(); doMergeAlignment(unmappedSam, Collections.singletonList(alignedSam), null, null, null, null, false, true, false, 1, "0", "1.0", "align!", "myAligner", true, fasta, output, SamPairUtil.PairOrientation.FR, MergeBamAlignment.PrimaryAlignmentStrategy.EarliestFragment, null, null, null, null); Assert.fail("Exception was not thrown"); }
Example 20
Source File: MergeBamAlignmentTest.java From picard with MIT License | 4 votes |
/** * @return a 2-element array in which the first element is the unmapped SAM, and the second the mapped SAM. */ private File[] createSamFilesToBeMerged(final MultipleAlignmentSpec[] specs) { try { final File unmappedSam = File.createTempFile("unmapped.", ".sam"); unmappedSam.deleteOnExit(); final SAMFileWriterFactory factory = new SAMFileWriterFactory(); final SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.queryname); final SAMRecord unmappedRecord = new SAMRecord(header); unmappedRecord.setReadName("theRead"); unmappedRecord.setReadString("ACGTACGTACGTACGT"); unmappedRecord.setBaseQualityString("5555555555555555"); unmappedRecord.setReadUnmappedFlag(true); final SAMFileWriter unmappedWriter = factory.makeSAMWriter(header, false, unmappedSam); unmappedWriter.addAlignment(unmappedRecord); unmappedWriter.close(); final File alignedSam = File.createTempFile("aligned.", ".sam"); alignedSam.deleteOnExit(); final String sequence = "chr1"; // Populate the header with SAMSequenceRecords header.setSequenceDictionary(SAMSequenceDictionaryExtractor.extractDictionary(sequenceDict2.toPath())); final SAMFileWriter alignedWriter = factory.makeSAMWriter(header, false, alignedSam); for (final MultipleAlignmentSpec spec : specs) { final SAMRecord alignedRecord = new SAMRecord(header); alignedRecord.setReadName(unmappedRecord.getReadName()); alignedRecord.setReadBases(unmappedRecord.getReadBases()); alignedRecord.setBaseQualities(unmappedRecord.getBaseQualities()); alignedRecord.setReferenceName(sequence); alignedRecord.setAlignmentStart(1); alignedRecord.setReadNegativeStrandFlag(spec.reverseStrand); alignedRecord.setCigarString(spec.cigar); alignedRecord.setMappingQuality(spec.mapQ); if (spec.oneOfTheBest) { alignedRecord.setAttribute(ONE_OF_THE_BEST_TAG, 1); } alignedWriter.addAlignment(alignedRecord); } alignedWriter.close(); return new File[]{unmappedSam, alignedSam}; } catch (IOException e) { throw new PicardException(e.getMessage(), e); } }