Search in sources :

Example 1 with TeeInputStream

use of com.github.lindenb.jvarkit.io.TeeInputStream in project jvarkit by lindenb.

the class VcfBiomart method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VcfIterator iter, final VariantContextWriter out) {
    HttpGet httpGet = null;
    final Pattern tab = Pattern.compile("[\t]");
    try {
        final TransformerFactory factory = TransformerFactory.newInstance();
        final Transformer transformer = factory.newTransformer();
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        final VCFHeader header = iter.getHeader();
        StringBuilder desc = new StringBuilder("Biomart query. Format: ");
        desc.append(this.attributes.stream().map(S -> this.printLabels ? S + "|" + S : S).collect(Collectors.joining("|")));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
        header.addMetaDataLine(new VCFInfoHeaderLine(this.TAG, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.String, desc.toString()));
        out.writeHeader(header);
        final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header).logger(LOG);
        while (iter.hasNext()) {
            final VariantContext ctx = progress.watch(iter.next());
            final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
            vcb.rmAttribute(this.TAG);
            this.filterColumnContig.set(ctx.getContig());
            this.filterColumnStart.set(String.valueOf(ctx.getStart()));
            this.filterColumnEnd.set(String.valueOf(ctx.getEnd()));
            final StringWriter domToStr = new StringWriter();
            transformer.transform(new DOMSource(this.domQuery), new StreamResult(domToStr));
            final URIBuilder builder = new URIBuilder(this.serviceUrl);
            builder.addParameter("query", domToStr.toString());
            // System.err.println("\nwget -O - 'http://grch37.ensembl.org/biomart/martservice?query="+escapedQuery+"'\n");
            // escapedQuery = URLEncoder.encode(escapedQuery,"UTF-8");
            httpGet = new HttpGet(builder.build());
            final CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            int responseCode = httpResponse.getStatusLine().getStatusCode();
            if (responseCode != 200) {
                throw new RuntimeIOException("Response code was not 200. Detected response was " + responseCode);
            }
            InputStream response = httpResponse.getEntity().getContent();
            if (this.teeResponse) {
                response = new TeeInputStream(response, stderr(), false);
            }
            final BufferedReader br = new BufferedReader(new InputStreamReader(response));
            final Set<String> infoAtts = br.lines().filter(L -> !StringUtil.isBlank(L)).filter(L -> !L.equals("[success]")).map(L -> tab.split(L)).map(T -> {
                final StringBuilder sb = new StringBuilder();
                for (int i = 0; i < this.attributes.size(); i++) {
                    if (i > 0)
                        sb.append("|");
                    if (this.printLabels)
                        sb.append(escapeInfo(this.attributes.get(i))).append("|");
                    sb.append(i < T.length ? escapeInfo(T[i]) : "");
                }
                return sb.toString();
            }).collect(Collectors.toCollection(LinkedHashSet::new));
            CloserUtil.close(br);
            CloserUtil.close(response);
            CloserUtil.close(httpResponse);
            if (!infoAtts.isEmpty()) {
                vcb.attribute(this.TAG, new ArrayList<>(infoAtts));
            }
            out.add(vcb.make());
        }
        progress.finish();
        return 0;
    } catch (final Exception err) {
        LOG.error(err);
        throw new RuntimeIOException(err);
    }
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) VCFHeader(htsjdk.variant.vcf.VCFHeader) StreamResult(javax.xml.transform.stream.StreamResult) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) Attr(org.w3c.dom.Attr) ArrayList(java.util.ArrayList) StringUtil(htsjdk.samtools.util.StringUtil) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) Document(org.w3c.dom.Document) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Node(org.w3c.dom.Node) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) LinkedHashSet(java.util.LinkedHashSet) CloserUtil(htsjdk.samtools.util.CloserUtil) TeeInputStream(com.github.lindenb.jvarkit.io.TeeInputStream) VCFHeaderLineType(htsjdk.variant.vcf.VCFHeaderLineType) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) URIBuilder(org.apache.http.client.utils.URIBuilder) StringWriter(java.io.StringWriter) Logger(com.github.lindenb.jvarkit.util.log.Logger) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) Set(java.util.Set) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Element(org.w3c.dom.Element) HttpGet(org.apache.http.client.methods.HttpGet) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) VariantContext(htsjdk.variant.variantcontext.VariantContext) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) VCFHeaderLineCount(htsjdk.variant.vcf.VCFHeaderLineCount) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) HttpClients(org.apache.http.impl.client.HttpClients) InputStream(java.io.InputStream) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) HttpGet(org.apache.http.client.methods.HttpGet) VariantContext(htsjdk.variant.variantcontext.VariantContext) StringWriter(java.io.StringWriter) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) VCFHeader(htsjdk.variant.vcf.VCFHeader) Pattern(java.util.regex.Pattern) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) TransformerFactory(javax.xml.transform.TransformerFactory) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) TeeInputStream(com.github.lindenb.jvarkit.io.TeeInputStream) InputStream(java.io.InputStream) TeeInputStream(com.github.lindenb.jvarkit.io.TeeInputStream) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) URIBuilder(org.apache.http.client.utils.URIBuilder) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) BufferedReader(java.io.BufferedReader)

