5049 |
23 Oct 18 |
nicklas |
1 |
package net.sf.basedb.reggie.baf; |
5049 |
23 Oct 18 |
nicklas |
2 |
|
5049 |
23 Oct 18 |
nicklas |
3 |
import java.io.IOException; |
5049 |
23 Oct 18 |
nicklas |
4 |
import java.io.OutputStream; |
5049 |
23 Oct 18 |
nicklas |
5 |
import java.io.OutputStreamWriter; |
5056 |
26 Oct 18 |
nicklas |
6 |
import java.util.Date; |
5049 |
23 Oct 18 |
nicklas |
7 |
|
5049 |
23 Oct 18 |
nicklas |
8 |
import net.sf.basedb.core.DbControl; |
5049 |
23 Oct 18 |
nicklas |
9 |
import net.sf.basedb.core.DerivedBioAssay; |
5056 |
26 Oct 18 |
nicklas |
10 |
import net.sf.basedb.reggie.Reggie; |
6035 |
29 Oct 20 |
nicklas |
11 |
import net.sf.basedb.reggie.script.RFunction; |
6036 |
02 Nov 20 |
nicklas |
12 |
import net.sf.basedb.reggie.script.ScriptResult; |
6035 |
29 Oct 20 |
nicklas |
13 |
import net.sf.basedb.reggie.script.RScriptDefinition; |
6036 |
02 Nov 20 |
nicklas |
14 |
import net.sf.basedb.reggie.script.ScriptDefinition; |
6035 |
29 Oct 20 |
nicklas |
15 |
import net.sf.basedb.reggie.script.TemporaryWorkDir; |
5049 |
23 Oct 18 |
nicklas |
16 |
import net.sf.basedb.util.FileUtil; |
5049 |
23 Oct 18 |
nicklas |
17 |
import net.sf.basedb.util.export.TableWriter; |
5049 |
23 Oct 18 |
nicklas |
18 |
|
5049 |
23 Oct 18 |
nicklas |
19 |
/** |
5049 |
23 Oct 18 |
nicklas |
Represents an R script for generating a mBAF plot for all regions. |
5049 |
23 Oct 18 |
nicklas |
The script is located in 'bafPlot.R' and is copied to the working |
5049 |
23 Oct 18 |
nicklas |
directory when running this class. |
5049 |
23 Oct 18 |
nicklas |
23 |
|
5049 |
23 Oct 18 |
nicklas |
@author nicklas |
5049 |
23 Oct 18 |
nicklas |
@since 4.20 |
5049 |
23 Oct 18 |
nicklas |
26 |
*/ |
5049 |
23 Oct 18 |
nicklas |
27 |
public class MBafPlot |
5049 |
23 Oct 18 |
nicklas |
28 |
extends RScriptDefinition |
5049 |
23 Oct 18 |
nicklas |
29 |
{ |
5049 |
23 Oct 18 |
nicklas |
30 |
|
5050 |
24 Oct 18 |
nicklas |
31 |
private final MBafOptions options; |
5050 |
24 Oct 18 |
nicklas |
32 |
private final RFunction mBafPlot; |
5431 |
16 May 19 |
nicklas |
33 |
private final TemporaryWorkDir tmp; |
5049 |
23 Oct 18 |
nicklas |
34 |
|
5050 |
24 Oct 18 |
nicklas |
35 |
public MBafPlot(MBafOptions options) |
5049 |
23 Oct 18 |
nicklas |
36 |
{ |
5056 |
26 Oct 18 |
nicklas |
37 |
try |
5056 |
26 Oct 18 |
nicklas |
38 |
{ |
5431 |
16 May 19 |
nicklas |
39 |
this.tmp = TemporaryWorkDir.create("BAFPlot", Reggie.CONVERTER_DATE_TO_STRING.convert(new Date()), this); |
5431 |
16 May 19 |
nicklas |
40 |
setWorkDir(tmp.getWorkDir().getAbsolutePath()); |
5056 |
26 Oct 18 |
nicklas |
41 |
setScript("./bafPlot.R"); |
5056 |
26 Oct 18 |
nicklas |
42 |
this.mBafPlot = addFunction("mBafPlot"); |
5056 |
26 Oct 18 |
nicklas |
43 |
this.options = options; |
5056 |
26 Oct 18 |
nicklas |
44 |
} |
5056 |
26 Oct 18 |
nicklas |
45 |
catch (IOException ex) |
5056 |
26 Oct 18 |
nicklas |
46 |
{ |
5056 |
26 Oct 18 |
nicklas |
47 |
throw new RuntimeException(ex); |
5056 |
26 Oct 18 |
nicklas |
48 |
} |
5049 |
23 Oct 18 |
nicklas |
49 |
} |
5049 |
23 Oct 18 |
nicklas |
50 |
|
5049 |
23 Oct 18 |
nicklas |
51 |
/** |
5049 |
23 Oct 18 |
nicklas |
Run the mBAF plot script for the given aligned sequences. |
5049 |
23 Oct 18 |
nicklas |
53 |
*/ |
5049 |
23 Oct 18 |
nicklas |
54 |
public Result run(DbControl dc, DerivedBioAssay alignment, BafData bafData) |
5049 |
23 Oct 18 |
nicklas |
55 |
{ |
6036 |
02 Nov 20 |
nicklas |
56 |
mBafPlot.setParameter("sampleName", "'"+ScriptDefinition.checkValidScriptParameter(alignment.getName())+"'"); |
5050 |
24 Oct 18 |
nicklas |
57 |
mBafPlot.setParameter("pval", options.getSignificantPVal()); |
5051 |
24 Oct 18 |
nicklas |
58 |
mBafPlot.setParameter("minCount", options.getMinSnpCountForRegion()); |
5049 |
23 Oct 18 |
nicklas |
59 |
Result result = run(new Result(bafData)); |
5049 |
23 Oct 18 |
nicklas |
60 |
return result; |
5049 |
23 Oct 18 |
nicklas |
61 |
} |
5056 |
26 Oct 18 |
nicklas |
62 |
|
5056 |
26 Oct 18 |
nicklas |
63 |
/** |
5431 |
16 May 19 |
nicklas |
Cleanup the temporary workdir |
5056 |
26 Oct 18 |
nicklas |
65 |
*/ |
5056 |
26 Oct 18 |
nicklas |
66 |
@Override |
5431 |
16 May 19 |
nicklas |
67 |
public void removeWorkDir() |
5056 |
26 Oct 18 |
nicklas |
68 |
{ |
5431 |
16 May 19 |
nicklas |
69 |
tmp.close(); |
5431 |
16 May 19 |
nicklas |
70 |
setWorkDir(null); |
5056 |
26 Oct 18 |
nicklas |
71 |
} |
5049 |
23 Oct 18 |
nicklas |
72 |
|
5049 |
23 Oct 18 |
nicklas |
73 |
public class Result |
6036 |
02 Nov 20 |
nicklas |
74 |
extends ScriptResult |
5049 |
23 Oct 18 |
nicklas |
75 |
{ |
5049 |
23 Oct 18 |
nicklas |
76 |
|
5049 |
23 Oct 18 |
nicklas |
77 |
private final BafData bafData; |
5049 |
23 Oct 18 |
nicklas |
78 |
|
5049 |
23 Oct 18 |
nicklas |
79 |
/** |
5049 |
23 Oct 18 |
nicklas |
Creates a new result object for the given raw bioassay. |
5049 |
23 Oct 18 |
nicklas |
81 |
*/ |
5049 |
23 Oct 18 |
nicklas |
82 |
public Result(BafData bafData) |
5049 |
23 Oct 18 |
nicklas |
83 |
{ |
5049 |
23 Oct 18 |
nicklas |
84 |
super(); |
5049 |
23 Oct 18 |
nicklas |
85 |
this.bafData = bafData; |
5049 |
23 Oct 18 |
nicklas |
86 |
} |
5049 |
23 Oct 18 |
nicklas |
87 |
|
5049 |
23 Oct 18 |
nicklas |
88 |
@Override |
5049 |
23 Oct 18 |
nicklas |
89 |
protected void loadFilesToWorkDir() |
5049 |
23 Oct 18 |
nicklas |
90 |
throws IOException |
5049 |
23 Oct 18 |
nicklas |
91 |
{ |
5049 |
23 Oct 18 |
nicklas |
92 |
OutputStream out = null; |
5049 |
23 Oct 18 |
nicklas |
93 |
try |
5049 |
23 Oct 18 |
nicklas |
94 |
{ |
5049 |
23 Oct 18 |
nicklas |
// Copy the R script and reference data |
5056 |
26 Oct 18 |
nicklas |
96 |
createWorkFile("bafPlot.R", false, MBafPlot.class.getResourceAsStream("/net/sf/basedb/reggie/baf/bafPlot.R"), true); |
5056 |
26 Oct 18 |
nicklas |
97 |
createWorkFile("normal-refs.txt", false, MBafPlot.class.getResourceAsStream("/net/sf/basedb/reggie/baf/normal-refs.txt"), true); |
5049 |
23 Oct 18 |
nicklas |
98 |
|
5049 |
23 Oct 18 |
nicklas |
// and save the mBAF data to a tab-separated file |
5056 |
26 Oct 18 |
nicklas |
100 |
out = createWorkFile("mbafdata.txt", true); |
5049 |
23 Oct 18 |
nicklas |
101 |
TableWriter tw = new TableWriter(new OutputStreamWriter(out)); |
5049 |
23 Oct 18 |
nicklas |
102 |
Object[] data = new Object[4]; |
5049 |
23 Oct 18 |
nicklas |
103 |
data[0] = "Region"; |
5049 |
23 Oct 18 |
nicklas |
104 |
data[1] = "Count"; |
5049 |
23 Oct 18 |
nicklas |
105 |
data[2] = "AvgMBaf"; |
5049 |
23 Oct 18 |
nicklas |
106 |
data[3] = "PVal"; |
5049 |
23 Oct 18 |
nicklas |
107 |
tw.tablePrintData(data); |
5049 |
23 Oct 18 |
nicklas |
108 |
|
5049 |
23 Oct 18 |
nicklas |
109 |
for (RegionStat rs : bafData.getRegionStat()) |
5049 |
23 Oct 18 |
nicklas |
110 |
{ |
5049 |
23 Oct 18 |
nicklas |
111 |
data[0] = rs.getRegion().getName(); |
5051 |
24 Oct 18 |
nicklas |
112 |
data[1] = rs.getSnpCount(); |
5049 |
23 Oct 18 |
nicklas |
113 |
data[2] = rs.getAvgMBaf(); |
5049 |
23 Oct 18 |
nicklas |
114 |
data[3] = rs.getPValue(); |
5049 |
23 Oct 18 |
nicklas |
115 |
tw.tablePrintData(data); |
5049 |
23 Oct 18 |
nicklas |
116 |
} |
5049 |
23 Oct 18 |
nicklas |
117 |
|
5049 |
23 Oct 18 |
nicklas |
118 |
tw.flush(); |
5049 |
23 Oct 18 |
nicklas |
119 |
tw.close(); |
5049 |
23 Oct 18 |
nicklas |
120 |
} |
5049 |
23 Oct 18 |
nicklas |
121 |
finally |
5049 |
23 Oct 18 |
nicklas |
122 |
{ |
5049 |
23 Oct 18 |
nicklas |
123 |
FileUtil.close(out); |
5049 |
23 Oct 18 |
nicklas |
124 |
} |
5049 |
23 Oct 18 |
nicklas |
125 |
} |
5049 |
23 Oct 18 |
nicklas |
126 |
} |
5049 |
23 Oct 18 |
nicklas |
127 |
|
5049 |
23 Oct 18 |
nicklas |
128 |
} |