Java Code Examples for htsjdk.samtools.SAMRecord#setFirstOfPairFlag()
The following examples show how to use
htsjdk.samtools.SAMRecord#setFirstOfPairFlag() .
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: MergeBamAlignmentTest.java From picard with MIT License | 6 votes |
private void addAlignmentForMostStrategy( final SAMFileWriter writer, final SAMRecord unmappedRecord, final MostDistantStrategyAlignmentSpec spec, final boolean reverse) { final SAMRecord alignedRecord = new SAMRecord(writer.getFileHeader()); alignedRecord.setReadName(unmappedRecord.getReadName()); alignedRecord.setReadBases(unmappedRecord.getReadBases()); alignedRecord.setBaseQualities(unmappedRecord.getBaseQualities()); alignedRecord.setReferenceName(spec.sequence); alignedRecord.setAlignmentStart(spec.alignmentStart); alignedRecord.setReadNegativeStrandFlag(reverse); alignedRecord.setCigarString(unmappedRecord.getReadBases().length + "M"); alignedRecord.setMappingQuality(spec.mapQ); alignedRecord.setReadPairedFlag(unmappedRecord.getReadPairedFlag()); alignedRecord.setFirstOfPairFlag(unmappedRecord.getFirstOfPairFlag()); alignedRecord.setSecondOfPairFlag(unmappedRecord.getSecondOfPairFlag()); alignedRecord.setMateUnmappedFlag(true); writer.addAlignment(alignedRecord); }
Example 3
Source File: ClippingUtilityTest.java From picard with MIT License | 6 votes |
@Test(dataProvider="clipPairedTestData") public void testPairedEndClip(final String testName, final String read1, final String read2, final AdapterPair expected) { final SAMRecord rec1 = new SAMRecord(new SAMFileHeader()); rec1.setReadString(read1); rec1.setFirstOfPairFlag(true); final SAMRecord rec2 = new SAMRecord(new SAMFileHeader()); rec2.setReadString(read2); rec2.setSecondOfPairFlag(true); final AdapterPair result = ClippingUtility.adapterTrimIlluminaPairedReads(rec1, rec2, IlluminaAdapterPair.INDEXED, IlluminaAdapterPair.PAIRED_END); if (result != null) { Assert.assertEquals(result.getName(), expected.getName(), testName); } else { Assert.assertNull(expected, testName); } }
Example 4
Source File: Read.java From cramtools with Apache License 2.0 | 6 votes |
SAMRecord firstSAMRecord(SAMFileHeader header) { SAMRecord r = new SAMRecord(header); r.setReadName(evidenceRecord.getReadName()); r.setReferenceName(evidenceRecord.Chromosome); r.setAlignmentStart(Integer.valueOf(evidenceRecord.OffsetInReference) + 1); r.setMappingQuality(Integer.valueOf(evidenceRecord.ScoreAllele0)); r.setReadPairedFlag(true); r.setReadUnmappedFlag(false); r.setReadNegativeStrandFlag(negative); r.setFirstOfPairFlag(evidenceRecord.side == 0); r.setSecondOfPairFlag(!r.getFirstOfPairFlag()); r.setCigar(new Cigar(Utils.toCigarOperatorList(firstHalf.samCigarElements))); r.setReadBases(Utils.toByteArray(firstHalf.readBasesBuf)); r.setBaseQualities(Utils.toByteArray(firstHalf.readScoresBuf)); r.setAttribute("GC", Utils.toString(firstHalf.gcList)); r.setAttribute("GS", Utils.toString(firstHalf.gsBuf)); r.setAttribute("GQ", SAMUtils.phredToFastq(Utils.toByteArray(firstHalf.gqBuf))); return r; }
Example 5
Source File: Read.java From cramtools with Apache License 2.0 | 6 votes |
SAMRecord secondSAMRecord(SAMFileHeader header) { SAMRecord r = new SAMRecord(header); r.setReadName(evidenceRecord.getReadName()); r.setReferenceName(evidenceRecord.Chromosome); r.setAlignmentStart(Integer.valueOf(evidenceRecord.MateOffsetInReference) + 1); r.setMappingQuality(Integer.valueOf(evidenceRecord.ScoreAllele0)); r.setReadPairedFlag(true); r.setReadUnmappedFlag(false); r.setReadNegativeStrandFlag(negative); r.setFirstOfPairFlag(evidenceRecord.side == 1); r.setSecondOfPairFlag(!r.getFirstOfPairFlag()); r.setCigar(new Cigar(Utils.toCigarOperatorList(secondHalf.samCigarElements))); r.setReadBases(Utils.toByteArray(secondHalf.readBasesBuf)); r.setBaseQualities(Utils.toByteArray(secondHalf.readScoresBuf)); r.setAttribute("GC", Utils.toString(secondHalf.gcList)); r.setAttribute("GS", Utils.toString(secondHalf.gsBuf)); r.setAttribute("GQ", SAMUtils.phredToFastq(Utils.toByteArray(secondHalf.gqBuf))); return r; }
Example 6
Source File: FastqToSam.java From picard with MIT License | 5 votes |
/** More complicated method that takes two fastq files and builds pairing information in the SAM. */ protected int doPaired(final FastqReader freader1, final FastqReader freader2, final SAMFileWriter writer) { int readCount = 0; final ProgressLogger progress = new ProgressLogger(LOG); for ( ; freader1.hasNext() && freader2.hasNext() ; readCount++) { final FastqRecord frec1 = freader1.next(); final FastqRecord frec2 = freader2.next(); final String frec1Name = SequenceUtil.getSamReadNameFromFastqHeader(frec1.getReadHeader()); final String frec2Name = SequenceUtil.getSamReadNameFromFastqHeader(frec2.getReadHeader()); final String baseName = getBaseName(frec1Name, frec2Name, freader1, freader2); final SAMRecord srec1 = createSamRecord(writer.getFileHeader(), baseName, frec1, true) ; srec1.setFirstOfPairFlag(true); srec1.setSecondOfPairFlag(false); writer.addAlignment(srec1); progress.record(srec1); final SAMRecord srec2 = createSamRecord(writer.getFileHeader(), baseName, frec2, true) ; srec2.setFirstOfPairFlag(false); srec2.setSecondOfPairFlag(true); writer.addAlignment(srec2); progress.record(srec2); } if (freader1.hasNext() || freader2.hasNext()) { throw new PicardException("Input paired fastq files must be the same length"); } return readCount; }
Example 7
Source File: MergeBamAlignmentTest.java From picard with MIT License | 5 votes |
private SAMRecord makeRead(final SAMFileHeader alignedHeader, final SAMRecord unmappedRec, final HitSpec hitSpec, final boolean firstOfPair, final int hitIndex) { if (hitSpec == null) return null; final SAMRecord rec = makeRead(alignedHeader, unmappedRec, hitSpec, hitIndex); rec.setReadPairedFlag(true); if (firstOfPair) { rec.setFirstOfPairFlag(true); rec.setAlignmentStart(hitIndex + 1); } else { rec.setSecondOfPairFlag(true); rec.setAlignmentStart(hitIndex + 201); } return rec; }
Example 8
Source File: UmiUtilTest.java From picard with MIT License | 5 votes |
@Test(dataProvider = "topStrandDataProvider") public void testIsTopStrand(final int referenceIndex, final int alignmentStart, final int mateReferenceIndex, final int mateAlignmentStart, final boolean firstOfPairFlag, final boolean negativeStrandFlag, final boolean mateNegativeStrandFlag, final boolean mapped, final boolean mateMapped, final UmiUtil.ReadStrand strand) { final int readLength = 15; final int contigLength = 500; final SAMFileHeader header = new SAMFileHeader(); final SAMSequenceDictionary sequenceDictionary = new SAMSequenceDictionary(); sequenceDictionary.addSequence(new SAMSequenceRecord("chr1", contigLength)); sequenceDictionary.addSequence(new SAMSequenceRecord("chr2", contigLength)); header.setSequenceDictionary(sequenceDictionary); final SAMRecord rec = new SAMRecord(header); rec.setReadUnmappedFlag(!mapped); rec.setMateUnmappedFlag(!mateMapped); rec.setReadPairedFlag(true); rec.setCigarString(readLength + "M"); rec.setAttribute("MC", readLength + "M"); rec.setReferenceIndex(referenceIndex); rec.setAlignmentStart(alignmentStart); rec.setMateReferenceIndex(mateReferenceIndex); rec.setMateAlignmentStart(mateAlignmentStart); rec.setFirstOfPairFlag(firstOfPairFlag); rec.setReadNegativeStrandFlag(negativeStrandFlag); rec.setMateNegativeStrandFlag(mateNegativeStrandFlag); Assert.assertEquals(UmiUtil.getStrand(rec), strand); }
Example 9
Source File: FastqRead.java From cramtools with Apache License 2.0 | 5 votes |
public SAMRecord toSAMRecord(SAMFileHeader header) { SAMRecord record = new SAMRecord(header); String name = null; if (data[nameLen - 1] == '/' && Character.isDigit(data[nameLen])) name = new String(data, 1, nameLen - 2); else name = new String(data, 1, nameLen - 2); record.setReadName(name); int readLen = (data.length - this.nameLen - 4 - 1) / 2; byte[] bases = Arrays.copyOfRange(data, nameLen + 2, nameLen + 2 + readLen); record.setReadBases(bases); byte[] scores = Arrays.copyOfRange(data, nameLen + 3 + 1 + readLen + 1, nameLen + 3 + 1 + 2 * readLen + 1); record.setBaseQualityString(new String(scores)); record.setReadUnmappedFlag(true); switch (templateIndex) { case 0: record.setReadPairedFlag(false); break; case 1: record.setReadPairedFlag(true); record.setFirstOfPairFlag(true); break; case 2: record.setReadPairedFlag(true); record.setSecondOfPairFlag(true); break; default: break; } return record; }
Example 10
Source File: DuplicateSamFilterTest.java From rtg-tools with BSD 2-Clause "Simplified" License | 4 votes |
public void testPairedEnd() { final DuplicateSamFilter dsf = new DuplicateSamFilter(); assertFalse(dsf.acceptRecord(null)); final SAMRecord rec1f = new SAMRecord(null); assertFalse(dsf.acceptRecord(rec1f)); final String firstReadName = "IronSheik"; final String secondReadName = "Sabu"; rec1f.setReadName(firstReadName); rec1f.setReadPairedFlag(true); rec1f.setFirstOfPairFlag(true); assertTrue(dsf.acceptRecord(rec1f)); rec1f.setSecondaryAlignment(true); final SAMRecord rec2f = new SAMRecord(null); rec2f.setReadPairedFlag(true); rec2f.setFirstOfPairFlag(true); rec2f.setReadName(secondReadName); rec2f.setSecondaryAlignment(true); final SAMRecord rec3fdup = new SAMRecord(null); rec3fdup.setReadPairedFlag(true); rec3fdup.setFirstOfPairFlag(true); rec3fdup.setReadName(firstReadName); rec3fdup.setSecondaryAlignment(true); final SAMRecord rec1s = new SAMRecord(null); rec1s.setReadName(firstReadName); rec1s.setReadPairedFlag(true); rec1s.setFirstOfPairFlag(false); rec1s.setSecondaryAlignment(true); final SAMRecord rec2s = new SAMRecord(null); rec2s.setReadPairedFlag(true); rec2s.setFirstOfPairFlag(false); rec2s.setReadName(secondReadName); rec2s.setSecondaryAlignment(true); final SAMRecord rec3sdup = new SAMRecord(null); rec3sdup.setReadPairedFlag(true); rec3sdup.setFirstOfPairFlag(false); rec3sdup.setReadName(firstReadName); rec3sdup.setSecondaryAlignment(true); assertTrue(dsf.acceptRecord(rec1f)); assertTrue(dsf.acceptRecord(rec1s)); assertTrue(dsf.acceptRecord(rec2f)); assertTrue(dsf.acceptRecord(rec2s)); assertFalse(dsf.acceptRecord(rec1f)); assertFalse(dsf.acceptRecord(rec1s)); assertFalse(dsf.acceptRecord(rec3fdup)); assertFalse(dsf.acceptRecord(rec3sdup)); }
Example 11
Source File: ClusterDataToSamConverter.java From picard with MIT License | 4 votes |
/** * Creates a new SAM record from the basecall data */ private SAMRecord createSamRecord(final ReadData readData, final String readName, final boolean isPf, final boolean firstOfPair, final String unmatchedBarcode, final String barcodeQuality, final List<String> molecularIndexes, final List<String> molecularIndexQualities) { final SAMRecord sam = new SAMRecord(null); sam.setReadName(readName); sam.setReadBases(readData.getBases()); sam.setBaseQualities(readData.getQualities()); // Flag values sam.setReadPairedFlag(isPairedEnd); sam.setReadUnmappedFlag(true); sam.setReadFailsVendorQualityCheckFlag(!isPf); if (isPairedEnd) { sam.setMateUnmappedFlag(true); sam.setFirstOfPairFlag(firstOfPair); sam.setSecondOfPairFlag(!firstOfPair); } if (filters.filterOut(sam)) { sam.setAttribute(ReservedTagConstants.XN, 1); } if (this.readGroupId != null) { sam.setAttribute(SAMTag.RG.name(), readGroupId); } // If it's a barcoded run and it has been decided that the original BC value should be added to the record, do it if (unmatchedBarcode != null) { sam.setAttribute(SAMTag.BC.name(), unmatchedBarcode); if (barcodeQuality != null ) { sam.setAttribute(SAMTag.QT.name(), barcodeQuality); } } if (!molecularIndexes.isEmpty()) { if (!this.MOLECULAR_INDEX_TAG.isEmpty()) { sam.setAttribute(this.MOLECULAR_INDEX_TAG, String.join(MOLECULAR_INDEX_, molecularIndexes)); } if (!this.MOLECULAR_INDEX_QUALITY_TAG.isEmpty()) { sam.setAttribute(this.MOLECULAR_INDEX_QUALITY_TAG, String.join(MOLECULAR_INDEX_, molecularIndexQualities)); } if (!this.tagPerMolecularIndex.isEmpty()) { if (tagPerMolecularIndex.size() != molecularIndexes.size()) { throw new PicardException("Found " + molecularIndexes.size() + " molecular indexes but only " + tagPerMolecularIndex.size() + " SAM tags given."); } for (int i = 0; i < this.tagPerMolecularIndex.size(); i++) { sam.setAttribute(this.tagPerMolecularIndex.get(i), molecularIndexes.get(i)); } } } return sam; }
Example 12
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 13
Source File: MergeBamAlignmentTest.java From picard with MIT License | 4 votes |
private void testBestFragmentMapqStrategy(final String testName, final int[] firstMapQs, final int[] secondMapQs, final boolean includeSecondary, final int expectedFirstMapq, final int expectedSecondMapq) throws Exception { 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 readName = "theRead"; final SAMRecord firstUnmappedRead = new SAMRecord(header); firstUnmappedRead.setReadName(readName); firstUnmappedRead.setReadString("ACGTACGTACGTACGT"); firstUnmappedRead.setBaseQualityString("5555555555555555"); firstUnmappedRead.setReadUnmappedFlag(true); firstUnmappedRead.setMateUnmappedFlag(true); firstUnmappedRead.setReadPairedFlag(true); firstUnmappedRead.setFirstOfPairFlag(true); final SAMRecord secondUnmappedRead = new SAMRecord(header); secondUnmappedRead.setReadName(readName); secondUnmappedRead.setReadString("TCGAACGTTCGAACTG"); secondUnmappedRead.setBaseQualityString("6666666666666666"); secondUnmappedRead.setReadUnmappedFlag(true); secondUnmappedRead.setMateUnmappedFlag(true); secondUnmappedRead.setReadPairedFlag(true); secondUnmappedRead.setSecondOfPairFlag(true); final SAMFileWriter unmappedWriter = factory.makeSAMWriter(header, false, unmappedSam); unmappedWriter.addAlignment(firstUnmappedRead); unmappedWriter.addAlignment(secondUnmappedRead); 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); addAlignmentsForBestFragmentMapqStrategy(alignedWriter, firstUnmappedRead, sequence, firstMapQs); addAlignmentsForBestFragmentMapqStrategy(alignedWriter, secondUnmappedRead, sequence, secondMapQs); alignedWriter.close(); final File output = File.createTempFile("testBestFragmentMapqStrategy." + testName, ".sam"); output.deleteOnExit(); 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.BestEndMapq, null, includeSecondary, null, null); final SamReader reader = SamReaderFactory.makeDefault().open(output); int numFirstRecords = 0; int numSecondRecords = 0; int firstPrimaryMapq = -1; int secondPrimaryMapq = -1; for (final SAMRecord rec: reader) { Assert.assertTrue(rec.getReadPairedFlag()); if (rec.getFirstOfPairFlag()) ++numFirstRecords; else if (rec.getSecondOfPairFlag()) ++ numSecondRecords; else Assert.fail("unpossible!"); if (!rec.getReadUnmappedFlag() && !rec.getNotPrimaryAlignmentFlag()) { if (rec.getFirstOfPairFlag()) { Assert.assertEquals(firstPrimaryMapq, -1); firstPrimaryMapq = rec.getMappingQuality(); } else { Assert.assertEquals(secondPrimaryMapq, -1); secondPrimaryMapq = rec.getMappingQuality(); } } else if (rec.getNotPrimaryAlignmentFlag()) { Assert.assertTrue(rec.getMateUnmappedFlag()); } } reader.close(); Assert.assertEquals(firstPrimaryMapq, expectedFirstMapq); Assert.assertEquals(secondPrimaryMapq, expectedSecondMapq); if (!includeSecondary) { Assert.assertEquals(numFirstRecords, 1); Assert.assertEquals(numSecondRecords, 1); } else { // If no alignments for an end, there will be a single unmapped record Assert.assertEquals(numFirstRecords, Math.max(1, firstMapQs.length)); Assert.assertEquals(numSecondRecords, Math.max(1, secondMapQs.length)); } }