-
Notifications
You must be signed in to change notification settings - Fork 1
/
MakeCSS_CNEPPredict.java
180 lines (144 loc) · 4.92 KB
/
MakeCSS_CNEPPredict.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
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
public class MakeCSS_CNEPPredict
{
public static void main(String[] args) throws IOException
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
nf.setGroupingUsed(false);
nf.setMinimumFractionDigits(3);
File dir = new File(Constants.CSSCNEP_OUTPUTDIR);
if (!dir.exists())
{
if (!dir.mkdirs())
{
throw new IllegalArgumentException(Constants.CSSNCEP_AVERAGEDIR+" does not exist and could not be created!");
}
}
String szcurrchrom = args[0]; //should include chr prefix
String szLine;
int nchromsize = -1;
BufferedReader brchromsizes = Util.getBufferedReader(Constants.CHROMSIZES);
while ((szLine = brchromsizes.readLine())!=null)
{
StringTokenizer st = new StringTokenizer(szLine,"\t");
String szchrom = st.nextToken();
if (szchrom.equals(szcurrchrom))
{
nchromsize = Integer.parseInt(st.nextToken());
break;
}
}
brchromsizes.close();
int numlabels = 0;
BufferedReader brlabels = Util.getBufferedReader(Constants.LABELLIST);
while ((szLine = brlabels.readLine())!=null)
{
numlabels++;
}
brlabels.close();
int numcombos = Constants.NUMCONSHMM;
for (int na = 1; na <= numlabels; na++)
{
numcombos = 2*numcombos;
}
double dstatecount = 0;
double[] mapping = new double[numcombos];
BufferedReader brmapping = Util.getBufferedReader(Constants.CSSNCEP_AVERAGEDIR+"/average_exclude_"+szcurrchrom+".txt");
while ((szLine = brmapping.readLine())!=null)
{
StringTokenizer st = new StringTokenizer(szLine,"\t");
int ncombo = Integer.parseInt(st.nextToken());
String szval = st.nextToken();
double dcount = Double.parseDouble(st.nextToken());
if (dcount > 0)
{
mapping[ncombo] = Double.parseDouble(szval)/1000;
}
//mapping[Integer.parseInt(st.nextToken())] = Double.parseDouble(st.nextToken())/1000;
}
brmapping.close();
boolean[][] bconserved = new boolean[numlabels][nchromsize];
String[] szlabelsA = new String[numlabels];
int nel = 0;
brlabels = Util.getBufferedReader(Constants.LABELLIST);
String szLabelFile;
while ((szLabelFile = brlabels.readLine())!=null)
{
File f = new File(szLabelFile);
if (!f.exists())
{
szLabelFile += ".gz";
}
BufferedReader brconserved = Util.getBufferedReader(Constants.LABELBEDDIR+"/"+//+"COORDS/"+
szLabelFile);
boolean[] bconserved_nel = bconserved[nel];
while ((szLine = brconserved.readLine())!=null)
{
StringTokenizer st = new StringTokenizer(szLine);
String szchrom = st.nextToken();
if (szchrom.equals(szcurrchrom))
{
int nbegin = Integer.parseInt(st.nextToken());
int nend = Integer.parseInt(st.nextToken());
for (int nj = nbegin; nj < nend; nj++)
{
bconserved_nel[nj] = true;
}
}
}
brconserved.close();
nel++;
}
GZIPOutputStream pw = new GZIPOutputStream(new FileOutputStream(Constants.CSSCNEP_OUTPUTDIR+"/csscnep_"+szcurrchrom+".wig.gz"));
//"CONSHMMCOMBO_AVERAGECNEP/conshmmcombo_avgcnep_"+szcurrchrom+".wig.gz"));
int[] state = new int[nchromsize];
BufferedReader brstate = Util.getBufferedReader(Constants.CONSHMM_SEGMENTS);
// "CONSHMM/ernst.cass.idre.ucla.edu/public/ConsHMM/Segmentation/"+szcurrchrom+"/"+szcurrchrom+"_segmentation.bed.gz");
int nmaxchrom = -1;
while ((szLine = brstate.readLine())!=null)
{
StringTokenizer st = new StringTokenizer(szLine,"\t");
String szchrom = st.nextToken();
if (!szchrom.equals(szcurrchrom)) continue;
int nbegin = Integer.parseInt(st.nextToken());
int nend = Integer.parseInt(st.nextToken());
int nval = Integer.parseInt(st.nextToken().substring(1))-1;
for (int nk = nbegin; nk < nend; nk++)
{
state[nk] = nval;
}
if (nmaxchrom < nend)
{
nmaxchrom = nend;
}
}
brstate.close();
byte[] btheader1 = ("track type=wiggle_0 name=\"css_cnep\" description=\"css_cnep\" visibility=2\n").getBytes();
pw.write(btheader1, 0, btheader1.length);
byte[] btheader2 = ("fixedStep chrom="+szcurrchrom+" start=1 step=1\n").getBytes();
pw.write(btheader2, 0, btheader2.length);
for (int na = 0; na < nmaxchrom; na++)
{
int nconshmmstate = state[na];
int nconservedcombo = 0;
int npow = 1;
for (int nb = 0; nb < numlabels; nb++)
{
if (bconserved[nb][na])
{
nconservedcombo += npow;
}
npow = npow*2;
}
int nstate = Constants.NUMCONSHMM*nconservedcombo + nconshmmstate;
byte[] btformat =(nf.format(mapping[nstate])+"\n").getBytes();
pw.write(btformat,0,btformat.length);
}
pw.finish();
pw.close();
}
}