Search in sources :

Example 6 with LineIteratorImpl

use of htsjdk.tribble.readers.LineIteratorImpl in project jvarkit by lindenb.

the class GenomicJaspar method doWork.

@Override
public int doWork(List<String> args) {
    if (jasparUri == null) {
        LOG.error("Undefined jaspar-uri");
        return -1;
    }
    PrintWriter out = null;
    try {
        out = super.openFileOrStdoutAsPrintWriter(OUT);
        LOG.info("Reading " + jasparUri);
        LineReader lr = IOUtils.openURIForLineReader(jasparUri);
        LineIterator liter = new LineIteratorImpl(lr);
        Iterator<Matrix> miter = Matrix.iterator(liter);
        while (miter.hasNext()) {
            Matrix matrix = miter.next();
            this.jasparDb.add(matrix.convertToPWM());
        }
        lr.close();
        LOG.info("JASPAR size: " + this.jasparDb.size());
        if (args.isEmpty()) {
            LOG.info("Reading from stdin");
            run(out, new InputStreamReader(stdin()));
        } else {
            for (final String fname : args) {
                LOG.info("Opening " + fname);
                Reader in = IOUtils.openURIForBufferedReading(fname);
                run(out, in);
                in.close();
            }
        }
        out.flush();
        out.close();
        out = null;
        return 0;
    } catch (Throwable err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(out);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LineReader(htsjdk.tribble.readers.LineReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) LineReader(htsjdk.tribble.readers.LineReader) LineIteratorImpl(htsjdk.tribble.readers.LineIteratorImpl) LineIterator(htsjdk.tribble.readers.LineIterator) PrintWriter(java.io.PrintWriter)

Example 7 with LineIteratorImpl

use of htsjdk.tribble.readers.LineIteratorImpl in project jvarkit by lindenb.

the class VcfRegistryCGI method doWork.

private void doWork(XMLStreamWriter w, final GroupFile gf) throws XMLStreamException {
    Position pos = parsePosition();
    if (pos == null)
        return;
    w.writeStartElement("div");
    w.writeStartElement("h2");
    w.writeCharacters(pos.chrom + ":" + pos.pos);
    w.writeEndElement();
    w.writeStartElement("table");
    w.writeStartElement("thead");
    w.writeStartElement("tr");
    for (String header : new String[] { "CHROM", "POS", "ID", "REF", "QUAL", "Sample", "Alleles", "DP", "GQ", "File" }) {
        w.writeStartElement("th");
        w.writeCharacters(header);
        // td
        w.writeEndElement();
    }
    // tr
    w.writeEndElement();
    // thead
    w.writeEndElement();
    w.writeStartElement("tbody");
    Set<String> samplesWithGenotypes = new HashSet<String>();
    Set<String> allSamples = new HashSet<String>();
    for (VcfFile f : getVcfFiles(gf)) {
        TabixReader tabixReader = null;
        TabixReader.Iterator iter = null;
        BlockCompressedInputStream bgzin = null;
        VCFHeader header = null;
        AbstractVCFCodec vcfCodec = VCFUtils.createDefaultVCFCodec();
        LineIterator lineIterator = null;
        for (int i = 0; i < 2; i++) {
            try {
                if (i == 0) {
                    bgzin = new BlockCompressedInputStream(f.file);
                    lineIterator = new LineIteratorImpl(new SynchronousLineReader(bgzin));
                    header = (VCFHeader) vcfCodec.readActualHeader(lineIterator);
                    allSamples.addAll(header.getGenotypeSamples());
                } else {
                    tabixReader = new TabixReader(f.file.getPath());
                    String line;
                    int[] x = tabixReader.parseReg(pos.chrom + ":" + pos.pos + "-" + (pos.pos));
                    if (x != null && x.length > 2 && x[0] != -1) {
                        iter = tabixReader.query(x[0], x[1], x[2]);
                    } else {
                    }
                    while (iter != null && (line = iter.next()) != null) {
                        VariantContext var = vcfCodec.decode(line);
                        for (String sample : header.getSampleNamesInOrder()) {
                            final Genotype genotype = var.getGenotype(sample);
                            if (genotype == null || !genotype.isCalled())
                                continue;
                            if (!genotype.isAvailable())
                                continue;
                            samplesWithGenotypes.add(sample);
                            w.writeStartElement("tr");
                            w.writeStartElement("td");
                            w.writeCharacters(var.getContig());
                            w.writeEndElement();
                            w.writeStartElement("td");
                            w.writeCharacters(String.valueOf(var.getStart()));
                            w.writeEndElement();
                            if (var.hasID()) {
                                w.writeStartElement("td");
                                if (var.getID().matches("rs[0-9]+")) {
                                    w.writeStartElement("a");
                                    w.writeAttribute("href", "http://www.ncbi.nlm.nih.gov/snp/" + var.getID().substring(2));
                                    w.writeCharacters(var.getID());
                                    // a
                                    w.writeEndElement();
                                } else {
                                    w.writeCharacters(var.getID());
                                }
                                // td
                                w.writeEndElement();
                            } else {
                                w.writeEmptyElement("td");
                            }
                            if (var.getReference() != null) {
                                w.writeStartElement("td");
                                w.writeCharacters(var.getReference().getBaseString());
                                w.writeEndElement();
                            } else {
                                w.writeEmptyElement("td");
                            }
                            if (var.hasLog10PError()) {
                                w.writeStartElement("td");
                                w.writeCharacters(String.valueOf((int) var.getPhredScaledQual()));
                                w.writeEndElement();
                            } else {
                                w.writeEmptyElement("td");
                            }
                            w.writeStartElement("td");
                            w.writeCharacters(sample);
                            w.writeEndElement();
                            List<Allele> alleles = genotype.getAlleles();
                            w.writeStartElement("td");
                            w.writeStartElement("span");
                            if (genotype.isHomRef()) {
                                w.writeAttribute("style", "color:green;");
                            } else if (genotype.isHomVar()) {
                                w.writeAttribute("style", "color:red;");
                            } else if (genotype.isHet()) {
                                w.writeAttribute("style", "color:blue;");
                            }
                            for (int j = 0; j < alleles.size(); ++j) {
                                if (j > 0)
                                    w.writeCharacters(genotype.isPhased() ? "|" : "/");
                                w.writeCharacters(alleles.get(j).getBaseString());
                            }
                            // span
                            w.writeEndElement();
                            w.writeEndElement();
                            if (genotype.hasDP()) {
                                w.writeStartElement("td");
                                w.writeCharacters(String.valueOf(genotype.getDP()));
                                w.writeEndElement();
                            } else {
                                w.writeEmptyElement("td");
                            }
                            if (genotype.hasGQ()) {
                                w.writeStartElement("td");
                                w.writeCharacters(String.valueOf(genotype.getGQ()));
                                w.writeEndElement();
                            } else {
                                w.writeEmptyElement("td");
                            }
                            w.writeStartElement("td");
                            w.writeCharacters(f.file.getName());
                            w.writeEndElement();
                            // tr
                            w.writeEndElement();
                            w.flush();
                        }
                    }
                }
            } catch (Exception err) {
                w.writeComment("BOUM " + err);
                header = null;
                lastException = err;
            } finally {
                CloserUtil.close(lineIterator);
                CloserUtil.close(bgzin);
                CloserUtil.close(tabixReader);
                CloserUtil.close(iter);
            }
            if (i == 0 && header == null)
                break;
        }
        w.flush();
    }
    // tbody
    w.writeEndElement();
    // table
    w.writeEndElement();
    allSamples.removeAll(samplesWithGenotypes);
    if (!allSamples.isEmpty()) {
        w.writeStartElement("h3");
        w.writeCharacters("Samples not found");
        w.writeEndElement();
        w.writeStartElement("ol");
        for (String sample : new TreeSet<String>(allSamples)) {
            w.writeStartElement("li");
            w.writeCharacters(sample);
            w.writeEndElement();
        }
        w.writeEndElement();
    }
    writeHTMLException(w, this.lastException);
    // div
    w.writeEndElement();
}
Also used : TabixReader(htsjdk.tribble.readers.TabixReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) Genotype(htsjdk.variant.variantcontext.Genotype) AbstractVCFCodec(htsjdk.variant.vcf.AbstractVCFCodec) LineIterator(htsjdk.tribble.readers.LineIterator) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Allele(htsjdk.variant.variantcontext.Allele) TreeSet(java.util.TreeSet) SynchronousLineReader(htsjdk.tribble.readers.SynchronousLineReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) LineIteratorImpl(htsjdk.tribble.readers.LineIteratorImpl) BlockCompressedInputStream(htsjdk.samtools.util.BlockCompressedInputStream) HashSet(java.util.HashSet)

Example 8 with LineIteratorImpl

use of htsjdk.tribble.readers.LineIteratorImpl in project jvarkit by lindenb.

the class ConvertBedChromosomes method doWork.

@SuppressWarnings("resource")
protected int doWork(InputStream in, PrintStream out) throws IOException {
    final int chromColumn0 = chromColumn1 - 1;
    Pattern tab = Pattern.compile("[\t]");
    LineIterator lr = new LineIteratorImpl(new AsciiLineReader(in));
    while (lr.hasNext()) {
        String line = lr.next();
        if (BedLine.isBedHeader(line)) {
            out.println(line);
            continue;
        }
        final String[] tokens = tab.split(line, (chromColumn0 + 2));
        if (chromColumn0 >= tokens.length)
            throw new IOException("Bad BED line : " + line + " extected at least " + (chromColumn0 + 2) + " columns");
        final String chrom = convertName(tokens[chromColumn0]);
        if (chrom == null)
            continue;
        for (int i = 0; i < tokens.length; ++i) {
            if (i > 0)
                out.print("\t");
            out.print(i == chromColumn0 ? chrom : tokens[i]);
        }
        out.println();
    }
    out.flush();
    return 0;
}
Also used : Pattern(java.util.regex.Pattern) AsciiLineReader(htsjdk.tribble.readers.AsciiLineReader) IOException(java.io.IOException) LineIteratorImpl(htsjdk.tribble.readers.LineIteratorImpl) LineIterator(htsjdk.tribble.readers.LineIterator)

Example 9 with LineIteratorImpl

use of htsjdk.tribble.readers.LineIteratorImpl in project gatk by broadinstitute.

the class SAMPileupCodecUnitTest method testDecodeLoc.

@Test(dataProvider = "stringFeature")
private void testDecodeLoc(final String pileupString, final SAMPileupFeature expected) throws Exception {
    final Feature feature = CODEC.decodeLoc(new LineIteratorImpl(makeReader(pileupString)));
    Assert.assertEquals(feature.getContig(), expected.getContig());
    Assert.assertEquals(feature.getStart(), expected.getStart());
    Assert.assertEquals(feature.getEnd(), expected.getEnd());
}
Also used : LineIteratorImpl(htsjdk.tribble.readers.LineIteratorImpl) Feature(htsjdk.tribble.Feature) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 10 with LineIteratorImpl

use of htsjdk.tribble.readers.LineIteratorImpl in project gatk by broadinstitute.

the class TableCodecUnitTest method testDecodeComment.

@Test
public void testDecodeComment() {
    TableCodec tc = new TableCodec();
    LineReader reader = makeReader(asList("#HEADER a b c", "HEADER d e f"));
    LineIterator li = new LineIteratorImpl(reader);
    List<String> hd = tc.readActualHeader(li);
    Assert.assertEquals(hd, asList("HEADER", "d", "e", "f"));
}
Also used : LineReader(htsjdk.tribble.readers.LineReader) LineIteratorImpl(htsjdk.tribble.readers.LineIteratorImpl) LineIterator(htsjdk.tribble.readers.LineIterator) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

LineIteratorImpl (htsjdk.tribble.readers.LineIteratorImpl)14 LineIterator (htsjdk.tribble.readers.LineIterator)13 LineReader (htsjdk.tribble.readers.LineReader)9 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)7 Test (org.testng.annotations.Test)7 SynchronousLineReader (htsjdk.tribble.readers.SynchronousLineReader)5 IOException (java.io.IOException)5 VariantContext (htsjdk.variant.variantcontext.VariantContext)4 HashSet (java.util.HashSet)4 VCFHeader (htsjdk.variant.vcf.VCFHeader)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 TreeSet (java.util.TreeSet)3 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)2 AbstractVCFCodec (htsjdk.variant.vcf.AbstractVCFCodec)2 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)2 DataInputStream (java.io.DataInputStream)2 InputStream (java.io.InputStream)2 JvarkitException (com.github.lindenb.jvarkit.lang.JvarkitException)1 Counter (com.github.lindenb.jvarkit.util.Counter)1