Skip to content

Commit

Permalink
Fine tuned server-side behavior of some parameters.
Browse files Browse the repository at this point in the history
Added a sample analysis folder.
Added new executable for converting SIF files to json.
  • Loading branch information
ozgunbabur committed Jan 27, 2021
1 parent b003bf0 commit 06cd6a1
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 25 deletions.
142 changes: 130 additions & 12 deletions src/main/java/org/panda/causalpath/network/GraphWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.panda.causalpath.analyzer.NSCForNonCorr;
import org.panda.causalpath.analyzer.NetworkSignificanceCalculator;
import org.panda.causalpath.data.*;
import org.panda.utility.ArrayUtil;
import org.panda.utility.CollectionUtil;
import org.panda.utility.FileUtil;
import org.panda.utility.ValToColor;
Expand Down Expand Up @@ -477,18 +478,7 @@ else if (data instanceof ActivityData)

for (String sym : data.getGeneSymbols())
{
if (!geneMap.containsKey(sym))
{
HashMap<String, Object> node = new HashMap<>();
geneMap.put(sym, node);
nodes.add(node);
Map<String, Object> d = new HashMap<>();
node.put("data", d);
d.put("id", sym);
d.put("text", sym);
List<Map> sites = new ArrayList<>();
d.put("sites", sites);
}
initJsonNode(geneMap, nodes, sym);

Map node = geneMap.get(sym);

Expand Down Expand Up @@ -541,6 +531,134 @@ else if (data instanceof ActivityData)
writer.close();
}

/**
* This method takes in a SIF graph defined by two files (.sif and .format), and generates a corresponding .json
* file that the webserver can display.
*
* @param sifFileanme SIF filename
* @param formatFilename Format filename
* @param outJasonFilename JASON filename to produce
*/
public static void convertSIFToJSON(String sifFileanme, String formatFilename, String outJasonFilename) throws IOException
{
Map<String, Object> map = new HashMap<>();
List<Map> nodes = new ArrayList<>();
List<Map> edges = new ArrayList<>();
map.put("nodes", nodes);
map.put("edges", edges);

Map<String, Map> nodeMap = new HashMap<>();
Set<String> relMem = new HashSet<>();

Files.lines(Paths.get(sifFileanme)).map(l -> l.split("\t")).forEach(t ->
{
if (t.length > 2)
{
String key = t[0] + "\t" + t[1] + "\t" + t[2];
if (relMem.contains(key)) return;
else relMem.add(key);

Map<String, Object> edge = new HashMap<>();
edges.add(edge);
Map<String, Object> dMap = new HashMap<>();
edge.put("data", dMap);
dMap.put("source", t[0]);
dMap.put("target", t[2]);
dMap.put("edgeType", t[1]);
if (t.length > 4 && !t[4].trim().isEmpty())
{
dMap.put("tooltipText", t[2] + "-" + CollectionUtil.merge(Arrays.asList(t[4].split(";")), "-"));
}

if (t.length > 3 && !t[3].trim().isEmpty())
{
List<String> medList = Arrays.asList(t[3].split(";| "));
if (!medList.isEmpty())
{
dMap.put("pcLinks", medList);
}
}
initJsonNode(nodeMap, nodes, t[2]);
}

if (t.length > 0 && !t[0].isEmpty()) initJsonNode(nodeMap, nodes, t[0]);
});

Map<String, String> defaultColors = new HashMap<>();
String defBGCKey = "node BG color";
String defBorCKey = "node border color";

Files.lines(Paths.get(formatFilename)).map(l -> l.split("\t")).filter(t -> t.length > 3).forEach(t ->
{
if (t[1].equals("all-nodes"))
{
if (t[2].equals("color")) defaultColors.put(defBGCKey, jasonizeColor(t[3]));
else if (t[2].equals("bordercolor")) defaultColors.put(defBorCKey, jasonizeColor(t[3]));
}

if (t[0].equals("node"))
{
String name = t[1];
Map node = nodeMap.get(name);
if (node != null)
{
if (!node.containsKey("css")) node.put("css", new HashMap<>());

switch (t[2])
{
case "rppasite":
String[] x = t[3].split("\\|");
Map site = new HashMap();
((List) ((Map) node.get("data")).get("sites")).add(site);
site.put("siteText", x[1]);
site.put("siteInfo", x[0] + (x.length > 4 ? (" " + x[4]) : ""));
site.put("siteBackgroundColor", jasonizeColor(x[2]));
site.put("siteBorderColor", jasonizeColor(x[3]));
break;
case "color":
((Map) node.get("css")).put("backgroundColor", jasonizeColor(t[3]));
break;
case "bordercolor":
((Map) node.get("css")).put("borderColor", jasonizeColor(t[3]));
break;
case "borderwidth":
((Map) node.get("css")).put("borderWidth", t[3] + "px");
break;
case "tooltip":
((Map) node.get("data")).put("tooltipText", t[3]);
break;
}
}
}
});

BufferedWriter writer = Files.newBufferedWriter(Paths.get(outJasonFilename));
JsonUtils.writePrettyPrint(writer, map);
writer.close();
}

