Java Code Examples for htsjdk.samtools.SAMRecord#setReadNegativeStrandFlag()
The following examples show how to use
htsjdk.samtools.SAMRecord#setReadNegativeStrandFlag() .
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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: SamUtils.java From rtg-tools with BSD 2-Clause "Simplified" License | 5 votes |
/** * Convert an aligned record to being unmapped * @param record the record to adjust */ public static void convertToUnmapped(SAMRecord record) { record.setReadUnmappedFlag(true); record.setProperPairFlag(false); record.setReadNegativeStrandFlag(false); record.setMappingQuality(0); record.setInferredInsertSize(0); record.setCigarString("*"); record.setAttribute(ATTRIBUTE_NUM_MISMATCHES, null); }
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: 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 10
Source File: SortedSAMWriter.java From abra2 with MIT License | 5 votes |
public void addAlignment(int sampleIdx, SAMRecordWrapper samRecord, int chromosomeChunkIdx) { Feature chunk = this.chromosomeChunker.getChunks().get(chromosomeChunkIdx); SAMRecord read = samRecord.getSamRecord(); // Only output reads with original start pos within specified chromosomeChunk // Avoids reads being written in 2 different chunks int origAlignmentStart = read.getAlignmentStart(); String yo = read.getStringAttribute("YO"); if (yo != null) { String[] fields = yo.split(":"); origAlignmentStart = Integer.parseInt(fields[1]); } if (origAlignmentStart >= chunk.getStart() && origAlignmentStart <= chunk.getEnd()) { if (samRecord.isUnalignedRc() && read.getReadUnmappedFlag()) { // This read was reverse complemented, but not updated. // Change it back to its original state. read.setReadString(rc.reverseComplement(read.getReadString())); read.setBaseQualityString(rc.reverse(read.getBaseQualityString())); read.setReadNegativeStrandFlag(!read.getReadNegativeStrandFlag()); } writers[sampleIdx][chromosomeChunkIdx].addAlignment(read); } }
Example 11
Source File: TestUtils.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 boolean negativeStrand) { final SAMRecord record = new SAMRecord(null); record.setAlignmentStart(alignmentStart); record.setCigarString(cigar); record.setReadString(readString); record.setReadNegativeStrandFlag(negativeStrand); record.setBaseQualityString(qualities); record.setMappingQuality(20); record.setDuplicateReadFlag(false); record.setReadUnmappedFlag(false); return record; }
Example 12
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 13
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 14
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 15
Source File: TagReadWithGeneExonFunctionTest.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 16
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 17
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 18
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 19
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); } }
Example 20
Source File: ClippingUtilityTest.java From picard with MIT License | 4 votes |
@Test(dataProvider = "clipTestData") public void testSingleEndSamRecordClip(final String testName, final String read, final String clip, final int minMatch, final double errRate, final int expected) { if (read == null) return; // Silly case final SingleEndAdapter adapter = new SingleEndAdapter(testName, clip); for (final boolean reverse : new boolean[]{false, true}) { final SAMRecord rec = new SAMRecord(null); if (reverse) { rec.setReadString(SequenceUtil.reverseComplement(read)); rec.setReadNegativeStrandFlag(true); } else { rec.setReadString(read); } final AdapterPair matchedAdapter = ClippingUtility.adapterTrimIlluminaSingleRead(rec, minMatch, errRate, adapter); if (expected == -1) { Assert.assertNull(matchedAdapter, testName); Assert.assertNull(rec.getAttribute(ReservedTagConstants.XT), testName); } else { Assert.assertEquals(matchedAdapter, adapter, testName); Assert.assertEquals(rec.getAttribute(ReservedTagConstants.XT), expected + 1, testName); // Test that if minMatch is decreased, it still matches for (int i = 1; i < minMatch - 3; ++i) { Assert.assertEquals(ClippingUtility.adapterTrimIlluminaSingleRead(rec, minMatch - i, errRate, adapter), adapter, testName); } // Skip this test for high error rates, because almost anything will match. if (errRate < 0.5) { // Test that if minMatch is increased, it now fails to match Assert.assertNull(ClippingUtility.adapterTrimIlluminaSingleRead(rec, minMatch + 1, errRate, adapter), testName); // Test that if errRate is increased, it still matches Assert.assertNotNull(ClippingUtility.adapterTrimIlluminaSingleRead(rec, minMatch, errRate + 0.1, adapter), testName); } // Very low error threshold should cause match failure Assert.assertNull(ClippingUtility.adapterTrimIlluminaSingleRead(rec, minMatch, 0.01, adapter), testName); } } }