-
Notifications
You must be signed in to change notification settings - Fork 2
/
Ascii_genome_illustrator.pm
executable file
·157 lines (104 loc) · 3.57 KB
/
Ascii_genome_illustrator.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
package Ascii_genome_illustrator;
use strict;
use warnings;
use Carp;
sub new {
my $packagename = shift;
my ($molecule_name, $illustration_length) = @_;
my $self = { name => $molecule_name,
illustration_length => $illustration_length,
features => [],
};
bless ($self, $packagename);
return ($self);
}
####
sub add_features { ## accepts list of features
my $self = shift;
my @features = @_;
foreach my $feature (@features) {
unless (ref $feature eq 'ARRAY') { confess "Error, improper params; should be a list of feature array refs"; }
$self->add_feature(@$feature);
}
return;
}
####
sub add_feature {
my $self = shift;
my ($feature_name, $feature_end5, $feature_end3, $glyph) = @_;
unless ($feature_name && $feature_end5 =~ /^\d+$/ && $feature_end3 =~ /^\d+$/ && defined($glyph) ) {
confess "Error, improper params";
}
unless (length($glyph) == 1 && $glyph !~ /\s/) { confess "Error, glyph must be a single non ws character.";}
my $orient = ($feature_end5 < $feature_end3) ? '+' : '-';
my ($lend, $rend) = sort {$a<=>$b} ($feature_end5, $feature_end3);
#print "Feature: $feature_name, $lend => $rend ($orient)\n";
my $feature_struct = { name => $feature_name,
lend => $lend,
rend => $rend,
orient => $orient,
glyph => $glyph,
};
push (@{$self->{features}}, $feature_struct);
return;
}
####
sub get_features {
my $self = shift;
return (@{$self->{features}});
}
####
sub illustrate {
my $self = shift;
my ($mol_lend, $mol_rend) = @_;
unless ($mol_lend && $mol_rend) { confess "invalid params"; }
my $illustration_length = $self->{illustration_length};
my $text = sprintf("%+20s ", "$mol_lend-$mol_rend") . "[" . ("=" x ($illustration_length-2)) . "]\t" . $self->{name} . "\n";
foreach my $feature ($self->get_features()) {
my ($name, $lend, $rend, $orient, $glyph) = ($feature->{name}, $feature->{lend}, $feature->{rend}, $feature->{orient}, $feature->{glyph});
my @illustration_array;
## init to clean palette
for (my $i = 0; $i < $illustration_length; $i++) { $illustration_array[$i] = " "; }
my ($pos_lend, $pos_rend) = $self->_compute_palette_position([$lend, $rend], [$mol_lend, $mol_rend]);
# draw
for (my $i = $pos_lend; $i <= $pos_rend; $i++) { $illustration_array[$i] = $glyph; }
if ($orient eq '+') {
$illustration_array[$pos_rend] = '>';
}
elsif ($orient eq '-') {
$illustration_array[$pos_lend] = '<';
}
else {
confess "Don't recognize orient:$orient";
}
$text .= sprintf ("%+20s$orient ", "$lend-$rend") . join ("", @illustration_array) . "\t$name\n";
}
return ($text);
}
####
sub _compute_palette_position {
my $self = shift;
my ($feature_coords_aref, $mol_coords_aref) = @_;
my $illustration_length = $self->{illustration_length};
my ($feature_lend, $feature_rend) = @$feature_coords_aref;
my ($mol_lend, $mol_rend) = @$mol_coords_aref;
unless ($feature_lend <= $mol_rend && $feature_rend >= $mol_lend) {
## no overlap
return (-1, -1);
}
if ($feature_lend < $mol_lend) {
$feature_lend = $mol_lend;
}
if ($feature_rend > $mol_rend) {
$feature_rend = $mol_rend;
}
my $mol_region_length = $mol_rend - $mol_lend + 1;
my $delta_left = $feature_lend - $mol_lend + 1;
my $delta_right = $feature_rend - $mol_lend + 1;
my $pos_left = int($delta_left / $mol_region_length * $illustration_length + 0.5) - 1;
$pos_left = 0 if $pos_left < 0;
my $pos_right = int($delta_right / $mol_region_length * $illustration_length + 0.5) - 1;
$pos_right = 0 if $pos_right < 0;
return ($pos_left, $pos_right);
}
1; #EOM