private static void initJsonNode(Map<String, Map> nodeMap, List<Map> nodes, String name)
{
if (!nodeMap.containsKey(name))
{
HashMap<String, Object> node = new HashMap<>();
nodeMap.put(name, node);
nodes.add(node);
Map<String, Object> d = new HashMap<>();
node.put("data", d);
d.put("id", name);
d.put("text", name);
List<Map> sites = new ArrayList<>();
d.put("sites", sites);
}
}

private static String jasonizeColor(String c)
{
String[] t = c.split(" ");
return "rgb(" + ArrayUtil.getString(",", t[0], t[1], t[2]) + ")";
}

private Set<ExperimentData> getExperimentDataToDraw()
{
if (experimentDataToDraw != null) return experimentDataToDraw;
Expand Down
34 changes: 21 additions & 13 deletions src/main/java/org/panda/causalpath/run/CausalPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,8 @@ enum ValueTransformation
"detection, using the threshold-for-data-significance.", false),

MAX("max", "The value with maximum absolute is used for the analysis. There should only be one group of " +
"values (marked with value-column), the values have to be distributed around " +
"zero, and a threshold value should be provided for significance detection, using the " +
"threshold-for-data-significance.", false),
"values (marked with value-column), the values have to be distributed around zero, and a threshold value " +
"should be provided for significance detection, using the threshold-for-data-significance.", false),

DIFFERENCE_OF_MEANS("difference-of-means", "There should be control and test values, whose difference would " +
"be used for significance detection. The threshold for significance (threshold-for-data-significance) " +
Expand All @@ -1052,13 +1051,13 @@ enum ValueTransformation
SIGNIFICANT_CHANGE_OF_MEAN("significant-change-of-mean", "There should be sufficient amount of control and " +
"test values to detect the significance of change with a t-test. Technically there should be more than 3" +
" controls and 3 tests, practically, they should be much more to provide statistical power. The " +
"threshold-for-data-significance should be used for a p-value threshold, or " +
"alternatively, fdr-threshold-for-data-significance should be used for " +
"controlling significance at the false discovery rate level.", true),
"threshold-for-data-significance should be used for a p-value threshold, or alternatively, " +
"fdr-threshold-for-data-significance should be used for controlling significance at the false discovery " +
"rate level.", true),

SIGNIFICANT_CHANGE_OF_MEAN_PAIRED("significant-change-of-mean-paired", "There should be sufficient amount of " +
"control and test values to detect the significance of change with a paired t-test. Technically there " +
"should be more than 3 controls and 3 tests, practically, they should be much more to provide statistical" +
"should be at least 3 controls and 3 tests, practically, they should be much more to provide statistical" +
" power. The order of control and test value columns indicate the pairing. First control column in the " +
"parameters file is paired with first test column, second is paired with second, etc. The " +
"threshold-for-data-significance should be used for a p-value threshold, or alternatively, " +
Expand All @@ -1068,11 +1067,18 @@ enum ValueTransformation
SIGNED_P_VALUES("signed-p-values", "If the dataset has its own calculation of p-values desired to be used " +
"directly, then for each comparison, there must be a column in the dataset that has these p-values " +
"multiplied with the sign of the change. For instance a value -0.001 means downregulation with a p-value " +
"of 0.001. If an FDR control is desired, then the p-values should not be adjusted.", false),
"of 0.001. Don't use 0 and -0 values in the data file with this option. Java cannot distinguish between " +
"the two. Instead, convert 0 values to very small but nonzero values, such as 1e-10 or -1e-10. " +
"If an FDR control is desired, there are two ways to have it. First way is to use unadjusted p-values in " +
"the data file and then use the fdr-threshold-for-data-significance parameter to set the desired FDR " +
"level. Second way is to use adjusted p-values in the data file and use the " +
"threshold-for-data-significance parameter to set the FDR level. Don't mix these two ways. " +
"If fdr-threshold-for-data-significance is used over already-adjusted p-values, then the code will apply " +
"the Benjamini-Hochberg procedure over those already-adjusted p-values.", false),

