-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersectFiles.pl
More file actions
executable file
·419 lines (342 loc) · 13.1 KB
/
intersectFiles.pl
File metadata and controls
executable file
·419 lines (342 loc) · 13.1 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# (c) 2014 - Sun Ruping
# rs3412@columbia.edu
#
# TODO: compare two files of coordinates, with a minimal consumption of memory and running-time
use strict;
use Data::Dumper;
use File::Basename;
my $maskfile;
my $original;
my $bgzip = 0;
my $gzip = 0;
my $vcf = 0;
my $t=0; #tolerant
my $count = 0;
my $overlap = 0;
my $nonoverlap = 0;
my $column='';
my $csplit='';
my $extraOM = '';
my $michr = 0;
my $mistart = 1;
my $miend = 1;
my $oichr = 0;
my $oistart = 1;
my $oiend = 1;
my $NAvalue = 'NA';
my %printer;
while ($ARGV[0]) {
my $arg = shift @ARGV;
if ($arg eq '-m'){$maskfile = shift @ARGV;}
elsif ($arg eq '-t'){$t = shift @ARGV;}
elsif ($arg eq '-o'){$original = shift @ARGV;}
elsif ($arg eq '-bgz'){$bgzip = 1;}
elsif ($arg eq '-gz'){$gzip = 1;}
elsif ($arg eq '-vcf'){$vcf = 1;}
elsif ($arg eq '-michr'){$michr = shift @ARGV;}
elsif ($arg eq '-mistart'){$mistart = shift @ARGV;}
elsif ($arg eq '-miend'){$miend = shift @ARGV;}
elsif ($arg eq '-oichr'){$oichr = shift @ARGV;}
elsif ($arg eq '-oistart'){$oistart = shift @ARGV;}
elsif ($arg eq '-oiend'){$oiend = shift @ARGV;}
elsif ($arg eq '-count'){$count = 1;}
elsif ($arg eq '-nonoverlap'){$nonoverlap = 1;}
elsif ($arg eq '-overlap'){$overlap = 1;}
elsif ($arg eq '-column'){$column = shift @ARGV;}
elsif ($arg eq '-csplit'){$csplit = shift @ARGV;}
elsif ($arg eq '-extraOM'){$extraOM = shift @ARGV;} #extra constrain to identify lines with the same coordinates, in the order of oringal,mask
elsif ($arg eq '-na'){$NAvalue = shift @ARGV;}
elsif ($arg eq '-h'){print "useage: intersectFiles.pl -o: origninal_filename -m: maskfile [-vcf:vcf file input] [-count/overlap/nonoverlap] [-column:get that column from mask file, columnName or Indexes, e.g. 1-7] [-csplit: split tags for the column] [-t:tolerant]\n"; exit 0;}
else {print "useage: intersectFiles.pl -o: origninal_filename -m: maskfile [-vcf: vcf file input] [-t:tolerant]\n"; exit 0;}
}
my @extraOM;
if ($extraOM ne ''){
@extraOM = split(',', $extraOM);
}
my @columnIndex = ();
my @columnNames = ();
my %maskChrJumper = &getchrpos($maskfile);
print STDERR Dumper(\%maskChrJumper);
print STDERR "column set: $column\tindex:\n";
print STDERR Dumper(\@columnIndex);
print STDERR Dumper(\@columnNames);
my $line;
my $old_chr = "SRP";
#regions for the input of region file
my @variants; #it is an Array like a deque
my $isComment = 0;
open ORI, "$original";
$line = <ORI>;
$isComment = eatline($line, \@variants);
while ($isComment == 1) {
#print comment
if ($count == 1) {
chomp($line);
$line =~ s/[\s\n]$//;
my $maskadd = basename($maskfile);
print "$line\t$maskadd\n";
} elsif ($column ne '') {
chomp($line);
$line =~ s/[\s\n]$//;
my $maskadd = basename($maskfile);
if (scalar(@columnIndex) == 1){
print "$line\t$maskadd\.$column\n" if $csplit eq '';
print "$line\t$maskadd\.$column\.$csplit\n" if $csplit ne '';
} else {
chomp($columnNames[$#columnNames]);
$maskadd = join("\t", @columnNames);
print "$line\t$maskadd\n";
}
} else {
chomp($line);
$line =~ s/[\s\n]$//;
print "$line\n";
}
$line = <ORI>;
$isComment = eatline($line, \@variants); # eat a new line
} # is comment
my $it = 0;
while ( $variants[$it]->{'chr'} ne $old_chr ) {
$old_chr = $variants[$it]->{'chr'}; # set the current chr as old chr
my $chr_id = $variants[$it]->{'chr'};
if ( ! exists($maskChrJumper{$chr_id}) ) { # reference not found
while ( $it <= $#variants and $variants[$it]->{'chr'} eq $old_chr ) {
&var_processing($variants[$it]); # print the old region info
splice(@variants, $it, 1); # erase the current region ########################################################################
}
while ( $#variants == -1 ) {
$line = <ORI>;
if (!defined $line) {
print STDERR "finished: end of region file, zone 0\n";
exit 0;
}
eatline($line, \@variants);
$it = 0;
if ( $variants[$it]->{'chr'} eq $old_chr ) {
&var_processing($variants[$it]);
@variants = ();
next;
}
}
next;
} #reference not found
my $jumper = $maskChrJumper{$chr_id};
print STDERR "$chr_id\tjumper: $jumper\t$old_chr\t$variants[$it]->{'chr'}\t$variants[$it]->{'start'}\t$variants[$it]->{'end'}\n";
open MASK, "$maskfile";
seek(MASK, $jumper, 0);
while ( <MASK> ) {
chomp;
my @cols = split /\t/;
my $chr_now = $cols[$michr];
$chr_now = 'chr'.$chr_now unless ($chr_now =~ /^chr/);
$chr_now = 'chrM' if ($chr_now eq 'chrMT');
my $dbStart = $cols[$mistart];
my $dbEnd = $cols[$miend];
if ($chr_now ne $old_chr) { # jump out if reaching the next chromosome
last;
}
my $iter = 0;
#skip wierd thing with empty chr and information##############################################################
if ($variants[$iter]->{'chr'} eq '') {
splice(@variants, $iter, 1);
if ( $#variants == -1 ) {
$line = <ORI>;
if (!defined $line) {
print STDERR "finished: end of region file, zone 2.2\n";
exit 0;
}
eatline($line, \@variants); # eat a line and put it into the duque
$iter = 0;
}
}
##############################################################################################################
if ( $variants[$iter]->{'start'} > $dbEnd ) {
next; # skip reads not overlapping with the first region
}
while ( $variants[$iter]->{'chr'} eq $old_chr and $variants[$iter]->{'start'} <= $dbEnd and $iter <= $#variants ) {
if ( $variants[$iter]->{'end'} < $dbStart ) { # the region end is beyond the alignmentStart
&var_processing($variants[$iter]); # processing
splice(@variants, $iter, 1); # this region should be removed
if ( $#variants == -1 ) {
$line = <ORI>;
if (!defined $line) {
print STDERR "finished: end of region file, zone 3\n";
exit 0;
}
eatline($line, \@variants); # eat a line and put it into the duque
$iter = 0;
}
next;
}
if ( $variants[$iter]->{'end'} >= $dbStart and $variants[$iter]->{'start'} <= $dbEnd ) { #overlapping, should take action
if ($extraOM ne ''){ #check the additional O-M mapping identifier, for matching items only, e.g., snv matching
my @originalItems = split (/\t/, $variants[$iter]->{'info'});
my @maskItems = split (/\t/, $_);
push(@{$variants[$iter]->{'printer'}}, $_) if ($originalItems[$extraOM[0]] eq $maskItems[$extraOM[1]]);
} else {
push(@{$variants[$iter]->{'printer'}}, $_);
}
} # overlapping take action!
if ( $iter != $#variants ) {
$iter++; # if this region is not the last element in the deque
} else { # the last element
$line = <ORI>; # get a line of region file
if (!defined $line){
#&var_processing($variants[$iter-1]);
#print STDERR "finished: end of region file, zone 4\n";
#exit 0;
last;
}
eatline($line, \@variants); # eat a line and put it into the duque
$iter = $#variants;
} #the last element
} #while
} # read a mask file line
# somehow to loop back/
$it = 0; # reset to begin
while ( $it <= $#variants and $variants[$it]->{'chr'} eq $old_chr ) {
&var_processing($variants[$it]); # print the old region info
splice(@variants, $it, 1); # erase the current region
}
while ( $#variants == -1 ) {
$line = <ORI>;
if (!defined $line) {
print STDERR "finished: end of region file, zone 5\n";
exit 0;
}
eatline($line, \@variants);
$it = 0;
if ( $variants[$it]->{'chr'} eq $old_chr ) {
&var_processing($variants[$it]);
@variants = ();
next;
}
}
} # region chr != old chr
sub eatline {
my $line = shift;
chomp($line);
$line =~ s/[\s\n]$//;
my $variants = shift;
my $isComment = 0;
if ($line =~ /^#/ || $line =~ /^[cC][hH][rR]\t/) {
$isComment = 1;
return $isComment;
}
my @cols = split (/\t/, $line);
my %variant;
$variant{'chr'} = $cols[$oichr];
$variant{'chr'} = 'chr'.$variant{'chr'} if ($variant{'chr'} !~ /^chr/);
$variant{'chr'} = 'chrM' if ($variant{'chr'} eq 'chrMT');
$variant{'start'} = $cols[$oistart] - $t;
$variant{'end'} = $cols[$oiend] + $t;
$variant{'info'} = $line;
push(@{$variants}, \%variant);
return $isComment;
}
sub getchrpos {
my $dbfile = shift;
my $chr_old = "UNDEF";
my %chr_start;
my $jumper = 0;
open DBFILE, "$dbfile" or die "The db file read error!";
while ( <DBFILE> ) {
if ($_ =~ /^[\#\@]/ or $_ =~ /^[cC][hH][rR]\t/ or $_ =~ /^FID/) {
$jumper = tell DBFILE;
if ($column ne '' and ((($_ =~ /^#/ or $_ =~ /^FID/ or $_ =~ /^[cC][hH][rR]\t/) and $vcf == 0) or ($_ =~ /^#CHROM\t/ and $vcf == 1))) { #vcf header or not: 1 or 0
$_ =~ s/^#//;
my @colnames = split(/\t/, $_);
if ($column =~ /^(\d+)\-(\d+)$/) {
foreach my $i ($1..$2){
push(@columnIndex, $i);
}
} elsif ($column =~ /\,/) {
my @colneed = split(",", $column);
foreach my $i (@colneed) {
push(@columnIndex, $i);
}
} else {
for (my $i = 0; $i <= $#colnames; $i++) {
chomp($colnames[$i]);
$colnames[$i] =~ s/[\s\n]$//;
if ($colnames[$i] eq $column) {
push(@columnIndex, $i); #this is a global variable
}
}
}
foreach my $index (@columnIndex){
push(@columnNames, $colnames[$index]);
}
} #find column index
next;
}
chomp;
my @cols = split /\t/;
my $chr = $cols[$michr];
$chr = 'chr'.$chr unless ($chr =~ /^chr/);
$chr = 'chrM' if ($chr eq 'chrMT');
if ($chr ne $chr_old) {
$chr_start{$chr} = $jumper;
}
$chr_old = $chr;
$jumper = tell DBFILE;
}
close DBFILE;
print STDERR "$dbfile chr_start loaded\n";
return %chr_start;
}
sub var_processing {
my $variant = shift;
my $original = $variant->{'info'};
return if $original eq '';
if ($count == 0 and $overlap == 0 and $nonoverlap == 0 and $column eq '') {
print "###\t$original\n";
foreach my $printer ( @{$variant->{'printer'}} ) {
print "$printer\n";
}
} elsif ($count == 1) {
my $nump = 0;
foreach my $printer ( @{$variant->{'printer'}} ) {
$nump++;
}
print "$original\t$nump\n" if ($count == 1 and $nonoverlap == 0 and $overlap == 0);
print "$original\t$nump\n" if ($count == 1 and $nonoverlap == 1 and $nump == 0);
print "$original\t$nump\n" if ($count == 1 and $overlap == 1 and $nump > 0);
} elsif ($column ne '') {
my $columnAdd;
foreach my $printer ( @{$variant->{'printer'}} ) {
my @tmp = split (/\t/, $printer);
my @columnAdd;
foreach my $index (@columnIndex) {
push(@columnAdd, $tmp[$index]);
}
if (scalar(@columnAdd) == 1) {
if ($csplit eq ''){
$columnAdd = $columnAdd[0];
} else {
$columnAdd[0] =~ /$csplit\=(.+?);/; #vcf tags
my $tag = $1;
if ($tag eq '') { #tag not found
if ($columnAdd[0] =~ /^RS\=(.+?);/){ #RS found
$columnAdd = 'rs'.$1;
} else { #RS not found
$columnAdd = $columnAdd[0];
}
} else { #tag found
$columnAdd = $tag;
}
}
} else {
$columnAdd = join("\t", @columnAdd);
}
}
if ($columnAdd eq '') {
my $colneed = scalar(@columnIndex);
$columnAdd = ($NAvalue."\t")x$colneed;
$columnAdd =~ s/\t$//;
}
unless ($overlap == 1 and $columnAdd eq $NAvalue){ #do not print only when it is required to be overlapped and nonthing overlap
print "$original\t$columnAdd\n";
}
}
}