6035 |
29 Oct 20 |
nicklas |
1 |
package net.sf.basedb.reggie.script; |
2863 |
28 Oct 14 |
nicklas |
2 |
|
2871 |
28 Oct 14 |
nicklas |
3 |
import java.io.IOException; |
2863 |
28 Oct 14 |
nicklas |
4 |
import java.util.ArrayList; |
2863 |
28 Oct 14 |
nicklas |
5 |
import java.util.Arrays; |
3506 |
23 Sep 15 |
nicklas |
6 |
import java.util.Collections; |
2863 |
28 Oct 14 |
nicklas |
7 |
import java.util.List; |
2863 |
28 Oct 14 |
nicklas |
8 |
import java.util.Map; |
2863 |
28 Oct 14 |
nicklas |
9 |
|
2863 |
28 Oct 14 |
nicklas |
10 |
import net.sf.basedb.core.DbControl; |
2863 |
28 Oct 14 |
nicklas |
11 |
import net.sf.basedb.reggie.Reggie; |
2863 |
28 Oct 14 |
nicklas |
12 |
import net.sf.basedb.reggie.XmlConfig; |
2863 |
28 Oct 14 |
nicklas |
13 |
import net.sf.basedb.reggie.dao.Rawbioassay; |
6062 |
18 Nov 20 |
nicklas |
14 |
import net.sf.basedb.reggie.pdf.PdfUtil7; |
2863 |
28 Oct 14 |
nicklas |
15 |
import net.sf.basedb.util.Values; |
2863 |
28 Oct 14 |
nicklas |
16 |
|
2863 |
28 Oct 14 |
nicklas |
17 |
|
2863 |
28 Oct 14 |
nicklas |
18 |
/** |
3506 |
23 Sep 15 |
nicklas |
Helper class for running the 'GeneReport' R script. |
2863 |
28 Oct 14 |
nicklas |
20 |
|
2863 |
28 Oct 14 |
nicklas |
@author nicklas |
2863 |
28 Oct 14 |
nicklas |
@since 2.18 |
2863 |
28 Oct 14 |
nicklas |
23 |
*/ |
2863 |
28 Oct 14 |
nicklas |
24 |
public class GeneReport |
2863 |
28 Oct 14 |
nicklas |
25 |
extends RScriptDefinition |
2863 |
28 Oct 14 |
nicklas |
26 |
{ |
3055 |
19 Dec 14 |
nicklas |
27 |
|
2863 |
28 Oct 14 |
nicklas |
28 |
private final List<String> genes; |
3506 |
23 Sep 15 |
nicklas |
29 |
private final float plotWidth; |
3506 |
23 Sep 15 |
nicklas |
30 |
private final float plotHeight; |
2863 |
28 Oct 14 |
nicklas |
31 |
|
2991 |
02 Dec 14 |
nicklas |
32 |
private RFunction scanB; |
2991 |
02 Dec 14 |
nicklas |
33 |
private RFunction valid; |
2991 |
02 Dec 14 |
nicklas |
34 |
|
2863 |
28 Oct 14 |
nicklas |
35 |
/** |
2863 |
28 Oct 14 |
nicklas |
Create a new GeneReport instance. It will be initialized with default |
2863 |
28 Oct 14 |
nicklas |
parameters. Path to R script must be in configuration file at |
3506 |
23 Sep 15 |
nicklas |
<cfg>/path. Directory with reference data can optionally be |
3506 |
23 Sep 15 |
nicklas |
specified by <cfg>/ref-dir-scanb and <cfg>/ref-dir-validation |
3506 |
23 Sep 15 |
nicklas |
or it will be assumed that it is found in the same directory |
3506 |
23 Sep 15 |
nicklas |
as the R script. |
2863 |
28 Oct 14 |
nicklas |
42 |
*/ |
3620 |
24 Nov 15 |
nicklas |
43 |
public GeneReport(String cfg, String parameterSet, float plotWidth, float plotHeight, String... genes) |
2993 |
03 Dec 14 |
nicklas |
44 |
throws IOException |
2863 |
28 Oct 14 |
nicklas |
45 |
{ |
3506 |
23 Sep 15 |
nicklas |
46 |
this.genes = Collections.unmodifiableList(Arrays.asList(genes)); |
3506 |
23 Sep 15 |
nicklas |
47 |
this.plotWidth = plotWidth; |
3506 |
23 Sep 15 |
nicklas |
48 |
this.plotHeight = plotHeight; |
3506 |
23 Sep 15 |
nicklas |
49 |
|
2863 |
28 Oct 14 |
nicklas |
50 |
XmlConfig config = Reggie.getConfig(); |
3506 |
23 Sep 15 |
nicklas |
// Get and check path to script file |
3620 |
24 Nov 15 |
nicklas |
52 |
String script_path = config.getRequiredConfig(cfg+"/path", parameterSet); |
3506 |
23 Sep 15 |
nicklas |
53 |
Reggie.checkFile(script_path, false); |
3506 |
23 Sep 15 |
nicklas |
54 |
setScript(script_path); |
2863 |
28 Oct 14 |
nicklas |
55 |
|
3620 |
24 Nov 15 |
nicklas |
56 |
String ref_dir_scanb = config.getConfig(cfg+"/ref-dir-scanb", parameterSet, getScriptDir()); |
3620 |
24 Nov 15 |
nicklas |
57 |
String ref_dir_validation = config.getConfig(cfg+"/ref-dir-validation", parameterSet, getScriptDir()); |
3506 |
23 Sep 15 |
nicklas |
58 |
|
2993 |
03 Dec 14 |
nicklas |
// Check that files and directories exists |
3506 |
23 Sep 15 |
nicklas |
60 |
Reggie.checkFile(ref_dir_scanb, true); |
3506 |
23 Sep 15 |
nicklas |
61 |
Reggie.checkFile(ref_dir_validation, true); |
2991 |
02 Dec 14 |
nicklas |
62 |
|
2991 |
02 Dec 14 |
nicklas |
63 |
scanB = addFunction("geneReport"); |
3002 |
04 Dec 14 |
nicklas |
64 |
setDefaultGeneReportParameters(scanB); |
2991 |
02 Dec 14 |
nicklas |
65 |
scanB.setParameter("ref.dir", "'" + ref_dir_scanb + "'"); |
2991 |
02 Dec 14 |
nicklas |
66 |
scanB.setParameter("file.prefix", "'scanb'"); |
2991 |
02 Dec 14 |
nicklas |
67 |
|
2991 |
02 Dec 14 |
nicklas |
68 |
valid = addFunction("geneReport"); |
3002 |
04 Dec 14 |
nicklas |
69 |
setDefaultGeneReportParameters(valid); |
2991 |
02 Dec 14 |
nicklas |
70 |
valid.setParameter("ref.dir", "'" + ref_dir_validation + "'"); |
2991 |
02 Dec 14 |
nicklas |
71 |
valid.setParameter("file.prefix", "'valid'"); |
2863 |
28 Oct 14 |
nicklas |
72 |
} |
2863 |
28 Oct 14 |
nicklas |
73 |
|
3002 |
04 Dec 14 |
nicklas |
74 |
private void setDefaultGeneReportParameters(RFunction f) |
3002 |
04 Dec 14 |
nicklas |
75 |
{ |
3002 |
04 Dec 14 |
nicklas |
// These are really required to generate the result that |
3002 |
04 Dec 14 |
nicklas |
// is compatible with the template pdf |
6062 |
18 Nov 20 |
nicklas |
78 |
f.setParameter("width", plotWidth / PdfUtil7.DPI); |
6062 |
18 Nov 20 |
nicklas |
79 |
f.setParameter("height", plotHeight / PdfUtil7.DPI); |
3002 |
04 Dec 14 |
nicklas |
80 |
f.setParameter("pointsize", "6"); |
3002 |
04 Dec 14 |
nicklas |
81 |
f.setParameter("outfile", "'pdf'"); |
3002 |
04 Dec 14 |
nicklas |
82 |
f.setParameter("extra.text", "F"); |
3002 |
04 Dec 14 |
nicklas |
// These are default in the script but if we set them here |
3002 |
04 Dec 14 |
nicklas |
// there is no way to test other combinations |
3002 |
04 Dec 14 |
nicklas |
85 |
/* |
3002 |
04 Dec 14 |
nicklas |
f.setParameter("use.fix.xlim", "T"); |
3002 |
04 Dec 14 |
nicklas |
f.setParameter("box", "T"); |
3002 |
04 Dec 14 |
nicklas |
f.setParameter("line", "F"); |
3002 |
04 Dec 14 |
nicklas |
f.setParameter("density", "T"); |
3002 |
04 Dec 14 |
nicklas |
f.setParameter("weight.density", "T"); |
3002 |
04 Dec 14 |
nicklas |
f.setParameter("no.yaxis", "F"); |
3002 |
04 Dec 14 |
nicklas |
92 |
*/ |
3002 |
04 Dec 14 |
nicklas |
93 |
} |
3002 |
04 Dec 14 |
nicklas |
94 |
|
2863 |
28 Oct 14 |
nicklas |
95 |
/** |
2863 |
28 Oct 14 |
nicklas |
Get the list of genes to get a report for. |
2863 |
28 Oct 14 |
nicklas |
97 |
*/ |
2863 |
28 Oct 14 |
nicklas |
98 |
public List<String> getGenes() |
2863 |
28 Oct 14 |
nicklas |
99 |
{ |
2863 |
28 Oct 14 |
nicklas |
100 |
return genes; |
2863 |
28 Oct 14 |
nicklas |
101 |
} |
2863 |
28 Oct 14 |
nicklas |
102 |
|
2863 |
28 Oct 14 |
nicklas |
103 |
/** |
2863 |
28 Oct 14 |
nicklas |
Run the gene report script for the given raw bioassay. It will calculate |
2863 |
28 Oct 14 |
nicklas |
the sum of fpkm for the specified genes and then submit those values to |
2863 |
28 Oct 14 |
nicklas |
the R script. |
2863 |
28 Oct 14 |
nicklas |
@see Rawbioassay#getFpkmSum(DbControl, java.util.Collection) |
2863 |
28 Oct 14 |
nicklas |
108 |
*/ |
2871 |
28 Oct 14 |
nicklas |
109 |
public Result run(DbControl dc, Rawbioassay raw) |
2863 |
28 Oct 14 |
nicklas |
110 |
{ |
2990 |
02 Dec 14 |
nicklas |
111 |
|
2863 |
28 Oct 14 |
nicklas |
// Calculate sum(fpkm) for given genes |
2863 |
28 Oct 14 |
nicklas |
113 |
Map<String, Float> sums = raw.getFpkmSum(dc, genes); |
2863 |
28 Oct 14 |
nicklas |
114 |
|
2863 |
28 Oct 14 |
nicklas |
115 |
List<Float> v = new ArrayList<Float>(); |
2863 |
28 Oct 14 |
nicklas |
116 |
for (String gene : genes) |
2863 |
28 Oct 14 |
nicklas |
117 |
{ |
2863 |
28 Oct 14 |
nicklas |
118 |
Float s = sums.get(gene); |
2863 |
28 Oct 14 |
nicklas |
119 |
v.add(s == null ? 0 : s); |
2863 |
28 Oct 14 |
nicklas |
120 |
} |
2863 |
28 Oct 14 |
nicklas |
121 |
|
2993 |
03 Dec 14 |
nicklas |
122 |
String values = Values.getString(v, ",", true); |
6036 |
02 Nov 20 |
nicklas |
123 |
String caseName = ScriptDefinition.checkValidScriptParameter(raw.getName()); |
2993 |
03 Dec 14 |
nicklas |
124 |
|
2993 |
03 Dec 14 |
nicklas |
125 |
valid.setParameter("value", "c(" + values + ")"); |
2993 |
03 Dec 14 |
nicklas |
126 |
valid.setParameter("case", "'"+caseName+"'"); |
2993 |
03 Dec 14 |
nicklas |
127 |
scanB.setParameter("value", "c(" + values + ")"); |
2993 |
03 Dec 14 |
nicklas |
128 |
scanB.setParameter("case", "'"+caseName+"'"); |
2863 |
28 Oct 14 |
nicklas |
129 |
|
3506 |
23 Sep 15 |
nicklas |
130 |
Result result = run(new Result(raw, genes)); |
2863 |
28 Oct 14 |
nicklas |
131 |
return result; |
2863 |
28 Oct 14 |
nicklas |
132 |
} |
2863 |
28 Oct 14 |
nicklas |
133 |
|
3506 |
23 Sep 15 |
nicklas |
134 |
/** |
3506 |
23 Sep 15 |
nicklas |
The results keep track of the raw bioassay and genes that |
3506 |
23 Sep 15 |
nicklas |
was used. |
3506 |
23 Sep 15 |
nicklas |
137 |
*/ |
2871 |
28 Oct 14 |
nicklas |
138 |
public class Result |
6036 |
02 Nov 20 |
nicklas |
139 |
extends ScriptResult |
2871 |
28 Oct 14 |
nicklas |
140 |
{ |
3506 |
23 Sep 15 |
nicklas |
141 |
public final Rawbioassay raw; |
3506 |
23 Sep 15 |
nicklas |
142 |
public final List<String> genes; |
3506 |
23 Sep 15 |
nicklas |
143 |
|
2871 |
28 Oct 14 |
nicklas |
144 |
/** |
2871 |
28 Oct 14 |
nicklas |
Creates a new result object for the given raw bioassay. |
2871 |
28 Oct 14 |
nicklas |
146 |
*/ |
3506 |
23 Sep 15 |
nicklas |
147 |
public Result(Rawbioassay raw, List<String> genes) |
2871 |
28 Oct 14 |
nicklas |
148 |
{ |
2991 |
02 Dec 14 |
nicklas |
149 |
super(); |
2871 |
28 Oct 14 |
nicklas |
150 |
this.raw = raw; |
3506 |
23 Sep 15 |
nicklas |
151 |
this.genes = genes; |
2871 |
28 Oct 14 |
nicklas |
152 |
} |
2871 |
28 Oct 14 |
nicklas |
153 |
|
2871 |
28 Oct 14 |
nicklas |
154 |
} |
2863 |
28 Oct 14 |
nicklas |
155 |
} |