Search in sources :

Example 76 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project jvarkit by lindenb.

the class WorldMapGenome method loadWorld.

private void loadWorld() throws IOException, XMLStreamException {
    Source source;
    LOG.warning("openingg " + svgMapUri);
    if (IOUtils.isRemoteURI(svgMapUri)) {
        source = new StreamSource(svgMapUri);
    } else {
        source = new StreamSource(new File(svgMapUri));
    }
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLEventReader xef = xif.createXMLEventReader(source);
    while (xef.hasNext()) {
        XMLEvent evt = xef.nextEvent();
        if (!evt.isStartElement()) {
            continue;
        }
        StartElement E = evt.asStartElement();
        String localName = E.getName().getLocalPart();
        if (!localName.equals("path"))
            continue;
        Attribute att = E.getAttributeByName(new QName("id"));
        if (att == null)
            continue;
        String country = att.getValue().toLowerCase().replaceAll("[ ]+", "");
        att = E.getAttributeByName(new QName("d"));
        if (att == null)
            continue;
        GeneralPath path = null;
        char op = '\0';
        Scanner scanner = new Scanner(att.getValue().replaceAll("[ \t\n\r,]+", " "));
        path = new GeneralPath();
        while (scanner.hasNext()) {
            if (op == '\0') {
                op = scanner.next().charAt(0);
            }
            switch(op) {
                case 'M':
                    path.moveTo(scanner.nextDouble(), scanner.nextDouble());
                    break;
                case 'C':
                    path.curveTo(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
                    break;
                case 'Z':
                    {
                        path.closePath();
                        break;
                    }
                default:
                    throw new IOException("bad operator " + op);
            }
            if (scanner.hasNext("[MCZ]")) {
                op = scanner.next().charAt(0);
            }
        }
        scanner.close();
        this.country2shape.put(country, scaleWorld(path));
    }
    xef.close();
}
Also used : Scanner(java.util.Scanner) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 77 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project jvarkit by lindenb.

the class ReduceBlast method run.

private void run(final XMLEventReader r, final XMLEventWriter w) throws XMLStreamException, JAXBException {
    final XMLEventFactory eventFactory = XMLEventFactory.newFactory();
    boolean prev_was_iteration = false;
    while (r.hasNext()) {
        final XMLEvent evt = r.peek();
        QName qname = null;
        if (evt.isStartElement() && (qname = evt.asStartElement().getName()).getLocalPart().equals("Iteration")) {
            final Iteration iteration = this.unmarshaller.unmarshal(r, Iteration.class).getValue();
            if (!iteration.getIterationHits().getHit().isEmpty()) {
                if (!this.keep_message)
                    iteration.setIterationMessage(null);
                if (!this.keep_stats)
                    iteration.setIterationStat(null);
                this.marshaller.marshal(new JAXBElement<Iteration>(qname, Iteration.class, iteration), w);
                w.add(eventFactory.createCharacters("\n"));
            }
            prev_was_iteration = true;
            continue;
        } else if (prev_was_iteration && evt.isCharacters() && is_empty(evt.asCharacters().getData())) {
            r.nextEvent();
            continue;
        }
        w.add(r.nextEvent());
        prev_was_iteration = false;
    }
}
Also used : XMLEventFactory(javax.xml.stream.XMLEventFactory) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) Iteration(gov.nih.nlm.ncbi.blast.Iteration)

Example 78 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project jvarkit by lindenb.

the class EvsToVcf method doWork.

