Search in sources :

Example 1 with MakeTabixIndex

use of sortBgzipIndex.MakeTabixIndex in project ASCIIGenome by dariober.

the class TrackWiggles method tabixBedgraphToTmpFile.

private String tabixBedgraphToTmpFile(String inBdg) throws IOException, ClassNotFoundException, InvalidRecordException, SQLException {
    File tmp = Utils.createTempFile(".asciigenome." + new File(inBdg).getName() + ".", ".bedGraph.gz");
    File tmpTbi = new File(tmp.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
    tmp.deleteOnExit();
    tmpTbi.deleteOnExit();
    new MakeTabixIndex(inBdg, tmp, TabixFormat.BED);
    return tmp.getAbsolutePath();
}
Also used : MakeTabixIndex(sortBgzipIndex.MakeTabixIndex) File(java.io.File)

Example 2 with MakeTabixIndex

use of sortBgzipIndex.MakeTabixIndex 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)

Example 3 with MakeTabixIndex

use of sortBgzipIndex.MakeTabixIndex in project ASCIIGenome by dariober.

the class TrackBookmark method addBookmark.

/**
 * Add current genomic position to track.
 * @throws IOException
 * @throws SQLException
 * @throws InvalidRecordException
 * @throws ClassNotFoundException
 * @throws InvalidGenomicCoordsException
 */
@Override
public void addBookmark(GenomicCoords gc, String nameForBookmark) throws IOException, ClassNotFoundException, InvalidRecordException, SQLException, InvalidGenomicCoordsException {
    // Adding a bookmark means re-preparing the bgzip file again.
    // First write the current position to a new tmp file, then append to this file
    // all the bookmarks previously added. Then bgzip and compress replacing the old bookmark file.
    File plainNew = new File(this.getWorkFilename() + ".add");
    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.contains("\t__ignore_me__")) {
            // Hack to circumvent issue #38
            continue;
        }
        wr.write(line + "\n");
    }
    // Add new bookamrk
    wr.write(this.positionToGffLine(gc, nameForBookmark) + "\n");
    br.close();
    wr.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 : GZIPInputStream(java.util.zip.GZIPInputStream) 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) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) TabixReader(htsjdk.tribble.readers.TabixReader) BufferedReader(java.io.BufferedReader) File(java.io.File) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter)

Example 4 with MakeTabixIndex

use of sortBgzipIndex.MakeTabixIndex in project ASCIIGenome by dariober.

the class TrackSeqRegex method findRegex.

/**
 * Find regex matches in current genomic interval and update the IntervalFeature set and list.
 */
private void findRegex() throws IOException, InvalidGenomicCoordsException, ClassNotFoundException, InvalidRecordException, SQLException {
    // Find matches
    // ============
    Pattern pattern = Pattern.compile(this.seqRegex, Pattern.CASE_INSENSITIVE);
    if (this.isCaseSensitive) {
        pattern = Pattern.compile(this.seqRegex);
    }
    byte[] seq = this.getGc().getSequenceFromFasta();
    Matcher matcher = pattern.matcher(new String(seq));
    // One list for matches on forward, one for reverse, one for palindromic
    Set<String> regionListPos = new HashSet<String>();
    Set<String> regionListNeg = new HashSet<String>();
    Set<String> regionListPalind = new HashSet<String>();
    // Forward match
    while (matcher.find()) {
        int matchStart = this.getGc().getFrom() + matcher.start() - 1;
        int matchEnd = this.getGc().getFrom() + matcher.end() - 1;
        String reg = this.getGc().getChrom() + "\t" + matchStart + "\t" + matchEnd + "\t" + this.trimMatch(matcher.group(), 100);
        regionListPos.add(reg);
    }
    // Reverse comp match
    SequenceUtil.reverseComplement(seq);
    matcher = pattern.matcher(new String(seq));
    while (matcher.find()) {
        int matchStart = this.getGc().getTo() - matcher.end();
        int matchEnd = this.getGc().getTo() - matcher.start();
        String reg = this.getGc().getChrom() + "\t" + matchStart + "\t" + matchEnd + "\t" + this.trimMatch(matcher.group(), 100);
        if (regionListPos.contains(reg)) {
            regionListPos.remove(reg);
            regionListPalind.add(reg);
        } else {
            regionListNeg.add(reg);
        }
    }
    // Prepare tmp file
    // ================
    File tmpfile = Utils.createTempFile(".asciigenome.regexMatchTrack", ".tmp.bed");
    String tmpname = tmpfile.getAbsolutePath();
    // This is a hack: Copy the newly created tmp file to another file. This overcomes some
    // permission (?) problems later with the tabix indexing.
    String regexMatchFile = tmpname.replaceAll("\\.tmp\\.bed$", ".bed");
    FileUtils.copyFile(new File(tmpname), new File(regexMatchFile));
    new File(tmpname).delete();
    new File(regexMatchFile).deleteOnExit();
    BufferedWriter wr = new BufferedWriter(new FileWriter(new File(regexMatchFile)));
    for (String reg : regionListPos) {
        reg += "\t.\t+\n";
        if (this.featureIsVisible(reg)) {
            wr.write(reg);
        }
    }
    for (String reg : regionListNeg) {
        reg += "\t.\t-\n";
        if (this.featureIsVisible(reg)) {
            wr.write(reg);
        }
    }
    for (String reg : regionListPalind) {
        reg += "\t.\t.\n";
        if (this.featureIsVisible(reg)) {
            wr.write(reg);
        }
    }
    wr.close();
    // Compress, index, read back as list of IntervalFeatures
    // ======================================================
    File regexMatchBgzip = new File(regexMatchFile + ".gz");
    File regexMatchIndex = new File(regexMatchFile + ".gz.tbi");
    regexMatchBgzip.deleteOnExit();
    regexMatchIndex.deleteOnExit();
    new MakeTabixIndex(regexMatchFile, regexMatchBgzip, TabixFormat.BED);
    new File(regexMatchFile).delete();
    TrackIntervalFeature regexMatchTrack = new TrackIntervalFeature(regexMatchBgzip.getAbsolutePath(), this.getGc());
    regexMatchBgzip.delete();
    regexMatchIndex.delete();
    this.intervalFeatureList = regexMatchTrack.getIntervalFeatureList();
}
Also used : MakeTabixIndex(sortBgzipIndex.MakeTabixIndex) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileWriter(java.io.FileWriter) File(java.io.File) HashSet(java.util.HashSet) BufferedWriter(java.io.BufferedWriter)

Aggregations

File (java.io.File)4 MakeTabixIndex (sortBgzipIndex.MakeTabixIndex)4 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 TabixReader (htsjdk.tribble.readers.TabixReader)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1