Search in sources :

Example 1 with BigWigIterator

use of org.broad.igv.bbfile.BigWigIterator in project ASCIIGenome by dariober.

the class Utils method initRegionFromBigWig.

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();
    }
    // Just get chrom to start with
    String region = reader.getChromosomeNames().get(0);
    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;
}
Also used : WigItem(org.broad.igv.bbfile.WigItem) BBFileReader(org.broad.igv.bbfile.BBFileReader) BigWigIterator(org.broad.igv.bbfile.BigWigIterator)

Example 2 with BigWigIterator

use of org.broad.igv.bbfile.BigWigIterator in project jvarkit by lindenb.

the class VcfEnsemblReg method annotate.

private void annotate(Track track, File inf, File outf) throws IOException {
    boolean contained = false;
    LOG.info("Processing " + track.id + " (" + track.shortLabel + ") " + track.url);
    VcfIterator in = VCFUtils.createVcfIteratorFromFile(inf);
    VCFHeader header = in.getHeader();
    VCFInfoHeaderLine info = null;
    SeekableStream sstream = SeekableStreamFactory.getInstance().getStreamFor(track.url);
    BBFileReader bigFile = new BBFileReader(track.url.toString(), new SeekableStreamAdaptor(sstream));
    VariantContextWriter w1 = VCFUtils.createVariantContextWriter(outf);
    if (bigFile.isBigWigFile()) {
        info = new VCFInfoHeaderLine(track.id, 1, VCFHeaderLineType.Float, String.valueOf(track.longLabel) + " " + track.url);
    } else {
        info = new VCFInfoHeaderLine(track.id, 1, VCFHeaderLineType.String, String.valueOf(track.longLabel) + " " + track.url);
    }
    header.addMetaDataLine(info);
    w1.writeHeader(in.getHeader());
    while (in.hasNext()) {
        VariantContext ctx = in.next();
        String chrom = ctx.getContig();
        if (!chrom.startsWith("chr"))
            chrom = "chr" + chrom;
        if (!chrom.matches("(chrX|chrY|chr[0-9]|chr1[0-9]|chr2[12])")) {
            w1.add(ctx);
        } else if (bigFile.isBigWigFile()) {
            BigWigIterator iter = bigFile.getBigWigIterator(chrom, ctx.getStart() - 1, chrom, ctx.getStart(), contained);
            Float wigValue = null;
            while (iter != null && iter.hasNext() && wigValue == null) {
                WigItem item = iter.next();
                wigValue = item.getWigValue();
            }
            if (wigValue == null) {
                w1.add(ctx);
                continue;
            }
            VariantContextBuilder vcb = new VariantContextBuilder(ctx);
            vcb.attribute(track.id, wigValue);
            w1.add(vcb.make());
        } else {
            BigBedIterator iter = bigFile.getBigBedIterator(chrom, ctx.getStart() - 1, chrom, ctx.getStart(), contained);
            Set<String> bedValues = new HashSet<String>();
            while (iter != null && iter.hasNext()) {
                BedFeature item = iter.next();
                String[] rest = item.getRestOfFields();
                if (rest == null || rest.length != 6) {
                    System.err.println(track.id + " " + Arrays.toString(item.getRestOfFields()));
                    continue;
                }
                String color = null;
                if (track.parent != null) {
                    if (track.parent.startsWith("Segway_17SegmentationSummaries")) {
                        color = segway_17SegmentationSummaries(rest[5]);
                    } else if (track.parent.startsWith("ProjectedSegments")) {
                        color = projectedSegments(rest[5]);
                    } else if (track.parent.startsWith("RegBuildOverview")) {
                        color = regBuildOverview(rest[5]);
                    } else if (track.parent.startsWith("Segway_17CellSegments")) {
                        color = segway_17CellSegments(rest[5]);
                    } else {
                        System.err.println("Unknown parent:" + track.parent);
                    }
                }
                if (color == null)
                    continue;
                bedValues.add(rest[0] + "|" + color);
            }
            if (bedValues.isEmpty()) {
                w1.add(ctx);
                continue;
            }
            StringBuilder sb = new StringBuilder();
            for (String s : bedValues) {
                if (sb.length() != 0)
                    sb.append(",");
                sb.append(s);
            }
            VariantContextBuilder vcb = new VariantContextBuilder(ctx);
            vcb.attribute(track.id, sb.toString());
            w1.add(vcb.make());
        }
    }
    sstream.close();
    in.close();
    w1.close();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) SeekableStream(htsjdk.samtools.seekablestream.SeekableStream) VariantContext(htsjdk.variant.variantcontext.VariantContext) BigBedIterator(org.broad.igv.bbfile.BigBedIterator) BedFeature(org.broad.igv.bbfile.BedFeature) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) WigItem(org.broad.igv.bbfile.WigItem) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) SeekableStreamAdaptor(com.github.lindenb.jvarkit.util.igv.SeekableStreamAdaptor) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) BBFileReader(org.broad.igv.bbfile.BBFileReader) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) BigWigIterator(org.broad.igv.bbfile.BigWigIterator)

Example 3 with BigWigIterator

use of org.broad.igv.bbfile.BigWigIterator in project ASCIIGenome by dariober.

the class TrackWiggles method bigWigToScores.

