Example usage for java.util.zip GZIPInputStream close

List of usage examples for java.util.zip GZIPInputStream close

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:be.ibridge.kettle.trans.step.sortrows.SortRows.java

private Row getBuffer() {
    int i, f;/*from w w  w  . j a v a  2 s .  c  o  m*/
    int smallest;
    Row r1, r2;
    Row retval;

    // Open all files at once and read one row from each file...
    if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
        logBasic("Opening " + data.files.size() + " tmp-files...");

        try {
            for (f = 0; f < data.files.size() && !isStopped(); f++) {
                FileObject fileObject = (FileObject) data.files.get(f);
                String filename = KettleVFS.getFilename(fileObject);
                if (log.isDetailed())
                    logDetailed("Opening tmp-file: [" + filename + "]");
                InputStream fi = fileObject.getContent().getInputStream();
                DataInputStream di;
                data.fis.add(fi);
                if (meta.getCompress()) {
                    GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi));
                    di = new DataInputStream(gzfi);
                    data.gzis.add(gzfi);
                } else {
                    di = new DataInputStream(fi);
                }
                data.dis.add(di);

                // How long is the buffer?
                int buffersize = di.readInt();

                if (log.isDetailed())
                    logDetailed("[" + filename + "] expecting " + buffersize + " rows...");

                if (buffersize > 0) {
                    // Read a row from each temp-file
                    Row metadata = (Row) data.rowMeta.get(f);
                    data.rowbuffer.add(new Row(di, metadata.size(), metadata)); // new row
                }
            }
        } catch (Exception e) {
            logError("Error reading back tmp-files : " + e.toString());
            e.printStackTrace();
        }
    }

    if (data.files.size() == 0) {
        if (data.buffer.size() > 0) {
            retval = (Row) data.buffer.get(0);
            data.buffer.remove(0);
        } else {
            retval = null;
        }
    } else {
        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            // We now have "filenr" rows waiting: which one is the smallest?
            //
            for (i = 0; i < data.rowbuffer.size() && !isStopped(); i++) {
                Row b = (Row) data.rowbuffer.get(i);
                if (log.isRowLevel())
                    logRowlevel("--BR#" + i + ": " + b.toString());
            }
            //

            smallest = 0;
            r1 = (Row) data.rowbuffer.get(smallest);
            for (f = 1; f < data.rowbuffer.size() && !isStopped(); f++) {
                r2 = (Row) data.rowbuffer.get(f);

                if (r2 != null && r2.compare(r1, data.fieldnrs, meta.getAscending()) < 0) {
                    smallest = f;
                    r1 = (Row) data.rowbuffer.get(smallest);
                }
            }
            retval = r1;

            data.rowbuffer.remove(smallest);
            if (log.isRowLevel())
                logRowlevel("Smallest row selected on [" + smallest + "] : " + retval);

            // now get another Row for position smallest

            FileObject file = (FileObject) data.files.get(smallest);
            DataInputStream di = (DataInputStream) data.dis.get(smallest);
            InputStream fi = (InputStream) data.fis.get(smallest);
            GZIPInputStream gzfi = (meta.getCompress()) ? (GZIPInputStream) data.gzis.get(smallest) : null;

            try {
                Row metadata = (Row) data.rowMeta.get(smallest);
                data.rowbuffer.add(smallest, new Row(di, metadata.size(), metadata));
            } catch (KettleFileException fe) // empty file or EOF mostly
            {
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null)
                        gzfi.close();
                    file.delete();
                } catch (IOException e) {
                    logError("Unable to close/delete file #" + smallest + " --> " + file.toString());
                    setErrors(1);
                    stopAll();
                    return null;
                }

                data.files.remove(smallest);
                data.dis.remove(smallest);
                data.fis.remove(smallest);
                if (gzfi != null)
                    data.gzis.remove(smallest);
                data.rowMeta.remove(smallest);
            }
        }
    }
    return retval;
}

From source file:com.panet.imeta.trans.steps.sort.SortRows.java