@Override
public int doWork(List<String> args) {
    VariantContextWriter out = null;
    try {
        if (!args.isEmpty()) {
            LOG.error("Illegal number of arguments");
            return -1;
        }
        JAXBContext jc = JAXBContext.newInstance(SnpData.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        out = VCFUtils.createVariantContextWriterToStdout();
        SAMSequenceDictionary dict = new SAMSequenceDictionary();
        _fillDict(dict, "1", 249250621);
        _fillDict(dict, "2", 243199373);
        _fillDict(dict, "3", 198022430);
        _fillDict(dict, "4", 191154276);
        _fillDict(dict, "5", 180915260);
        _fillDict(dict, "6", 171115067);
        _fillDict(dict, "7", 159138663);
        _fillDict(dict, "8", 146364022);
        _fillDict(dict, "9", 141213431);
        _fillDict(dict, "10", 135534747);
        _fillDict(dict, "11", 135006516);
        _fillDict(dict, "12", 133851895);
        _fillDict(dict, "13", 115169878);
        _fillDict(dict, "14", 107349540);
        _fillDict(dict, "15", 102531392);
        _fillDict(dict, "16", 90354753);
        _fillDict(dict, "17", 81195210);
        _fillDict(dict, "18", 78077248);
        _fillDict(dict, "19", 59128983);
        _fillDict(dict, "20", 63025520);
        _fillDict(dict, "21", 48129895);
        _fillDict(dict, "22", 51304566);
        _fillDict(dict, "X", 155270560);
        _fillDict(dict, "Y", 59373566);
        _fillDict(dict, "MT", 16569);
        VCFHeader header = new VCFHeader();
        header.setSequenceDictionary(dict);
        header.addMetaDataLine(new VCFInfoHeaderLine("CONS", VCFHeaderLineCount.INTEGER, VCFHeaderLineType.Float, "conservationScore"));
        header.addMetaDataLine(new VCFInfoHeaderLine("GERP", VCFHeaderLineCount.INTEGER, VCFHeaderLineType.Float, "conservationScoreGERP"));
        header.addMetaDataLine(new VCFInfoHeaderLine("uaMAF", VCFHeaderLineCount.INTEGER, VCFHeaderLineType.Float, "conservationScoreGERP"));
        header.addMetaDataLine(new VCFInfoHeaderLine("aaMAF", VCFHeaderLineCount.INTEGER, VCFHeaderLineType.Float, "conservationScoreGERP"));
        header.addMetaDataLine(new VCFInfoHeaderLine("totalMAF", VCFHeaderLineCount.INTEGER, VCFHeaderLineType.Float, "conservationScoreGERP"));
        header.addMetaDataLine(new VCFInfoHeaderLine("DP", VCFHeaderLineCount.INTEGER, VCFHeaderLineType.Integer, "conservationScoreGERP"));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkVersion", HtsjdkVersion.getVersion()));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkHome", HtsjdkVersion.getHome()));
        out.writeHeader(header);
        Pattern comma = Pattern.compile("[,]");
        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
        XMLEventReader xmlr = xif.createXMLEventReader(System.in);
        while (xmlr.hasNext() && !System.out.checkError()) {
            XMLEvent evt = xmlr.peek();
            if (!evt.isStartElement() || !evt.asStartElement().getName().getLocalPart().equals("snpList")) {
                xmlr.nextEvent();
                continue;
            }
            SnpData snpData = unmarshaller.unmarshal(xmlr, SnpData.class).getValue();
            VariantContextBuilder vcb = new VariantContextBuilder();
            Set<Allele> alleles = new HashSet<Allele>();
            alleles.add(Allele.create(snpData.getRefAllele(), true));
            for (String s : comma.split(snpData.getAltAlleles())) {
                if (isEmpty(s))
                    continue;
                alleles.add(Allele.create(s, false));
            }
            vcb.chr(snpData.getChromosome());
            vcb.start(snpData.getChrPosition());
            vcb.stop(snpData.getChrPosition() + snpData.getRefAllele().length() - 1);
            if (!isEmpty(snpData.getRsIds()) && !snpData.getRsIds().equals("none")) {
                vcb.id(snpData.getRsIds());
            }
            vcb.alleles(alleles);
            Float d = parseDouble(snpData.getConservationScore());
            if (d != null) {
                vcb.attribute("CONS", d);
            }
            d = parseDouble(snpData.getConservationScoreGERP());
            if (d != null) {
                vcb.attribute("GERP", d);
            }
            vcb.attribute("uaMAF", (float) snpData.getUaMAF());
            vcb.attribute("aaMAF", (float) snpData.getAaMAF());
            vcb.attribute("totalMAF", (float) snpData.getTotalMAF());
            vcb.attribute("DP", snpData.getAvgSampleReadDepth());
            out.add(vcb.make());
        }
        xmlr.close();
        out.close();
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(out);
    }
}
Also used : Pattern(java.util.regex.Pattern) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) JAXBContext(javax.xml.bind.JAXBContext) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) SnpData(edu.washington.gs.evs.SnpData) Unmarshaller(javax.xml.bind.Unmarshaller) VCFHeader(htsjdk.variant.vcf.VCFHeader) XMLInputFactory(javax.xml.stream.XMLInputFactory) HashSet(java.util.HashSet)

