Search in sources :

Example 1 with Iterator

use of htsjdk.tribble.readers.TabixReader.Iterator in project ASCIIGenome by dariober.

the class MakeTabixFileTest method canCompressAndIndexHeaderlessVCF.

@Test
public void canCompressAndIndexHeaderlessVCF() throws ClassNotFoundException, IOException, InvalidRecordException, SQLException {
    String infile = "test_data/noheader.vcf";
    File outfile = new File("test_data/noheader.vcf.gz");
    outfile.deleteOnExit();
    File expectedTbi = new File(outfile.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
    expectedTbi.deleteOnExit();
    new MakeTabixIndex(infile, outfile, TabixFormat.VCF);
    assertTrue(outfile.exists());
    assertTrue(outfile.length() > 200);
    assertTrue(expectedTbi.exists());
    assertTrue(expectedTbi.length() > 100);
    TabixReader tbx = new TabixReader(outfile.getAbsolutePath());
    Iterator x = tbx.query("1", 1, 10000000);
    assertTrue(x.next().startsWith("1"));
}
Also used : TabixReader(htsjdk.tribble.readers.TabixReader) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterator(htsjdk.tribble.readers.TabixReader.Iterator) File(java.io.File) Test(org.junit.Test)

Example 2 with Iterator

use of htsjdk.tribble.readers.TabixReader.Iterator in project ASCIIGenome by dariober.

the class MakeTabixFileTest method canCompressAndIndexVCF.

@Test
public void canCompressAndIndexVCF() throws ClassNotFoundException, IOException, InvalidRecordException, SQLException {
    String infile = "test_data/CHD.exon.2010_03.sites.unsorted.vcf";
    File outfile = new File("test_data/tmp6.bed.gz");
    outfile.deleteOnExit();
    File expectedTbi = new File(outfile.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
    expectedTbi.deleteOnExit();
    new MakeTabixIndex(infile, outfile, TabixFormat.VCF);
    assertTrue(outfile.exists());
    assertTrue(outfile.length() > 1000);
    assertTrue(expectedTbi.exists());
    assertTrue(expectedTbi.length() > 1000);
    TabixReader tbx = new TabixReader(outfile.getAbsolutePath());
    Iterator x = tbx.query("1", 20000000, 30000000);
    assertTrue(x.next().startsWith("1"));
    // Check you can read ok
    this.vcfTester(outfile.getAbsolutePath());
}
Also used : TabixReader(htsjdk.tribble.readers.TabixReader) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterator(htsjdk.tribble.readers.TabixReader.Iterator) File(java.io.File) Test(org.junit.Test)

Example 3 with Iterator

use of htsjdk.tribble.readers.TabixReader.Iterator in project ASCIIGenome by dariober.

the class TrackWiggles method bedGraphToScores.

/**
 * Get values for bedgraph
 * @throws InvalidRecordException
 * @throws InvalidGenomicCoordsException
 */
private void bedGraphToScores(String fileName) throws IOException, InvalidRecordException, InvalidGenomicCoordsException {
    List<ScreenWiggleLocusInfo> screenWigLocInfoList = new ArrayList<ScreenWiggleLocusInfo>();
    for (int i = 0; i < getGc().getUserWindowSize(); i++) {
        screenWigLocInfoList.add(new ScreenWiggleLocusInfo());
    }
    try {
        TabixReader tabixReader = new TabixReader(fileName);
        Iterator qry = tabixReader.query(this.getGc().getChrom(), this.getGc().getFrom() - 1, this.getGc().getTo());
        while (true) {
            String q = qry.next();
            if (q == null) {
                break;
            }
            if (q.contains("\t__ignore_me__")) {
                // Hack to circumvent issue #38
                continue;
            }
            if (!this.isValidBedGraphLine(q)) {
                continue;
            }
            String[] tokens = q.split("\t");
            int screenFrom = Utils.getIndexOfclosestValue(Integer.valueOf(tokens[1]) + 1, this.getGc().getMapping());
            int screenTo = Utils.getIndexOfclosestValue(Integer.valueOf(tokens[2]), this.getGc().getMapping());
            float value = Float.valueOf(tokens[this.bdgDataColIdx - 1]);
            for (int i = screenFrom; i <= screenTo; i++) {
                screenWigLocInfoList.get(i).increment(value);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("Could not open tabix file: " + fileName);
        System.err.println("Is the file sorted and indexed? After sorting by position (sort e.g. -k1,1 -k2,2n), compress with bgzip and index with e.g.:");
        System.err.println("\nbgzip " + fileName);
        System.err.println("tabix -p bed " + fileName + "\n");
    }
    List<Float> screenScores = new ArrayList<Float>();
    for (ScreenWiggleLocusInfo x : screenWigLocInfoList) {
        screenScores.add((float) x.getMeanScore());
    }
    this.setScreenScores(screenScores);
    return;
}
Also used : TabixReader(htsjdk.tribble.readers.TabixReader) ArrayList(java.util.ArrayList) Iterator(htsjdk.tribble.readers.TabixReader.Iterator) BigWigIterator(org.broad.igv.bbfile.BigWigIterator) IOException(java.io.IOException)

Example 4 with Iterator

use of htsjdk.tribble.readers.TabixReader.Iterator in project ASCIIGenome by dariober.

the class MakeTabixFileTest method canCompressAndIndexSortedFile.

@Test
public void canCompressAndIndexSortedFile() throws IOException, InvalidRecordException, ClassNotFoundException, SQLException {
    String infile = "test_data/overlapped.bed";
    File outfile = new File("test_data/tmp.bed.gz");
    outfile.deleteOnExit();
    File expectedTbi = new File(outfile.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
    expectedTbi.deleteOnExit();
    new MakeTabixIndex(infile, outfile, TabixFormat.BED);
    assertTrue(outfile.exists());
    assertTrue(outfile.length() > 80);
    assertTrue(expectedTbi.exists());
    assertTrue(expectedTbi.length() > 80);
    TabixReader tbx = new TabixReader(outfile.getAbsolutePath());
    Iterator x = tbx.query("chr1", 1, 1000000);
    assertTrue(x.next().startsWith("chr1"));
}
Also used : TabixReader(htsjdk.tribble.readers.TabixReader) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterator(htsjdk.tribble.readers.TabixReader.Iterator) File(java.io.File) Test(org.junit.Test)

Example 5 with Iterator

use of htsjdk.tribble.readers.TabixReader.Iterator in project ASCIIGenome by dariober.

the class MakeTabixFileTest method canCompressAndIndexSortedGzipFile.

@Test
public void canCompressAndIndexSortedGzipFile() throws IOException, InvalidRecordException, ClassNotFoundException, SQLException {
    String infile = "test_data/hg19_genes.gtf.gz";
    File outfile = new File("test_data/tmp2.bed.gz");
    outfile.deleteOnExit();
    File expectedTbi = new File(outfile.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
    expectedTbi.deleteOnExit();
    new MakeTabixIndex(infile, outfile, TabixFormat.GFF);
    assertTrue(outfile.exists());
    assertTrue(outfile.length() > 7000000);
    assertTrue(expectedTbi.exists());
    assertTrue(expectedTbi.length() > 500000);
    TabixReader tbx = new TabixReader(outfile.getAbsolutePath());
    Iterator x = tbx.query("chr1", 1, 1000000);
    assertTrue(x.next().startsWith("chr1"));
}
Also used : TabixReader(htsjdk.tribble.readers.TabixReader) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterator(htsjdk.tribble.readers.TabixReader.Iterator) File(java.io.File) Test(org.junit.Test)

Aggregations

TabixReader (htsjdk.tribble.readers.TabixReader)5 Iterator (htsjdk.tribble.readers.TabixReader.Iterator)5 CloseableIterator (htsjdk.samtools.util.CloseableIterator)4 File (java.io.File)4 Test (org.junit.Test)4 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 BigWigIterator (org.broad.igv.bbfile.BigWigIterator)1