-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStairway_plot_output_summary_commandline.java
153 lines (147 loc) · 6.38 KB
/
Stairway_plot_output_summary_commandline.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Stairway_plot_output_summary_commandline
{
public static void main(String[] args)
throws Exception
{
if (args.length != 4)
{
System.out.println("Usage: java Stairway_plot_output_summary_commandline folder_to_allTheta_files mutation_rate year_per_generation output_file_name ");
System.out.println(" folder_to_allTheta_files: the folder containing all (and only) the .addTheta files");
System.out.println(" mutation_rate: assumed mutation rate per site per generation, e.g. 1.2e-8.");
System.out.println(" year_per_generation: assumed generation time (in years), e.g. 24.");
System.out.println(" output_file_name: name for the output summary file");
System.exit(1);
}
String dir = correctDirectory(args[0]);
File folder = new File(dir);
File[] listOfFiles = folder.listFiles();
String[] infiles = folder.list();
Arrays.sort(infiles);
double mu = Double.parseDouble(args[1]);
double year_per_generation = Double.parseDouble(args[2]);
String outfile = args[3];
PrintWriter out = new PrintWriter(new FileWriter(outfile), true);
BufferedReader in = new BufferedReader(new FileReader(dir + infiles[0]));
String line = in.readLine();
out.println(line);
StringTokenizer t = new StringTokenizer(line, "\t");
int nt = t.countTokens();
String[] mspara = new String[nt];
for (int i = 0; i < nt; i++) {
mspara[i] = t.nextToken();
}
int nseq = Integer.parseInt(mspara[1]);
int nrep = infiles.length;
long L = Long.parseLong(mspara[2]);
System.out.println("nrep=" + nrep);
out.println("nrep=" + nrep);
double[][] allest = new double[nseq - 1][nrep];
double[] pMisPol = new double[nrep];
out.println("final fitness:");
for (int ii = 0; ii < nrep - 1; ii++) {
out.print(infiles[ii] + "\t");
}
out.println(infiles[(nrep - 1)]);
for (int ii = 0; ii < nrep; ii++)
{
in = new BufferedReader(new FileReader(dir + infiles[ii]));
in.readLine();
in.readLine();
System.out.println(ii + 1 + "\t" + infiles[ii]);
line = in.readLine();
double fitness = 0.0D;
while (line.indexOf("final model:") == -1) {
line = in.readLine();
}
t = new StringTokenizer(line, "\t");
t.nextToken();
fitness = Double.parseDouble(t.nextToken());
if (ii == nrep - 1) {
out.print(fitness);
} else {
out.print(fitness + "\t");
}
line = in.readLine();
t = new StringTokenizer(line);
int dim = t.countTokens();
int[][] group = new int[dim][];
for (int i = 0; i < dim; i++)
{
String tmp = t.nextToken();
StringTokenizer t2 = new StringTokenizer(tmp, ",");
int nxi = t2.countTokens();
group[i] = new int[nxi];
for (int j = 0; j < nxi; j++) {
group[i][j] = Integer.parseInt(t2.nextToken());
}
}
line = in.readLine();
t = new StringTokenizer(line);
for (int i = 0; i < dim; i++)
{
double theta = Double.parseDouble(t.nextToken());
for (int j = 0; j < group[i].length; j++) {
allest[(group[i][j] - 2)][ii] = theta;
}
}
pMisPol[ii] = Double.parseDouble(t.nextToken());
in.close();
}
out.println();
int dim = nseq - 1;
double[] time = new double[dim];
for (int i = 0; i < dim; i++)
{
Arrays.sort(allest[i]);
time[i] = (allest[i][(nrep / 2)] / L / (i + 2) / (i + 1));
}
for (int i = dim - 2; i >= 0; i--) {
time[i] += time[(i + 1)];
}
Arrays.sort(pMisPol);
out.println("pMisPol_median\tpMisPol_2.5%\tpMisPol_97.5%");
out.println(pMisPol[(nrep / 2)] + "\t" + pMisPol[((int)(nrep * 0.025D))] + "\t" + pMisPol[((int)(nrep * 0.975D))]);
out.println("mutation_per_site\ttheta\ttheta_per_site_median\ttheta_per_site_2.5%\ttheta_per_site_97.5%\tyear\tNe_median\tNe_2.5%\tNe_97.5%");
for (int i = dim - 1; i >= 0; i--) {
if (i == dim - 1)
{
out.print("0\t" + (i + 2) + "\t" + allest[i][(nrep / 2)] / L + "\t" + allest[i][((int)(nrep * 0.025D))] / L + "\t" + allest[i][((int)(nrep * 0.975D))] / L + "\t" + 0.0D / mu * year_per_generation + "\t" + allest[i][(nrep / 2)] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.025D))] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.975D))] / L / mu / 4.0D);
out.println();
out.print(time[i] + "\t" + (i + 2) + "\t" + allest[i][(nrep / 2)] / L + "\t" + allest[i][((int)(nrep * 0.025D))] / L + "\t" + allest[i][((int)(nrep * 0.975D))] / L + "\t" + time[i] / mu * year_per_generation + "\t" + allest[i][(nrep / 2)] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.025D))] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.975D))] / L / mu / 4.0D);
out.println();
}
else
{
out.print(time[(i + 1)] + "\t" + (i + 2) + "\t" + allest[i][(nrep / 2)] / L + "\t" + allest[i][((int)(nrep * 0.025D))] / L + "\t" + allest[i][((int)(nrep * 0.975D))] / L + "\t" + time[(i + 1)] / mu * year_per_generation + "\t" + allest[i][(nrep / 2)] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.025D))] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.975D))] / L / mu / 4.0D);
out.println();
out.print(time[i] + "\t" + (i + 2) + "\t" + allest[i][(nrep / 2)] / L + "\t" + allest[i][((int)(nrep * 0.025D))] / L + "\t" + allest[i][((int)(nrep * 0.975D))] / L + "\t" + time[i] / mu * year_per_generation + "\t" + allest[i][(nrep / 2)] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.025D))] / L / mu / 4.0D + "\t" + allest[i][((int)(nrep * 0.975D))] / L / mu / 4.0D);
out.println();
}
}
out.close();
}
private static String correctDirectory(String dir)
{
StringTokenizer t = new StringTokenizer(dir, "/\\\"'");
int nt = t.countTokens();
String sep = File.separator;
String dir2 = "";
for (int i = 0; i < nt; i++) {
dir2 = dir2 + t.nextToken() + sep;
}
if (((dir.charAt(0) == '"') || (dir.charAt(0) == '\'')) && ((dir.charAt(1) == '/') || (dir.charAt(1) == '\\'))) {
dir2 = sep + dir2;
} else if ((dir.charAt(0) == '/') || (dir.charAt(0) == '\\')) {
dir2 = sep + dir2;
}
return dir2;
}
}