Search in sources :

Example 1 with INSDFeatureIntervals

use of gov.nih.nlm.ncbi.insdseq.INSDFeatureIntervals 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;
}
Also used : INSDFeature(gov.nih.nlm.ncbi.insdseq.INSDFeature) INSDInterval(gov.nih.nlm.ncbi.insdseq.INSDInterval) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) INSDFeatureIntervals(gov.nih.nlm.ncbi.insdseq.INSDFeatureIntervals) URL(java.net.URL) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Aggregations

INSDFeature (gov.nih.nlm.ncbi.insdseq.INSDFeature)1 INSDFeatureIntervals (gov.nih.nlm.ncbi.insdseq.INSDFeatureIntervals)1 INSDInterval (gov.nih.nlm.ncbi.insdseq.INSDInterval)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLEvent (javax.xml.stream.events.XMLEvent)1