-
Notifications
You must be signed in to change notification settings - Fork 7
/
exblxtransform.pl
executable file
·155 lines (131 loc) · 4.52 KB
/
exblxtransform.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
#!/usr/bin/perl
use GFFTransform;
$mspindexprog = "mspindex.pl";
$usage .= "$0 -- convert MSPcrunch -d output into new co-ordinate system\n";
$usage .= "\n";
$usage .= "Usage: $0 [-verbose] [-noindex] [-sym|-asym] [-self] <GFF file> [<MSP file>]";
$usage .= "\n";
$usage .= "Use -sym and -asym to symmetrise/asymmetrise MSP data\n";
$usage .= "Use -self to allow self-hits\n";
$usage .= "Use -noindex to suppress lookup/generation of <MSP file>.index\n";
$usage .= "\n";
while (@ARGV) {
last unless $ARGV[0] =~ /^-/;
$opt = lc shift;
if ($opt eq "-verbose") { $verbose = 1 }
elsif ($opt eq "-noindex") { $noindex = 1 }
elsif ($opt eq "-sym") { $sym = 1 }
elsif ($opt eq "-asym") { $asym = 1 }
elsif ($opt eq "-self") { $self = 1 }
else { die "$usage\nUnknown option: $opt\n" }
}
$sym = $sym || $asym;
@ARGV==1 or @ARGV==2 or die $usage;
$gfffile = shift;
read_transformation($gfffile);
if (@ARGV && !$noindex) {
$mspfile = shift;
$mspindex = "$mspfile.index";
unless (-e $mspindex) {
warn "Building index for $mspfile...\n" if $verbose;
system "$mspindexprog $mspfile";
}
open MSPINDEX, $mspindex or die "$0: couldn't open $mspindex: $!";
open MSPFILE, $mspfile or die "$0: couldn't open $mspfile: $!";
while ($tell = tell MSPINDEX, $_ = <MSPINDEX>) {
(@oldname[0,1],$pos) = split;
$oldnamepair = "@oldname";
die "$mspindex unsorted" if $oldname[0] ne $lastoldname[0] && $oldname[1] ne $lastoldname[1] && exists $pos{$oldnamepair};
if ($oldname[0] ne $lastoldname[0]) {
warn "Reading index for (@oldname)...\n" if $verbose;
$mspindexpos{$oldname[0]} = $tell;
}
check_validity($oldname[0]);
check_validity($oldname[1]);
@lastoldname = @oldname;
}
sort_seqnames();
foreach $newname1 (@newnames) {
foreach $newname2 (@newnames) {
@newname = ($newname1,$newname2);
next if $asym && $newname[0] gt $newname[1];
warn "Preparing (@newname) output...\n" if $verbose;
@oldname1 = sort { $start{$a} <=> $start{$b} } @{$oldnames{$newname[0]}};
while (@oldname1) {
@oldname1_samestart = ();
while (@oldname1_samestart == 0 || (@oldname1 && $start{$oldname1[0]} == $start{$oldname1_samestart[0]})) {
push @oldname1_samestart, shift(@oldname1);
}
%mspfilepos = %flip = ();
foreach $oldname (@oldname1_samestart) {
next unless exists $mspindexpos{$oldname};
seek MSPINDEX, $mspindexpos{$oldname}, 0;
while (<MSPINDEX>) {
(@oldname[0,1],$pos) = split;
last if $oldname[0] ne $oldname;
if ($name{$oldname[1]} eq $newname[1]) {
$mspfilepos{"@oldname"} = $pos;
$flip{"@oldname"} = 0;
}
}
}
if ($sym) {
foreach $oldname (@{$oldnames{$newname[1]}}) {
next unless exists $mspindexpos{$oldname};
seek MSPINDEX, $mspindexpos{$oldname}, 0;
while (<MSPINDEX>) {
(@oldname[0,1],$pos) = split;
last if $oldname[0] ne $oldname;
if (grep($_ eq $oldname[1], @oldname1_samestart) && !exists($flip{"@oldname[1,0]"})) {
$mspfilepos{"@oldname"} = $pos;
$flip{"@oldname"} = 1;
}
}
}
}
%mspstart = ();
foreach $oldnamepair (keys %mspfilepos) {
@oldname = split /\s+/, $oldnamepair;
$flip = $flip{$oldnamepair};
seek MSPFILE, $mspfilepos{$oldnamepair}, 0;
while (<MSPFILE>) {
@f = split;
last unless $f[4] eq $oldname[0] && $f[7] eq $oldname[1];
@msp = transform_msp(\@f,$flip);
$mspstart{join("\t",@msp)."\n"} = $msp[2];
if ($sym && $oldname[0] eq $oldname[1]) {
@msp[2..4,5..7] = @msp[5..7,2..4];
if ($msp[2] > $msp[3]) { @msp[2,3,5,6] = @msp[3,2,6,5] }
$mspstart{join("\t",@msp)."\n"} = $msp[2];
}
}
}
warn "Sorting (@{[keys %mspfilepos]}) output...\n" if $verbose && keys(%mspstart);
foreach (sort { $mspstart{$a} <=> $mspstart{$b} or (split(/\s+/,$a))[5] <=> (split(/\s+/,$b))[5] } keys %mspstart) {
@f = split;
print unless ($asym && $newname[0] eq $newname[1] && ($f[5] < $f[2] || $f[6] < $f[2])) || (!$self && $f[2] == $f[5] && $f[3] == $f[6] && $f[4] eq $f[7]);
}
}
}
}
close MSPFILE;
close MSPINDEX;
} else {
while (<>) {
@f = split;
print join("\t",transform_msp(\@f))."\n";
}
}
sub transform_msp {
my ($g,$flip) = @_;
my @f = @$g;
check_validity($g->[4]);
check_validity($g->[7]);
@f[4,2] = transform(@$g[4,2]);
@f[4,3] = transform(@$g[4,3]);
@f[7,5] = transform(@$g[7,5]);
@f[7,6] = transform(@$g[7,6]);
if ($flip) { @f[2..4,5..7] = @f[5..7,2..4] }
if ($f[2] > $f[3]) { @f[2,3,5,6] = @f[3,2,6,5] }
@f;
}