Search in sources :

Example 1 with INSDFeature

use of gov.nih.nlm.ncbi.insdseq.INSDFeature 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)

Example 2 with INSDFeature

use of gov.nih.nlm.ncbi.insdseq.INSDFeature in project jvarkit by lindenb.

the class Biostar3654 method parseIteration.

private void parseIteration(String database, Iteration iteration) throws Exception {
    this.pw.println("QUERY: " + iteration.getIterationQueryDef());
    this.pw.println("       ID:" + iteration.getIterationQueryID() + " Len:" + iteration.getIterationQueryLen());
    for (Hit hit : iteration.getIterationHits().getHit()) {
        this.pw.println(">" + hit.getHitDef());
        this.pw.println(" " + hit.getHitAccession());
        this.pw.println(" id:" + hit.getHitId() + " len:" + hit.getHitLen());
        for (Hsp hsp : hit.getHitHsps().getHsp()) {
            List<INSDFeature> qFeatures = fetchAnnotations(database, iteration.getIterationQueryID(), Integer.parseInt(hsp.getHspQueryFrom()), Integer.parseInt(hsp.getHspQueryTo()));
            List<INSDFeature> hFeatures = fetchAnnotations(database, hit.getHitAccession(), Integer.parseInt(hsp.getHspHitFrom()), Integer.parseInt(hsp.getHspHitTo()));
            this.pw.println();
            this.pw.println("   e-value:" + hsp.getHspEvalue() + " gap:" + hsp.getHspGaps() + " bitScore:" + hsp.getHspBitScore());
            this.pw.println();
            // create the Printer for the Query and the Hit
            QPrinter qPrinter = new QPrinter(hsp, qFeatures);
            HPrinter hPrinter = new HPrinter(hsp, hFeatures);
            // loop over the lines
            while (qPrinter.next() && hPrinter.next()) {
                qPrinter.print();
                this.pw.printf("QUERY %0" + margin + "d ", qPrinter.seqStart);
                this.pw.print(hsp.getHspQseq().substring(qPrinter.stringStart, qPrinter.stringEnd));
                this.pw.printf(" %0" + margin + "d", qPrinter.seqEnd - (qPrinter.sign));
                this.pw.println();
                this.pw.printf("      %" + margin + "s ", "");
                this.pw.print(hsp.getHspMidline().substring(qPrinter.stringStart, qPrinter.stringEnd));
                this.pw.println();
                this.pw.printf("HIT   %0" + margin + "d ", hPrinter.seqStart);
                this.pw.print(hsp.getHspHseq().substring(hPrinter.stringStart, hPrinter.stringEnd));
                this.pw.printf(" %0" + margin + "d", hPrinter.seqEnd - (hPrinter.sign));
                this.pw.println();
                hPrinter.print();
                this.pw.println();
            }
            this.pw.flush();
        }
    }
// System.err.println("OK");
}
Also used : Hit(gov.nih.nlm.ncbi.blast.Hit) INSDFeature(gov.nih.nlm.ncbi.insdseq.INSDFeature) Hsp(gov.nih.nlm.ncbi.blast.Hsp)

Aggregations

INSDFeature (gov.nih.nlm.ncbi.insdseq.INSDFeature)2 Hit (gov.nih.nlm.ncbi.blast.Hit)1 Hsp (gov.nih.nlm.ncbi.blast.Hsp)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