Java Code Examples for htsjdk.samtools.reference.ReferenceSequence#getBases()
The following examples show how to use
htsjdk.samtools.reference.ReferenceSequence#getBases() .
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: SequenceDictionaryUtils.java From picard with MIT License | 6 votes |
/** * Create one SAMSequenceRecord from a single fasta sequence */ private SAMSequenceRecord makeSequenceRecord(final ReferenceSequence refSeq) { final SAMSequenceRecord ret = new SAMSequenceRecord(refSeq.getName(), refSeq.length()); // Compute MD5 of upcased bases final byte[] bases = refSeq.getBases(); for (int i = 0; i < bases.length; ++i) { bases[i] = StringUtil.toUpperCase(bases[i]); } ret.setAttribute(SAMSequenceRecord.MD5_TAG, md5Hash(bases)); if (genomeAssembly != null) { ret.setAttribute(SAMSequenceRecord.ASSEMBLY_TAG, genomeAssembly); } ret.setAttribute(SAMSequenceRecord.URI_TAG, uri); if (species != null) { ret.setAttribute(SAMSequenceRecord.SPECIES_TAG, species); } return ret; }
Example 2
Source File: RefRepo.java From cramtools with Apache License 2.0 | 6 votes |
@Override public List<Entry> call() throws Exception { List<Entry> list = new ArrayList<RefRepo.Entry>(); ReferenceSequenceFile rsFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(file); ReferenceSequence sequence = null; while ((sequence = rsFile.nextSequence()) != null) { sequence.getBases(); Entry e = new Entry(); e.md5 = Utils.calculateMD5String(sequence.getBases()); e.file = "file://" + file.getAbsolutePath(); e.name = sequence.getName(); e.length = sequence.length(); log.info(String.format("New entry: %s", e.toString())); list.add(e); } return list; }
Example 3
Source File: CreateSequenceDictionary.java From varsim with BSD 2-Clause "Simplified" License | 6 votes |
/** * Create one SAMSequenceRecord from a single fasta sequence */ private SAMSequenceRecord makeSequenceRecord(final ReferenceSequence refSeq) { final SAMSequenceRecord ret = new SAMSequenceRecord(refSeq.getName(), refSeq.length()); // Compute MD5 of upcased bases final byte[] bases = refSeq.getBases(); for (int i = 0; i < bases.length; ++i) { bases[i] = StringUtil.toUpperCase(bases[i]); } ret.setAttribute(SAMSequenceRecord.MD5_TAG, md5Hash(bases)); if (GENOME_ASSEMBLY != null) { ret.setAttribute(SAMSequenceRecord.ASSEMBLY_TAG, GENOME_ASSEMBLY); } ret.setAttribute(SAMSequenceRecord.URI_TAG, URI); if (SPECIES != null) { ret.setAttribute(SAMSequenceRecord.SPECIES_TAG, SPECIES); } return ret; }
Example 4
Source File: GcBiasUtils.java From picard with MIT License | 6 votes |
public static int[] calculateRefWindowsByGc(final int windows, final File referenceSequence, final int windowSize) { final ReferenceSequenceFile refFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(referenceSequence); ReferenceSequence ref; final int [] windowsByGc = new int [windows]; while ((ref = refFile.nextSequence()) != null) { final byte[] refBases = ref.getBases(); StringUtil.toUpperCase(refBases); final int refLength = refBases.length; final int lastWindowStart = refLength - windowSize; final CalculateGcState state = new GcBiasUtils().new CalculateGcState(); for (int i = 1; i < lastWindowStart; ++i) { final int windowEnd = i + windowSize; final int gcBin = calculateGc(refBases, i, windowEnd, state); if (gcBin != -1) windowsByGc[gcBin]++; } } return windowsByGc; }
Example 5
Source File: ReferenceResource.java From VarDictJava with MIT License | 6 votes |
/** * Method convert fasta data to array contains header and nucleotide bases. * @param fasta path to fasta file * @param chr chromosome name of region * @param start start position of region * @param end end position of region * @return array of nucleotide bases in the region of fasta */ public String[] retrieveSubSeq(String fasta, String chr, int start, int end) { try { IndexedFastaSequenceFile idx = fetchFasta(fasta); ReferenceSequence seq = idx.getSubsequenceAt(chr, start, end); byte[] bases = seq.getBases(); return new String[]{">" + chr + ":" + start + "-" + end, bases != null ? new String(bases) : ""}; } catch (SAMException e){ if (UNABLE_FIND_CONTIG.matcher(e.getMessage()).find()){ throw new WrongFastaOrBamException(chr, e); } else if (WRONG_START_OR_END.matcher(e.getMessage()).find()){ throw new RegionBoundariesException(chr, start, end, e); } else { throw e; } } }
Example 6
Source File: AnnotateTargetsIntegrationTest.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void checkOutputGCContent(final ReferenceFileSource reference, final SimpleInterval interval, final double observed) { final ReferenceSequence sequence = reference.queryAndPrefetch(interval); int total = 0; int gc = 0; for (final byte base : sequence.getBases()) { switch (Character.toLowerCase(base)) { case 'g': case 'c': gc++; total++; break; case 'a': case 't': case 'u': total++; break; default: } } final double expectedGCContent = total == 0 ? Double.NaN : ((double) gc / (double) total); if (Double.isNaN(expectedGCContent)) { Assert.assertTrue(Double.isNaN(observed)); } else { Assert.assertEquals(observed, expectedGCContent, 0.0001); } }
Example 7
Source File: LiftoverVcf.java From picard with MIT License | 5 votes |
/** * utility function to attempt to add a variant. Checks that the reference allele still matches the reference (which may have changed) * * @param vc new {@link VariantContext} * @param refSeq {@link ReferenceSequence} of new reference * @param source the original {@link VariantContext} to use for putting the original location information into vc */ private void tryToAddVariant(final VariantContext vc, final ReferenceSequence refSeq, final VariantContext source) { if (!refSeq.getName().equals(vc.getContig())) { throw new IllegalStateException("The contig of the VariantContext, " + vc.getContig() + ", doesnt match the ReferenceSequence: " + refSeq.getName()); } // Check that the reference allele still agrees with the reference sequence final boolean mismatchesReference; final Allele allele = vc.getReference(); final byte[] ref = refSeq.getBases(); final String refString = StringUtil.bytesToString(ref, vc.getStart() - 1, vc.getEnd() - vc.getStart() + 1); if (!refString.equalsIgnoreCase(allele.getBaseString())) { // consider that the ref and the alt may have been swapped in a simple biallelic SNP if (vc.isBiallelic() && vc.isSNP() && refString.equalsIgnoreCase(vc.getAlternateAllele(0).getBaseString())) { totalTrackedAsSwapRefAlt++; if (RECOVER_SWAPPED_REF_ALT) { addAndTrack(LiftoverUtils.swapRefAlt(vc, TAGS_TO_REVERSE, TAGS_TO_DROP), source); return; } } mismatchesReference = true; } else { mismatchesReference = false; } if (mismatchesReference) { rejectedRecords.add(new VariantContextBuilder(source) .filter(FILTER_MISMATCHING_REF_ALLELE) .attribute(ATTEMPTED_LOCUS, String.format("%s:%d-%d", vc.getContig(), vc.getStart(), vc.getEnd())) .attribute(ATTEMPTED_ALLELES, vc.getReference().toString() + "->" + String.join(",", vc.getAlternateAlleles().stream().map(Allele::toString).collect(Collectors.toList()))) .make()); failedAlleleCheck++; trackLiftedVariantContig(rejectsByContig, source.getContig()); } else { addAndTrack(vc, source); } }
Example 8
Source File: GcBiasMetricsCollector.java From picard with MIT License | 5 votes |
@Override public void acceptRecord(final GcBiasCollectorArgs args) { final SAMRecord rec = args.getRec(); if (logCounter < 100 && rec.getReadBases().length == 0) { log.warn("Omitting read " + rec.getReadName() + " with '*' in SEQ field."); if (++logCounter == 100) { log.warn("There are more than 100 reads with '*' in SEQ field in file."); } return; } if (!rec.getReadUnmappedFlag()) { if (referenceIndex != rec.getReferenceIndex() || gc == null) { final ReferenceSequence ref = args.getRef(); refBases = ref.getBases(); StringUtil.toUpperCase(refBases); final int refLength = refBases.length; final int lastWindowStart = refLength - scanWindowSize; gc = GcBiasUtils.calculateAllGcs(refBases, lastWindowStart, scanWindowSize); referenceIndex = rec.getReferenceIndex(); } addReadToGcData(rec, this.gcData); if (ignoreDuplicates && !rec.getDuplicateReadFlag()) { addReadToGcData(rec, this.gcDataNonDups); } } else { updateTotalClusters(rec, this.gcData); if (ignoreDuplicates && !rec.getDuplicateReadFlag()) { updateTotalClusters(rec, this.gcDataNonDups); } } }
Example 9
Source File: ValidateReference.java From Drop-seq with MIT License | 5 votes |
private void validateReferenceBases(File referenceFile) { final ReferenceSequenceFile refSeqFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(referenceFile, true); ReferenceSequence sequence; while ((sequence = refSeqFile.nextSequence()) != null) { for (final byte base: sequence.getBases()) { if (!IUPAC_TABLE[base]) { messages.baseErrors = String.format("WARNING: AT least one invalid base '%c' (decimal %d) in reference sequence named %s", StringUtil.byteToChar(base), base, sequence.getName()); break; } } } }
Example 10
Source File: ExtractSequences.java From picard with MIT License | 5 votes |
@Override protected int doWork() { IOUtil.assertFileIsReadable(INTERVAL_LIST); IOUtil.assertFileIsReadable(REFERENCE_SEQUENCE); IOUtil.assertFileIsWritable(OUTPUT); final IntervalList intervals = IntervalList.fromFile(INTERVAL_LIST); final ReferenceSequenceFile ref = ReferenceSequenceFileFactory.getReferenceSequenceFile(REFERENCE_SEQUENCE); SequenceUtil.assertSequenceDictionariesEqual(intervals.getHeader().getSequenceDictionary(), ref.getSequenceDictionary()); final BufferedWriter out = IOUtil.openFileForBufferedWriting(OUTPUT); for (final Interval interval : intervals) { final ReferenceSequence seq = ref.getSubsequenceAt(interval.getContig(), interval.getStart(), interval.getEnd()); final byte[] bases = seq.getBases(); if (interval.isNegativeStrand()) SequenceUtil.reverseComplement(bases); try { out.write(">"); out.write(interval.getName()); out.write("\n"); for (int i=0; i<bases.length; ++i) { if (i > 0 && i % LINE_LENGTH == 0) out.write("\n"); out.write(bases[i]); } out.write("\n"); } catch (IOException ioe) { throw new PicardException("Error writing to file " + OUTPUT.getAbsolutePath(), ioe); } } CloserUtil.close(out); return 0; }
Example 11
Source File: ReferenceFileSparkSource.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public ReferenceBases getReferenceBases(final SimpleInterval interval) throws IOException { try ( ReferenceSequenceFile referenceSequenceFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(getReferencePath()) ) { ReferenceSequence sequence = referenceSequenceFile.getSubsequenceAt(interval.getContig(), interval.getStart(), interval.getEnd()); return new ReferenceBases(sequence.getBases(), interval); } }
Example 12
Source File: RefSequence.java From hmftools with GNU General Public License v3.0 | 5 votes |
@VisibleForTesting RefSequence(final ReferenceSequence sequence) { this.sequence = sequence; this.start = sequence.getContigIndex() + 1; this.end = start + sequence.getBases().length - 1; this.indexedBases = new IndexedBases(start, 0, sequence.getBases()); }
Example 13
Source File: IndexedFastaDataSource.java From chipster with MIT License | 5 votes |
public String query(Chromosome chr, Long start, Long end) { String chrString = chromosomeNameUnnormaliser.unnormalise(chr); try { ReferenceSequence picardSequence = picard.getSubsequenceAt(chrString, start, end); return new String(picardSequence.getBases()); } catch (SAMException e) { e.printStackTrace(); //Catch "Query asks for data past end of contig" to prevent this thread from ending return null; } }
Example 14
Source File: ReferenceSource.java From cramtools with Apache License 2.0 | 5 votes |
protected byte[] findBasesByName(String name, boolean tryVariants) { if (rsFile == null || !rsFile.isIndexed()) return null; ReferenceSequence sequence = null; if (fastaSequenceIndex != null) if (fastaSequenceIndex.hasIndexEntry(name)) sequence = rsFile.getSequence(name); else sequence = null; if (sequence != null) return sequence.getBases(); if (tryVariants) { for (String variant : getVariants(name)) { try { sequence = rsFile.getSequence(variant); } catch (Exception e) { log.info("Sequence not found: " + variant); } if (sequence != null) { log.debug("Reference found in memory cache for name %s by variant %s", name, variant); return sequence.getBases(); } } } return null; }
Example 15
Source File: RrbsMetricsCollector.java From picard with MIT License | 4 votes |
public void acceptRecord(final SAMRecordAndReference args) { mappedRecordCount++; final SAMRecord samRecord = args.getSamRecord(); final ReferenceSequence referenceSequence = args.getReferenceSequence(); final byte[] readBases = samRecord.getReadBases(); final byte[] readQualities = samRecord.getBaseQualities(); final byte[] refBases = referenceSequence.getBases(); if (samRecord.getReadLength() < minReadLength) { smallReadCount++; return; } else if (SequenceUtil.countMismatches(samRecord, refBases, true) > Math.round(samRecord.getReadLength() * maxMismatchRate)) { mismatchCount++; return; } // We only record non-CpG C sites if there was at least one CpG in the read, keep track of // the values for this record and then apply to the global totals if valid int recordCpgs = 0; for (final AlignmentBlock alignmentBlock : samRecord.getAlignmentBlocks()) { final int blockLength = alignmentBlock.getLength(); final int refFragmentStart = alignmentBlock.getReferenceStart() - 1; final int readFragmentStart = alignmentBlock.getReadStart() - 1; final byte[] refFragment = getFragment(refBases, refFragmentStart, blockLength); final byte[] readFragment = getFragment(readBases, readFragmentStart, blockLength); final byte[] readQualityFragment = getFragment(readQualities, readFragmentStart, blockLength); if (samRecord.getReadNegativeStrandFlag()) { // In the case of a negative strand, reverse (and complement for base arrays) the reference, // reads & qualities so that it can be treated as a positive strand for the rest of the process SequenceUtil.reverseComplement(refFragment); SequenceUtil.reverseComplement(readFragment); SequenceUtil.reverseQualities(readQualityFragment); } for (int i=0; i < blockLength-1; i++) { final int curRefIndex = getCurRefIndex(refFragmentStart, blockLength, i, samRecord.getReadNegativeStrandFlag()); // Look at a 2-base window to see if we're on a CpG site, and if so check for conversion // (CG -> TG). We do not consider ourselves to be on a CpG site if we're on the last base of a read if ((SequenceUtil.basesEqual(refFragment[i], SequenceUtil.C)) && (SequenceUtil.basesEqual(refFragment[i+1], SequenceUtil.G))) { // We want to catch the case where there's a CpG in the reference, even if it is not valid // to prevent the C showing up as a non-CpG C down below. Otherwise this could have been all // in one if statement if (isValidCpg(refFragment, readFragment, readQualityFragment, i)) { recordCpgs++; final CpgLocation curLocation = new CpgLocation(samRecord.getReferenceName(), curRefIndex); cpgTotal.increment(curLocation); if (SequenceUtil.isBisulfiteConverted(readFragment[i], refFragment[i])) { cpgConverted.increment(curLocation); } } i++; } else if (isC(refFragment[i], readFragment[i]) && isAboveCytoQcThreshold(readQualities, i) && SequenceUtil.bisulfiteBasesEqual(false, readFragment[i+1], refFragment[i+1])) { // C base in the reference that's not associated with a CpG nCytoTotal++; if (SequenceUtil.isBisulfiteConverted(readFragment[i], refFragment[i])) { nCytoConverted++; } } } } if (recordCpgs == 0) { noCpgCount++; } }
Example 16
Source File: NormalizeFasta.java From picard with MIT License | 4 votes |
@Override protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsWritable(OUTPUT); if (INPUT.getAbsoluteFile().equals(OUTPUT.getAbsoluteFile())) { throw new IllegalArgumentException("Input and output cannot be the same file."); } final ReferenceSequenceFile ref = ReferenceSequenceFileFactory.getReferenceSequenceFile(INPUT, TRUNCATE_SEQUENCE_NAMES_AT_WHITESPACE); final BufferedWriter out = IOUtil.openFileForBufferedWriting(OUTPUT); ReferenceSequence seq = null; while ((seq = ref.nextSequence()) != null) { final String name = seq.getName(); final byte[] bases = seq.getBases(); try { out.write(">"); out.write(name); out.newLine(); if (bases.length == 0) { log.warn("Sequence " + name + " contains 0 bases."); } else { for (int i=0; i<bases.length; ++i) { if (i > 0 && i % LINE_LENGTH == 0) out.write("\n"); out.write(bases[i]); } out.write("\n"); } } catch (IOException ioe) { throw new PicardException("Error writing to file " + OUTPUT.getAbsolutePath(), ioe); } } try { out.close(); } catch (IOException e) { throw new RuntimeIOException(e); } return 0; }
Example 17
Source File: NonNFastaSize.java From picard with MIT License | 4 votes |
@Override protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsWritable(OUTPUT); // set up the reference and a mask so that we only count the positions requested by the user final ReferenceSequenceFile ref = ReferenceSequenceFileFactory.getReferenceSequenceFile(INPUT); final ReferenceSequenceMask referenceSequenceMask; if (INTERVALS != null) { IOUtil.assertFileIsReadable(INTERVALS); final IntervalList intervalList = IntervalList.fromFile(INTERVALS); referenceSequenceMask = new IntervalListReferenceSequenceMask(intervalList); } else { final SAMFileHeader header = new SAMFileHeader(); header.setSequenceDictionary(ref.getSequenceDictionary()); referenceSequenceMask = new WholeGenomeReferenceSequenceMask(header); } long nonNbases = 0L; for (final SAMSequenceRecord rec : ref.getSequenceDictionary().getSequences()) { // pull out the contig and set up the bases final ReferenceSequence sequence = ref.getSequence(rec.getSequenceName()); final byte[] bases = sequence.getBases(); StringUtil.toUpperCase(bases); for (int i = 0; i < bases.length; i++) { // only investigate this position if it's within our mask if (referenceSequenceMask.get(sequence.getContigIndex(), i+1)) { nonNbases += bases[i] == SequenceUtil.N ? 0 : 1; } } } try { final BufferedWriter out = IOUtil.openFileForBufferedWriting(OUTPUT); out.write(nonNbases + "\n"); out.close(); } catch (IOException ioe) { throw new PicardException("Error writing to file " + OUTPUT.getAbsolutePath(), ioe); } return 0; }
Example 18
Source File: ReferenceHadoopSparkSource.java From gatk with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public ReferenceBases getReferenceBases(final SimpleInterval interval) { ReferenceSequenceFile referenceSequenceFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(new GATKPath(referenceURI.toString()).toPath()); ReferenceSequence sequence = referenceSequenceFile.getSubsequenceAt(interval.getContig(), interval.getStart(), interval.getEnd()); return new ReferenceBases(sequence.getBases(), interval); }
Example 19
Source File: GatherGeneGCLengthTest.java From Drop-seq with MIT License | 4 votes |
@Test public void testBasic() throws IOException { final GatherGeneGCLength clp = new GatherGeneGCLength(); final File outputFile = File.createTempFile("GatherGeneGCLengthTest.", ".gc_length_metrics"); final File outputTranscriptSequencesFile = File.createTempFile("GatherGeneGCLengthTest.", ".transcript_sequences.fasta"); final File outputTranscriptLevelFile = File.createTempFile("GatherGeneGCLengthTest.", ".transcript_level"); outputFile.deleteOnExit(); outputTranscriptSequencesFile.deleteOnExit(); outputTranscriptLevelFile.deleteOnExit(); final String[] args = new String[] { "ANNOTATIONS_FILE=" + GTF.getAbsolutePath(), "REFERENCE_SEQUENCE=" + FASTA.getAbsolutePath(), "OUTPUT=" + outputFile.getAbsolutePath(), "OUTPUT_TRANSCRIPT_SEQUENCES=" + outputTranscriptSequencesFile.getAbsolutePath(), "OUTPUT_TRANSCRIPT_LEVEL=" + outputTranscriptLevelFile.getAbsolutePath() }; Assert.assertEquals(clp.instanceMain(args), 0); final ReferenceSequence transcriptSequence = ReferenceSequenceFileFactory.getReferenceSequenceFile(outputTranscriptSequencesFile, false).nextSequence(); final ReferenceSequence genomicSequence = ReferenceSequenceFileFactory.getReferenceSequenceFile(FASTA).nextSequence(); // For ERCC, the first sequence is the first gene, which is a single exon final String geneName = "ERCC-00002"; final String transcriptName = "DQ459430"; Assert.assertEquals(transcriptSequence.getBaseString(), genomicSequence.getBaseString()); Assert.assertEquals(transcriptSequence.getName(), geneName + " " + transcriptName); int numG = 0; int numC = 0; for (byte b : transcriptSequence.getBases()) if (b == SequenceUtil.G) ++numG; else if (b == SequenceUtil.C) ++numC; final double pctG = 100 * numG/(double)transcriptSequence.getBases().length; final double pctC = 100 * numC/(double)transcriptSequence.getBases().length; final double pctGC = 100 * (numC + numG)/(double)transcriptSequence.getBases().length; TabbedTextFileWithHeaderParser.Row metric = new TabbedTextFileWithHeaderParser(outputFile).iterator().next(); Assert.assertEquals(metric.getField("GENE"), geneName); Assert.assertEquals(Integer.parseInt(metric.getField("START")), 1); Assert.assertEquals(Integer.parseInt(metric.getField("END")), transcriptSequence.getBases().length); Assert.assertEquals(Double.parseDouble(metric.getField("PCT_GC_UNIQUE_EXON_BASES")), pctGC, 0.05); Assert.assertEquals(Double.parseDouble(metric.getField("PCT_C_ISOFORM_AVERAGE")), pctC, 0.05); Assert.assertEquals(Double.parseDouble(metric.getField("PCT_G_ISOFORM_AVERAGE")), pctG, 0.05); Assert.assertEquals(Integer.parseInt(metric.getField("NUM_TRANSCRIPTS")), 1); TabbedTextFileWithHeaderParser.Row transcriptLevel = new TabbedTextFileWithHeaderParser(outputTranscriptLevelFile).iterator().next(); Assert.assertEquals(transcriptLevel.getField("TRANSCRIPT"), transcriptName); Assert.assertEquals(transcriptLevel.getField("CHR"), genomicSequence.getName()); Assert.assertEquals(Integer.parseInt(transcriptLevel.getField("START")), 1); Assert.assertEquals(Integer.parseInt(transcriptLevel.getField("END")), transcriptSequence.getBases().length); Assert.assertEquals(Double.parseDouble(transcriptLevel.getField("PCT_GC")), pctGC, 0.05); Assert.assertEquals(Double.parseDouble(transcriptLevel.getField("PCT_C")), pctC, 0.05); Assert.assertEquals(Double.parseDouble(transcriptLevel.getField("PCT_G")), pctG, 0.05); }
Example 20
Source File: AbstractWgsMetricsCollector.java From picard with MIT License | 2 votes |
/** * Checks if reference base at given position is unknown. * * @param position to check the base * @param ref reference sequence * @return true if reference base at position represents a no call, otherwise false */ boolean isReferenceBaseN(final int position, final ReferenceSequence ref) { final byte base = ref.getBases()[position - 1]; return SequenceUtil.isNoCall(base); }