-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.pl
158 lines (144 loc) · 3.89 KB
/
join.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
use strict;
use Getopt::Long;
my $samplesFile = "";
my $comboFile = "";
my $leaveIntermediate = 0;
my $zipOutput = 0;
my $print = "0";
GetOptions ("samplesFile=s" => \$samplesFile,
"comboFile=s" => \$comboFile,
"leaveIntermediate" => \$leaveIntermediate,
"zipOutput" => \$zipOutput,
"print" => \$print,
)
or die("Error in command line arguments\n");
$samplesFile = shift @ARGV if $samplesFile eq "";
$samplesFile = "samples.txt" if $samplesFile eq "";
$comboFile = "all.cpg" if $comboFile eq "";
print "params:
samplesFile: $samplesFile
comboFile: $comboFile
zipOutput: $zipOutput
print: $print
";
my $zipString = "";
$zipString = "--zipOutput" if $zipOutput;
if ($print)
{
#die "qsub -q short -N join -b y -cwd -o join.pl.out -e join.pl.err perl ~/scripts/cpgAnalysis/regions/join.pl -samplesFile $samplesFile -comboFile $comboFile $zipString";
die "qsub -q broad -l h_vmem=4g -l h_rt=08:00:00 -N join -b y -cwd -o join.pl.out -e join.pl.err perl ~/scripts/cpgAnalysis/regions/join.pl -samplesFile $samplesFile -comboFile $comboFile $zipString";
}
open SAM, $samplesFile or die "Cannot open samples file '$samplesFile'\n$!";
my @sampleList = ();
my %sampleLocs = ();
while (my $line = <SAM>)
{
chomp $line;
my ($sample, $loc) = split "\t", $line;
die "already seen sample $sample" if exists $sampleLocs{$sample};
$sampleLocs{$sample} = $loc;
die "bed file does not exist for sample $sample at '$loc'\n" unless -e $loc;
push @sampleList, $sample;
}
if (!-e $comboFile)
{
foreach my $sample (@sampleList)
{
runCommand("perl ~/scripts/addBedTarget.pl $comboFile $sampleLocs{$sample} 0 $sample");
if (-e "$comboFile.unfinished")
{
die "Unfinished file exists at '$comboFile.unfinished'\n";
}
}
}
else #combo file exists
{
open S, $comboFile or die $!;
my $head = <S>;
chomp $head;
close S;
my @headEls = split "\t", $head;
shift @headEls; #chr
shift @headEls; #start
for (my $i = 0; $i < @sampleList; $i++)
{
my $headEl = $headEls[$i];
my $sample = $sampleList[$i];
if ($headEl ne "" && $headEl ne $sample)
{
die "mismatch at sample $i: expecting '$sample', got '$headEl'\n";
}
elsif ($headEl eq $sample)
{
next;
}
runCommand("perl ~/scripts/addBedTarget.pl $comboFile $sampleLocs{$sample} 0 $sample");
if (-e "$comboFile.unfinished")
{
die "Unfinished file exists at '$comboFile.unfinished'\n";
}
}
}
if (!-e "$comboFile.GT0decimal.na" && !-e "$comboFile.GT0decimal.na.gz")
{
runCommand("perl ~/scripts/toDecimal.pl $comboFile 0");
runCommand("perl ~/scripts/filter/filterNAs.pl $comboFile.GT0decimal .2");
if ($leaveIntermediate)
{
if ($zipOutput)
{
runCommand("gzip $comboFile.GT0decimal");
runCommand("gzip $comboFile.GT0decimal.na");
}
}
else
{
runCommand("rm $comboFile.GT0decimal");
if ($zipOutput)
{
runCommand("gzip $comboFile.GT0decimal.na");
}
}
}
if (!-e "$comboFile.GT4decimal.na" && !-e "$comboFile.GT4decimal.na.gz")
{
runCommand("perl ~/scripts/toDecimal.pl $comboFile 4");
runCommand("perl ~/scripts/filter/filterNAs.pl $comboFile.GT4decimal .2");
if ($leaveIntermediate)
{
if ($zipOutput)
{
runCommand("gzip $comboFile.GT4decimal");
runCommand("gzip $comboFile.GT4decimal.na");
}
}
else
{
runCommand("rm $comboFile.GT4decimal");
if ($zipOutput)
{
runCommand("gzip $comboFile.GT4decimal.na");
}
}
}
runCommand("gzip $comboFile") if $zipOutput;
sub runCommand
{
my $command = shift;
my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$minute = sprintf("%02d",$minute);
$second = sprintf("%02d",$second);
print "$hour:$minute:$second\n$command\n";
print `$command 2>&1`;
my $exit_value = $? >> 8;
my $signal_num = $? & 127;
my $dumped_core = $? & 128;
if ($exit_value)
{
print "========\nJob failed\n========\n";
print "Exit value: $exit_value\n";
print "Signal num: $signal_num\n";
print "Dumped core: $dumped_core\n";
die;
}
}