org.biopax.paxtools.PaxtoolsMain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of paxtools-console Show documentation
Show all versions of paxtools-console Show documentation
Paxtools Console Application and Examples
package org.biopax.paxtools;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.biopax.paxtools.controller.*;
import org.biopax.paxtools.converter.LevelUpgrader;
import org.biopax.paxtools.converter.psi.PsiToBiopax3Converter;
import org.biopax.paxtools.io.*;
import org.biopax.paxtools.io.gsea.GSEAConverter;
import org.biopax.paxtools.io.sbgn.L3ToSBGNPDConverter;
import org.biopax.paxtools.model.*;
import org.biopax.paxtools.model.level2.entity;
import org.biopax.paxtools.model.level3.*;
import org.biopax.paxtools.pattern.miner.*;
import org.biopax.paxtools.pattern.util.Blacklist;
import org.biopax.paxtools.query.QueryExecuter;
import org.biopax.paxtools.query.algorithm.Direction;
import org.biopax.paxtools.client.BiopaxValidatorClient;
import org.biopax.paxtools.client.BiopaxValidatorClient.RetFormat;
import org.biopax.validator.jaxb.Behavior;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.zip.GZIPInputStream;
/**
* A command line accessible utility for basic Paxtools operations.
*/
public class PaxtoolsMain {
public static Log log = LogFactory.getLog(PaxtoolsMain.class);
private static SimpleIOHandler io = new SimpleIOHandler();
public static void main(String[] argv) throws IOException,
InvocationTargetException, IllegalAccessException
{
io.mergeDuplicates(true);
if (argv.length == 0) {
help();
} else {
String command = argv[0];
if(command.startsWith("--")) //accept both w and w/out --
command = command.substring(2);
Command.valueOf(command).run(argv);
}
}
public static void toGSEA(String[] argv) throws IOException
{
Model model = io.convertFromOWL(getInputStream(argv[1]));
GSEAConverter gseaConverter; //to be initialized below
boolean specCheckEnabled = (argv.length>4) && Boolean.parseBoolean(argv[4]);
if(argv.length < 6 || argv[5].equalsIgnoreCase("false")) {
log.info("Collecting proteins for a pathway, the converter will also consider " +
"its sub-pathways, their sub-pathways, etc.");
gseaConverter = new GSEAConverter(argv[3], specCheckEnabled);
} else {
boolean skipAllSubPathways = Boolean.parseBoolean(argv[5]);
if (skipAllSubPathways) {
log.info("The converter won't traverse into sub-pathways of any pathway to collect its protein IDs.");
gseaConverter = new GSEAConverter(argv[3], specCheckEnabled, true);
} else { //must be a list of data source URIs, or it's an error...
final Set skipSubPathways = new HashSet();
for(String uri: argv[5].split(";")) {
Provenance pro = (Provenance) model.getByID(uri);
if(pro != null) {
skipSubPathways.add(pro);
log.info("GSEA converter won't traverse into sub-pathways of pathways from the data source: "
+ uri);
} else {
log.error("No Provenance found by uri: " + uri + " (thus - ignored)");
}
}
if(skipSubPathways.isEmpty()) {
throw new IllegalArgumentException("The last arg. of the Paxtools command is bad; " +
"no known Provenance found (if these were semicolon-separated Provenance URIs): " + argv[5]);
} else {
log.info("Collecting proteins, the converter will skip sub-pathways of pathways " +
"of the following datasources: " + skipSubPathways.toString());
gseaConverter = new GSEAConverter(argv[3], specCheckEnabled, skipSubPathways);
}
}
}
//convert and write
gseaConverter.writeToGSEA(model, new FileOutputStream(argv[2]));
}
public static void getNeighbors(String[] argv) throws IOException
{
// set strings vars
String in = argv[1];
String[] ids = argv[2].split(",");
String out = argv[3];
// read BioPAX from the file
Model model = io.convertFromOWL(getInputStream(in));
// get elements (entities)
Set elements = new HashSet();
for (Object id : ids) {
BioPAXElement e = model.getByID(id.toString());
if (e != null && (e instanceof Entity || e instanceof entity)) {
elements.add(e);
} else {
log.warn("Source element not found: " + id);
}
}
// execute the 'nearest neighborhood' query
Set result = QueryExecuter
.runNeighborhood(elements, model, 1, Direction.BOTHSTREAM);
// auto-complete/clone the results in a new model
// (this also cuts some less important edges, right?..)
Completer c = new Completer(io.getEditorMap());
result = c.complete(result, model);
Cloner cln = new Cloner(io.getEditorMap(), io.getFactory());
model = cln.clone(model, result); // new sub-model
if (model != null) {
log.info("Elements in the result model: " + model.getObjects().size());
// export to OWL
io.convertToOWL(model, new FileOutputStream(out));
} else {
log.error("NULL model returned.");
}
}
public static void fetch(String[] argv) throws IOException {
// set strings vars
String in = argv[1];
String[] uris = argv[2].split(",");
String out = argv[3];
Model model = io.convertFromOWL(getInputStream(in));
io.setFactory(model.getLevel().getDefaultFactory());
// extract and save the sub-model (defined by ids)
io.convertToOWL(model, new FileOutputStream(out), uris);
}
/*
* Detects and converts a BioPAX Level 1, 2,
* or PSI-MI/MITAB model to BioPAX Level3.
*
* (-Dpaxtools.converter.psi.interaction=complex
* java option can be used to generate Complex
* instead of MolecularInteraction entities from PSI interactions).
*/
public static void toLevel3(String[] argv) throws IOException {
final String input = argv[1];
final String output = argv[2];
InputStream is = getInputStream(input);
FileOutputStream os = new FileOutputStream(output);
boolean forcePsiInteractionToComplex = false;
String val = System.getProperty("paxtools.converter.psi.interaction");
if("complex".equalsIgnoreCase(val))
forcePsiInteractionToComplex = true;
Type type = detect(input);
try {
switch (type) {
case BIOPAX:
Model model = io.convertFromOWL(is);
model = (new LevelUpgrader()).filter(model);
if (model != null) {
io.setFactory(model.getLevel().getDefaultFactory());
io.convertToOWL(model, os); //os is closed already
}
break;
case PSIMI:
PsiToBiopax3Converter psimiConverter = new PsiToBiopax3Converter();
psimiConverter.convert(is, os, forcePsiInteractionToComplex);
os.close();
break;
default: //MITAB
psimiConverter = new PsiToBiopax3Converter();
psimiConverter.convertTab(is, os, forcePsiInteractionToComplex);
os.close();
break;
}
} catch (Exception e) {
throw new RuntimeException("Failed to convert " +
input + "to BioPAX L3", e);
}
}
private enum Type {
BIOPAX,
PSIMI,
PSIMITAB
}
//read a few lines to detect it's a BioPAX vs. PSI-MI vs. PSI-MITAB data.
private static Type detect(String input) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(input));
int linesToCheck = 20;
while (linesToCheck-- > 0) {
sb.append(reader.readLine()).append('\n');
}
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
String buf = sb.toString();
if (buf.contains("BioPAX Validator Webservice
*/
public static void validate(String[] argv) throws IOException
{
String input = argv[1];
String output = argv[2];
// default options
RetFormat outf = RetFormat.HTML;
boolean fix = false;
Integer maxErrs = null;
Behavior level = null; //will report both errors and warnings
String profile = null;
// match optional args
for (int i = 3; i < argv.length; i++) {
if ("html".equalsIgnoreCase(argv[i])) {
outf = RetFormat.HTML;
} else if ("xml".equalsIgnoreCase(argv[i])) {
outf = RetFormat.XML;
} else if ("biopax".equalsIgnoreCase(argv[i])) {
outf = RetFormat.OWL;
} else if ("auto-fix".equalsIgnoreCase(argv[i])) {
fix = true;
} else if ("only-errors".equalsIgnoreCase(argv[i])) {
level = Behavior.ERROR;
} else if ((argv[i]).toLowerCase().startsWith("maxerrors=")) {
String num = argv[i].substring(10);
maxErrs = Integer.valueOf(num);
} else if ("notstrict".equalsIgnoreCase(argv[i])) {
profile = "notstrict";
}
}
Collection files = new HashSet();
File fileOrDir = new File(input);
if (!fileOrDir.canRead()) {
System.out.println("Cannot read " + input);
}
// collect files
if (fileOrDir.isDirectory()) {
// validate all the OWL files in the folder
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".owl"));
}
};
for (String s : fileOrDir.list(filter)) {
files.add(new File(fileOrDir.getCanonicalPath()
+ File.separator + s));
}
} else {
files.add(fileOrDir);
}
// upload and validate using the default URL:
// http://www.biopax.org/biopax-validator/check.html
OutputStream os = new FileOutputStream(output);
try {
if (!files.isEmpty()) {
BiopaxValidatorClient val = new BiopaxValidatorClient();
val.validate(fix, profile, outf, level, maxErrs, null, files.toArray(new File[]{}), os);
}
} catch (Exception ex) {
// fall-back: not using the remote validator; trying to read files
String msg = "Unable to check with the biopax-validator web service: \n " +
ex.toString() +
"\n Fall-back: trying to parse the file(s) with paxtools " +
"(up to the first syntax error in each file)...\n";
log.error(msg, ex);
os.write(msg.getBytes());
for (File f : files) {
try {
Model m = io.convertFromOWL(getInputStream(f.getPath()));
msg = "Model that contains "
+ m.getObjects().size()
+ " elements is successfully created from "
+ f.getPath()
+ " (check the console output for warnings).\n";
os.write(msg.getBytes());
} catch (Exception e) {
msg = "Error: " + e +
" in building a BioPAX Model from: " +
f.getPath() + "\n";
os.write(msg.getBytes());
e.printStackTrace();
log.error(msg);
}
os.flush();
}
}
}
public static void toSifnx(String[] argv) throws IOException {
CommonIDFetcher idFetcher = new CommonIDFetcher();
idFetcher.setUseUniprotIDs(argv.length > 3 && argv[3].equals("uniprot"));
SIFSearcher searcher = new SIFSearcher(idFetcher, SIFEnum.values());
File blacklistFile = new File("blacklist.txt");
if(blacklistFile.exists()) {
log.info("toSifnx: will use the blacklist.txt (found in the current directory)");
searcher.setBlacklist(new Blacklist(new FileInputStream(blacklistFile)));
} else {
log.info("toSifnx: not blacklisting ubiquitous molecules (no blacklist.txt found)");
}
Model model = getModel(io, argv[1]);
ModelUtils.mergeEquivalentInteractions(model);
Set binaryInts = searcher.searchSIF(model);
OldFormatWriter.write(binaryInts, new FileOutputStream(argv[2]));
}
public static void toSif(String[] argv) throws IOException {
CommonIDFetcher idFetcher = new CommonIDFetcher();
List otherParam = new ArrayList();
otherParam.addAll(Arrays.asList(argv).subList(3, argv.length));
idFetcher.setUseUniprotIDs(otherParam.contains("uniprot"));
SIFSearcher searcher = new SIFSearcher(idFetcher, SIFEnum.values());
File blacklistFile = new File("blacklist.txt");
if(blacklistFile.exists()) {
log.info("toSif: will use the blacklist.txt (found in the current directory)");
searcher.setBlacklist(new Blacklist(new FileInputStream(blacklistFile)));
} else {
log.info("toSif: not blacklisting ubiquitous molecules (no blacklist.txt found)");
}
// check for custom fields
List fieldList = new ArrayList();
for (String param : otherParam)
{
OutputColumn.Type type = OutputColumn.Type.getType(param);
if ((type != null && type != OutputColumn.Type.CUSTOM) ||
param.contains("/"))
{
fieldList.add(param);
}
}
Model model = getModel(io, argv[1]);
ModelUtils.mergeEquivalentInteractions(model);
if (fieldList.isEmpty())
{
searcher.searchSIF(model, new FileOutputStream(argv[2]), false);
}
else if (fieldList.size() == 1 &&
fieldList.contains(OutputColumn.Type.MEDIATOR.name().toLowerCase()))
{
searcher.searchSIF(model, new FileOutputStream(argv[2]), true);
}
else
{
searcher.searchSIF(model, new FileOutputStream(argv[2]),
new CustomFormat(fieldList.toArray(new String[fieldList.size()])));
}
}
public static void integrate(String[] argv) throws IOException {
Model model1 = getModel(io, argv[1]);
Model model2 = getModel(io, argv[2]);
Integrator integrator =
new Integrator(SimpleEditorMap.get(model1.getLevel()), model1, model2);
integrator.integrate();
io.setFactory(model1.getLevel().getDefaultFactory());
io.convertToOWL(model1, new FileOutputStream(argv[3]));
}
public static void merge(String[] argv) throws IOException {
Model model1 = getModel(io, argv[1]);
Model model2 = getModel(io, argv[2]);
Merger merger = new Merger(SimpleEditorMap.get(model1.getLevel()));
merger.merge(model1, model2);
io.setFactory(model1.getLevel().getDefaultFactory());
io.convertToOWL(model1, new FileOutputStream(argv[3]));
}
/*
* Generates a blacklist file
* (to optionally use it to exclude ubiquitous small molecules,
* like ATP, when performing graph queries and exporting to
* SIF formats).
*/
public static void blacklist(String[] argv) throws IOException {
Model model = getModel(io, argv[1]);
BlacklistGenerator gen = new BlacklistGenerator();
Blacklist blacklist = gen.generateBlacklist(model);
blacklist.write(new FileOutputStream(argv[2]));
}
public static void help() {
System.out.println("(Paxtools Console) Available Operations:\n");
for (Command cmd : Command.values()) {
System.out.println(cmd.name() + " " + cmd.description);
}
System.out.println("Commands can also use compressed input files (only '.gz').\n");
}
public static void pattern(String[] argv) {
Dialog.main(argv);
}
private static Model getModel(BioPAXIOHandler io, String fName) throws IOException {
return io.convertFromOWL(getInputStream(fName));
}
//----- Section: Printing summary -------------------------------------------------------------|
public static void summarize(String[] argv) throws IOException {
log.debug("Importing the input model from " + argv[1] + "...");
Model model = getModel(io, argv[1]);
log.debug("Analyzing...");
summarize(model, argv.length > 2 ? new PrintStream(argv[2]) : null);
}
public static void summarize(Model model, PrintStream out) throws IOException {
// Produce a simplified version of the summary
if (out == null) out = System.out;
HashMap hm = new HashMap();
final SimpleEditorMap em = (model.getLevel() == BioPAXLevel.L3)
? SimpleEditorMap.L3 : SimpleEditorMap.L2;
for (Class extends BioPAXElement> clazz : sortToName(em.getKnownSubClassesOf(BioPAXElement.class)))
{
Set extends BioPAXElement> set = model.getObjects(clazz);
int initialSize = set.size();
set = filterToExactClass(set, clazz);
String s = clazz.getSimpleName() + " = " + set.size();
if (initialSize != set.size())
s += " (and " + (initialSize - set.size()) + " children)";
out.println(s);
Set editors = em.getEditorsOf(clazz);
for (PropertyEditor editor : editors)
{
Method getMethod = editor.getGetMethod();
Class> returnType = getMethod.getReturnType();
Map