Search in sources :

Example 1 with GTFCodec

use of com.github.lindenb.jvarkit.util.bio.gtf.GTFCodec in project jvarkit by lindenb.

the class Gtf2Xml method doWork.

@Override
public int doWork(final List<String> args) {
    LineIterator r = null;
    XMLStreamWriter w = null;
    FileWriter fw = null;
    try {
        String inputName = oneFileOrNull(args);
        r = (StringUtil.isBlank(inputName) ? IOUtils.openStreamForLineIterator(stdin()) : IOUtils.openURIForLineIterator(inputName));
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        if (this.outputFile == null) {
            w = xof.createXMLStreamWriter(stdout(), "UTF-8");
        } else {
            w = xof.createXMLStreamWriter((fw = new FileWriter(this.outputFile)));
        }
        final GTFCodec codec = this.formatChooser.makeCodec();
        w.writeStartDocument("UTF-8", "1.0");
        w.writeStartElement("gtf");
        final GTFCodec.GTFHeader header = codec.readActualHeader(r);
        for (final String headerLine : header.getLines()) {
            if (!headerLine.startsWith("#!"))
                continue;
            final int ws = headerLine.indexOf(' ');
            // ??
            if (ws == -1)
                continue;
            w.writeAttribute(headerLine.substring(2, ws), headerLine.substring(ws + 1).trim());
        }
        while (r.hasNext()) {
            final String line = r.next();
            GTFLine gtfline = codec.decode(line);
            if (gtfline == null)
                continue;
            write(w, gtfline);
        }
        if (!this.disable_att_keys) {
            w.writeStartElement("attributes");
            for (String k : this.att_keys) {
                w.writeStartElement("attribute");
                w.writeCharacters(k);
                w.writeEndElement();
                w.writeCharacters("\n");
            }
            w.writeEndElement();
            w.writeCharacters("\n");
        }
        if (!this.disable_feature_type) {
            w.writeStartElement("types");
            for (String k : this.types) {
                w.writeStartElement("type");
                w.writeCharacters(k);
                w.writeEndElement();
                w.writeCharacters("\n");
            }
            w.writeEndElement();
            w.writeCharacters("\n");
        }
        if (!this.disable_sources) {
            w.writeStartElement("sources");
            for (final String k : this.sources) {
                w.writeStartElement("source");
                w.writeCharacters(k);
                w.writeEndElement();
                w.writeCharacters("\n");
            }
            w.writeEndElement();
            w.writeCharacters("\n");
        }
        if (!this.disable_dict) {
            w.writeStartElement("dict");
            for (final String k : this.seqdict.keySet()) {
                w.writeEmptyElement("chrom");
                w.writeAttribute("name", k);
                w.writeAttribute("length", String.valueOf(this.seqdict.get(k)));
                w.writeCharacters("\n");
            }
            w.writeEndElement();
            w.writeCharacters("\n");
        }
        w.writeEndElement();
        w.writeEndDocument();
        w.flush();
        return 0;
    } catch (Exception e) {
        LOG.error(e);
        return -1;
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(fw);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FileWriter(java.io.FileWriter) GTFCodec(com.github.lindenb.jvarkit.util.bio.gtf.GTFCodec) LineIterator(htsjdk.tribble.readers.LineIterator) GTFLine(com.github.lindenb.jvarkit.util.bio.gtf.GTFLine) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

GTFCodec (com.github.lindenb.jvarkit.util.bio.gtf.GTFCodec)1 GTFLine (com.github.lindenb.jvarkit.util.bio.gtf.GTFLine)1 LineIterator (htsjdk.tribble.readers.LineIterator)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1