Example 79 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project jvarkit by lindenb.

the class NgsStage method loadSnippets.

protected List<SnippetCode> loadSnippets() {
    final String rsrc = getSnippetResourcePath();
    if (rsrc == null || rsrc.isEmpty())
        return Collections.emptyList();
    final List<SnippetCode> snippets = new ArrayList<>();
    InputStream in = null;
    XMLEventReader r = null;
    try {
        in = getClass().getResourceAsStream(rsrc);
        if (in != null) {
            final XMLInputFactory xif = XMLInputFactory.newFactory();
            xif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
            r = xif.createXMLEventReader(in);
            final QName isFunctionAtt = new QName("is-function");
            final QName scopeAtt = new QName("scope");
            final QName labelAtt = new QName("label");
            final QName nameAtt = new QName("name");
            while (r.hasNext()) {
                final XMLEvent evt = r.nextEvent();
                if (!evt.isStartElement())
                    continue;
                final StartElement start = evt.asStartElement();
                if (!start.getName().getLocalPart().equals("code"))
                    continue;
                final Attribute isFunction = start.getAttributeByName(isFunctionAtt);
                final Attribute scope = start.getAttributeByName(scopeAtt);
                Attribute attLabel = start.getAttributeByName(labelAtt);
                if (attLabel == null)
                    attLabel = start.getAttributeByName(nameAtt);
                if (attLabel != null && r.hasNext() && r.peek().isCharacters()) {
                    final SnippetCode snippet = new SnippetCode();
                    snippet.label = attLabel.getValue();
                    snippet.code = r.nextEvent().asCharacters().getData();
                    snippet.function = isFunction != null && isFunction.getValue().equals("true");
                    snippet.scope = (scope == null ? "" : scope.getValue());
                    snippets.add(snippet);
                }
            }
        } else {
            LOG.warning("Cannot read snippets " + rsrc);
        }
    } catch (Exception err) {
        LOG.warning(err.getMessage());
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(in);
    }
    return snippets;
}
Also used : StartElement(javax.xml.stream.events.StartElement) Attribute(javax.xml.stream.events.Attribute) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 80 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project jvarkit by lindenb.

the class TreePackApp method scan.

private Node scan(InputStream in) throws XMLStreamException {
    Node n = null;
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLEventReader r = xif.createXMLEventReader(in);
    while (r.hasNext()) {
        XMLEvent evt = r.nextEvent();
        if (evt.isStartElement()) {
            QName qName = evt.asStartElement().getName();
            if (qName.getLocalPart().equals("node")) {
                n = parseNode(r, evt.asStartElement(), null);
            }
        }
    }
    r.close();
    return n;
}
Also used : QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)242 XMLEventReader (javax.xml.stream.XMLEventReader)111 XMLStreamException (javax.xml.stream.XMLStreamException)91 StartElement (javax.xml.stream.events.StartElement)86 XMLInputFactory (javax.xml.stream.XMLInputFactory)63 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)40 Attribute (javax.xml.stream.events.Attribute)40 EndElement (javax.xml.stream.events.EndElement)33 ArrayList (java.util.ArrayList)31 InputStream (java.io.InputStream)28 XMLEventWriter (javax.xml.stream.XMLEventWriter)23 Characters (javax.xml.stream.events.Characters)21 Test (org.junit.Test)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringWriter (java.io.StringWriter)16 StringReader (java.io.StringReader)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 HashMap (java.util.HashMap)13 XMLEventFactory (javax.xml.stream.XMLEventFactory)13