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);
}
}
Aggregations