Debian Codegraph @

atac-driver/interscaffold-gaps.pl

raw
1 #!/usr/bin/perl
2
3 use strict;
4
5 my $atacFile = undef;
6 my $minLen = 10000;
7 my $maxChr = 24;
8 my $reference = 0;
9
10 while (scalar(@ARGV)) {
11 my $arg = shift @ARGV;
12
13 if ($arg eq "-m") {
14 $minLen = shift @ARGV;
15 } elsif ($arg eq "-c") {
16 $maxChr = shift @ARGV;
17 } elsif ($arg eq "-a") {
18 $atacFile = shift @ARGV;
19 } elsif ($arg eq "-A") {
20 $reference = 0;
21 } elsif ($arg eq "-B") {
22 $reference = 1;
23 } else {
24 die "Invalid option '$arg'\n";
25 }
26 }
27
28 if (!defined($atacFile)) {
29 print STDERR "usage: $0 [-m minlen] [-c maxchr] [-A | -B] -a <some.atac.ckpLast>\n";
30 print STDERR " -m m Include matches larger than 'm'. Default: 10000\n";
31 print STDERR " -c c Include chromosomes in the reference below 'c'. Default: 24 (1-22+X+Y)\n";
32 print STDERR " -A | -B The reference genome is sequence A (B).\n";
33 print STDERR " -a x Process matches from atac file 'x'\n";
34 exit(1);
35 }
36
37 #if (! -e "$atacFile.gaps.sorted") {
38 open(F, "< $atacFile");
39 open(G, "| sort -k1n -k2n > $atacFile.gaps.sorted");
40 while (<F>) {
41 if (m/^M\sr\s/) {
42 my @vals = split '\s+', $_;
43 (undef, $vals[4]) = split ':', $vals[4];
44 (undef, $vals[8]) = split ':', $vals[8];
45
46 if (($reference == 0) && ($vals[4] < $maxChr) && ($vals[6] > $minLen)) {
47 print G "$vals[4] $vals[5] $vals[6] - $vals[8] $vals[9] $vals[10] - $vals[11]\n";
48 }
49 if (($reference == 1) && ($vals[8] < $maxChr) && ($vals[10] > $minLen)) {
50 print G "$vals[8] $vals[9] $vals[10] - $vals[4] $vals[5] $vals[6] - $vals[11]\n";
51 }
52 }
53 }
54 close(F);
55 close(G);
56 #}
57
58
59 my $lastscf = -1;
60 my @chr;
61 my @pos;
62 my @len;
63 my @scf;
64
65 open(F, "< $atacFile.gaps.sorted");
66 while (<F>) {
67 my @vals = split '\s+', $_;
68 push @chr, $vals[0];
69 push @pos, $vals[1];
70 push @len, $vals[2];
71 push @scf, $vals[4];
72 }
73 close(F);
74
75 push @scf, -1;
76 push @scf, -1;
77
78 my $num = scalar(@chr) - 1;
79
80 # We compute stats on the distance between elements i and j
81 #
82 my $i = 0;
83 my $j = 1;
84
85 print "GAPS\n";
86
87 while ($i < $num) {
88
89 $j = $i+1;
90
91 if ($chr[$i] != $chr[$j]) {
92 $i++;
93 next;
94 }
95
96 again:
97
98 # Move j ahead if it's an interleaved scaffold
99 #
100 # If our current scaffold is interleaved by someone else,
101 # skip that someone else. Yes, the end result of this
102 # is to have i and j point to the same scaffold.
103 #
104 if (($chr[$i] == $chr[$j]) &&
105 ($scf[$i] == $scf[$j+1]) &&
106 ($len[$j] < 5000)) {
107 $j++;
108 }
109
110 # Move j ahead if it's the same scaffold
111 #
112 if (($chr[$i] == $chr[$j]) &&
113 ($scf[$i] == $scf[$j])) {
114 $i = $j;
115 $j++;
116 goto again;
117 }
118
119
120
121 # Report, if begin and end are on the same chromosome.
122 if ($chr[$i] == $chr[$j]) {
123 my $aend = $pos[$i] + $len[$i];
124 my $bsta = $pos[$j];
125 my $gapl = $bsta - $aend;
126
127 if ($gapl > 0) {
128 print "GAP: $gapl -- $i ($chr[$i] $pos[$i] $len[$i] $scf[$i]) -- $j ($chr[$j] $pos[$j] $len[$j] $scf[$j])\n";
129 }
130 }
131
132 $i = $j;
133 }
134