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);
}
}
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();
}
}
Aggregations