endChunk = wigItem.getEndBase() / chunkSize; partial = Math.min(wigItem.getEndBase(), pos + chunkSize) - Math.max(wigItem.getStartBase(), pos); values.add((int) (partial * wigItem.getWigValue()));
@Override public List<GenericContinuousRecord> getRecords(String ref, RangeAdapter range, Resolution resolution, RecordFilterAdapter filt) throws IOException, InterruptedException { List<GenericContinuousRecord> result = new ArrayList<GenericContinuousRecord>(); try { BigWigIterator wigIterator = bbReader.getBigWigIterator(ref, range.getFrom(), ref, range.getTo(), false); int nextPos = range.getFrom(); int rangeEnd = range.getTo(); while (wigIterator.hasNext()) { WigItem wig = wigIterator.next(); int datumStart = wig.getStartBase(); int datumEnd = wig.getEndBase(); float value = wig.getWigValue(); while (nextPos < datumStart && nextPos <= rangeEnd) { result.add(GenericContinuousRecord.valueOf(ref, nextPos, Float.NaN)); nextPos++; } while (nextPos < datumEnd && nextPos <= rangeEnd) { result.add(GenericContinuousRecord.valueOf(ref, nextPos, value)); nextPos++; } if (Thread.interrupted()) { throw new InterruptedException(); } } } catch (RuntimeException ignored) { // If BigWig reader has no data in the given range, it throws a RuntimeException. Really? Who does that? } return result; }
@Override protected synchronized DataTile getRawData(String chr, int start, int end) { if (chr.equals(Globals.CHR_ALL)) { return null; } if (currentInterval != null && currentInterval.contains(chr, start, end)) { return currentInterval.tile; } // TODO -- fetch data directly in arrays to avoid creation of multiple "WigItem" objects? IntArrayList startsList = new IntArrayList(100000); IntArrayList endsList = new IntArrayList(100000); FloatArrayList valuesList = new FloatArrayList(100000); String chrAlias = chrNameMap.containsKey(chr) ? chrNameMap.get(chr) : chr; Iterator<WigItem> iter = reader.getBigWigIterator(chrAlias, start, chrAlias, end, false); while (iter.hasNext()) { WigItem wi = iter.next(); startsList.add(wi.getStartBase()); endsList.add(wi.getEndBase()); valuesList.add(wi.getWigValue()); } DataTile tile = new DataTile(startsList.toArray(), endsList.toArray(), valuesList.toArray(), null); currentInterval = new RawDataInterval(chr, start, end, tile); return tile; }
wItem = bigWigIterator.next(); chunkStart = (Math.max(region.getStart(), wItem.getStartBase()) - region.getStart()) / windowSize; chunkEnd = (Math.min(region.getEnd(), wItem.getEndBase()) - region.getStart() - 1) / windowSize; for (int chunk = chunkStart; chunk <= chunkEnd; chunk++) { length = Math.min(wItem.getEndBase() - region.getStart(), chunk * windowSize + windowSize) - Math.max(wItem.getStartBase() - region.getStart(), chunk * windowSize); chunks[chunk] += (wItem.getWigValue() * length);
/** Populate object using bigWig data * @throws IOException * @throws InvalidGenomicCoordsException */ private void bigWigToScores(BBFileReader reader) throws InvalidGenomicCoordsException, IOException{ // List of length equal to screen size. Each inner map contains info about the screen locus List<ScreenWiggleLocusInfo> screenWigLocInfoList= new ArrayList<ScreenWiggleLocusInfo>(); for(int i= 0; i < getGc().getUserWindowSize(); i++){ screenWigLocInfoList.add(new ScreenWiggleLocusInfo()); } BigWigIterator iter = reader.getBigWigIterator(getGc().getChrom(), getGc().getFrom(), getGc().getChrom(), getGc().getTo(), false); while(iter.hasNext()){ WigItem bw = iter.next(); for(int i= bw.getStartBase(); i <= bw.getEndBase(); i++){ int idx= Utils.getIndexOfclosestValue(i, this.getGc().getMapping()); // Where should this position be mapped on screen? screenWigLocInfoList.get(idx).increment(bw.getWigValue()); } } List<Float> screenScores= new ArrayList<Float>(); for(ScreenWiggleLocusInfo x : screenWigLocInfoList){ screenScores.add((float)x.getMeanScore()); } this.setScreenScores(screenScores); }
/** * Query by a given region. * * @param region Region target * @return Array of floating values for that region * @throws IOException */ public float[] query(Region region) throws IOException { BigWigIterator bigWigIterator = iterator(region); float[] values = new float[region.getEnd() - region.getStart() + 1]; while (bigWigIterator.hasNext()) { WigItem wigItem = bigWigIterator.next(); for (int i = wigItem.getStartBase(), j = wigItem.getStartBase() - region.getStart(); i <= wigItem.getEndBase(); i++, j++) { values[j] = wigItem.getWigValue(); } } return values; }