use of gov.nih.nlm.ncbi.insdseq.INSDInterval in project jvarkit by lindenb.
the class Biostar3654 method fetchAnnotations.
/**
* fetches the annotation for a given entry if the name starts with gi|....
*/
private List<INSDFeature> fetchAnnotations(final String database, final String acn, int start, int end) throws Exception {
InputStream in = null;
XMLEventReader r = null;
final List<INSDFeature> L = new ArrayList<INSDFeature>();
if (start > end)
return fetchAnnotations(database, acn, end, start);
try {
if (acn != null && !acn.isEmpty() && !acn.startsWith("Query")) {
String uri = NcbiConstants.efetch() + "?db=" + database + "&id=" + URLEncoder.encode(acn, "UTF-8") + "&rettype=gbc&retmode=xml&seq_start=" + start + "&seq_stop=" + end + this.ncbiApiKey.getAmpParamValue();
LOG.info(uri);
in = new URL(uri).openStream();
r = this.xif.createXMLEventReader(in);
while (r.hasNext()) {
XMLEvent evt = r.peek();
if (evt.isStartElement() && evt.asStartElement().getName().getLocalPart().equals("INSDFeature")) {
INSDFeature feature = this.unmarshaller.unmarshal(r, INSDFeature.class).getValue();
INSDFeatureIntervals its = feature.getINSDFeatureIntervals();
if (its == null || its.getINSDInterval().isEmpty())
continue;
for (INSDInterval interval : its.getINSDInterval()) {
// when using seq_start and seq_stop , the NCBI shifts the data...
if (interval.getINSDIntervalFrom() != null && interval.getINSDIntervalTo() != null) {
interval.setINSDIntervalFrom(String.valueOf(Integer.parseInt(interval.getINSDIntervalFrom()) + start - 1));
interval.setINSDIntervalTo(String.valueOf(Integer.parseInt(interval.getINSDIntervalTo()) + start - 1));
} else if (interval.getINSDIntervalPoint() != null) {
interval.setINSDIntervalPoint(String.valueOf(Integer.parseInt(interval.getINSDIntervalPoint()) + start - 1));
}
}
L.add(feature);
} else {
// consumme
r.next();
}
}
}
} catch (Exception err) {
LOG.error(err);
} finally {
CloserUtil.close(r);
CloserUtil.close(in);
}
LOG.info("N(INSDFeature)=" + L.size());
// not found, return empty table
return L;
}
Aggregations