-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLoci_assigner.pm
executable file
·196 lines (161 loc) · 5.3 KB
/
Loci_assigner.pm
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
package Loci_assigner;
use strict;
my $DEBUG;
## Globals
my @genes;
my %loci;
my $core_locus;
my $AVG_DIST;
my $PAD_TOTAL = 0;
####
sub assign_loci {
my ($gene_struct_list_aref, $preexisting_loci_href) = @_;
## gene struct should have the following format:
# gene = { locus => (value || undef),
# feat_name => $feat_name,
# end5 => $end5,
# end3 => $end3 }
#
## Attributes added herein:
# loc_pos #integer value of trailing locus ID
# core_locus #prefix to locus assignment minus ".loc_pos" suffix
# newLocus #value of newly assigned locus
# index #int specifying index in the sorted gene list
## init globals
$core_locus = undef;
my @gene_structs = @$gene_struct_list_aref;
%loci = %$preexisting_loci_href;
$AVG_DIST = 10; #default;
## sort structs by midPt
@gene_structs = sort { ( ($a->{end5} + $a->{end3})/2)
<=>
( ($b->{end5} + $b->{end3}) / 2) } @gene_structs;
## assign indices:
for (my $i=0; $i <= $#gene_structs; $i++) {
$gene_structs[$i]->{index} = $i;
}
foreach my $gene_struct (@gene_structs) {
my $locus = $gene_struct->{locus};
if ($locus && $locus =~ /^(.*)\.(\d+)$/) {
$loci{$locus} = 1; #store that locus already exists.
my $core_loc_val = $1;
my $loc_pos = $2;
if ($loc_pos =~ /^0/) { # padded number
$PAD_TOTAL = length ($loc_pos);
}
$gene_struct->{loc_pos} = $loc_pos; ## add attribute here.
$gene_struct->{core_locus} = $core_loc_val;
} elsif ($locus) {
die "Error, locus ($locus) lacks the expected format.\n";
}
}
## determine avg distance between assigned locus positions.:
my $count;
my $sum;
for (my $i=0; $i < $#gene_structs; $i++) {
my $curr_pos = $gene_structs[$i]->{loc_pos};
my $next_pos = $gene_structs[$i+1]->{loc_pos};
my $curr_core = $gene_structs[$i]->{core_locus};
my $next_core = $gene_structs[$i]->{core_locus};
unless ($curr_core eq $next_core) {
next;
}
if (defined ($curr_pos) && defined($next_pos)) {
$sum += abs ($next_pos - $curr_pos);
$count++;
}
}
if ($count) {
$AVG_DIST = int ($sum/$count + 0.5);
}
## Perform Loci suggestions.
my $startVal = 0;
my @loci_to_assign;
for (my $i=0; $i <= $#gene_structs; $i++) {
my $struct = $gene_structs[$i];
my $locus = $struct->{locus};
if (!defined($locus)) {
push (@loci_to_assign, $struct);
} else {
my $endVal = $struct->{loc_pos};
if (@loci_to_assign) {
&process_new_loci(\@gene_structs, \@loci_to_assign, $startVal, $endVal);
}
@loci_to_assign = (); #reinitialize
$startVal = $endVal;
}
}
if (@loci_to_assign) {
&process_new_loci (\@gene_structs, \@loci_to_assign, $startVal, undef);
}
return (@gene_structs);
exit(0);
}
####
sub process_new_loci {
my ($gene_structs_aref, $list_to_assign_aref, $start,$end) = @_;
my @list = @$list_to_assign_aref;
my $num_elements = $#list + 1;
print "Number of elements: $num_elements\n" if $DEBUG;
my $increment = 0;
print "start: $start, end: $end\n" if $DEBUG;
## check for consistent core_locus (adjacent genes should be list boundaries or genes with assigned loci)
my $core_locus;
my $consistent_core_locus = 1;
my $left_assigned_locus;
my $right_assigned_locus;
my $first_entry_index = $list[0]->{index};
my $last_entry_index = $list[$#list]->{index};
if ($first_entry_index > 0) {
$left_assigned_locus = $gene_structs_aref->[$first_entry_index-1]->{core_locus};
}
if (my $last_struct = $gene_structs_aref->[$last_entry_index+1]) {
$right_assigned_locus = $last_struct->{core_locus};
}
unless ($left_assigned_locus || $right_assigned_locus) {
die "Error, no locus adjacent to list of genes requiring assignments\n";
}
if ($left_assigned_locus && $right_assigned_locus && (lc($left_assigned_locus) ne lc($right_assigned_locus))) {
$consistent_core_locus = 0;
}
if ($left_assigned_locus) {
$core_locus = $left_assigned_locus;
} else {
$core_locus = $right_assigned_locus;
}
if ($consistent_core_locus && $end) {
## determine incrementation:
my $diff = $end - $start;
print "diff\t$diff\n" if $DEBUG;
$increment = int($diff/($num_elements+1) + 0.5);
print "increment: $increment\n" if $DEBUG;
if ($increment < 1) {
$increment = 1;
}
} else {
$increment = $AVG_DIST; #default
}
print "increment: $increment\n" if $DEBUG;
my $begin = $start;
foreach my $struct (@list) {
$begin += $increment;
if ($PAD_TOTAL && (length("$begin") < $PAD_TOTAL)) {
my $num_pad = $PAD_TOTAL - length("$begin");
$begin = ('0' x $num_pad) . $begin;
}
my $tentative_locus = $core_locus . "." . $begin;
print "tentative_locus: $tentative_locus\n" if $DEBUG;
while ($loci{$tentative_locus}) {
$begin += 1;
if ($PAD_TOTAL && (length("$begin") < $PAD_TOTAL)) {
my $num_pad = $PAD_TOTAL - length("$begin");
$begin = ('0' x $num_pad) . $begin;
}
$tentative_locus = $core_locus . "." . $begin;
print "\ttentative locus in use. Trying new one: $tentative_locus\n" if $DEBUG;
}
$struct->{newLocus} = $tentative_locus;
$loci{$tentative_locus} = 1;
}
}
1;