| 1 | # -*-CPerl-*- | ||||||||||
| 2 | # Last changed Time-stamp: <2017-06-10 19:02:34 michl> | ||||||||||
| 3 | |||||||||||
| 4 | package Bio::ViennaNGS::FeatureChain; | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Bio::ViennaNGS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use version; our $VERSION = version->declare("$Bio::ViennaNGS::VERSION"); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | with 'MooseX::Clone'; | |||||||||
| 13 | |||||||||||
| 14 | 1 | has 'type' => ( | |||||||||
| 15 | is => 'rw', | ||||||||||
| 16 | isa => 'Str', # [exon,intron,SJ,promoter,TSS,...] | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | has 'chain' => ( | ||||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | traits => ['Array', 'Clone' => {to=>'ArrayRef'}], | ||||||||||
| 22 | isa => 'ArrayRef[Bio::ViennaNGS::Feature]', | ||||||||||
| 23 | 0 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | 1 | predicate => 'has_chain', | |||||||||
| 25 | auto_deref => 1, | ||||||||||
| 26 | handles => { | ||||||||||
| 27 | all => 'elements', | ||||||||||
| 28 | count => 'count', | ||||||||||
| 29 | add => 'push', | ||||||||||
| 30 | pop => 'pop', | ||||||||||
| 31 | shift_chain => 'shift', | ||||||||||
| 32 | sip => 'sort_in_place', | ||||||||||
| 33 | }, | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | 1 | has 'start' => ( | |||||||||
| 37 | is => 'ro', | ||||||||||
| 38 | isa => 'Int', | ||||||||||
| 39 | predicate => 'has_start', | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | 1 | has '_entries' => ( | |||||||||
| 43 | is => 'rw', | ||||||||||
| 44 | isa => 'Int', | ||||||||||
| 45 | predicate => 'nr_entries', | ||||||||||
| 46 | init_arg => undef, # make this unsettable via constructor | ||||||||||
| 47 | builder => 'count_entries', | ||||||||||
| 48 | lazy => 1, | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | #has 'foo' => ( | ||||||||||
| 52 | # is => 'ro', | ||||||||||
| 53 | # isa => 'Str', | ||||||||||
| 54 | # default => "AAA", | ||||||||||
| 55 | # ); | ||||||||||
| 56 | |||||||||||
| 57 | sub BUILD { | ||||||||||
| 58 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 59 | 0 | my $this_function = (caller(0))[3]; | |||||||||
| 60 | # carp"INFO [$this_function]"; | ||||||||||
| 61 | 0 | ▶ | confess "ERROR [$this_function] \$self->chain not available" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | unless ($self->has_chain); | ||||||||||
| 63 | 0 | $self->count_entries(); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub count_entries { | ||||||||||
| 67 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
count_entries: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 68 | 0 | my $cnt = scalar @{$self->chain}; | |||||||||
| 69 | 0 | $self->_entries($cnt); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | before 'as_bed12_line' => sub { | ||||||||||
| 73 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.1
| |||||||||||
| 74 | 0 | my $this_function = (caller(0))[3]; | |||||||||
| 75 | 0 | ▶ | $self->sip( sub { $_[0]->start <=> $_[1]->start} ) | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 76 | 0 | }; | |||||||||
| 77 | |||||||||||
| 78 | sub sort_chain_ascending { | ||||||||||
| 79 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
sort_chain_ascending: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 80 | 0 | my $this_function = (caller(0))[3]; | |||||||||
| 81 | 0 | ▶ | $self->sip( sub { $_[0]->start <=> $_[1]->start} ) | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 82 | 0 | } | |||||||||
| 83 | |||||||||||
| 84 | sub as_bed12_line{ | ||||||||||
| 85 | 0 | ▶ | my ($self,$name,$score,$strand) = @_; | ||||||||
|
Subroutine
as_bed12_line: not called CC=5 SCAR=34.0
| |||||||||||
| 86 | 0 | my ($i,$chr,$start,$end,$feat,$bed12,$bsizes,$bstarts); | |||||||||
| 87 | 0 | my $count=0; | |||||||||
| 88 | 0 | my @blockSizes = (); | |||||||||
| 89 | 0 | my @blockStarts = (); | |||||||||
| 90 | # TODO check whether all features have the same chromosome id | ||||||||||
| 91 | 0 | $chr = @{$self->chain}[0]->chromosome; | |||||||||
| 92 | 0 | $start = @{$self->chain}[0]->start; | |||||||||
| 93 | 0 | $end = @{$self->chain}[$#{$self->chain}]->end; | |||||||||
| 94 | 0 | ▶ | unless (defined $name){$name=@{$self->chain}[0]->name;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | ▶ | unless (defined $score){$score=@{$self->chain}[0]->score;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | ▶ | unless (defined $strand){$strand=@{$self->chain}[0]->strand;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | # TODO populate blockSizes and blockStarts | ||||||||||
| 99 | 0 | ▶ | for ($i=0;$i<=$#{$self->chain};$i++){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | $count++; | |||||||||
| 101 | 0 | $feat = @{$self->chain}[$i]; | |||||||||
| 102 | 0 | push @blockSizes, eval($feat->end - $feat->start); | |||||||||
| 103 | 0 | push @blockStarts, ($feat->start - $start);; | |||||||||
| 104 | } | ||||||||||
| 105 | 0 | $bsizes = join (",",@blockSizes); | |||||||||
| 106 | 0 | $bstarts = join (",", @blockStarts); | |||||||||
| 107 | 0 | $bed12 = join ("\t",$chr,$start,$end,$name,$score,$strand,$start,$end,"0",$count,$bsizes,$bstarts); | |||||||||
| 108 | 0 | return $bed12; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | sub as_bed6_array{ | ||||||||||
| 112 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
as_bed6_array: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 113 | 0 | $self->count_entries(); | |||||||||
| 114 | 0 | my @bed6array=(); | |||||||||
| 115 | 0 | ▶ | return 0 unless ($self->has_chain); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | ▶ | for (my $i=0;$i<$self->_entries;$i++){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | push @bed6array, join ("\t", | ||||||||||
| 118 | 0 | @{$self->chain}[$i]->chromosome, | |||||||||
| 119 | 0 | @{$self->chain}[$i]->start, | |||||||||
| 120 | 0 | @{$self->chain}[$i]->end, | |||||||||
| 121 | 0 | @{$self->chain}[$i]->name, | |||||||||
| 122 | 0 | @{$self->chain}[$i]->score, | |||||||||
| 123 | 0 | @{$self->chain}[$i]->strand); | |||||||||
| 124 | } | ||||||||||
| 125 | 0 | return \@bed6array; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | #sub clone { | ||||||||||
| 129 | # my ( $self, %params ) = @_; | ||||||||||
| 130 | # $self->meta->clone_object($self, %params); | ||||||||||
| 131 | # return $self; | ||||||||||
| 132 | #} | ||||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 135 | |||||||||||
| 136 | 1 | 1; | |||||||||
| 137 | |||||||||||
| 138 | __END__ | ||||||||||
| 139 | |||||||||||
| 140 | =head1 NAME | ||||||||||
| 141 | |||||||||||
| 142 | Bio::ViennaNGS::FeatureChain - Generic Moose wrapper class for | ||||||||||
| 143 | combined/linked genomic intervals, eg BED12 elements | ||||||||||
| 144 | |||||||||||
| 145 | =head1 SYNOPSIS | ||||||||||
| 146 | |||||||||||
| 147 | use Bio::ViennaNGS::Feature; | ||||||||||
| 148 | use Bio::ViennaNGS::FeatureChain; | ||||||||||
| 149 | |||||||||||
| 150 | # get some new sequence features as instances of Bio::ViennaNGS::Feature | ||||||||||
| 151 | my $start1 = 1100; my $start2 = 2345; my $start3 = 2987; | ||||||||||
| 152 | my $end1 = 1346; my $end2 = 2544; my $end3 = 3076; | ||||||||||
| 153 | my $name1 = "feat1"; my $name2 = "feat2"; my $name3 = "feat3"; | ||||||||||
| 154 | my $strand = "+"; | ||||||||||
| 155 | my $chr = "chr1"; | ||||||||||
| 156 | my $feat1 = Bio::ViennaNGS::Feature->(chromosome => $chr, | ||||||||||
| 157 | start => $start1, | ||||||||||
| 158 | end => $end1, | ||||||||||
| 159 | name => $name1, | ||||||||||
| 160 | strand => $strand, | ||||||||||
| 161 | ); | ||||||||||
| 162 | my $feat2 = Bio::ViennaNGS::Feature->(chromosome => $chr, | ||||||||||
| 163 | start => $start2, | ||||||||||
| 164 | end => $end2, | ||||||||||
| 165 | name => $name2, | ||||||||||
| 166 | strand => $strand, | ||||||||||
| 167 | ); | ||||||||||
| 168 | my $feat3 = Bio::ViennaNGS::Feature->(chromosome => $chr, | ||||||||||
| 169 | start => $start3, | ||||||||||
| 170 | end => $end3, | ||||||||||
| 171 | name => $name3, | ||||||||||
| 172 | strand => $strand, | ||||||||||
| 173 | ); | ||||||||||
| 174 | |||||||||||
| 175 | # initialize a FeatureChain for two of these intervals | ||||||||||
| 176 | my $fc = Bio::ViennaNGS::FeatureChain->new(type => "exon", | ||||||||||
| 177 | chain => [$feat1,$feat3], | ||||||||||
| 178 | ); | ||||||||||
| 179 | |||||||||||
| 180 | # append a genomic intervald to the chain | ||||||||||
| 181 | $fc->add($feat2); | ||||||||||
| 182 | |||||||||||
| 183 | # sort the chain in ascending order by start coordinates | ||||||||||
| 184 | $fc->sort_chain_ascending(); | ||||||||||
| 185 | |||||||||||
| 186 | # get the number of elements in the chain | ||||||||||
| 187 | $fc->count_entries(); | ||||||||||
| 188 | |||||||||||
| 189 | |||||||||||
| 190 | =head1 DESCRIPTION | ||||||||||
| 191 | |||||||||||
| 192 | |||||||||||
| 193 | =head1 METHODS | ||||||||||
| 194 | |||||||||||
| 195 | =over | ||||||||||
| 196 | |||||||||||
| 197 | =item sort_chain_ascending | ||||||||||
| 198 | |||||||||||
| 199 | =item as_bed12_line | ||||||||||
| 200 | |||||||||||
| 201 | =item as_bed6_array | ||||||||||
| 202 | |||||||||||
| 203 | =back | ||||||||||
| 204 | |||||||||||
| 205 | =head1 DEPENDENCIES | ||||||||||
| 206 | |||||||||||
| 207 | =over | ||||||||||
| 208 | |||||||||||
| 209 | =item L<Carp> | ||||||||||
| 210 | |||||||||||
| 211 | =back | ||||||||||
| 212 | |||||||||||
| 213 | =head1 AUTHORS | ||||||||||
| 214 | |||||||||||
| 215 | Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> | ||||||||||
| 216 | |||||||||||
| 217 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 218 | |||||||||||
| 219 | Copyright (C) 2014-2017 Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> | ||||||||||
| 220 | |||||||||||
| 221 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 222 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||||||||||
| 223 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 224 | |||||||||||
| 225 | This program is distributed in the hope that it will be useful, but | ||||||||||
| 226 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 227 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||
| 228 | General Public License for more details. | ||||||||||
| 229 | |||||||||||
| 230 | |||||||||||
| 231 | =cut | ||||||||||