use of htsjdk.samtools.util.RuntimeIOException 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 htsjdk.samtools.util.RuntimeIOException 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();
}
}
use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.
the class VcfDerby01 method openDerby.
private void openDerby() {
try {
boolean create;
final Properties props = new Properties();
final File derbyDir = getDerbyDirectory();
LOG.info("open derby :" + getDerbyDirectory());
if (derbyDir.exists()) {
if (!derbyDir.isDirectory()) {
throw new RuntimeIOException("derby database is not a directory : " + derbyDir);
}
if (derbyDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isFile()) {
if (pathname.getName().equals("service.properties"))
return true;
if (pathname.getName().equals("README_DO_NOT_TOUCH_FILES.txt"))
return true;
}
if (pathname.isDirectory()) {
if (pathname.getName().startsWith("log"))
return true;
}
return false;
}
}).length == 0) {
throw new RuntimeIOException("derby database exist but doesn't look like a derby directory : " + derbyDir);
}
create = false;
} else {
create = true;
}
props.setProperty("create", String.valueOf(create));
this.conn = DriverManager.getConnection("jdbc:derby:" + derbyDir, props);
if (create) {
final String tableId = "ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY";
final Statement stmt = this.conn.createStatement();
final String[] sqls = { "CREATE TABLE ROWCONTENT(" + tableId + ",MD5SUM CHAR(32) UNIQUE,CONTENT CLOB,CONTIG VARCHAR(20),FILTERED SMALLINT NOT NULL,START INT,STOP INT,ALLELE_REF VARCHAR(" + MAX_REF_BASE_LENGTH + "))", "CREATE TABLE VCF(" + tableId + ",NAME VARCHAR(255))", "CREATE TABLE VCFROW(" + tableId + ",VCF_ID INTEGER CONSTRAINT row2vcf REFERENCES VCF,ROW_ID INTEGER CONSTRAINT row2content REFERENCES ROWCONTENT)" };
for (final String sql : sqls) {
LOG.warn(sql);
stmt.execute(sql);
}
stmt.close();
}
this.conn.setAutoCommit(true);
} catch (Exception e) {
CloserUtil.close(this.conn);
this.conn = null;
throw new RuntimeException(e);
}
}
use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.
the class VcfAnnotWithBeacon method doVcfToVcf.
@Override
protected int doVcfToVcf(String inputName, final VcfIterator iter, final VariantContextWriter out) {
CloseableHttpClient httpClient = null;
InputStream contentInputStream = null;
try {
final org.apache.http.impl.client.HttpClientBuilder hb = HttpClients.custom();
if (this.ignoreCertErrors) {
// http://stackoverflow.com/questions/24720013/apache-http-client-ssl-certificate-error
System.setProperty("jsse.enableSNIExtension", "false");
final SSLContext sslContext = org.apache.http.conn.ssl.SSLContexts.custom().loadTrustMaterial(null, new org.apache.http.conn.ssl.TrustStrategy() {
@Override
public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
return true;
}
}).useTLS().build();
final org.apache.http.conn.ssl.SSLConnectionSocketFactory connectionFactory = new org.apache.http.conn.ssl.SSLConnectionSocketFactory(sslContext, new org.apache.http.conn.ssl.AllowAllHostnameVerifier());
hb.setSSLSocketFactory(connectionFactory);
}
httpClient = hb.build();
HttpGet httpGetRequest = null;
final Set<String> available_chromosomes = new HashSet<>();
try {
httpGetRequest = new HttpGet(baseurl + "/chromosomes");
httpGetRequest.setHeader("Accept", ContentType.APPLICATION_JSON.getMimeType());
contentInputStream = httpClient.execute(httpGetRequest).getEntity().getContent();
JsonParser jsonparser = new JsonParser();
final JsonElement root = jsonparser.parse(new InputStreamReader(contentInputStream));
Iterator<JsonElement> jsr = root.getAsJsonArray().iterator();
while (jsr.hasNext()) {
final String ctg = jsr.next().getAsString();
available_chromosomes.add(ctg);
}
LOG.debug(available_chromosomes);
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(contentInputStream);
}
final Set<String> available_alleles = new HashSet<>();
try {
httpGetRequest = new HttpGet(baseurl + "/alleles");
httpGetRequest.setHeader("Accept", ContentType.APPLICATION_JSON.getMimeType());
contentInputStream = httpClient.execute(httpGetRequest).getEntity().getContent();
JsonParser jsonparser = new JsonParser();
final JsonElement root = jsonparser.parse(new InputStreamReader(contentInputStream));
Iterator<JsonElement> jsr = root.getAsJsonArray().iterator();
while (jsr.hasNext()) {
final String allele = jsr.next().getAsString();
available_alleles.add(allele);
}
LOG.debug(available_alleles);
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(contentInputStream);
}
final StoredResponseBinding storedResponseBinding = new StoredResponseBinding();
final VCFHeader header = new VCFHeader(iter.getHeader());
final VCFInfoHeaderLine infoHeaderLine = new VCFInfoHeaderLine(this.infoTag, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.String, "Tag inserted with " + getProgramName());
header.addMetaDataLine(infoHeaderLine);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
out.writeHeader(header);
while (iter.hasNext()) {
final VariantContext ctx = iter.next();
if (!ctx.isVariant() || ctx.getReference().isSymbolic()) {
out.add(ctx);
continue;
}
if (ctx.hasAttribute(infoHeaderLine.getID()) && this.dontUpdateIfInfoIsPresent) {
out.add(ctx);
continue;
}
String beaconContig = ctx.getContig();
if (!available_chromosomes.contains(beaconContig)) {
if (beaconContig.startsWith("chr")) {
beaconContig = beaconContig.substring(3);
}
if (!available_chromosomes.contains(beaconContig)) {
out.add(ctx);
continue;
}
}
final List<Allele> altAlleles = ctx.getAlternateAlleles();
if (altAlleles.isEmpty()) {
out.add(ctx);
continue;
}
final Set<String> newInfo = new HashSet<>();
for (final Allele alt : altAlleles) {
if (alt.isSymbolic() || alt.isNoCall())
continue;
final StringBuilder buildUrl = new StringBuilder();
buildUrl.append("chrom=");
buildUrl.append(URLEncoder.encode(beaconContig, "UTF-8"));
buildUrl.append("&pos=");
/*
* "Coordinate within a chromosome. Position is a number and is 0-based"
* .
*/
buildUrl.append(ctx.getStart() - 1);
buildUrl.append("&allele=");
final String allele;
if (ctx.getReference().length() > alt.length()) {
// del
allele = "D";
} else if (ctx.getReference().length() > alt.length()) {
// ins
allele = "I";
} else {
allele = alt.getDisplayString();
}
if (!available_alleles.contains(allele))
continue;
buildUrl.append(allele);
buildUrl.append("&ref=");
buildUrl.append(URLEncoder.encode(this.genomeBuild, "UTF-8"));
final String queryUrl = buildUrl.toString();
boolean foundInBdb = false;
Set<String> foundIn = null;
if (this.beaconDatabase != null) {
StringBinding.stringToEntry(queryUrl, key);
if (this.beaconDatabase.get(this.txn, key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
StoredResponse response = storedResponseBinding.entryToObject(data);
if (// TODO check how old is
response.timeStamp < 0) // that data
{
response = null;
this.beaconDatabase.delete(this.txn, key);
}
if (response != null) {
foundInBdb = true;
foundIn = response.foundIn;
}
}
}
if (foundIn == null) {
foundIn = new HashSet<>();
try {
httpGetRequest = new HttpGet(baseurl + "/responses?" + queryUrl);
httpGetRequest.setHeader("Accept", ContentType.APPLICATION_JSON.getMimeType());
LOG.debug(httpGetRequest.getURI());
contentInputStream = httpClient.execute(httpGetRequest).getEntity().getContent();
JsonParser jsonparser = new JsonParser();
final JsonElement root = jsonparser.parse(new InputStreamReader(contentInputStream));
Iterator<JsonElement> jsr = root.getAsJsonArray().iterator();
while (jsr.hasNext()) {
final JsonObject b = jsr.next().getAsJsonObject();
if (!(b.has("beacon") && b.has("response")))
continue;
final String beacon_id = b.get("beacon").getAsJsonObject().get("id").getAsString();
final JsonElement response_prim = b.get("response");
if (response_prim.isJsonPrimitive() && response_prim.getAsBoolean()) {
foundIn.add(beacon_id);
}
}
} catch (final Exception err) {
LOG.error(err);
if (stopOnNetworkError) {
throw new RuntimeIOException(err);
}
} finally {
CloserUtil.close(contentInputStream);
}
}
if (this.beaconDatabase != null && !foundInBdb) {
StoredResponse response = new StoredResponse();
response.timeStamp = System.currentTimeMillis();
response.foundIn = foundIn;
}
// 17&pos=41244981&=G&ref=GRCh37")
newInfo.addAll(foundIn.stream().map(S -> alt.getDisplayString() + "|" + S).collect(Collectors.toSet()));
}
if (newInfo.isEmpty()) {
out.add(ctx);
continue;
}
final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
vcb.attribute(infoHeaderLine.getID(), new ArrayList<String>(newInfo));
out.add(vcb.make());
}
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(httpClient);
}
}
use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.
the class DefaultVcfFileList method get.
@Override
public VariantContext get(final int index) {
if (index < 0 || index >= this.size())
throw new IndexOutOfBoundsException("0<" + index + "<" + size() + " in " + vcfFile);
try {
final String line;
if (this.last_list_index == -1 || this.last_list_index + 1 != index) {
this.indexio.seek((long) VcfOffsetsIndexFactory.MAGIC.length + (long) index * (long) Long.BYTES);
final long offset = this.indexio.readLong();
if (this.bgzfin != null) {
this.bgzfin.seek(offset);
line = this.bgzfin.readLine();
} else {
this.vcfrandom.seek(offset);
line = this.vcfrandom.readLine();
}
} else {
if (this.bgzfin != null) {
line = this.bgzfin.readLine();
} else {
line = this.vcfrandom.readLine();
}
}
this.last_list_index = index;
return this.codec.decode(line);
} catch (final IOException err) {
throw new RuntimeIOException(err);
}
}
Aggregations