Example 2 with TeeInputStream

use of com.github.lindenb.jvarkit.io.TeeInputStream in project jvarkit by lindenb.

the class VcfEnsemblVepRest method callVepToDom.

/**
 * send a pool of variants to VEP, returns the DOM document
 */
private Document callVepToDom(final List<VariantContext> contexts, boolean xml_answer) throws IOException {
    LOG.info("Running VEP " + contexts.size());
    InputStream response = null;
    HttpPost httpPost = null;
    try {
        if (this.lastMillisec != -1L && this.lastMillisec + 5000 < System.currentTimeMillis()) {
            LOG.debug("waiting");
            try {
                Thread.sleep(1000);
            } catch (Exception err) {
            }
        }
        httpPost = new HttpPost(this.server + this.extension);
        final StringBuilder queryb = new StringBuilder();
        queryb.append("{ \"variants\" : [");
        for (int i = 0; i < contexts.size(); ++i) {
            final VariantContext ctx = contexts.get(i);
            if (i > 0)
                queryb.append(",");
            queryb.append("\"").append(createInputContext(ctx)).append("\"");
        }
        queryb.append("]");
        for (final String s : new String[] { "canonical", "ccds", "domains", "hgvs", "numbers", "protein", "xref_refseq", "tsl", "uniprot" }) {
            queryb.append(",\"").append(s).append("\":1");
        }
        queryb.append("}");
        final byte[] postBody = queryb.toString().getBytes();
        httpPost.setHeader("Content-Type", ContentType.APPLICATION_JSON.getMimeType());
        httpPost.setHeader("Accept", ContentType.TEXT_XML.getMimeType());
        // httpPost.setHeader("Content-Length", Integer.toString(postBody.length));
        httpPost.setEntity(new ByteArrayEntity(postBody, ContentType.APPLICATION_JSON));
        final CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        int responseCode = httpResponse.getStatusLine().getStatusCode();
        if (responseCode != 200) {
            throw new RuntimeIOException("Response code was not 200. Detected response was " + responseCode);
        }
        // response = new TeeInputStream( httpConnection.getInputStream(),System.err,false);
        response = httpResponse.getEntity().getContent();
        if (this.teeResponse) {
            stderr().println(queryb);
            response = new TeeInputStream(response, stderr(), false);
        }
        final Document dom = documentBuilder.parse(response);
        return dom;
    } catch (final Throwable err) {
        if (this.ignoreNetworkErrors) {
            LOG.error(err);
            return documentBuilder.newDocument();
        }
        throw new IOException(err);
    } finally {
        CloserUtil.close(response);
        if (httpPost != null)
            httpPost.releaseConnection();
        this.lastMillisec = System.currentTimeMillis();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) TeeInputStream(com.github.lindenb.jvarkit.io.TeeInputStream) InputStream(java.io.InputStream) VariantContext(htsjdk.variant.variantcontext.VariantContext) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException) TeeInputStream(com.github.lindenb.jvarkit.io.TeeInputStream) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

TeeInputStream (com.github.lindenb.jvarkit.io.TeeInputStream)2 RuntimeIOException (htsjdk.samtools.util.RuntimeIOException)2 VariantContext (htsjdk.variant.variantcontext.VariantContext)2 InputStream (java.io.InputStream)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 Document (org.w3c.dom.Document)2 Parameter (com.beust.jcommander.Parameter)1 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)1 Program (com.github.lindenb.jvarkit.util.jcommander.Program)1 Logger (com.github.lindenb.jvarkit.util.log.Logger)1 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)1 VCFUtils (com.github.lindenb.jvarkit.util.vcf.VCFUtils)1 VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)1 CloserUtil (htsjdk.samtools.util.CloserUtil)1 StringUtil (htsjdk.samtools.util.StringUtil)1 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)1 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)1 VCFHeader (htsjdk.variant.vcf.VCFHeader)1 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)1 VCFHeaderLineCount (htsjdk.variant.vcf.VCFHeaderLineCount)1