CORRELATION("correlation", "There should be one group of values (marked with value-column). There must be at " +
"least 3 value columns technically, but many more " +
"than that practically to have some statistical power for significant correlation. ", false);
"least 3 value columns technically, but many more than that practically to have some statistical power " +
"for significant correlation. ", false);

ValueTransformation(String name, String description, boolean twoGroupComparison)
{
Expand Down Expand Up @@ -1332,7 +1338,9 @@ enum Parameter
CALCULATE_NETWORK_SIGNIFICANCE((value, cp) -> cp.calculateNetworkSignificance = webServerMode ? false : Boolean.valueOf(value),
"Calculate network significance",
"Whether to calculate significances of the properties of the graph. When turned on, a p-value for network" +
" size, and also downstream activity enrichment p-values for each gene on the graph are calculated.",
" size, and also downstream activity enrichment p-values for each gene on the graph are calculated. " +
"This parameter is ignored by webserver due to resource limitations. To calculate network significance, " +
"please run CausalPath locally from its JAR file.",
new EntryType(Boolean.class), new Boolean[][]{{Boolean.FALSE}}, true, false, new Cond(Logical.NOT)),
PERMUTATIONS_FOR_SIGNIFICANCE((value, cp) -> cp.permutationCount = Integer.valueOf(value),
"Number of permutations for calculating network significance",
Expand Down Expand Up @@ -1410,7 +1418,7 @@ enum Parameter
new String[]{NetworkLoader.ResourceType.PC.name()},
new String[]{NetworkLoader.ResourceType.PhosphoNetworks.name()},
new String[]{NetworkLoader.ResourceType.IPTMNet.name()}},
true, true, null),
false, true, null),
RELATION_FILTER_TYPE((value, cp) ->
{
if (!cp.cs.hasGraphFilter())
Expand Down Expand Up @@ -1453,7 +1461,7 @@ enum Parameter
"Specifies the value where node colors reach most intense color. Has to be a positive value, and used " +
"symmetrically. In the case of value-transformation is significant-change-of-mean, the value is " +
"-log(p) with a sign associated to it.",
new EntryType(Double.class), new String[][]{{"1"}}, false, false,
new EntryType(Double.class), new String[][]{{"10"}}, false, false,
new Cond( Logical.NOT, new Cond(VALUE_TRANSFORMATION.getText(), ValueTransformation.CORRELATION.name))),
SHOW_ALL_GENES_WITH_PROTEOMIC_DATA((value, cp) -> cp.showAllGenesWithProteomicData = Boolean.valueOf(value),
"Show all genes with significant proteomic data",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.panda.causalpath.run;

import org.panda.causalpath.network.GraphWriter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* This class is used for converting SIF and format files to json files that can be uploaded to the webserver for
* visualization.
*
* Usage: java -jar causalpath.jar <in-dir> <sif-name> <out-dir> <json-name>
*
*/
public class JasonizeResultGraphsRecursively
{
public static void main(String[] args) throws IOException
{
String inBase = new File(args[0]).getPath();
String sifNameWOExtension = args[1];
String outBase = new File(args[2]).getPath();
String jsonName = args.length > 3 ? args[3] : "causative.json";

generate(inBase, inBase, sifNameWOExtension, outBase, jsonName);
}

private static void generate(String inBase, String inDir, String sifName, String outBase, String jsonName) throws IOException
{
String sifPath = inDir + File.separator + sifName + ".sif";
String formatPath = inDir + File.separator + sifName + ".format";

if (Files.exists(Paths.get(sifPath)) && Files.exists(Paths.get(formatPath)))
{
String outDir = inDir.replace(inBase, outBase);
Files.createDirectories(Paths.get(outDir));

GraphWriter.convertSIFToJSON(sifPath, formatPath, outDir + File.separator + jsonName);
}

for (File sub : new File(inDir).listFiles())
{
if (sub.isDirectory()) generate(inBase, sub.getPath(), sifName, outBase, jsonName);
}
}
}
Binary file added wiki/sample-data-and-parameters.zip
Binary file not shown.

0 comments on commit 06cd6a1

Please sign in to comment.