forked from hanig/TEISER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_mi_gene_list.pl
404 lines (342 loc) · 11.8 KB
/
run_mi_gene_list.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/perl
my $teiserdir ;
BEGIN{
if ((!$ENV{TEISERDIR}) || ($ENV{TEISERDIR} eq '')) {
$teiserdir="./" ;
print "The TEISERDIR environment variable is not set. It is set to default.\n";
}
else{
print "The TEISERDIR environment variable is ".$ENV{TEISERDIR}."\n" ;
$teiserdir = $ENV{TEISERDIR};
}
}
my $cmdline = "perl run_mi_gene_list.pl";
foreach my $r (@ARGV) {
$cmdline .= " $r";
}
my $programdir = $teiserdir."/Programs" ;
my $scriptdir = $teiserdir."/Scripts" ;
my $pwd = `pwd`; $pwd =~ s/\n//;
use lib "$teiserdir/Scripts";
use PBS ;
use strict;
use Sets;
use Table;
use Getopt::Long;
use Data::Dumper;
if (@ARGV == 0) {
die "Usage: perl run_mi_gene_list.pl --expfile=FILE --exptype=TXT --species=SP\n";
}
my @argv_copy = @ARGV;
my $expfile = undef ;
my $genefile = undef ;
my $listfile = undef ;
my $homologyfile = undef ;
my $fastafile_rna = undef ;
my $species = undef ;
my $exptype = "discrete" ;
my $ebins = undef ;
my $quantized = 1 ;
my $divbins = 50.0 ;
my $mbins = 2 ;
my $type = "DN" ;
my $shuffle_mifind = 1500000 ;
my $maxp_mifind = 0.00000001 ;
my $shuffle_mimotif = 1000000 ;
my $maxp_mimotif = 0.0000001 ;
my $shuffle_page = 10000 ;
my $maxp_page = 0.005 ;
my $maxz_mioptimize = -1 ;
my $maxz_mifind = -1 ;
my $jn_t = 8 ;
my $min_r = 2.0 ;
my $dG_t = -1.0 ;
my $clusters = 5 ;
my $draw_min = -10 ;
my $draw_max = 10 ;
my $removecols_draw = undef ;
my $colmap_matrix = "$scriptdir/HEATMAPS/cmap_1.txt" ;
my $colmap_mimatrix = "$scriptdir/HEATMAPS/cmap_2.txt" ;
my $colmap_page = "$scriptdir/HEATMAPS/cmap_3.txt" ;
my $order = 0 ;
my $dodrawmatrix = 1 ;
my $dodrawmimatrix = 1 ;
my $dodrawpagematrix = 1 ;
my $dodrawmotifs = 1 ;
my $doskipdiscovery = 0 ;
my $domifind = 1 ;
my $doskipoptimization = 0 ;
my $domioptimize = 1 ;
my $doonlypositive = 0 ;
my $domireport = 1 ;
my $doremovedups = 1 ;
my $doremoveextra = 1 ;
my $dopagerun = 1 ;
my $domotifind = 1 ;
my $doconservation = 1 ;
my $suffix = "" ;
my $submit = 0 ;
my $platform = undef ;
my $walltime = "72:00:00" ;
my $queue = undef ;
my $jobid = undef ;
GetOptions ('expfile=s' => \$expfile,
'genefile=s' => \$genefile,
'listfile=s' => \$listfile,
'species=s' => \$species,
'exptype=s' => \$exptype,
'ebins=i' => \$ebins,
'quatized=i' => \$quantized,
'divbins=i' => \$divbins,
'mbins=i' => \$mbins,
'type=s' => \$type,
'shuffle_mifind=i' => \$shuffle_mifind,
'maxp_mifind=f' => \$maxp_mifind,
'shuffle_mimotif=i' => \$shuffle_mimotif,
'maxp_mimotif=f' => \$maxp_mimotif,
'shuffle_page=i' => \$shuffle_page,
'maxp_page=f' => \$maxp_page,
'maxz_mioptimize=f' => \$maxz_mioptimize,
'maxz_mifind=f' => \$maxz_mifind,
'jn_t=i' => \$jn_t,
'min_r=f' => \$min_r,
'dG_t=f' => \$dG_t,
'cluaters=i' => \$clusters,
'order=i' => \$order,
'draw_min=f' => \$draw_min,
'draw_max=f' => \$draw_max,
'removecols_draw=i' => \$removecols_draw,
'dodrawmatrix=i' => \$dodrawmatrix,
'dodrawmimatrix=i' => \$dodrawmimatrix,
'dodrawpagematrix=i' => \$dodrawpagematrix,
'dodrawmotifs=i' => \$dodrawmotifs,
'doskipdiscovery=i' => \$doskipdiscovery,
'doskipoptimization=i' => \$doskipoptimization,
'domifind=i' => \$domifind,
'domioptimize=i' => \$domioptimize,
'doonlypositive=i' => \$doonlypositive,
'domireport=i' => \$domireport,
'doremovedups=i' => \$doremovedups,
'doremoveextra=i' => \$doremoveextra,
'dopagerun=i' => \$dopagerun,
'domotifind=i' => \$domotifind,
'doconservation=i' => \$doconservation,
'suffix=s' => \$suffix,
'submit=i' => \$submit,
'platform=s' => \$platform,
'walltime=s' => \$walltime,) ;
if (!defined($expfile)) {
die("Please input an expression file (--expfile=FILE).\n");
}
if ($doskipdiscovery == 1) {
$domifind = 0;
$domioptimize = 0;
}
if ($doskipoptimization == 1) {
$domioptimize = 0;
}
if ($exptype eq "discrete") {
$quantized = 1 ;
} elsif ($exptype eq "continuous"){
$quantized = 0 ;
}
if (($quantized == 0) && (!defined($removecols_draw))) {
$removecols_draw = 0;
}
if (defined($species)) {
# read species file
my $species_data = readSpeciesData($species);
$fastafile_rna = $species_data->{"fastafile_rna"} if (!defined($fastafile_rna)) ;
$homologyfile = $species_data->{"homologyfile"} if (!defined($homologyfile)) ;
$listfile = $species_data->{"genelistfile"} if (!defined($listfile));
}
my $time = Sets::getNiceDateTime(1) ;
my $a_ref_files = Sets::getFiles($expfile);
foreach my $expfile (@$a_ref_files) {
if (&check_input_file($expfile, $exptype) == 0) {
die "Please correct input expression file.\n";
}
my $expfile_file = Sets::filename($expfile);
my $target_dir = "$expfile"."_GENESET"."/$suffix";
if ($suffix eq ""){
$target_dir = "$expfile"."_GENESET";
}
if (! -e "$expfile"."_GENESET") {
mkdir "$expfile"."_GENESET" ;
}
if (! -e $target_dir) {
mkdir $target_dir;
}
open OUTC, ">$target_dir/cmdline.txt"
or print "Cannot open $target_dir/cmdline.txt";
print OUTC "$cmdline\n";
close OUTC;
######## remove duplicates ########
my $expfile_nodups = "$target_dir/$expfile_file" ;
my $expfile_pre = "$target_dir/$expfile_file.pre" ;
my $pbs = PBS->new;
$pbs->setPlatform($platform) if (defined($platform));
$pbs->setQueue($queue) if (defined($queue));
$pbs->setWallTime($walltime);
$pbs->addCmd("export TEISERDIR=$teiserdir");
$pbs->addCmd("cd $pwd");
$pbs->setScriptName("$target_dir/$expfile_file.script");
$pbs->addCmd("date");
if ($doremoveextra == 1) {
$pbs->addCmd("echo \"Remove genes without sequence info, create $target_dir/$expfile_pre\"");
$pbs->addCmd("perl $scriptdir/remove_genes_without_sequence.pl --expfile=$expfile --listfile=$listfile --outfile=$expfile_pre");
}else{
system("cp $expfile $expfile_pre") ;
}
$pbs->addCmd("echo \"Remove duplicates, create $target_dir/$expfile_file\"");
if ($doremovedups == 1) {
my %PARAMS = ("expfile" => $expfile_pre,
"quantized" => $quantized,
"fastafile" => $fastafile_rna,
"dupfile" => undef,
"ebins" => $ebins,
"divbins" => $divbins,
"outfile" => $expfile_nodups) ;
my $cmd = &get_cmd_removedups(\%PARAMS) ;
$pbs->addCmd($cmd) ;
}else{
system("cp $expfile_pre $expfile_nodups") ;
}
######## quatize expression profile ########
my $expfile_quant = "$target_dir/$expfile_file.q" ;
if ($quantized == 0) {
$pbs->addCmd("echo \"Quantizing the input file.\"");
my $cmd = "perl $scriptdir/quantize_input_vector.pl --expfile=$expfile_nodups --outfile=$expfile_quant";
if (defined($ebins)) {
$cmd .= " -ebins $ebins ";
}
if (defined($divbins)) {
$cmd .= " -divbins $divbins ";
}
$pbs->addCmd($cmd);
}
######## mi report for gene list ########
my $matrixfile = "$target_dir/$expfile_file.matrix" ;
my $summaryfile = "$target_dir/$expfile_file.summary" ;
my $reportfile = "$target_dir/$expfile_file.report" ;
my $profile = "$target_dir/$expfile_file.profile" ;
$pbs->addCmd("echo \"step 2: seed optimization.\"");
my %PARAMS = ("expfile" => $expfile_nodups,
"quantized" => $quantized,
"shuffle" => $shuffle_mifind,
"genefile" => $genefile,
"ebins" => $ebins,
"divbins" => $divbins,
"minr" => $min_r,
"matrixfile" => $matrixfile,
"summaryfile" => $summaryfile,
"reportfile" => $reportfile,
"profile" => $profile,
"max_p" => $maxp_mifind,
"max_z" => $maxz_mioptimize) ;
my $cmd = &get_cmd_mi_report_gene_list(\%PARAMS);
$pbs->addCmd($cmd);
######## drawing matrix ########
if ($dodrawmatrix == 1){
$pbs->addCmd("echo \"step 7: drawing matrix.\"");
my %PARAMS = ("pvmatrixfile" => $matrixfile,
"summaryfile" => $summaryfile,
"expfile" => $expfile_nodups,
"quantized" => $quantized,
"colmap" => $colmap_matrix,
"order" => $order,
"min" => $draw_min,
"max" => $draw_max,
"cluster" => $clusters,
"suffix" => "",) ;
my $cmd = &get_cmd_drawmatrix(\%PARAMS);
$pbs->addCmd($cmd);
}
if ($submit == 0) {
$pbs->execute;
}else{
$jobid = $pbs->submit; print "Submitted job $jobid.\n";
}
}
sub readSpeciesData {
my ($species) = @_;
my %H = ();
open IN, "$ENV{TEISERDIR}/TEISER_Data/species_data/$species" or die "No data file for $species.\n";
while (my $l = <IN>) {
chomp $l;
my @a = split /\t/, $l, -1;
if ($a[1] =~ /^TEISER_Data/) {
$a[1] = "$ENV{TEISERDIR}/$a[1]";
}
$H{$a[0]} = $a[1];
}
close IN;
return \%H;
}
sub check_input_file {
my ($expfile, $exptype) = @_;
my $ta = Table->new;
$ta->loadFile($expfile);
my $a_ref = $ta->getArray();
my $r = shift @$a_ref;
if ($r->[1] =~ /^\d/) {
print "WARNING: your file might not contain a header line ($r->[1]).\n";
}
if (scalar(@$a_ref)<2){
print "ERROR: your expfile is not valid. Most probably it contains '\r' instead of '\n'\n" ;
return 1 ;
}
my %H = ();
my %V = ();
foreach my $r (@$a_ref) {
if (defined($H{$r->[0]})) {
print "Your files contains multiple rows with the same gene id. Please correct that before applying TEISER.\n";
return 0;
}
$H{$r->[0]} ++;
$V{$r->[1]} = 1;
}
my @v = values ( %V );
@v = sort { $a <=> $b } @v;
my $max = $#v;
if (($exptype eq 'discrete') && (scalar(@v) != $max+1)) {
my $n1 = scalar(@v);
my $n2 = $max + 1;
die "Problem. Your discrete vector is missing some symbols ($n1 != $n2).\n";
return 0;
}
return 1;
}
sub get_cmd_removedups {
my ($p) = @_;
my $todo = "perl $scriptdir/remove_duplicates.pl --expfile=$p->{expfile} --quantized=$p->{quantized} --fastafile=$p->{fastafile} --outfile=$p->{outfile}";
if (defined($p->{dupfile})) {
$todo .= " --dupfile=$p->{dupfile} ";
}
if (defined($p->{ebins})) {
$todo .= " --ebins=$p->{ebins} ";
}
if (defined($p->{divbins})) {
$todo .= " --divbins=$p->{divbins} ";
}
return $todo;
}
sub get_cmd_mi_report_gene_list {
my ($p) = @_;
my $todo = "$programdir/mi_report_gene_list -expfile $p->{expfile} -genefile $p->{genefile} -quantized $p->{quantized} -shuffle $p->{shuffle} -matrixfile $p->{matrixfile} -summaryfile $p->{summaryfile} -profile $p->{profile}" ;
if (defined($p->{ebins})) {
$todo .= " -ebins $p->{ebins} ";
}
if (defined($p->{jn_t})) {
$todo .= " -jn_t $p->{jn_t} ";
}
if (defined($p->{divbins})) {
$todo .= " -divbins $p->{divbins} ";
}
return $todo;
}
sub get_cmd_drawmatrix {
my ($p) = @_;
my $todo = "perl $scriptdir/teiser_draw_matrix.pl --pvmatrixfile=$p->{pvmatrixfile} --summaryfile=$p->{summaryfile} --expfile=$p->{expfile} --quantized=$p->{quantized} --colmap=$p->{colmap} --order=$p->{order} --min=$p->{min} --max=$p->{max} --cluster=$p->{cluster} --suffix=$p->{suffix}" ;
return $todo;
}