Search in sources :

Example 1 with TabixReader

use of htsjdk.tribble.readers.TabixReader 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 TabixReader

use of htsjdk.tribble.readers.TabixReader 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 TabixReader

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

the class Utils method hasTabixIndex.

// public static LinkedHashMap<String, Integer> xterm256ColorCodes(){
// // See http://misc.flogisoft.com/bash/tip_colors_and_formatting
// // From http://jonasjacek.github.io/colors/
// LinkedHashMap<String, Integer> colourCodes= new LinkedHashMap<String, Integer>();
// colourCodes.put("default", 39);
// colourCodes.put("black", 30);
// colourCodes.put("red", 31);
// colourCodes.put("green", 32);
// colourCodes.put("yellow", 33);
// colourCodes.put("blue", 34);
// colourCodes.put("magenta", 35);
// colourCodes.put("cyan", 36);
// colourCodes.put("light_grey", 37);
// colourCodes.put("grey", 90);
// colourCodes.put("light_red", 91);
// colourCodes.put("light_green", 92);
// colourCodes.put("light_yellow", 93);
// colourCodes.put("light_blue", 94);
// colourCodes.put("light_magenta", 95);
// colourCodes.put("light_cyan", 96);
// colourCodes.put("white", 97);
// // To be continued
// return colourCodes;
// }
// public static Color ansiColourToGraphicsColor(int ansiColor) throws InvalidColourException{
// 
// if(!xterm256ColorCodes().entrySet().contains(ansiColor)){
// throw new InvalidColourException();
// }
// if(ansiColor == 30){ return Color.BLACK; }
// if(ansiColor == 31 || ansiColor == 91){ return Color.RED; }
// if(ansiColor == 32 || ansiColor == 92){ return Color.GREEN; }
// if(ansiColor == 33 || ansiColor == 93){ return Color.YELLOW; }
// if(ansiColor == 34 || ansiColor == 94){ return Color.BLUE; }
// if(ansiColor == 35 || ansiColor == 95){ return Color.MAGENTA; }
// if(ansiColor == 36 || ansiColor == 96){ return Color.CYAN; }
// if(ansiColor == 37){ return Color.LIGHT_GRAY; }
// if(ansiColor == 90){ return Color.DARK_GRAY; }
// if(ansiColor == 97){ return Color.WHITE; }
// return Color.BLACK;
// }
/**
 * Return true if fileName has a valid tabix index.
 * @throws IOException
 */
public static boolean hasTabixIndex(String fileName) throws IOException {
    if ((new UrlValidator()).isValid(fileName) && fileName.startsWith("ftp")) {
        // Because of issue #51
        return false;
    }
    try {
        TabixReader tabixReader = new TabixReader(fileName);
        tabixReader.readLine();
        tabixReader.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}
Also used : TabixReader(htsjdk.tribble.readers.TabixReader) UrlValidator(org.apache.commons.validator.routines.UrlValidator) InvalidCommandLineException(exceptions.InvalidCommandLineException) InvalidRecordException(exceptions.InvalidRecordException) InvalidColourException(exceptions.InvalidColourException) SQLException(java.sql.SQLException) IOException(java.io.IOException) UnindexableFastaFileException(faidx.UnindexableFastaFileException) FileNotFoundException(java.io.FileNotFoundException) InvalidGenomicCoordsException(exceptions.InvalidGenomicCoordsException) MalformedURLException(java.net.MalformedURLException)

Example 4 with TabixReader

use of htsjdk.tribble.readers.TabixReader 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 5 with TabixReader

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

the class TrackBookmark method removeBookmark.

/**
 * Remove the bookmark matching the exact coordinates of the current position.
 * Bookmarks partially overlapping are not removed.
 * @param bookmarkRegion
 * @throws IOException
 * @throws SQLException
 * @throws InvalidRecordException
 * @throws ClassNotFoundException
 * @throws InvalidGenomicCoordsException
 */
public void removeBookmark(GenomicCoords bookmarkRegion) throws IOException, ClassNotFoundException, InvalidRecordException, SQLException, InvalidGenomicCoordsException {
    // To remove a bookmark, iterate through the bgzip file writing records to a tmp file.
    // The record(s) matching this position is not written.
    // 
    File plainNew = new File(this.getWorkFilename() + ".remove");
    plainNew.deleteOnExit();
    BufferedWriter wr = new BufferedWriter(new FileWriter(plainNew));
    InputStream fileStream = new FileInputStream(this.getWorkFilename());
    Reader decoder = new InputStreamReader(new GZIPInputStream(fileStream), "UTF-8");
    BufferedReader br = new BufferedReader(decoder);
    String line;
    while ((line = br.readLine()) != null) {
        if (!line.startsWith("#")) {
            // In case of comment lines
            List<String> pos = Lists.newArrayList(Splitter.on("\t").split(line));
            if (bookmarkRegion.getChrom().equals(pos.get(0)) && bookmarkRegion.getFrom() == Integer.parseInt(pos.get(3)) && bookmarkRegion.getTo() == Integer.parseInt(pos.get(4))) {
                continue;
            }
        }
        wr.write(line + "\n");
    }
    wr.close();
    br.close();
    // Recompress and index replacing the original bgzip file
    new MakeTabixIndex(plainNew.getAbsolutePath(), new File(this.getWorkFilename()), TabixFormat.GFF);
    plainNew.delete();
    this.tabixReader = new TabixReader(this.getWorkFilename());
    // Update track.
    this.update();
}
Also used : MakeTabixIndex(sortBgzipIndex.MakeTabixIndex) InputStreamReader(java.io.InputStreamReader) TabixReader(htsjdk.tribble.readers.TabixReader) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) TabixReader(htsjdk.tribble.readers.TabixReader) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedReader(java.io.BufferedReader) File(java.io.File)

Aggregations

TabixReader (htsjdk.tribble.readers.TabixReader)10 File (java.io.File)6 Iterator (htsjdk.tribble.readers.TabixReader.Iterator)5 CloseableIterator (htsjdk.samtools.util.CloseableIterator)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 BufferedReader (java.io.BufferedReader)3 VariantContext (htsjdk.variant.variantcontext.VariantContext)2 VCFHeader (htsjdk.variant.vcf.VCFHeader)2 BufferedWriter (java.io.BufferedWriter)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileWriter (java.io.FileWriter)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 MakeTabixIndex (sortBgzipIndex.MakeTabixIndex)2 InvalidColourException (exceptions.InvalidColourException)1 InvalidCommandLineException (exceptions.InvalidCommandLineException)1