private Object[] getBuffer() throws KettleValueException {
    Object[] retval;/*from  w  ww.ja  v a 2  s .  co  m*/

    // Open all files at once and read one row from each file...
    if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
        if (log.isBasic())
            logBasic("Opening " + data.files.size() + " tmp-files...");

        try {
            for (int f = 0; f < data.files.size() && !isStopped(); f++) {
                FileObject fileObject = (FileObject) data.files.get(f);
                String filename = KettleVFS.getFilename(fileObject);
                if (log.isDetailed())
                    logDetailed("Opening tmp-file: [" + filename + "]");
                InputStream fi = KettleVFS.getInputStream(fileObject);
                DataInputStream di;
                data.fis.add(fi);
                if (data.compressFiles) {
                    GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi));
                    di = new DataInputStream(gzfi);
                    data.gzis.add(gzfi);
                } else {
                    di = new DataInputStream(new BufferedInputStream(fi, 50000));
                }
                data.dis.add(di);

                // How long is the buffer?
                int buffersize = data.bufferSizes.get(f);

                if (log.isDetailed())
                    logDetailed("[" + filename + "] expecting " + buffersize + " rows...");

                if (buffersize > 0) {
                    Object[] row = (Object[]) data.outputRowMeta.readData(di);
                    data.rowbuffer.add(row); // new row from input stream
                    data.tempRows.add(new RowTempFile(row, f));
                }
            }

            // Sort the data row buffer
            Collections.sort(data.tempRows, data.comparator);
        } catch (Exception e) {
            logError("Error reading back tmp-files : " + e.toString());
            logError(Const.getStackTracker(e));
        }
    }

    if (data.files.size() == 0) {
        if (data.getBufferIndex < data.buffer.size()) {
            retval = (Object[]) data.buffer.get(data.getBufferIndex);
            data.getBufferIndex++;
        } else {
            retval = null;
        }
    } else {
        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            // We now have "filenr" rows waiting: which one is the smallest?
            //
            if (log.isRowLevel()) {
                for (int i = 0; i < data.rowbuffer.size() && !isStopped(); i++) {
                    Object[] b = (Object[]) data.rowbuffer.get(i);
                    logRowlevel("--BR#" + i + ": " + data.outputRowMeta.getString(b));
                }
            }

            RowTempFile rowTempFile = data.tempRows.remove(0);
            retval = rowTempFile.row;
            int smallest = rowTempFile.fileNumber;

            // now get another Row for position smallest

            FileObject file = (FileObject) data.files.get(smallest);
            DataInputStream di = (DataInputStream) data.dis.get(smallest);
            InputStream fi = (InputStream) data.fis.get(smallest);
            GZIPInputStream gzfi = (data.compressFiles) ? (GZIPInputStream) data.gzis.get(smallest) : null;

            try {
                Object[] row2 = (Object[]) data.outputRowMeta.readData(di);
                RowTempFile extra = new RowTempFile(row2, smallest);

                int index = Collections.binarySearch(data.tempRows, extra, data.comparator);
                if (index < 0) {
                    data.tempRows.add(index * (-1) - 1, extra);
                } else {
                    data.tempRows.add(index, extra);
                }
            } catch (KettleFileException fe) // empty file or EOF mostly
            {
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null)
                        gzfi.close();
                    file.delete();
                } catch (IOException e) {
                    logError("Unable to close/delete file #" + smallest + " --> " + file.toString());
                    setErrors(1);
                    stopAll();
                    return null;
                }

                data.files.remove(smallest);
                data.dis.remove(smallest);
                data.fis.remove(smallest);

                if (gzfi != null)
                    data.gzis.remove(smallest);

                // Also update all file numbers in in data.tempRows if they are larger than smallest.
                //
                for (RowTempFile rtf : data.tempRows) {
                    if (rtf.fileNumber > smallest)
                        rtf.fileNumber--;
                }

            } catch (SocketTimeoutException e) {
                throw new KettleValueException(e); // should never happen on local files
            }
        }
    }
    return retval;
}

From source file:com.datatorrent.lib.io.fs.AbstractFileOutputOperatorTest.java

