use of com.github.lindenb.jvarkit.util.swing.ColorUtils in project jvarkit by lindenb.
the class GoUtils method doWork.
@Override
public int doWork(final List<String> args) {
PrintWriter out = null;
try {
this.mainGoTree = this.readingGo.createParser().setDebug(false).parse(this.readingGo.goUri);
final Map<GoTree.Term, UserTerm> userTerms = new HashMap<>();
for (final String s : this.userAccStrings) {
if (StringUtil.isBlank(s))
continue;
final GoTree.Term t = this.findTerm(s);
if (t == null) {
LOG.error("cannot find user term " + s);
return -1;
}
userTerms.put(t, new UserTerm(t));
}
if (this.accessionFile != null) {
final ColorUtils colorUtils = new ColorUtils();
final BufferedReader r = IOUtils.openFileForBufferedReading(this.accessionFile);
String line;
while ((line = r.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;
int last = 0;
for (last = 0; last < line.length(); ++last) {
if (Character.isWhitespace(line.charAt(last)))
break;
}
final String s = line.substring(0, last);
GoTree.Term t = this.findTerm(s);
if (t == null) {
r.close();
LOG.error("In " + this.accessionFile + " cannot find user term \"" + s + "\"");
return -1;
}
final UserTerm ut = new UserTerm(t);
userTerms.put(t, ut);
switch(this.action) {
case dump_gexf:
{
for (final String left : line.substring(last).trim().split("[ \t;]+")) {
if (left.isEmpty()) {
// cont
} else if (left.startsWith("color=") && ut.vizColor == null) {
ut.vizColor = colorUtils.parse(left.substring(6));
} else if (left.startsWith("size=") && ut.vizSize == null) {
ut.vizSize = Double.parseDouble(left.substring(5));
} else {
LOG.warning("Ignoring unknown modifier " + left + " in " + line);
}
}
break;
}
default:
break;
}
}
r.close();
}
switch(this.action) {
case dump_gexf:
{
final XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter w = null;
FileWriter fw = null;
if (this.outputFile == null) {
w = xof.createXMLStreamWriter(stdout(), "UTF-8");
} else {
w = xof.createXMLStreamWriter((fw = new FileWriter(this.outputFile)));
}
final Function<GoTree.Term, String> term2str = T -> T.getAcn().replaceAll("[\\:_#]+", "_");
w.writeStartDocument("UTF-8", "1.0");
w.writeStartElement("gexf");
w.writeAttribute("xmlns", GexfConstants.XMLNS);
w.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
w.writeAttribute("xmlns:viz", GexfConstants.XMLNS_VIZ);
w.writeAttribute("xsi:schemaLocation", GexfConstants.XSI_SCHEMA_LOCATION);
w.writeAttribute("version", GexfConstants.VERSION);
w.writeStartElement("meta");
w.writeStartElement("creator");
w.writeCharacters(getClass().getName() + " by Pierre Lindenbaum");
w.writeEndElement();
w.writeStartElement("description");
w.writeCharacters("Gene Ontology Tree to Gexf :" + getProgramCommandLine());
w.writeEndElement();
// meta
w.writeEndElement();
w.writeStartElement("graph");
w.writeAttribute("mode", "static");
w.writeAttribute("defaultedgetype", "directed");
w.writeStartElement("attributes");
w.writeAttribute("class", "edge");
w.writeAttribute("mode", "static");
// attributes
w.writeEndElement();
w.writeStartElement("attributes");
w.writeAttribute("class", "node");
w.writeAttribute("mode", "static");
w.writeEmptyElement("attribute");
w.writeAttribute("id", "0");
w.writeAttribute("title", "description");
w.writeAttribute("type", "string");
w.writeEmptyElement("attribute");
w.writeAttribute("id", "1");
w.writeAttribute("title", "accession");
w.writeAttribute("type", "string");
w.writeEmptyElement("attribute");
w.writeAttribute("id", "2");
w.writeAttribute("title", "userTerm");
w.writeAttribute("type", "boolean");
w.writeEmptyElement("attribute");
w.writeAttribute("id", "3");
w.writeAttribute("title", "parentOfUserTerm");
w.writeAttribute("type", "boolean");
w.writeEmptyElement("attribute");
w.writeAttribute("id", "4");
w.writeAttribute("title", "childOffUserTerm");
w.writeAttribute("type", "boolean");
// attributes
w.writeEndElement();
w.writeStartElement("nodes");
w.writeAttribute("count", String.valueOf(this.mainGoTree.size()));
for (final GoTree.Term term : this.mainGoTree.getTerms()) {
final UserTerm ut = userTerms.get(term);
w.writeStartElement("node");
w.writeAttribute("id", term2str.apply(term));
w.writeAttribute("label", term.getName());
w.writeStartElement("attvalues");
w.writeEmptyElement("attvalue");
w.writeAttribute("for", "0");
w.writeAttribute("value", term.getDefinition());
w.writeEmptyElement("attvalue");
w.writeAttribute("for", "1");
w.writeAttribute("value", term.getAcn());
w.writeEmptyElement("attvalue");
w.writeAttribute("for", "2");
w.writeAttribute("value", String.valueOf(ut != null));
w.writeEmptyElement("attvalue");
// is parent of any user term
w.writeAttribute("for", "3");
w.writeAttribute("value", String.valueOf(userTerms.keySet().stream().anyMatch(T -> T.isDescendantOf(term))));
w.writeEmptyElement("attvalue");
// is child of any user term
w.writeAttribute("for", "4");
w.writeAttribute("value", String.valueOf(userTerms.keySet().stream().anyMatch(T -> term.isDescendantOf(T))));
// attvalues
w.writeEndElement();
double viz_size = 1.0;
if (ut != null) {
if (ut.vizSize != null) {
viz_size = ut.vizSize;
}
if (ut.vizColor != null) {
// viz:color
w.writeEmptyElement("viz:color");
w.writeAttribute("r", String.valueOf(ut.vizColor.getRed()));
w.writeAttribute("g", String.valueOf(ut.vizColor.getGreen()));
w.writeAttribute("b", String.valueOf(ut.vizColor.getBlue()));
w.writeAttribute("a", String.valueOf("1.0"));
}
}
w.writeEmptyElement("viz:size");
w.writeAttribute("value", String.valueOf(viz_size));
// node
w.writeEndElement();
}
// nodes
w.writeEndElement();
w.writeStartElement("edges");
w.writeAttribute("count", String.valueOf(this.mainGoTree.getTerms().stream().mapToInt(N -> N.getRelations().size()).sum()));
for (final GoTree.Term term : this.mainGoTree.getTerms()) {
for (final GoTree.Relation rel : term.getRelations()) {
w.writeStartElement("edge");
w.writeAttribute("id", "E" + term2str.apply(term) + "_" + term2str.apply(rel.getTo()));
w.writeAttribute("type", "directed");
w.writeAttribute("source", term2str.apply(term));
w.writeAttribute("target", term2str.apply(rel.getTo()));
w.writeAttribute("label", rel.getType().name());
w.writeAttribute("weight", String.valueOf(1));
final Color vizColor;
switch(rel.getType()) {
case negatively_regulates:
vizColor = Color.RED;
break;
case positively_regulates:
vizColor = Color.GREEN;
break;
case regulates:
vizColor = Color.ORANGE;
break;
case part_of:
vizColor = Color.BLUE;
break;
// cont
case is_a:
default:
vizColor = Color.BLACK;
break;
}
// viz:color
w.writeEmptyElement("viz:color");
w.writeAttribute("r", String.valueOf(vizColor.getRed()));
w.writeAttribute("g", String.valueOf(vizColor.getGreen()));
w.writeAttribute("b", String.valueOf(vizColor.getBlue()));
w.writeAttribute("a", String.valueOf("1.0"));
w.writeEndElement();
}
}
// edges
w.writeEndElement();
// graph
w.writeEndElement();
// gexf
w.writeEndElement();
w.writeEndDocument();
w.flush();
if (fw != null) {
fw.flush();
CloserUtil.close(fw);
} else {
System.out.flush();
}
break;
}
// through
case dump_table:
default:
{
if (!args.isEmpty()) {
LOG.error("too many arguments");
return -1;
}
out = super.openFileOrStdoutAsPrintWriter(this.outputFile);
out.println("#ACN\tNAME\tDEFINITION");
for (final GoTree.Term t : this.mainGoTree.getTerms()) {
boolean keep = false;
if (userTerms.isEmpty()) {
keep = true;
} else {
for (final GoTree.Term userTerm : userTerms.keySet()) {
if (t.isDescendantOf(userTerm)) {
keep = true;
break;
}
}
}
if (this.inverse)
keep = !keep;
if (keep) {
out.print(t.getAcn());
out.print('\t');
out.print(t.getName());
out.print('\t');
out.print(t.getDefinition());
out.println();
}
}
out.flush();
out.close();
break;
}
}
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(out);
}
}
use of com.github.lindenb.jvarkit.util.swing.ColorUtils in project jvarkit by lindenb.
the class SamColorTag method doWork.
@Override
public int doWork(final List<String> args) {
SAMRecordIterator iter = null;
SamReader samFileReader = null;
SAMFileWriter sw = null;
final ColorUtils colorUtils = new ColorUtils();
try {
final CompiledScript script = super.compileJavascript(this.jsExpression, this.jsFile);
samFileReader = openSamReader(oneFileOrNull(args));
final SAMFileHeader srcheader = samFileReader.getFileHeader();
final SAMFileHeader header = srcheader.clone();
header.addComment(ColorUtils.YC_TAG + " attribute added with " + getProgramName() + " " + getProgramCommandLine());
sw = this.writingBamArgs.openSAMFileWriter(outputFile, header, true);
final Bindings bindings = script.getEngine().createBindings();
bindings.put("header", samFileReader.getFileHeader());
SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
iter = samFileReader.iterator();
while (iter.hasNext()) {
final SAMRecord record = progress.watch(iter.next());
bindings.put("record", record);
final Color color;
Object result;
try {
result = script.eval(bindings);
} catch (final Exception err) {
if (!ignoreErrors) {
LOG.error(err);
return -1;
}
result = null;
}
if (result == null) {
color = null;
} else if (result instanceof Color) {
color = Color.class.cast(result);
} else if (result instanceof String) {
final String s = (String) result;
Color c2 = null;
try {
if (s.trim().isEmpty()) {
c2 = null;
} else {
c2 = colorUtils.parse(s);
}
} catch (final Exception err) {
if (!ignoreErrors) {
LOG.error(err);
return -1;
}
c2 = null;
}
color = c2;
} else {
if (!ignoreErrors) {
LOG.error("Cannot cast to color a " + result.getClass());
return -1;
}
color = null;
}
if (color != null) {
record.setAttribute(ColorUtils.YC_TAG, ColorUtils.colorToSamAttribute(color));
} else {
// clear attribute
record.setAttribute(ColorUtils.YC_TAG, null);
}
sw.addAlignment(record);
}
sw.close();
sw = null;
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(iter);
CloserUtil.close(samFileReader);
CloserUtil.close(sw);
}
}
Aggregations