-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.pl
More file actions
313 lines (193 loc) · 6.86 KB
/
pipeline.pl
File metadata and controls
313 lines (193 loc) · 6.86 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw(sum);
my %hash;
my @LineTotalsArray;
my @output;
my $i = 1;
my $header;
my @values;
my @criteria;
my @previous_split;
my $previous_outline;
my @final_array;
my $match = 0;
my $dir=$ARGV[0] || die ("Please give me an INPUT directory eg Ae_Bicornis/ \n");
my $path = `pwd`;
chomp $path;
my $pathname = $path."/".$dir;
my @dirlist = `ls $pathname`;
chomp @dirlist;
foreach (@dirlist){
my $chr = substr($_, 0, 2);
my $inputfile = $pathname."/".$_;
open my $fh, '<', $inputfile or die $!;
my @array = `sort -k1g -k3,3 -k4,4n $inputfile`;
$header = $array[0];
### Remove flag field from header ###
$header =~ s/^Flag\t//;
shift @array;
#### push header onto @values array ###
push (@values, $header);
foreach(@array){
my $line = $_;
chomp $line;
my ($flag,$axiom,$chr,$pos,$rest) = split(/\t/, $line, 5);
#my ($axiom, $pos, $chr, $rest) = split(/\t/, $line, 4);
my $index=0;
for my $val ( split /\t/, $rest ) {
if ($val ne "-"){
$LineTotalsArray[ $index++ ] += $val;
}
else {$LineTotalsArray[ $index++ ] = $val;}
}
if($i == 10){
$LineTotalsArray[ $index++ ];
foreach my $x (@LineTotalsArray){
if ($x ne "-"){
$x = $x / $i;
}
else{ $x = 0;}
}
#print $axiom."\t".$pos."\t".$chr."\t";
#print $axiom."\t";
#print join("\t", @LineTotalsArray), "\n";
my $line = join("\t", @LineTotalsArray);
#### Remove Gary's flag ###
#my $outline = $flag."\t".$axiom."\t".$chr."\t".$pos."\t".$line;
my $outline = $axiom."\t".$chr."\t".$pos."\t".$line;
push (@output, $outline);
$i = 0;
undef @LineTotalsArray;
}
$i ++;
}
}
### Add Chr field to header ###
#my ($fld1, $fld2, $fld3, $fld4) = split(/\t/, $header, 4);
#$header = $fld1."\t".$fld2."\tChromosome\t".$fld3."\t".$fld4;
### Print out all files ###
foreach (@output){
push(@values, $_);
}
#####################################################
### Next part of pipeliine
#####################################################
my $header2 = shift(@values);
chomp $header2;
print $header2;
@values = grep(!/Axiom/, @values);
foreach(@values){
my $line = $_;
chomp $line;
#print "LINE = $line \n";
#my ($garyflag, $axiom, $chr, $pos, $rest) = split(/\t/, $line, 5);
my ($axiom, $chr, $pos, $rest) = split(/\t/, $line, 4);
#print $rest."\n";
my @scores = split(/\t/,$rest);
### Check if array contains an element with score greater than 0.4 ###
if (grep {$_ >= 0.1} @scores) {
# print "Yes, at least one in $axiom is bigger than 0.4\n";
push (@criteria, $line);
if ($match == 1) {
foreach (@criteria){
my $outline = $_;
my ($out_axiom, $out_pos, $out_chr, $out_rest) = split(/\t/, $outline, 4);
my @split_rest = split(/\t/, $out_rest);
my $new_string;
my $checkpoint;
#my @slice = @split_rest[0..405];
### Create a string for output
foreach(@split_rest){
if ($_ >= 0.4 ){
$new_string = $new_string."$_\t";
#print "[@split_rest] and [@previous] Match!!!!!\n" if elementwise_eq( \(@split_rest, @previous) );
}
else{
$new_string = $new_string."_\t";
}
}
### Initialize fla variables ###
my $flag = 0;
my $i = 3;
### check that current and previous variables were both greater than the 0.4 cutoff ###
for my $i (1 .. $#split_rest) {
if ($split_rest[$i] >= 0.4 && $previous_split[$i] >= 0.4) {
$flag = 1;
}
}
### Check if there is a preceeding bin using $flag variable ###
if ($flag == 1){
my ($pout_axiom, $pout_pos, $pout_chr, $pout_rest) = split(/\t/, $previous_outline, 4);
my @p_split_rest = split(/\t/, $pout_rest);
my $p_new_string;
foreach(@p_split_rest){
if ($_ >= 0.4 ){
$p_new_string = $p_new_string."$_\t";
}
else{
$p_new_string = $p_new_string."_\t";
}
}
### Assign output string to variable ###
my $p_final = $pout_axiom."\t".$pout_pos."\t".$pout_chr."\t".$p_new_string."\n";
my $final = $out_axiom."\t".$out_pos."\t".$out_chr."\t".$new_string."\n";
#print $pout_axiom."\t".$pout_pos."\t".$pout_chr."\t".$p_new_string."\n";
#print $out_axiom."\t".$out_pos."\t".$out_chr."\t".$new_string."\n";
push (@final_array, $p_final);
push (@final_array, $final);
}
else{
my ($pout_axiom, $pout_pos, $pout_chr, $pout_rest) = split(/\t/, $previous_outline, 4);
my @p_split_rest = split(/\t/, $pout_rest);
my $p_new_string;
foreach(@p_split_rest){
if ($_ >= 0.4 ){
$p_new_string = $p_new_string."$_\t";
}
else{
$p_new_string = $p_new_string."_\t";
}
}
### Assign output string to variable ###
my $p_final = $pout_axiom."\t".$pout_pos."\t".$pout_chr."\t".$p_new_string."\n";
my $final = $out_axiom."\t".$out_pos."\t".$out_chr."\t".$new_string."\n";
#print $pout_axiom."\t".$pout_pos."\t".$pout_chr."\t".$p_new_string."\n";
#print $out_axiom."\t".$out_pos."\t".$out_chr."\t".$new_string."\n";
push (@final_array, $p_final);
push (@final_array, $final);
}
@previous_split = @split_rest;
$previous_outline = $outline;
}
undef @criteria;
}
$match = 1;
}
## If array contains an element < 0.4 ####
else{
$match = 0;
# my $number_of_tabs = 100;
# my $tabbed_line = "_\t" x $number_of_tabs;
# my $blank_line = $axiom."\t".$pos."\t".$chr."\t".$tabbed_line;
my $blank_line = $line;
push (@final_array, $blank_line);
undef @criteria;
undef @previous_split;
undef $previous_outline;
}
undef @scores;
}
######### Print out final data ###########
my @filtered = uniq(@final_array);
### remove trailing tabs ###
s{\t+$}{}g foreach @filtered;
foreach(@filtered){
print $_;
}
### Subroutine to remove duplicates from array ###
sub uniq {
my %seen;
grep !$seen{$_}++, @_;
}