private void checkCompressedFile(File file, List<Long> offsets, int startVal, int totalWindows,
        int totalRecords, SecretKey secretKey, byte[] iv) throws IOException {
    FileInputStream fis;/*from   ww  w .j  a va2  s  .  co  m*/
    InputStream gss = null;
    GZIPInputStream gis = null;
    BufferedReader br = null;

    Cipher cipher = null;
    if (secretKey != null) {
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ivps = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivps);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    int numWindows = 0;
    try {
        fis = new FileInputStream(file);
        //fis.skip(startOffset);
        gss = fis;
        if (secretKey != null) {
            try {
                /*
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                IvParameterSpec ivps = new IvParameterSpec(iv);
                cipher.init(Cipher.DECRYPT_MODE, secretKey, ivps);
                */
                gss = new CipherInputStream(fis, cipher);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        long startOffset = 0;
        for (long offset : offsets) {
            // Skip initial case in case file is not yet created
            if (offset == 0) {
                continue;
            }
            long limit = offset - startOffset;
            LimitInputStream lis = new LimitInputStream(gss, limit);

            //gis = new GZIPInputStream(fis);
            gis = new GZIPInputStream(lis);
            br = new BufferedReader(new InputStreamReader(gis));
            //br = new BufferedReader(new InputStreamReader(gss));
            String eline = "" + (startVal + numWindows * 2);
            int count = 0;
            String line;
            while ((line = br.readLine()) != null) {
                Assert.assertEquals("File line", eline, line);
                ++count;
                if ((count % totalRecords) == 0) {
                    ++numWindows;
                    eline = "" + (startVal + numWindows * 2);
                }
            }
            startOffset = offset;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            br.close();
        } else {
            if (gis != null) {
                gis.close();
            } else if (gss != null) {
                gss.close();
            }
        }
    }
    Assert.assertEquals("Total", totalWindows, numWindows);
}

From source file:eu.eexcess.europeana.clickstream.EuropeanaQueryCollector.java

private void processDirectory(File dir, PrintWriter writer, QueryParser queryParser,
        Set<String> uniqueQueryCollector)
        throws IOException, FileNotFoundException, UnsupportedEncodingException {
    for (File file : dir.listFiles()) {
        if (!file.getName().endsWith(".gz")) {
            continue;
        }//from w w  w.j  a v a  2  s  .c o  m
        System.err.println("Parsing file: " + file);

        GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(file));
        LineIterator iterator = new LineIterator(new InputStreamReader(inputStream, "UTF-8"));
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            if (line.contains(QUERY_PREFIX) && !line.contains("query=DATA_PROVIDER")
                    && !line.contains("qf=DATA_PROVIDER") && !line.contains("bot") && !line.contains("slurp")
                    && !line.contains("spider")) {
                int start = line.indexOf(QUERY_PREFIX), end = line.indexOf("\"", start),
                        end2 = line.indexOf("&", start);
                if (end2 < end && end2 > 0) {
                    end = end2;
                }
                if (end < 0) {
                    end = line.length();
                }
                try {
                    String query = URLDecoder.decode(line.substring(start + QUERY_PREFIX.length(), end),
                            "UTF-8");
                    if (!query.contains(":")) {
                        Query parsedQuery = queryParser.parse(query);
                        if (parsedQuery instanceof BooleanQuery) {
                            List<BooleanClause> clauses = ((BooleanQuery) parsedQuery).clauses();
                            if (clauses != null) {
                                List<String> queryTerms = new ArrayList<String>();
                                boolean onlyTermQueries = true;
                                for (BooleanClause clause : clauses) {
                                    if (!(clause.getQuery() instanceof TermQuery)) {
                                        // there is at lease a single non term query
                                        onlyTermQueries = false;
                                        break;
                                    } else {
                                        TermQuery termQuery = (TermQuery) clause.getQuery();
                                        if (termQuery.getTerm().field().equals(DEFAULT_FIELD)) {
                                            queryTerms.add(termQuery.getTerm().text());
                                        }
                                    }
                                }

                                if (onlyTermQueries && queryTerms.size() == 2) {
                                    StringBuilder builder = new StringBuilder();
                                    for (String e : new TreeSet<String>(queryTerms)) {
                                        if (builder.length() > 0) {
                                            builder.append('\t');
                                        }
                                        builder.append(e);
                                    }
                                    // queryTerms.stream().map( (a, b) -> {b.append(a)});
                                    String normalisedQuery = builder.toString();
                                    if (uniqueQueryCollector.add(normalisedQuery)) {
                                        StringBuilder b = new StringBuilder();
                                        for (String e : queryTerms) {
                                            if (b.length() > 0) {
                                                b.append('\t');
                                            }
                                            b.append(e);
                                        }
                                        String queryInNaturalSequence = b.toString();
                                        writer.println(queryInNaturalSequence);
                                        System.out.println(queryInNaturalSequence);
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        iterator.close();
        inputStream.close();
    }
}

From source file:base.BasePlayer.AddGenome.java

static void indexFasta(File fastafile, String targetDir) {
    try {//from   ww  w  .  java2s .c  o  m

        ArrayList<String[]> faiList = new ArrayList<String[]>();

        BufferedReader reader;
        Boolean zip = false;
        GZIPInputStream gzip = null;
        BufferedWriter fastaWriter = null;
        BufferedWriter indexWriter = null;
        if (fastafile.getName().endsWith(".gz")) {
            zip = true;
            gzip = new GZIPInputStream(new FileInputStream(fastafile));
            reader = new BufferedReader(new InputStreamReader(gzip));
            if (!new File(targetDir + fastafile.getName().substring(0, fastafile.getName().indexOf(".gz")))
                    .exists()) {
                fastaWriter = new BufferedWriter(new FileWriter(
                        targetDir + fastafile.getName().substring(0, fastafile.getName().indexOf(".gz"))));
            }
            if (!new File(
                    targetDir + fastafile.getName().substring(0, fastafile.getName().indexOf(".gz")) + ".fai")
                            .exists()) {
                indexWriter = new BufferedWriter(new FileWriter(targetDir
                        + fastafile.getName().substring(0, fastafile.getName().indexOf(".gz")) + ".fai"));
            }
        } else {
            reader = new BufferedReader(new FileReader(fastafile));
            if (!new File(targetDir + "/" + fastafile.getName()).exists()) {
                fastaWriter = new BufferedWriter(new FileWriter(targetDir + fastafile.getName()));
            }
            if (!new File(targetDir + "/" + fastafile.getName() + ".fai").exists()) {
                indexWriter = new BufferedWriter(new FileWriter(targetDir + fastafile.getName() + ".fai"));
            }

        }
        String line;

        long counter = 0, chromlength = 0, pointer = 0;
        String[] split;
        Main.drawCanvas.loadbarAll = 0;

        while ((line = reader.readLine()) != null) {
            if (!Main.drawCanvas.loading) {
                break;
            }
            if (fastaWriter != null) {
                fastaWriter.write(line + "\n");
            }
            counter += line.length() + 1;
            if (line.startsWith(">")) {
                if (faiList.size() > 0) {
                    faiList.get(faiList.size() - 1)[1] = "" + chromlength;
                }
                //Main.drawCanvas.loadbarAll = (int)((counter/(double)filesize)*100);
                //Main.drawCanvas.loadBarSample = Main.drawCanvas.loadbarAll;

                String[] row = new String[5];
                faiList.add(row);
                split = line.split("\\s+");
                pointer = counter;
                row[0] = split[0].substring(1);

                row[2] = "" + pointer;
                line = reader.readLine();
                if (fastaWriter != null) {
                    fastaWriter.write(line + "\n");
                }
                chromlength = line.length();
                row[3] = "" + line.length();
                row[4] = "" + (line.length() + 1);

                counter += line.length() + 1;

            } else {
                chromlength += line.length();
            }
        }
        faiList.get(faiList.size() - 1)[1] = "" + chromlength;
        if (fastaWriter != null) {
            fastaWriter.close();
        }
        reader.close();

        if (zip) {
            gzip.close();
        }
        if (indexWriter != null) {
            for (int i = 0; i < faiList.size(); i++) {
                for (int j = 0; j < 4; j++) {
                    indexWriter.write(faiList.get(i)[j] + "\t");
                }
                indexWriter.write(faiList.get(i)[4] + "\n");
            }
            indexWriter.close();

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:base.BasePlayer.FileRead.java

static void setBedTrack(BedTrack addTrack) {
    try {/*from  w  ww .  j a v a  2s  .  co  m*/
        /* if(!addTrack.file.getName().endsWith(".gz")) {
            return;
         }*/
        if (addTrack.getBBfileReader() != null) {
            return;
        }
        String name = "";
        if (addTrack.file != null) {
            name = addTrack.file.getName().toLowerCase();
        } else {
            name = addTrack.url.toString().toLowerCase();
        }
        if (name.endsWith(".bw") || name.endsWith(".bigwig") || name.endsWith(".bb")
                || name.endsWith(".bigbed")) {
            return;
        }
        InputStream in = null;
        String[] split;
        BufferedReader reader = null;
        GZIPInputStream gzip = null;
        FileReader freader = null;
        if (addTrack.file != null) {
            if (addTrack.file.getName().endsWith(".gz") || addTrack.file.getName().endsWith(".bgz")) {
                gzip = new GZIPInputStream(new FileInputStream(addTrack.file));
                reader = new BufferedReader(new InputStreamReader(gzip));
            } else {
                freader = new FileReader(addTrack.file);
                reader = new BufferedReader(freader);
            }
        } else {
            in = addTrack.url.openStream();
            gzip = new GZIPInputStream(in);
            reader = new BufferedReader(new InputStreamReader(gzip));
        }

        int count = 0;
        if (name.endsWith(".gff.gz") || name.endsWith(".gff3.gz")) {
            addTrack.iszerobased = 1;
            addTrack.getZerobased().setSelected(false);
            while (count < 10) {
                if (reader.readLine().startsWith("#")) {
                    continue;
                }
                split = reader.readLine().split("\\t");

                if (split.length > 5) {
                    if (!Double.isNaN(Double.parseDouble(split[5]))) {
                        addTrack.hasvalues = true;
                    }

                }
                if (Main.SELEXhash.containsKey(split[2].replace(".pfm", ""))) {
                    addTrack.selex = true;
                    addTrack.getAffinityBox().setVisible(true);

                }
                count++;
            }
        } else if (name.endsWith(".bed.gz") || name.endsWith(".bed")) {
            while (count < 10) {
                if (reader.readLine().startsWith("#") || reader.readLine().startsWith("track")) {
                    continue;
                }
                split = reader.readLine().split("\\t");

                if (split.length > 4) {
                    try {
                        if (!Double.isNaN(Double.parseDouble(split[4]))) {
                            addTrack.hasvalues = true;
                        }
                    } catch (Exception e) {

                    }

                }
                if (split.length > 3 && Main.SELEXhash.containsKey(split[3])) {
                    addTrack.selex = true;
                    addTrack.getAffinityBox().setVisible(true);
                }
                count++;
            }

        } else if (name.endsWith(".tsv.gz") || name.endsWith(".tsv.bgz")) {
            if (addTrack.valuecolumn != null) {
                while (count < 10) {
                    if (reader.readLine().startsWith("#")) {
                        continue;
                    }

                    split = reader.readLine().split("\\t");

                    if (!Double.isNaN(Double.parseDouble(split[addTrack.valuecolumn]))) {

                        addTrack.hasvalues = true;
                        break;
                    }
                    count++;
                }
            }
        }

        if (addTrack.getBBfileReader() == null && !addTrack.hasvalues) {

            addTrack.getLimitField().setVisible(false);
        } else {
            addTrack.getLimitField().setVisible(true);
        }
        if (gzip != null) {
            gzip.close();
        }

        if (freader != null) {
            freader.close();
        }
        if (in != null) {
            in.close();
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:base.BasePlayer.FileRead.java

static void readGFF(File infile, String outfile, SAMSequenceDictionary dict) {
    BufferedReader reader = null;
    GZIPInputStream gzip = null;
    FileReader freader = null;/*from  ww w  . jav  a 2  s.  c o m*/
    String line = "", chrom = "-1";
    HashMap<String, String> lineHash;
    HashMap<String, Gene> genes = new HashMap<String, Gene>();
    HashMap<String, Transcript> transcripts = new HashMap<String, Transcript>();
    Gene addgene;
    Transcript addtranscript;

    try {

        if (infile.getName().endsWith(".gz")) {
            gzip = new GZIPInputStream(new FileInputStream(infile));
            reader = new BufferedReader(new InputStreamReader(gzip));
        } else {
            freader = new FileReader(infile);
            reader = new BufferedReader(freader);
        }
        //   line = reader.readLine();
        while ((line = reader.readLine()) != null) {

            if (line.startsWith("#")) {
                continue;
            }

            lineHash = makeHash(line.split("\t"));
            if (lineHash.get("type").startsWith("region")) {

                if (line.contains("unlocalized")) {
                    chrom = "unlocalized";
                } else if (lineHash.get("chromosome") != null) {
                    chrom = lineHash.get("chromosome").replace("chr", "");
                } else if (lineHash.get("name") != null) {
                    chrom = lineHash.get("name").replace("chr", "");
                }

                continue;
            }

            if (!lineHash.containsKey("parent")) {
                /*if(!lineHash.get("type").contains("gene")) {
                        
                   continue;
                }*/

                Gene gene = new Gene(chrom, lineHash);

                genes.put(getInfoValue(lineHash, "id"), gene);

                continue;
            }
            if (genes.containsKey(lineHash.get("parent"))) {

                addgene = genes.get(lineHash.get("parent"));
                transcripts.put(getInfoValue(lineHash, "id"), new Transcript(lineHash, addgene));
                if (lineHash.get("type").equals("exon")) {
                    addtranscript = transcripts.get(getInfoValue(lineHash, "id"));
                    addtranscript.addExon(lineHash, addtranscript);
                }
                if (addgene.getDescription().equals("-")) {
                    if (lineHash.containsKey("product")) {
                        addgene.setDescription(lineHash.get("product"));
                    }
                }
                continue;
            }
            if (transcripts.containsKey(lineHash.get("parent"))) {

                addtranscript = transcripts.get(lineHash.get("parent"));
                addtranscript.addExon(lineHash, addtranscript);
                continue;
            }

        }

    } catch (Exception e) {
        System.out.println(line);
        e.printStackTrace();
        System.exit(0);
    }
    try {

        Transcript transcript;
        Gene gene;
        StringBuffer exStarts, exEnds, exPhases;
        Iterator<Map.Entry<String, Gene>> it = genes.entrySet().iterator();
        ArrayList<String[]> geneArray = new ArrayList<String[]>();

        while (it.hasNext()) {
            Map.Entry<String, Gene> pair = (Map.Entry<String, Gene>) it.next();
            gene = pair.getValue();

            for (int i = 0; i < gene.getTranscripts().size(); i++) {
                transcript = gene.getTranscripts().get(i);
                exStarts = new StringBuffer("");
                exEnds = new StringBuffer("");
                exPhases = new StringBuffer("");
                for (int e = 0; e < transcript.exonArray.size(); e++) {
                    exStarts.append(transcript.exonArray.get(e).getStart() + ",");
                    exEnds.append(transcript.exonArray.get(e).getEnd() + ",");
                    exPhases.append(transcript.exonArray.get(e).getStartPhase() + ",");
                }

                String[] row = { gene.getChrom(), "" + transcript.getStart(), "" + transcript.getEnd(),
                        gene.getName(), "" + transcript.exonArray.size(),
                        MethodLibrary.getStrand(gene.getStrand()), gene.getID(), transcript.getENST(),
                        transcript.getUniprot(), "-", transcript.getBiotype(), "" + transcript.getCodingStart(),
                        "" + transcript.getCodingEnd(), exStarts.toString(), exEnds.toString(),
                        exPhases.toString(), transcript.getDescription() };
                geneArray.add(row);
            }

            it.remove();
        }

        gffSorter gffsorter = new gffSorter();
        Collections.sort(geneArray, gffsorter);

        if (outfile != null) {
            MethodLibrary.blockCompressAndIndex(geneArray, outfile, false, dict);
        }

        geneArray.clear();
        if (freader != null) {
            freader.close();
        }
        reader.close();

        if (gzip != null) {
            gzip.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:base.BasePlayer.FileRead.java

static void readGTF(File infile, String outfile, SAMSequenceDictionary dict) {
    BufferedReader reader = null;
    GZIPInputStream gzip = null;
    FileReader freader = null;//from w  w  w  .  j  av  a2s .co  m
    String line = "", chrom = "-1";
    HashMap<String, String> lineHash;
    HashMap<String, Gene> genes = new HashMap<String, Gene>();
    HashMap<String, Transcript> transcripts = new HashMap<String, Transcript>();
    Gene addgene;
    //Boolean found = false;
    Transcript addtranscript;

    try {

        if (infile.getName().endsWith(".gz")) {
            gzip = new GZIPInputStream(new FileInputStream(infile));
            reader = new BufferedReader(new InputStreamReader(gzip));
        } else {
            freader = new FileReader(infile);
            reader = new BufferedReader(freader);
        }
        //   line = reader.readLine();
        while ((line = reader.readLine()) != null) {

            if (line.startsWith("#")) {
                continue;
            }
            /*
            if(!line.contains("Rp1h")) {
               if(found) {
                  break;
               }
               continue;
            }
            found = true;
            */

            lineHash = makeHash(line.split("\t"));
            chrom = lineHash.get("seqid");

            if (!genes.containsKey(lineHash.get("gene_id"))) {
                /*if(genes.size() > 1) {
                   break;
                }*/
                Gene gene = new Gene(chrom, lineHash, true);

                genes.put(lineHash.get("gene_id"), gene);
                if (lineHash.get("transcript_id") == null) {
                    continue;
                }
                //continue;
            }
            if (!transcripts.containsKey(lineHash.get("transcript_id"))) {

                addgene = genes.get(lineHash.get("gene_id"));
                transcripts.put(getInfoValue(lineHash, "transcript_id"), new Transcript(lineHash, addgene));
                if (lineHash.get("type").equals("exon")) {
                    addtranscript = transcripts.get(getInfoValue(lineHash, "transcript_id"));
                    addtranscript.addExon(lineHash, addtranscript);
                }
                if (addgene.getDescription().equals("-")) {
                    if (lineHash.containsKey("gene_symbol")) {
                        addgene.setDescription(lineHash.get("gene_symbol"));
                    }
                }
                continue;
            }
            if (transcripts.containsKey(lineHash.get("transcript_id"))) {
                if (lineHash.get("type").contains("UTR")) {
                    continue;
                }
                addtranscript = transcripts.get(lineHash.get("transcript_id"));
                addtranscript.addExon(lineHash, addtranscript);

                continue;
            }

        }

    } catch (Exception e) {
        System.out.println(line);
        e.printStackTrace();
        System.exit(0);
    }
    try {

        Transcript transcript;
        Gene gene;
        StringBuffer exStarts, exEnds, exPhases;
        Iterator<Map.Entry<String, Gene>> it = genes.entrySet().iterator();
        ArrayList<String[]> geneArray = new ArrayList<String[]>();

        while (it.hasNext()) {
            Map.Entry<String, Gene> pair = (Map.Entry<String, Gene>) it.next();
            gene = pair.getValue();

            for (int i = 0; i < gene.getTranscripts().size(); i++) {
                transcript = gene.getTranscripts().get(i);
                exStarts = new StringBuffer("");
                exEnds = new StringBuffer("");
                exPhases = new StringBuffer("");
                for (int e = 0; e < transcript.exonArray.size(); e++) {
                    exStarts.append(transcript.exonArray.get(e).getStart() + ",");
                    exEnds.append(transcript.exonArray.get(e).getEnd() + ",");
                    exPhases.append(transcript.exonArray.get(e).getStartPhase() + ",");
                }

                String[] row = { gene.getChrom(), "" + transcript.getStart(), "" + transcript.getEnd(),
                        gene.getName(), "" + transcript.exonArray.size(),
                        MethodLibrary.getStrand(gene.getStrand()), gene.getID(), transcript.getENST(),
                        transcript.getUniprot(), "-", transcript.getBiotype(), "" + transcript.getCodingStart(),
                        "" + transcript.getCodingEnd(), exStarts.toString(), exEnds.toString(),
                        exPhases.toString(), transcript.getDescription() };
                if (transcript.getCodingEnd() == -1) {
                    row[11] = "" + gene.getEnd();
                    row[12] = "" + gene.getStart();
                }

                geneArray.add(row);
            }

            it.remove();
        }

        gffSorter gffsorter = new gffSorter();
        Collections.sort(geneArray, gffsorter);

        if (outfile != null) {
            MethodLibrary.blockCompressAndIndex(geneArray, outfile, false, dict);
        }

        geneArray.clear();
        if (freader != null) {
            freader.close();
        }
        reader.close();

        if (gzip != null) {
            gzip.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:base.BasePlayer.FileRead.java

static void checkMulti(Sample sample) {
    try {//from   w ww . j  a va  2s .  c  om
        Sample addSample;
        BufferedReader reader = null;
        GZIPInputStream gzip = null;
        FileReader freader = null;
        String line;
        Boolean somatic = Main.drawCanvas.drawVariables.somatic;
        String[] split;
        if (somatic != null && somatic) {
            asked = true;
        }
        if (sample.getTabixFile().endsWith(".gz")) {
            try {
                gzip = new GZIPInputStream(new FileInputStream(sample.getTabixFile()));
                reader = new BufferedReader(new InputStreamReader(gzip));
            } catch (Exception e) {
                Main.showError("Could not read the file: " + sample.getTabixFile()
                        + "\nCheck that you have permission to read the file or try to bgzip and recreate the index file.",
                        "Error");
                Main.drawCanvas.sampleList.remove(sample);
                Main.varsamples--;
                Main.samples--;

            }
        } else {
            freader = new FileReader(sample.getTabixFile());
            reader = new BufferedReader(freader);
        }

        line = reader.readLine();

        if (!sample.multipart && line != null) {

            while (line != null) {
                try {

                    if (line.startsWith("##INFO")) {
                        if (line.contains("Type=Float") || line.contains("Type=Integer")
                                || line.contains("Number=")) {
                            VariantHandler.addMenuComponents(line);
                        }
                    }
                    if (line.startsWith("##FILTER")) {
                        if (line.contains("ID=") || line.contains("Description=") || line.contains("Number=")) {
                            VariantHandler.addMenuComponents(line);
                        }
                    }
                    if (line.startsWith("##FORMAT")) {
                        if (line.contains("Type=Float") || line.contains("Type=Integer")
                                || line.contains("Number=")) {
                            VariantHandler.addMenuComponents(line);
                        }
                    }
                    if (line.toLowerCase().contains("#chrom")) {
                        headersplit = line.split("\t+");

                        if (headersplit.length > 10) {
                            if (headersplit.length == 11 && !asked) {
                                if (JOptionPane.showConfirmDialog(Main.drawScroll, "Is this somatic project?",
                                        "Somatic?", JOptionPane.YES_NO_OPTION,
                                        JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                                    somatic = true;
                                    Main.drawCanvas.drawVariables.somatic = true;
                                }
                                asked = true;
                            }
                            if (!somatic) {
                                sample.multiVCF = true;
                                Main.varsamples--;
                                for (int h = 9; h < headersplit.length; h++) {
                                    addSample = new Sample(headersplit[h], (short) (Main.samples), null);
                                    addSample.multipart = true;
                                    Main.drawCanvas.sampleList.add(addSample);
                                    Main.samples++;
                                    Main.varsamples++;
                                    if (sampleString == null) {
                                        sampleString = new StringBuffer("");
                                    }
                                    sampleString.append(addSample.getName() + ";");

                                }
                                VariantHandler.commonSlider.setMaximum(Main.varsamples);
                                VariantHandler.commonSlider.setUpperValue(Main.varsamples);
                                VariantHandler.geneSlider.setMaximum(Main.varsamples);
                                Main.drawCanvas.drawVariables.visiblesamples = (short) (Main.drawCanvas.sampleList
                                        .size());
                                Main.drawCanvas.checkSampleZoom();
                                Main.drawCanvas.resizeCanvas(Main.drawScroll.getViewport().getWidth(),
                                        Main.drawScroll.getViewport().getHeight());
                            }
                        }
                        line = reader.readLine();
                        break;
                    }
                    split = line.split("\t");
                    if (split.length > 2 && split[1].matches("\\d+")) {
                        break;
                    }

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                line = reader.readLine();
            }
            /*
            VariantHandler.menu.setPreferredSize(new Dimension(300,500));
            VariantHandler.menuPanel.setPreferredSize(new Dimension(300,500));*/
            VariantHandler.menuScroll.setPreferredSize(new Dimension(250, 500));
            VariantHandler.menuScrollIndel.setPreferredSize(new Dimension(250, 500));
            if (line == null) {
                return;
            }

            while (line != null && line.startsWith("#")) {
                line = reader.readLine();
            }
            split = line.split("\t");
            if (line.contains("\"")) {
                sample.oddchar = "\"";
            }
            if (split != null && split.length == 8) {
                sample.annoTrack = true;
            }
            if (line != null) {

                if (line.startsWith("chr")) {
                    sample.vcfchr = "chr";
                }
            }
            if (somatic != null && somatic) {
                line = reader.readLine();
                if (line != null) {

                    headersplit = line.split("\t");

                    if (headersplit.length == 11) {

                        if (headersplit[10].startsWith("0:")
                                || (headersplit[10].charAt(0) == '0' && headersplit[10].charAt(2) == '0')) {
                            sample.somaticColumn = 9;
                        } else {
                            sample.somaticColumn = 10;
                        }
                    }

                }
            }
            checkSamples();
            line = null;
            if (freader != null) {
                freader.close();
            }
            reader.close();
            if (gzip != null) {
                gzip.close();
            }
        } else {
            reader.close();
            if (gzip != null) {
                gzip.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}