-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrstmatrix.pl
executable file
·116 lines (99 loc) · 3.22 KB
/
rstmatrix.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
#!/usr/bin/perl
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
BEGIN {
use FindBin '$Bin';
require "$Bin/share/findinrst.pl";
require "$Bin/share/flagdefinitions.pl";
require "$Bin/share/mktemp_linux.pl";
}
# outputs interaction matrix
#
if ($#ARGV != 0) {
print "usage: ./rstmatrix.pl rsttable < classification > matrix\n";
exit;
};
my $rsttablefn = shift @ARGV;
our $TMPDIR;
# open input files
readrsttable($rsttablefn);
my $alignedfn=mktemp_linux("tmp.XXXXXXXX.couples") or
die "Could not create temporary file";
open(ALIGN, "| sort --parallel=8 --temporary-directory=$TMPDIR " .
"-g -k 1 -k 2 > $alignedfn");
# Filter and sort
our %rsttable;
while (<>) {
my @campi = split("\t");
my (undef, $flag, $leftchr, undef, $leftrst, $rightchr,
undef, $rightrst, undef, undef) = @campi;
if (aligned($flag)) {
die "Chromosome not found" unless
((exists $rsttable{$leftchr}) && (exists $rsttable{$rightchr}));
die "Restriction fragment not found (left), $leftchr, $leftrst"
unless exists $rsttable{$leftchr}->[$leftrst];
die "Restriction fragment not found (right), $rightchr, $rightrst"
unless exists $rsttable{$rightchr}->[$rightrst];
my $leftgrst = $rsttable{$leftchr}->[$leftrst]{index};
my $rightgrst = $rsttable{$rightchr}->[$rightrst]{index};
if ($leftgrst < $rightgrst) {
print ALIGN $leftgrst, "\t", $rightgrst, "\t", $flag, "\n";
} else {
print ALIGN $rightgrst, "\t", $leftgrst, "\t", $flag, "\n";
}
}
}
close(ALIGN);
#link($alignedfn, $alignedfn . ".dbg");
open(ALIGN, "< $alignedfn");
our @rstarray;
my @recnames;
for my $i (@rstarray) {
push @recnames, "\""
. join("~", $i->{index}, $i->{chr}, $i->{n}, $i->{st}, $i->{en})
. "\"";
}
# header
print join("\t", "\"RST\"", @recnames), "\n";
my $printed=0;
my $toprint = scalar(@rstarray);
my @intervector = (0) x scalar(@rstarray);
my $oldleftgrst = 0; my $oldrightgrst = 0;
while (<ALIGN>) {
my @campi = split("\t");
my ($leftgrst, $rightgrst, $flag) = @campi;
die "Wierd things happening"
if (($leftgrst < $oldleftgrst) ||
(($rightgrst < $oldrightgrst) &&
($leftgrst == $oldleftgrst)));
if ($leftgrst != $oldleftgrst) {
$printed++;
print join("\t", $recnames[$oldleftgrst],
@intervector[$oldleftgrst..$#intervector]), "\n";
for my $i (0 .. $#intervector) { $intervector[$i] = 0; };
for ( $oldleftgrst++; $oldleftgrst < $leftgrst; $oldleftgrst++) {
$printed++;
print join("\t", $recnames[$oldleftgrst],
@intervector[$oldleftgrst..$#intervector]), "\n";
}
print STDERR "\33[2K\rElaborating rst $printed out of $toprint"
unless ($printed % 500);
}
$intervector[$rightgrst]++;
$oldleftgrst = $leftgrst; $oldrightgrst = $rightgrst;
}
$printed++;
print join("\t", $recnames[$oldleftgrst],
@intervector[$oldleftgrst..$#intervector]), "\n";
for my $i (0 .. $#intervector) { $intervector[$i] = 0; };
for ( $oldleftgrst++; $oldleftgrst <= $#intervector; $oldleftgrst++) {
$printed++;
print join("\t", $recnames[$oldleftgrst],
@intervector[$oldleftgrst..$#intervector]), "\n";
}
die "Weird things happening, printed $printed out of $toprint"
unless ($printed == $toprint);
close(ALIGN);
print STDERR "\33[2K\rEND\n";
0;