-
Notifications
You must be signed in to change notification settings - Fork 1
/
MakeCNEPPredict.java
203 lines (170 loc) · 5.47 KB
/
MakeCNEPPredict.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
public class MakeCNEPPredict
{
//static int DEFAULT_NUMPORTIONS = 10;
public static void main(String[] args) throws IOException
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
nf.setGroupingUsed(false);
nf.setMinimumFractionDigits(3);
/*
String szmodeldir = args[0];
String szmodelfilelist = args[1];
String szinputbeddir = args[2];
String szfeaturelist = args[3];
String szoutputdir = args[4];
String szoutprefix = args[5];
String szchromfile = args[6];
String szchrom = args[7];
int nportion = Integer.parseInt(args[8]);
int numportions = Integer.parseInt(args[9]);
*/
String szmodeldir = Constants.MODELDIR;//"MODELS_UPDATED";//args[0];//"MODELS/l1_modelFiles1_GERP_chr10";
String szinputbeddir = Constants.INPUTBEDDIR;//"ALLINPUTBEDS";
String szfeaturelist = Constants.FEATUREFILELIST;//"featurelist_allinputbeds.txt";
String szoutputdir = Constants.PORTIONSDIR;//PREDICTIONSPORTIONS_UPDATED";
String szlabel = args[0];
//String szoutprefix = args[1];//"predicts_PhastCons";
File dir = new File(szoutputdir);
if (!dir.exists())
{
if (!dir.mkdirs())
{
throw new IllegalArgumentException(szoutputdir+" does not exist and could not be created!");
}
}
String szchromfile = Constants.CHROMSIZES;//"hg19.chrom.sizes";
String szchrom = args[1];//"chr10";
int nportion = Integer.parseInt(args[2]);//1;
int numportions = Constants.NUMPORTIONS;//10;//DEFAULT_NUMPORTIONS;
int nummodels = Constants.NUMENSEMBLES;//10;
if ((nportion < 0)||(nportion >= numportions))
{
throw new IllegalArgumentException("Invalid value of nportion "+nportion+" expecting value in [0,"+numportions+"]");
}
BufferedReader brchromfile = Util.getBufferedReader(szchromfile);
String szLine;
int nchromsize = -1;
while ((szLine = brchromfile.readLine())!=null)
{
StringTokenizer st = new StringTokenizer(szLine,"\t");
String szcurrchrom = st.nextToken();
if (szchrom.equals(szcurrchrom))
{
nchromsize = Integer.parseInt(st.nextToken());
break;
}
}
brchromfile.close();
int numfeatures = 0;
BufferedReader brfeatures = Util.getBufferedReader(szfeaturelist);
while ((szLine = brfeatures.readLine())!=null)
{
numfeatures++;
}
brfeatures.close();
double[][] weights = new double[nummodels][numfeatures];
double[] intercepts = new double[nummodels];
for (int nmodel = 0; nmodel < nummodels; nmodel++)
{
BufferedReader brmodel = Util.getBufferedReader(szmodeldir+"/train_samples_"+szchrom+"_"+nmodel+".gz_"+szlabel+".gz.txt.model.gz");
brmodel.readLine();
brmodel.readLine();
brmodel.readLine();
brmodel.readLine();
brmodel.readLine();
brmodel.readLine();
double[] weights_nmodel = weights[nmodel];
for (int nfeature = 0; nfeature < numfeatures; nfeature++)
{
weights_nmodel[nfeature] = Double.parseDouble(brmodel.readLine());
}
intercepts[nmodel] = Double.parseDouble(brmodel.readLine());
brmodel.close();
}
if (nchromsize == -1)
{
throw new IllegalArgumentException("chromosome size not found for "+szchrom+" in "+szchromfile);
}
brfeatures = Util.getBufferedReader(szfeaturelist);
int nregionsize = nchromsize/numportions;
int nregionstart = nregionsize*nportion;
int nregionend = nregionsize*(nportion+1); //using bed style end is exclusive
if (nportion == numportions-1)
{
nregionend = nchromsize;
nregionsize = nregionend-nregionstart;
}
double[][] weightsum = new double[nummodels][nregionsize];
int nfeature = 0;
while ((szLine = brfeatures.readLine())!=null)
{
boolean[] present = new boolean[nregionsize];
//System.out.println(szLine+"\t"+nfeature);
File f = new File(szLine);
if (!f.exists())
{
szLine += ".gz";
}
BufferedReader brcoords = Util.getBufferedReader(szinputbeddir+"/"+szLine);
String szLineCoords;
while ((szLineCoords = brcoords.readLine())!=null)
{
StringTokenizer st = new StringTokenizer(szLineCoords,"\t");
String szcurrchrom = st.nextToken();
if (szchrom.equals(szcurrchrom))
{
int nbegin = Integer.parseInt(st.nextToken());
int nend = Integer.parseInt(st.nextToken());
if (nbegin < nregionstart)
{
nbegin = nregionstart;
}
if (nend > nregionend)
{
nend = nregionend; //nregion end is inclusive
}
int npos = nbegin-nregionstart;
for (int nk = nbegin; nk < nend; nk++)
{
present[npos] = true;
npos++;
}
}
}
brcoords.close();
for (int nmodel = 0; nmodel < weights.length; nmodel++)
{
double dweights_nmodel_nfeature = weights[nmodel][nfeature];
double[] weightsum_nmodel = weightsum[nmodel];
for (int ni = 0; ni < present.length; ni++)
{
if (present[ni])
{
weightsum_nmodel[ni] += dweights_nmodel_nfeature;
}
}
}
nfeature++;
}
brfeatures.close();
GZIPOutputStream pw = new GZIPOutputStream(new FileOutputStream(szoutputdir+"/"+szlabel+"_"+szchrom+"_"+nportion+"_"+numportions+".wig.gz"));
for (int ni = 0; ni < nregionsize; ni++)
{
double dsum = 0;
for (int nmodel = 0; nmodel < weightsum.length; nmodel++)
{
dsum += 1.0/(1+Math.exp(-(weightsum[nmodel][ni]+intercepts[nmodel])));
}
double dprob = dsum/nummodels;
byte[] btformat =(nf.format(dprob)+"\n").getBytes();
pw.write(btformat,0,btformat.length);
}
pw.finish();
pw.close();
}
}