private static String initRegionFromBigWig(String bigWigFile) throws IOException{ BBFileReader reader= new BBFileReader(bigWigFile); if(! reader.isBigWigFile()){ System.err.println("File " + bigWigFile + " is not bigWig."); throw new RuntimeException(); } String region= reader.getChromosomeNames().get(0); // Just get chrom to start with for(String chrom : reader.getChromosomeNames()){ BigWigIterator iter = reader.getBigWigIterator(chrom, 0, chrom, Integer.MAX_VALUE, false); if(iter.hasNext()){ WigItem x = iter.next(); region= x.getChromosome() + ":" + (x.getStartBase() + 1); reader.close(); return region; } } reader.close(); return region; }
private static String initRegionFromBigBed(String bigBedFile) throws IOException{ BBFileReader reader= new BBFileReader(bigBedFile); if(! reader.isBigBedFile()){ System.err.println("File " + bigBedFile + " is not bigBed."); throw new RuntimeException(); } String region= reader.getChromosomeNames().get(0); // Just get chrom to start with for(String chrom : reader.getChromosomeNames()){ BigBedIterator iter = reader.getBigBedIterator(chrom, 0, chrom, Integer.MAX_VALUE, false); if(iter.hasNext()){ BedFeature x= (BedFeature) iter.next(); region= x.getChromosome() + ":" + (x.getStartBase() + 1); reader.close(); return region; } } reader.close(); return region; }
/** * Open file * @param fileName */ void open(String fileName) { try { label = Gpr.removeExt(Gpr.baseName(fileName)); // Open file BBFileReader readerBb = new BBFileReader(fileName); BBFileHeader bbFileHdr = readerBb.getBBFileHeader(); // Get header if (!bbFileHdr.isHeaderOK()) throw new IOException("Bad header for file '" + fileName + "'"); // Sanity check if (!bbFileHdr.isBigBed()) throw new RuntimeException("Unrecognized header type for file '" + fileName + "' (expecting BigBed)"); // Get file type iterator = readerBb.getBigBedIterator(); } catch (Exception e) { throw new RuntimeException("Error loading file '" + fileName + "'.", e); } }
private void init() throws IOException { FileUtils.checkPath(this.bigWigFilePath); bbFileReader = new BBFileReader(this.bigWigFilePath.toString()); zoomWindowSizes = new ArrayList<>(); for (int zoomLevel = 1; zoomLevel <= bbFileReader.getZoomLevelCount(); zoomLevel++) { zoomWindowSizes.add(bbFileReader.getZoomLevels().getZoomLevelHeader(zoomLevel).getReductionLevel()); } }
public void loadBWFile(ResourceLocator locator, List<Track> newTracks, Genome genome) throws IOException { String trackName = locator.getTrackName(); String trackId = locator.getPath(); String path = locator.getPath(); BBFileReader reader = new BBFileReader(path); BigWigDataSource bigwigSource = new BigWigDataSource(reader, genome); if (reader.isBigWigFile()) { DataSourceTrack track = new DataSourceTrack(locator, trackId, trackName, bigwigSource); newTracks.add(track); } else if (reader.isBigBedFile()) { if (locator.getPath().contains("RRBS_cpgMethylation") || locator.getPath().contains("BiSeq_cpgMethylation") || (reader.getAutoSql() != null && reader.getAutoSql().startsWith("table BisulfiteSeq"))) { loadMethylTrack(locator, reader, newTracks, genome); } else { FeatureTrack track = new FeatureTrack(locator, trackId, trackName, bigwigSource); newTracks.add(track); } } else { throw new RuntimeException("Unknown BIGWIG type: " + locator.getPath()); } }
@Test public void testBigBed() throws IOException { String path = TestUtils.DATA_DIR + "bb/chr21.refseq.bb"; BBFileReader bbReader = new BBFileReader(path); BBFileHeader bbFileHdr = bbReader.getBBFileHeader(); assertTrue(bbFileHdr.isBigBed()); String chr = "chr21"; int start = 26490012; int end = 42182827; for (BBZoomLevelHeader header : bbReader.getZoomLevels().getZoomLevelHeaders()) { assertNotNull(header); ZoomLevelIterator zlIter = bbReader.getZoomLevelIterator(header.getZoomLevel(), chr, start, chr, end, false); while (zlIter.hasNext()) { ZoomDataRecord rec = zlIter.next(); int n = rec.getBasesCovered(); if (n > 0) { assertEquals(chr, rec.getChromName()); assertTrue(rec.getChromEnd() >= start && rec.getChromStart() <= end); } } } }
public BigWigDataSource(URI uri) throws IOException { bbReader = new BBFileReader(NetworkUtils.getNeatPathFromURI(uri), new TribbleStream(uri)); if (!bbReader.isBigWigFile()) { throw new IOException("Input is not a BigWig file."); } }
this.bigBedReader = new BBFileReader(filename); // or url for remote access. if(!this.bigBedReader.getBBFileHeader().isBigBed()){ throw new RuntimeException("File " + filename + " is not bigBed.");
public BigWigDataSource(BBFileReader reader, Genome genome) throws IOException { super(genome); this.reader = reader; this.levels = reader.getZoomLevels(); this.wholeGenomeScores = new HashMap<>(); if (reader.isBigWigFile()) initMinMax(); // Assume 1000 pixel screen, pick visibility level to be @ highest resolution zoom. // TODO -- something smarter, like scaling by actual density if (levels != null && levels.getZoomHeaderCount() > 0) { BBZoomLevelHeader firstLevel = levels.getZoomLevelHeaders().get(0); // Highest res featureVisiblityWindow = firstLevel.getReductionLevel() * 2000; } if (genome != null) { Collection<String> chrNames = reader.getChromosomeNames(); for (String chr : chrNames) { String igvChr = genome.getCanonicalChrName(chr); if (igvChr != null && !igvChr.equals(chr)) { chrNameMap.put(igvChr, chr); } } } bedCodec = new IGVBEDCodec(genome); }
List<String> chrNames = reader.getChromosomeNames(); for (String chr : chrNames) { BigWigIterator iter = reader.getBigWigIterator(chr, 0, chr, Integer.MAX_VALUE, false); while (iter.hasNext()) { WigItem item = iter.next(); ZoomLevelIterator zlIter = reader.getZoomLevelIterator(z); if (zlIter.hasNext()) { while (zlIter.hasNext()) {
public void testZillerQuery() throws Exception { String testFile = "path-to-test-file.bb"; String chr = "chr7"; int start = 26133475; int end = 27333475; Genome genome = null; BBFileReader reader = new BBFileReader(testFile); MethylDataSource source = new BBMethylDataSource(reader, BBMethylDataSource.Type.ZILLER, genome); Iterator<MethylScore> iter = source.query(chr, start, end); // Should find at least 1 score. assertTrue(iter.hasNext()); while (iter.hasNext()) { MethylScore score = iter.next(); assertTrue(score.getEnd() >= start && score.getStart() <= end); assertTrue(score.getScore() >= 0 && score.getScore() <= 100); } }
if (!isBigWigFile()) return null; RPChromosomeRegion selectionRegion = getChromosomeBounds(startChromosome, startBase, endChromosome, endBase);
String endChromosome, int endBase, boolean contained) { if (!isBigBedFile()) return null; RPChromosomeRegion selectionRegion = getChromosomeBounds(startChromosome, startBase, endChromosome, endBase);
/** * Get the iterator for the given region. * * @param region Region target * @return Big Wig file iterator */ public BigWigIterator iterator(Region region) { return bbFileReader.getBigWigIterator(region.getChromosome(), region.getStart(), region.getChromosome(), region.getEnd(), false); }
int maxChromId = reader.getChromosomeNames().size() - 1; String firstChr = reader.getChromsomeFromId(0); String lastChr = reader.getChromsomeFromId(maxChromId); ZoomLevelIterator zlIter = reader.getZoomLevelIterator( lowestResHeader.getZoomLevel(), firstChr, 0, lastChr, Integer.MAX_VALUE, false);
protected List<LocusScore> getZoomSummaryScores(String chr, int start, int end, int zoom) { Chromosome c = genome.getChromosome(chr); if (c == null) return null; double nBins = Math.pow(2, zoom); double scale = c.getLength() / (nBins * 700); BBZoomLevelHeader zlHeader = getZoomLevelForScale(scale); if (zlHeader == null) return null; int bbLevel = zlHeader.getZoomLevel(); int reductionLevel = zlHeader.getReductionLevel(); // If we are at the highest precomputed resolution compare to the requested resolution. If they differ // by more than a factor of 2 compute "on the fly" String tmp = chrNameMap.get(chr); String querySeq = tmp == null ? chr : tmp; if (reader.isBigBedFile() || bbLevel > 1 || (bbLevel == 1 && (reductionLevel / scale) < 2)) { ArrayList<LocusScore> scores = new ArrayList(1000); ZoomLevelIterator zlIter = reader.getZoomLevelIterator(bbLevel, querySeq, start, querySeq, end, false); while (zlIter.hasNext()) { ZoomDataRecord rec = zlIter.next(); float v = getValue(rec); BasicScore bs = new BasicScore(rec.getChromStart(), rec.getChromEnd(), v); scores.add(bs); } return scores; } else { // No precomputed scores for this resolution level return null; } }
@Override public Set<String> getReferenceNames() { Set<String> result = new HashSet<String>(); for (String chr: bbReader.getChromosomeNames()) { result.add(chr); } return result; }
/** * Returns an iterator for BigWig values for all chromosome regions. * <p/> * Note: the BBFile type should be BigWig; else a null iterator is returned. * <p/> * Returns: * Iterator to provide BedFeature(s) for all chromosome regions. * Error conditions: * 1) An empty iterator is returned if region has no data available * 2) A null object is returned if the file is not BigWig.(see isBigWigFile method) */ public BigWigIterator getBigWigIterator() { if (!isBigWigFile()) return null; // get all regions bounds RPChromosomeRegion selectionRegion = chromosomeDataTree.getChromosomeBounds(); // compose an iterator boolean contained = true; // all regions are contained BigWigIterator wigIterator = new BigWigIterator(fis, chromosomeIDTree, chromosomeDataTree, selectionRegion, contained); return wigIterator; }
/** * Returns an iterator for BigBed features for all chromosome regions. * <p/> * Note: the BBFile type should be BigBed; else a null iterator is returned. * <p/> * Returns: * Iterator to provide BedFeature(s) for all chromosome regions. * Error conditions: * 1) An empty iterator is returned if region has no data available * 2) A null object is returned if the file is not BigBed.(see isBigBedFile method) */ public BigBedIterator getBigBedIterator() { if (!isBigBedFile()) return null; // get all region bounds RPChromosomeRegion selectionRegion = chromosomeDataTree.getChromosomeBounds(); // compose an iterator boolean contained = true; /// all regions are contained BigBedIterator bedIterator = new BigBedIterator(fis, chromosomeIDTree, chromosomeDataTree, selectionRegion, contained); return bedIterator; }
@Override public void close(){ if(this.bigWigReader != null){ this.bigWigReader.close(); } }