package gem;
import org.biopax.paxtools.io.simpleIO.SimpleExporter;
import org.biopax.paxtools.io.simpleIO.SimpleReader;
import org.biopax.paxtools.model.BioPAXElement;
import org.biopax.paxtools.model.BioPAXLevel;
import org.biopax.paxtools.model.Model;
import org.biopax.paxtools.model.level3.*;
import org.biopax.paxtools.model.level3.Process;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Ozgun Babur
*/
public class BioPAXWriter
{
private static final String IDBASE = "http://gem.biopax.org/";
private static final String EXPRESSION = "EXPRESSION";
public static void writeBioPAX(List<Triplet> trips, String filename) throws Throwable
{
Model model = BioPAXLevel.L3.getDefaultFactory().createModel();
Map<String, BioPAXElement> map = new HashMap<String, BioPAXElement>();
for (Triplet t : trips)
{
createGene(t.M, model, map);
createGene(t.F, model, map);
createGene(t.T, model, map);
createExpression(t.T, model, map);
boolean ft = true;
double coef = Difference.calcPairwiseCoefficient(t.F, t.T);
if (coef < 0)
{
double pval = Difference.calcPairwiseCoefficientPval(t.F, t.T);
if (pval < 0.05) ft = false;
}
createFT(t.F, t.T, ft, model, map);
double gamma = Difference.calcGamma(t);
boolean mf = (gamma > 0 && ft) || (gamma < 0 && !ft);
createModulation(t.M, t.F, t.T, ft, mf, model, map);
}
SimpleExporter se = new SimpleExporter(BioPAXLevel.L3);
se.convertToOWL(model, new FileOutputStream(filename));
}
private static void createGene(Gene g, Model model, Map<String, BioPAXElement> map)
{
if (map.containsKey(g.geneid)) return;
Protein prt = model.addNew(Protein.class, IDBASE + "Protein#" + g.geneid);
ProteinReference ref = model.addNew(ProteinReference.class, IDBASE + "ProteinRef#" + g.geneid);
prt.setEntityReference(ref);
map.put(g.geneid, prt);
Xref xref = model.addNew(UnificationXref.class, IDBASE + "RefEntrezGene#" + g.geneid);
xref.setDb("ENTREZ_GENE");
xref.setId(g.geneid);
prt.addXref(xref);
ref.addXref(xref);
xref = model.addNew(UnificationXref.class, IDBASE + "RefGeneSymbol#" + g.geneid);
xref.setDb("GENE_SYMBOL");
xref.setId(Triplet.getGeneToSymbolMap().get(g.geneid));
prt.addXref(xref);
ref.addXref(xref);
}
private static void createExpression(Gene g, Model model, Map<String, BioPAXElement> map)
{
assert map.containsKey(g.geneid);
String key = g.geneid + EXPRESSION;
if (map.containsKey(key)) return;
Conversion expr = model.addNew(Conversion.class, IDBASE + "Expr#" + g.geneid);
map.put(key, expr);
expr.addRight((Protein) map.get(g.geneid));
}
private static void createFT(Gene F, Gene T, boolean positive,
Model model, Map<String, BioPAXElement> map)
{
assert map.containsKey(F.geneid);
assert map.containsKey(T.geneid);
assert map.containsKey(T.geneid + EXPRESSION);
String key = F.geneid + "#" + positive + "#" + T.geneid;
if (map.containsKey(key)) return;
Control reg = model.addNew(
Control.class, IDBASE + "FT#" + key);
reg.addController((Controller) map.get(F.geneid));
reg.addControlled((Process) map.get(T.geneid + EXPRESSION));
reg.setControlType(positive ? ControlType.ACTIVATION : ControlType.INHIBITION);
map.put(key, reg);
}
private static void createModulation(Gene M, Gene F, Gene T, boolean ft, boolean mf,
Model model, Map<String, BioPAXElement> map)
{
assert map.containsKey(M.geneid);
assert map.containsKey(concat(F.geneid, ft, T.geneid));
String key = concat(M.geneid, mf, F.geneid, ft, T.geneid);
if (map.containsKey(key)) return;
Control control = model.addNew(Control.class, IDBASE + "Mod#" + key);
control.addController((Controller) map.get(M.geneid));
control.addControlled((Process) map.get(concat(F.geneid, ft, T.geneid)));
control.setControlType(mf ? ControlType.ACTIVATION : ControlType.INHIBITION);
map.put(key, control);
}
public static String concat(Object... o)
{
if (o.length == 0) return "";
String s = o[0].toString();
for (int i = 1; i < o.length; i++)
{
s += "#" + o[i].toString();
}
return s;
}
public static void main(String[] args) throws Throwable
{
List<Triplet> trips = Triplet.readTripsAndAssociate("result/MFT.xls",
"resource/experiments_expO_1.txt", "resource/experiments_expO_2.txt");
writeBioPAX(trips, "result/trips.owl");
}
}