This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCALLME.pl
298 lines (250 loc) · 9.18 KB
/
CALLME.pl
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/perl
use Getopt::Long;
use Pod::Usage;
use Cwd 'abs_path';
use POSIX;
# initialize user-definable parameters to default values
$dataFile='';
$nMarkers=0;
$rsOnly='false';
$cluster_centers='true';
$chip='';
$nprocs=1;
$T=2;
$aAlpha=0.2;
$lenProbe=75;
$libDir='.';
$sdHetBAF = 0.05;
$sdLRR = 0.33;
$remove='true';
# set user defined parameters
GetOptions('data_file=s' => \$dataFile,
'n_markers=s' => \$nMarkers,
'rs_only=s' => \$rsOnly,
'cluster_centers=s' => \$cluster_centers,
'chip=s' => \$chip,
'n_procs=s' => \$nprocs,
'T=s' => \$T,
'aAlpha=s' => \$aAlpha,
'min_seg_length=s' => \$lenProbe,
'sd_het_BAF_thres=s' => \$sdHetBAF,
'sd_LRR_thres=s' => \$sdLRR,
'remove_temp_files=s' => \$remove
) or pod2usage(1);
if (! -r $dataFile){
die("cannot read ".$dataFile." (--data_file)");
}
if($nMarkers==0){
die("you did not specify the maximum number of markers (--n_markers)");
}
if($chip eq '' & $cluster_centers ne "true"){
die("you did not specify the chip (--chip)");
}
$dir=abs_path($dataFile);
@sp=split(/\//,$dir);
$filename=$sp[scalar(@sp)-1];
$dir =~ s/\/$filename//;
$libDir=abs_path($libDir);
print "############### ANALYSIS BEGAN ".localtime()." ##############\n\n";
system("top -b -n1 | head > ".$dir."/topfile");
open(FHI,$dir."/topfile") || die("cannot open file ".$dir."/topfile");
$line = <FHI>;
$line = <FHI>;
$line = <FHI>;
$line = <FHI>;
@elts = split(/, /,$line);
$mem_available = $elts[2];
$mem_available =~ s/k free//g;
$mem_available = POSIX::ceil($mem_available/1000);
close(FHI);
unlink $dir."/topfile";
system("head -n1 ".$dataFile." > ".$dir."/header");
open(FHI,$dir."/header") || die("cannot open file ".$dir."/header");
$line = <FHI>;
close(FHI);
unlink $dir."/header";
@elts = split(/\t/,$line);
$nInd = (scalar(@elts) - 3)/3;
#$nInd=11900;
$mem_required = 42950.1764636666 + 0.0625582763528102*$nInd*$nMarkers;
$mem_required = POSIX::ceil($mem_required*(1.1)/1000);
$option = 1;
print "\n\n******* ".$nInd." samples to process *******\n\n";
print "\t".$mem_available." MB of RAM available\n";
print "\tApproximately ".$mem_required." MB of RAM needed for analysis\n\n";
if($mem_required > $mem_available){
print "\tWARNING : RAM required is higher than 80% of RAM available\n";
print "\t\tWhat do you want to do? \n";
print "\t\t\t - Still try to stock all data in the RAM (stock)\n";
print "\t\t\t - Use a slower but less RAM-consuming program (less)\n";
print "\t\t\t - Quit (quit)\n";
$answer=<>;
if($answer =~ m/quit/gi){
die();
}
if($answer =~ m/less/gi){
$option=2;
}
}
print "******* Extracting data *******\n\n";
#print $libDir."/split_samples.exe --data ".$dataFile." --nInd ".$nInd." --nSNPmax ".$nMarkers." --rsOnly ".$rsOnly." --nBlock ".$nprocs." --outDir ".$dir."\n";
if($option==1){
$myCommandLY = $libDir."/split_samples.exe --data ".$dataFile." --nInd ".$nInd." --nSNPmax ".$nMarkers." --rsOnly ".$rsOnly." --nBlock ".$nprocs." --outDir ".$dir;
#print "--> $myCommandLY";
$exit_status=system($myCommandLY);
if($exit_status != 0){
die("An error occured during data extraction");
}
}
else{
$finished=0;
$start=0;
$stop=250;
if($stop>=$nInd){
$stop=$nInd;
$finished++;
}
while($stop<=$nInd & $finished<2){
print"perl ".$libDir."/split_samples.pl --data_file=".$dataFile." --output_directory=".$dir." --rs_only=".$rsOnly." --samples=".$start."-".$stop."\n";
system("perl ".$libDir."/split_samples.pl --data_file=".$dataFile." --output_directory=".$dir." --rs_only=".$rsOnly." --samples=".$start."-".$stop);
if($exit_status != 0){
die("An error occured during data extraction");
}
$start=$stop;
$stop=$start+250;
if($stop>=$nInd){
$stop=$nInd;
$finished++
}
}
}
#question();
$nfilesPerList = POSIX::ceil($nInd/$nprocs);
system("R --vanilla --no-save --slave dir=".$dir."/normalization nFilesPerList=".$nfilesPerList." < ".$libDir."/split_samples.R > tmp");
unlink "tmp";
if($cluster_centers eq "true"){
print "\n\n******* Computing cluster centers *******\n\n";
mkdir $dir."/cluster_centers";
system("perl ".$libDir."/cluster_centers.pl --input_directory=".$dir."/normalization --geno_directory=".$dir."/genotypes --output_file=".$dir."/cluster_centers/clusters --samples_lists=".$dir."/normalization/lists.txt --lib_dir=".$libDir." --log_dir=".$dir."/cluster_centers/logs");
waitFor('analysis=cluster_centers');
checkLogs($dir."/cluster_centers/logs","cluster");
open(FHO,"> ".$dir."/cluster_centers/list.txt") || die("cannot open file ".$dir."/cluster_centers/list.txt for writing");
for($i=1;$i<=$nprocs;$i++){
print FHO $dir."/cluster_centers/clusters".$i."\n";
}
close(FHO);
if(! -d $libDir."/lib_tQN"){
mkdir $libDir."/lib_tQN";
}
system("perl ".$libDir."/join_cluster_and_samples_files.pl --file_list_clusters=".$dir."/cluster_centers/list.txt --out_file_clusters=".$libDir."/lib_tQN/my_chip_tQN_clusters.txt");
print "\n => custom cluster centers are available here :".$libDir."/lib_tQN/my_chip_tQN_clusters.txt\n";
$chip="my_chip";
if($remove =~ m/true/gi){
system("rm -rf ".$dir."/cluster_centers");
}
}
print "\n\n******* Performing quantile normalization *******\n\n";
system("perl ".$libDir."/tQN_normalize_samples.pl --beadchip=".$chip." --input_dir=".$dir."/normalization --output_dir=".$dir."/normalized --samples_lists=".$dir."/normalization/lists.txt --all_samples=".$dir."/normalization/sample_names.txt --tQN_dir=".$libDir."/lib_tQN");
checkLogs($dir."/normalized/logs","normalization");
checkLogs($dir."/normalized/logs","generate");
#question();
if($remove =~ m/true/gi){
system("rm -rf ".$dir."/normalization");
}
print "\n\n******* Performing GC wave correction *******\n\n";
system("R --vanilla --no-save --slave dir=".$dir."/normalized nFilesPerList=".$nfilesPerList." libDir=".$libDir." < ".$libDir."/launch_genomic_wave.R > tmp");
waitFor("genomic_wave.pl");
checkLogs($dir."/normalized/GC_corrected/logs","correction");
unlink "tmp";
#question();
print "\n\n******* Formatting data for segmentation *******\n\n";
system("R --vanilla --no-save -slave dir1=".$dir."/normalized/GC_corrected dir2=".$dir."/genotypes nFilesPerList=".$nfilesPerList." outdir=".$dir."/segmentation < ".$libDir."/launchFormatting.R > tmp");
waitFor("formatData3.pl");
checkLogs($dir."/segmentation/logs","formatData");
unlink "tmp";
unlink $dir."/segmentation/indivs.txt";
if($remove =~ m/true/gi){
system("rm -rf ".$dir."/normalized");
system("rm -rf ".$dir."/genotypes");
}
opendir(DIR, $dir."/segmentation") || die("Cannot open directory ".$dir."/segmentation");
@indivs = readdir(DIR);
close(DIR);
open(FHO,"> ".$dir."/segmentation/indivs.txt") || die("cannot open file ".$dir."/segmentation/indivs.txt");
for $ind (@indivs){
if($ind ne "logs" & $ind ne "." & $ind ne ".." & $ind !~ m/res/g & $ind !~ m/data/g){
print FHO $ind."\n";
}
}
close(FHO);
if($remove =~ m/true/gi){
system("rm -rf ".$dir."/normalized");
system("rm -rf ".$dir."/genotypes");
}
print "\n\n******* Calculating quality metrics on data *******\n\n";
system("perl ".$libDir."/checkData.pl --absolute_dir_path=".$dir."/segmentation/ --indivs_file=".$dir."/segmentation/indivs.txt --max_procs=".$nprocs." --lib_dir=".$libDir);
checkLogs($dir."/segmentation/logs","check");
print "\n\n******* Performing segmentation *******\n\n";
system("perl ".$libDir."/segmentation.pl --absolute_dir_path=".$dir."/segmentation/ --indivs_file=".$dir."/segmentation/indivs.txt --max_procs=".$nprocs." --T=".$T." --min_seg_length=".$lenProbe." --aAlpha=".$aAlpha." --sd_LRR_thres=".$sdLRR." --sd_het_BAF_thres=".$sdHetBAF." --lib_dir=".$libDir);
checkLogs($dir."/segmentation/logs","segmentation");
checkLogs($dir."/segmentation/logs","extraction");
system("cp -r ".$dir."/segmentation/results_T_".$T."_aAlpha_".$aAlpha."_minSegLength_".$lenProbe." ".$dir) ;
system("cp ".$dir."/segmentation/data_check_all.txt ".$dir) ;
system("cp ".$dir."/segmentation/indivs.txt ".$dir) ;
print "\n\n############### ANALYSIS FINISHED ".localtime()." ##############\n\n";
sub waitFor(){
$keyword = $_[0];
$nproc = 1;
while($nproc > 0){
system("ps ux > tmpfile");
open(FHps,"tmpfile") || die("cannot open file tmpfile");
$lin = <FHps>;
$nproc = 0;
while($lin=<FHps>){
chomp($lin);
if($lin =~ m/$keyword/g){
$nproc ++;
}
}
#print $nprocs."\n";
close(FHps);
unlink "tmpfile";
}
}
sub checkLogs(){
$logdir = @_[0];
$keyword = @_[1];
opendir(DIR, $logdir) || die("Cannot open directory ".$logdir);
@logs = readdir(DIR);
close(DIR);
for $log (@logs){
if($log ne '.' & $log ne '..' & $log =~ m/$keyword/g){
#print $log."\n";
open(LOG,$logdir."/".$log) || die("Cannot open file ".$log);
$OK=0;
$line = <LOG>;
$content = $line;
while($OK eq 0 & $line ne ""){
#print "ok : ".$OK."\n";
if($line =~ m/Analysis finished/g){
$OK = 1;
}
$line=<LOG>;
$content=$content.$line;
}
if($OK eq 0){
print "NOTICE : An error occured :\n";
open $content."\n\n";
}
close(LOG);
}
}
}
sub question(){
print "\tContinue (y or n) ?\n";
$ans = <>;
chomp($ans);
if($ans ne "y"){
die();
}
}