package gem;
import gem.util.GraphML;
import gem.util.Kronometre;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.*;
import java.util.List;
/**
* @author Ozgun Babur
*/
public class TripletGraphMLWriter extends GraphML
{
private static Map<String, Set<String>> tar2fac;
public static void main(String[] args) throws Throwable
{
Kronometre k = new Kronometre();
// writeTripsRecursive("result/temp/");
createSeriesForTargetAndFactor();
// writeTargetMachineryInGraphML("result/Result_fdr0.05_var1.0.xls");
k.stop();
k.print();
}
public static void createSeriesForTargetAndFactor() throws Throwable
{
List<Triplet> trips1 = Triplet.readTripsAndAssociate("result/All_mod-fac-tar_fdr0.05_var1.0.xls",
"resource/experiments_expO_1.txt", "resource/experiments_expO_2.txt");
Map<String, Map<String, List<Triplet>>> map1 =
new HashMap<String, Map<String, List<Triplet>>>();
for (Triplet t : trips1)
{
if (!map1.containsKey(t.tar_id)) map1.put(t.tar_id, new HashMap<String, List<Triplet>>());
if (!map1.get(t.tar_id).containsKey(t.fac_id)) map1.get(t.tar_id).put(t.fac_id, new ArrayList<Triplet>());
map1.get(t.tar_id).get(t.fac_id).add(t);
}
List<Triplet> trips2 = Triplet.readTripsAndAssociate("result/All_mod-mod-tar_fdr0.05_var1.0.xls",
"resource/experiments_expO_1.txt", "resource/experiments_expO_2.txt");
Map<String, List<Triplet>> map2 = new HashMap<String, List<Triplet>>();
for (Triplet t : trips2)
{
if (!map2.containsKey(t.tar_id)) map2.put(t.tar_id, new ArrayList<Triplet>());
map2.get(t.tar_id).add(t);
}
String loc = "result/GraphML/";
for (String tarid : map1.keySet())
{
List<Triplet> group2 = map2.get(tarid);
if (group2 == null || group2.size() < 5) continue;
String tar = Triplet.getGeneToSymbolMap().get(map1.get(tarid).values().iterator().next().iterator().next().target);
File tardir = new File(loc + tar + "|" + tarid);
if (!tardir.exists()) tardir.mkdirs();
for (String facid : map1.get(tarid).keySet())
{
String fac = Triplet.getGeneToSymbolMap().get(map1.get(tarid).get(facid).iterator().next().factor);
File dir = new File(tardir + "/" + fac + "|" + facid);
if (!map2.containsKey(tarid))
{
System.out.println("No matching T in mod-mod: " + dir);
continue;
}
if (!dir.exists()) dir.mkdirs();
List<Triplet> group1 = map1.get(tarid).get(facid);
if (group1 == null || group1.size() < 5) continue;
writeSet(group1, dir + "/MFT.graphml");
// Set<String> modsUp = new HashSet<String>();
// Set<String> modsDw = new HashSet<String>();
Set<String> mods = new HashSet<String>();
for (Triplet t : group1)
{
// double gamma = Difference.calcGamma(t);
//
// if (gamma > 0) modsUp.add(t.mod_id);
// else modsDw.add(t.mod_id);
mods.add(t.mod_id);
}
// writeSubset(modsUp, modsUp, group2, dir + "/up-up.graphml");
// writeSubset(modsDw, modsDw, group2, dir + "/dw-dw.graphml");
// writeSubset(modsUp, modsDw, group2, dir + "/up-dw.graphml");
// writeSubset(modsDw, modsUp, group2, dir + "/dw-up.graphml");
// Set<Gene> upstr = new HashSet<Gene>();
// Gene targ = null;
// for (Triplet t : group1)
// {
// upstr.add(t.M);
// upstr.add(t.F);
// targ = t.T;
// }
// drawCorrelations(upstr, targ, dir + "/correlations.graphml");
// List<Triplet> cortrip = MMCorrelation.getCorrelationTriplets(group1);
// if (!cortrip.isEmpty())
// {
// writeSet(cortrip, dir + "/correlation_deps.graphml");
// Triplet.record(cortrip, dir + "/cortrips.xls");
// }
Set<String> allupstr = new HashSet<String>();
allupstr.addAll(mods);
allupstr.add(facid);
writeSubset(mods, allupstr, group2, dir + "/MMT.graphml");
Triplet.record(group1, dir + "/MFT.xls");
}
writeSet(group2, tardir + "/all.graphml");
Triplet.record(group2, tardir + "/all.xls");
}
}
public static List<Triplet> subset(List<Triplet> trips, Set<Gene> mods, Set<Gene> facs)
{
List<Triplet> list = new ArrayList<Triplet>();
for (Triplet t : trips)
{
if (mods.contains(t.M) && facs.contains(t.F)) list.add(t);
}
return list;
}
public static void writeTripsRecursive(String path) throws Throwable
{
File dir = new File(path);
for (File f : dir.listFiles())
{
if (f.getName().endsWith(".xls"))
{
String filename = f.getPath().substring(0, f.getPath().lastIndexOf(".")) + ".graphml";
// if ((new File(filename)).exists()) continue;
List<Triplet> trips = Triplet.readTripsAndAssociate(f.getPath(),
"resource/experiments_expO_1.txt", "resource/experiments_expO_2.txt");
TripletGraphMLWriter.writeSet(trips, filename);
}
else if (f.isDirectory())
{
writeTripsRecursive(f.getPath());
}
}
}
public static void writeSubset(Set<String> from, Set<String> to, List<Triplet> trips,
String filename) throws Throwable
{
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(HEADER);
Set<String> nodes = new HashSet<String>();
for (Triplet t : trips)
{
if (from.contains(t.mod_id) && to.contains(t.fac_id))
{
if (!nodes.contains(t.mod_id))
{
writer.write(createNodeData(t.M, t.T));
nodes.add(t.mod_id);
}
if (!nodes.contains(t.fac_id))
{
writer.write(createNodeData(t.F, t.T));
nodes.add(t.fac_id);
}
writer.write(createEdgeData(t));
}
}
writer.write(FOOTER);
writer.close();
// System.out.println("Wrote file " + filename);
}
public static void writeSet(List<Triplet> trips, String filename) throws Throwable
{
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(HEADER);
Set<String> nodes = new HashSet<String>();
for (Triplet t : trips)
{
if (!nodes.contains(t.mod_id))
{
writer.write(createNodeData(t.M, t.T));
nodes.add(t.mod_id);
}
if (!nodes.contains(t.fac_id))
{
writer.write(createNodeData(t.F, t.T));
nodes.add(t.fac_id);
}
writer.write(createEdgeData(t));
}
writer.write(FOOTER);
writer.close();
// System.out.println("Wrote file " + filename);
}
private static void drawCorrelations(Collection<Gene> genes, Gene target, String filename)
throws Throwable
{
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(HEADER);
Set<String> nodes = new HashSet<String>();
for (Gene x : genes)
{
for (Gene y : genes)
{
if (x.geneid.equals(y.geneid)) continue;
double effect = calcPairwiseEffect(x, y);
if (Math.abs(effect) < 0.3) continue;
if (!nodes.contains(x.id))
{
writer.write(createNodeData(x, target));
nodes.add(x.id);
}
if (!nodes.contains(y.id))
{
writer.write(createNodeData(y, target));
nodes.add(y.id);
}
writer.write(createEdgeData(x.id, y.id,
getColor(effect, -0.8, 0, 0.8, DOWNR_C, NOTR_C, UPREG_C), true, true));
}
}
writer.write(FOOTER);
writer.close();
}
private static String createEdgeData(Triplet t)
{
double gamma = Difference.calcGamma(t);
boolean solid = (!t.categ.startsWith("OR") && gamma > 0) || t.categ.startsWith("OR_A");
return createEdgeData(t.mod_id, t.fac_id, getEdgeColor(t.categ), solid, true);
}
private static String createNodeData(Gene gene, Gene target)
{
assert gene != null;
assert target != null;
double effect = calcPairwiseEffect(gene, target);
boolean isfactor = tar2fac.get(target.geneid).contains(gene.geneid);
// boolean isfactor = false;
return createNodeData(gene.id, Triplet.getGeneToSymbolMap().get(gene.geneid),
// return createNodeData(gene.id, gene.geneid,
getColor(effect), effect > 0 ? 1 : 2, isfactor);
}
public static double calcPairwiseEffect(Gene x, Gene y)
{
int c[][] = CaseCounter.countTwo(x, y);
double p1 = c[1][1] / (double) (c[1][1] + c[1][0]);
double p0 = c[0][1] / (double) (c[0][1] + c[0][0]);
return p1 - p0;
}
private static Color getEdgeColor(String category)
{
Color c = edgeColorMap.get(category);
if (c != null) return c;
return Color.BLACK;
}
private static Map<String, Color> edgeColorMap = new HashMap<String, Color>();
static
{
edgeColorMap.put("Enhances Activation", new Color(33, 137, 33));
edgeColorMap.put("Attenuates Activation", new Color(144, 238, 144));
edgeColorMap.put("Inverts Activation", new Color(253, 48, 48));
edgeColorMap.put("Enhances Inhibition", new Color(106, 33, 137));
edgeColorMap.put("Attenuates Inhibition", new Color(210, 143, 238));
edgeColorMap.put("Inverts Inhibition", new Color(253, 207, 47));
edgeColorMap.put("MoA Insignificant", new Color(200, 200, 200));
edgeColorMap.put("OR_ACTIVATION", new Color(120, 120, 210));
edgeColorMap.put("OR_INHIBITION", new Color(120, 120, 210));
tar2fac = new HashMap<String, Set<String>>();
List<Triplet> trips = Triplet.readTrips("resource/triplets.txt");
for (Triplet t : trips)
{
if (!tar2fac.containsKey(t.target)) tar2fac.put(t.target, new HashSet<String>());
tar2fac.get(t.target).add(t.factor);
}
}
}