Java Code Examples for htsjdk.samtools.SAMRecord#setSecondOfPairFlag()
The following examples show how to use
htsjdk.samtools.SAMRecord#setSecondOfPairFlag() .
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: FilterBamByTagTest.java From Drop-seq with MIT License | 5 votes |
@Test public void filterByReadNumberTest() { FilterBamByTag t = new FilterBamByTag(); // record paired and read is 1st List<SAMRecord> recs = getPairedRead (); SAMRecord recFirstPaired = recs.get(0); SAMRecord recSecondPaired = recs.get(1); boolean flag1= t.retainByReadNumber(recFirstPaired, 1); boolean flag2= t.retainByReadNumber(recFirstPaired, 2); Assert.assertTrue(flag1); Assert.assertFalse(flag2); // record paired and read is 2st recSecondPaired.setProperPairFlag(true); recSecondPaired.setSecondOfPairFlag(true); flag1= t.retainByReadNumber(recSecondPaired, 1); flag2= t.retainByReadNumber(recSecondPaired, 2); Assert.assertTrue(flag2); Assert.assertFalse(flag1); // record unpaired and read is 1st SAMRecordSetBuilder builder = new SAMRecordSetBuilder(); builder.addUnmappedFragment("foo"); SAMRecord recFirstUnPaired = builder.getRecords().iterator().next(); flag1= t.retainByReadNumber(recFirstUnPaired, 1); flag2= t.retainByReadNumber(recFirstPaired, 2); Assert.assertTrue(flag1); Assert.assertFalse(flag2); }
Example 7
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 8
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 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: 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 11
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 12
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)); } }