-
Notifications
You must be signed in to change notification settings - Fork 0
/
superSetUTR.pl
233 lines (192 loc) · 5.85 KB
/
superSetUTR.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
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
use strict;
use Bio::DB::GFF;
use Data::Dumper;
# UTRs seem not to be associated with any CDS explicitly. (lame) Still going to agg them because they might have introns.
my $cds_utr = Bio::DB::GFF::Aggregator->new(-method=> 'utr',
-sub_parts=>[('three_prime_UTR')]); # CDS:curated',
my $db = Bio::DB::GFF->new(-adaptor => 'dbi::mysqlopt',
-dsn => 'dbi:mysql:WS229',
-aggregator=> [$cds_utr],
-user => 'root',
-password => 'argonaute'
);
my $file = IO::File->new("> UTRomeUTRs.fa") or die $!;
my $junk_file = IO::File->new("> bad_utr.txt") or die $!;
print "Enter\n";
foreach my $csome (qw /CHROMOSOME_I CHROMOSOME_II CHROMOSOME_III CHROMOSOME_IV CHROMOSOME_V CHROMOSOME_X/) #
{
my (%spaceUsedAlreadyPlus, %spaceUsedAlreadyMinus);
my $segment = $db->segment($csome); #
$segment->absolute(1);
my (%posToBasePos, %posToBaseNeg);
# zero based or 1 based? <<-- seemingly 1
print "getting dna\n";
my $dnaStr = $segment->dna;
my @dna = split //,$dnaStr;
$dnaStr = '';
my $revcom = join('',@dna); #reverse join('',@dna); reverse join('',@dna);
$revcom =~ tr/ACGTacgt/TGCAtgca/;
my @revDNA = split //,$revcom;
# print @revDNA[0..100];
# exit;
print "Done DNA\n";
print "getting utr\n";
my @utrs = $segment->contained_features('utr'); # arg agg above
print "Done UTR, looking up info\n";
foreach my $utr (@utrs)
{
# print "-->", $utr->sub_SeqFeature;
# get the strand, ignore stuff that's on the other strand.
$utr->absolute(1); # abs_start being used below
my $utr_strand = $utr->strand;
my $assocCDSs_href = lookup_CDS($utr, $segment);
foreach my $part ($utr->sub_SeqFeature)
{
# print $part->abs_low,"..",$part->abs_high,"\n";
foreach my $loc ($part->abs_low..$part->abs_high)
{
my %h;
$h{cds} = $assocCDSs_href;
$h{utr} = {$utr->name=>1};
$h{strand} = $utr_strand;
#print $loc,"->",$dna[$loc-1],"\n";
if($utr_strand == 1)
{
$h{nuc} = $dna[$loc-1];
$posToBasePos{$loc} = \%h;
}
else
{
$h{nuc}=$revDNA[$loc];
$posToBaseNeg{$loc} = \%h;
}
}
}
# print $junk_file $utr->name." Space used twice + \n" if ($spaceUsedAlreadyPlus{$_});
# print $junk_file $utr->name." Space used twice - \n" if ($spaceUsedAlreadyMinus{$_});
}
print "done info\n";
@revDNA = @dna = @utrs = ();
my $catDNA;
my (%cdsToUniq, %utrToUniq);
#, @posToBaseNeg
my $prevLoc = 0;
foreach my $loc (sort {$a<=>$b} keys %posToBasePos)
{
#print $loc,"\n";
#c489819,t489820,c489821,g489822,
if ($prevLoc+1 != $loc) # if we hit an elt in the arr that has no object attached, jumped off the end of a UTR, need to print out and dump. or last itration
{
my $cdsStr = join "_",keys %cdsToUniq;
my $utrStr = join "_",keys %utrToUniq;
if ($cdsStr ne '')
{
print $file join '|', (">$utrStr",$cdsStr,'+');
print $file "\n",$catDNA,"\n";
}
%cdsToUniq = ();
%utrToUniq = ();
$catDNA ='';
}
$prevLoc=$loc;
my %obj=%{$posToBasePos{$loc}};
# my $d = Data::Dumper->new([$obj]);
# print $d->Dump;
# next;
foreach(keys %{$obj{cds}})
{
$cdsToUniq{$_}=1;
}
foreach(keys %{$obj{utr}})
{
$utrToUniq{$_}=1;
}
$catDNA .= $obj{'nuc'};
# print $loc,",",$obj{'nuc'},"\n";
#('>'.$utr->name, $csome, $utr->abs_start, $utr->abs_end, $strand);
#@cds=keys(
}
my $cdsStrP = join "_",keys %cdsToUniq;
my $utrStrP = join "_",keys %utrToUniq;
print $file join '|', (">$utrStrP",$cdsStrP,'+');
print $file "\n",$catDNA,"\n";
%cdsToUniq = ();
%utrToUniq = ();
$catDNA ='';
foreach my $loc (sort {$b<=>$a} keys %posToBaseNeg)
{
if ($prevLoc-1 != $loc)
{
my $cdsStr = join "_",keys %cdsToUniq;
my $utrStr = join "_",keys %utrToUniq;
if ($cdsStr ne '')
{
print $file join '|', (">$utrStr",$cdsStr,'-');
print $file "\n",$catDNA,"\n";
}
%cdsToUniq = ();
%utrToUniq = ();
$catDNA ='';
}
$prevLoc=$loc;
my %obj=%{$posToBaseNeg{$loc}};
# my $d = Data::Dumper->new([$obj]);
# print $d->Dump;
# next;
foreach(keys %{$obj{cds}})
{
$cdsToUniq{$_}=1;
}
foreach(keys %{$obj{utr}})
{
$utrToUniq{$_}=1;
}
$catDNA .= $obj{'nuc'};
# print $loc,",",$obj{'nuc'};
}
}
$file->close;
$junk_file->close;
sub lookup_CDS() #look up the gene... # want to find a cds that has a stop near or at UTR start
{
my ($utr, $segment) = @_;
my $searchSeg = $segment->subseq($utr->abs_start-10, $utr->abs_end+10);
$searchSeg->absolute(1);
my @allCDS = $searchSeg->features('coding_exon:curated');
my (%good_cds, %utr_to_cds);
foreach my $cds (@allCDS)
{
next if $cds->strand != $utr->strand;
if ($utr->strand == 1)
{
if (($cds->abs_end >= $utr->abs_start-10) && ($utr->abs_end > $cds->abs_end))
{
$utr_to_cds{$cds->name}=1;
}
}
else
{
my ($cds_end, undef ) = sort {$a<=>$b} ($cds->abs_start, $cds->abs_end);
my ($utr_stop, $utr_start ) = sort {$a<=>$b} ($utr->abs_start, $utr->abs_end);
# $cds_of_interest{$cds->name} = 1 if (($cds_end <= $utr_start+10) && ($utr_stop < $cds_end));
$utr_to_cds{$cds->name}=1;
}
}
if (!%utr_to_cds)
{
foreach my $part (@allCDS)
{
print $junk_file $part->strand," ",$part->abs_start," ",$part->abs_end," ",$part->type," ",$part->name, "\n";
}
print $junk_file "utr seg->";
print $junk_file join "..", ('I',$utr->abs_start, $utr->abs_stop, $utr->strand, "\n\n");
}
return \%utr_to_cds;
}
# if (! %{$posToBasePos[$loc]{'cds'}})
# {
# %{$posToBasePos[$loc]{cds}) = {'cds'=>{""}};
# }
# my (undef, $cds_end) = sort {$a<=>$b} ($cds->abs_start, $cds->abs_end);
# my ($utr_start, $utr_stop) = sort {$a<=>$b} ($utr->abs_start, $utr->abs_end);
# $cds_names{$cds->name} = 1 if (($cds_end >= $utr_start-10) && ($utr_stop > $cds_end));