/**
 * 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++) {
            // Where should this position be mapped on screen?
            int idx = Utils.getIndexOfclosestValue(i, this.getGc().getMapping());
            screenWigLocInfoList.get(idx).increment(bw.getWigValue());
        }
    }
    List<Float> screenScores = new ArrayList<Float>();
    for (ScreenWiggleLocusInfo x : screenWigLocInfoList) {
        screenScores.add((float) x.getMeanScore());
    }
    this.setScreenScores(screenScores);
}
Also used : WigItem(org.broad.igv.bbfile.WigItem) ArrayList(java.util.ArrayList) BigWigIterator(org.broad.igv.bbfile.BigWigIterator)

Example 4 with BigWigIterator

use of org.broad.igv.bbfile.BigWigIterator in project ASCIIGenome by dariober.

the class TrackWigglesTest method canReadBigWigFromRemote.

@Test
public void canReadBigWigFromRemote() throws IOException {
    // String urlStr= "http://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/wgEncodeHaibTfbs/wgEncodeHaibTfbsA549Atf3V0422111Etoh02RawRep1.bigWig";
    String urlStr = "http://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/wgEncodeHaibTfbs/wgEncodeHaibTfbsA549Cebpbsc150V0422111RawRep1.bigWig";
    BBFileReader reader = new BBFileReader(urlStr);
    System.out.println(reader.getChromosomeNames());
    BigWigIterator iter = reader.getBigWigIterator("chr1", 1000000, "chr1", 2000000, true);
    while (iter.hasNext()) {
        System.out.println(iter.next().getStartBase());
    }
    System.out.println("NEW");
    iter = reader.getBigWigIterator("chr10", 1000000, "chr10", 2000000, true);
    while (iter.hasNext()) {
        System.out.println(iter.next().getStartBase());
    }
    reader.close();
}
Also used : BBFileReader(org.broad.igv.bbfile.BBFileReader) BigWigIterator(org.broad.igv.bbfile.BigWigIterator) Test(org.junit.Test)

Example 5 with BigWigIterator

use of org.broad.igv.bbfile.BigWigIterator in project jvarkit by lindenb.

the class Biostar105754 method run.

private void run(final BufferedReader r) throws IOException {
    final BedLineCodec codec = new BedLineCodec();
    String line;
    while ((line = r.readLine()) != null && !this.out.checkError()) {
        final BedLine bedLine = codec.decode(line);
        if (bedLine == null) {
            continue;
        }
        final String chrom = bedLine.getContig();
        int chromStart0 = bedLine.getStart() - 1;
        int chromEnd0 = bedLine.getEnd();
        if (chrom.isEmpty() || chromStart0 < 0L || chromEnd0 < chromStart0) {
            System.err.println("Bad BED line: " + line);
            continue;
        }
        // extends bed area until something was found
        int chromStart = chromStart0;
        int chromEnd = chromEnd0;
        for (; ; ) {
            BigWigIterator iter = this.bbFileReader.getBigWigIterator(chrom, chromStart, chrom, chromEnd, false);
            if (iter != null) {
                WigItem best = null;
                while (iter.hasNext()) {
                    WigItem wigItem = iter.next();
                    if (best == null || distance(chromStart, chromEnd, best.getStartBase(), best.getEndBase()) > distance(chromStart, chromEnd, wigItem.getStartBase(), wigItem.getEndBase())) {
                        best = wigItem;
                    }
                }
                if (best != null) {
                    this.out.print(best.getChromosome());
                    this.out.print("\t");
                    this.out.print(best.getStartBase());
                    this.out.print("\t");
                    this.out.print(best.getEndBase());
                    this.out.print("\t");
                    this.out.print(best.getWigValue());
                    this.out.print("\t");
                    this.out.print(line);
                    this.out.println();
                    break;
                }
            }
            // extend bed area
            long start2 = chromStart - EXTEND_SHIFT;
            long end2 = chromEnd + EXTEND_SHIFT;
            if (start2 < 0)
                start2 = 0;
            if (end2 > MAX_CHROM_END)
                end2 = MAX_CHROM_END;
            // too wide, break loop
            if (start2 == 0 && end2 == MAX_CHROM_END) {
                LOG.warn("no data found for\t" + line);
                break;
            }
            chromStart = (int) start2;
            chromEnd = (int) end2;
        }
    }
}
Also used : WigItem(org.broad.igv.bbfile.WigItem) BedLineCodec(com.github.lindenb.jvarkit.util.bio.bed.BedLineCodec) BedLine(com.github.lindenb.jvarkit.util.bio.bed.BedLine) BigWigIterator(org.broad.igv.bbfile.BigWigIterator)

Aggregations

BigWigIterator (org.broad.igv.bbfile.BigWigIterator)5 WigItem (org.broad.igv.bbfile.WigItem)4 BBFileReader (org.broad.igv.bbfile.BBFileReader)3 BedLine (com.github.lindenb.jvarkit.util.bio.bed.BedLine)1 BedLineCodec (com.github.lindenb.jvarkit.util.bio.bed.BedLineCodec)1 SeekableStreamAdaptor (com.github.lindenb.jvarkit.util.igv.SeekableStreamAdaptor)1 VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)1 SeekableStream (htsjdk.samtools.seekablestream.SeekableStream)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)1 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)1 VCFHeader (htsjdk.variant.vcf.VCFHeader)1 VCFInfoHeaderLine (htsjdk.variant.vcf.VCFInfoHeaderLine)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BedFeature (org.broad.igv.bbfile.BedFeature)1 BigBedIterator (org.broad.igv.bbfile.BigBedIterator)1 Test (org.junit.Test)1