| 1 | # -*-CPerl-*- | ||||||||||||||||||
| 2 | # Last changed Time-stamp: <2018-11-25 14:37:31 mtw> | ||||||||||||||||||
| 3 | package Bio::ViennaNGS::FeatureIO; | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | 1 | ▶ | use Bio::ViennaNGS; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 1 | ▶ | use Moose; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use File::Slurp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 1 | ▶ | use Bio::ViennaNGS::Bed; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 1 | ▶ | use Bio::ViennaNGS::Feature; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 1 | ▶ | use Bio::ViennaNGS::FeatureChain; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 1 | ▶ | use Bio::ViennaNGS::BedGraphEntry; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 1 | ▶ | use version; our $VERSION = version->declare("$Bio::ViennaNGS::VERSION"); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 1 | has 'file' => ( # file path | |||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||
| 18 | isa => 'Str', | ||||||||||||||||||
| 19 | predicate => 'has_file', | ||||||||||||||||||
| 20 | required => 1, | ||||||||||||||||||
| 21 | ); | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 1 | has 'filetype' => ( # BED6, BED12, GFF, GTF | |||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||
| 25 | isa => 'Str', | ||||||||||||||||||
| 26 | predicate => 'has_filetype', | ||||||||||||||||||
| 27 | required => 1, | ||||||||||||||||||
| 28 | ); | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 1 | has 'instanceOf' => ( | |||||||||||||||||
| 31 | is => 'rw', | ||||||||||||||||||
| 32 | isa => 'Str', # BedGraph, ContainerFeature | ||||||||||||||||||
| 33 | predicate => 'has_instanceOf', | ||||||||||||||||||
| 34 | required => 1, | ||||||||||||||||||
| 35 | writer => 'set_instanceOf', | ||||||||||||||||||
| 36 | ); | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | has 'data' => ( | ||||||||||||||||||
| 39 | is => 'rw', | ||||||||||||||||||
| 40 | isa => 'ArrayRef', | ||||||||||||||||||
| 41 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 42 | 1 | traits => ['Array'], | |||||||||||||||||
| 43 | predicate => 'has_data', | ||||||||||||||||||
| 44 | handles => { | ||||||||||||||||||
| 45 | all => 'elements', | ||||||||||||||||||
| 46 | count => 'count', | ||||||||||||||||||
| 47 | add => 'push', | ||||||||||||||||||
| 48 | pop => 'pop', | ||||||||||||||||||
| 49 | }, | ||||||||||||||||||
| 50 | ); | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 1 | has '_entries' => ( # of elements in $self->data | |||||||||||||||||
| 53 | is => 'rw', | ||||||||||||||||||
| 54 | isa => 'Int', | ||||||||||||||||||
| 55 | predicate => 'nr_entries', | ||||||||||||||||||
| 56 | init_arg => undef, # make this unsettable via constructor | ||||||||||||||||||
| 57 | builder => 'count_entries', | ||||||||||||||||||
| 58 | lazy => 1, | ||||||||||||||||||
| 59 | ); | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 1 | with 'Bio::ViennaNGS::FeatureBase'; | |||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | 0 | ▶ | sub BUILD { # call a parser method, depending on $self->instanceOf | ||||||||||||||||
|
Subroutine
BUILD: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||
| 65 | 0 | my $this_function = (caller(0))[3]; | |||||||||||||||||
| 66 | 0 | my $type; | |||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 0 | ▶ | confess "ERROR [$this_function] \$self->file not available" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | unless ($self->has_file); | ||||||||||||||||||
| 70 | 0 | ▶ | confess "ERROR [$this_function] \$self->filetype not available" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 71 | unless ($self->has_filetype); | ||||||||||||||||||
| 72 | 0 | ▶ | confess "ERROR [$this_function] \$self->instanceOf not available" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 73 | unless ($self->has_instanceOf); | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 0 | ▶ | if ($self->filetype eq "BedGraph"){ | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 76 | #carp "INFO [$this_function] \$self->instanceOf is BedGraph\n"; | ||||||||||||||||||
| 77 | 0 | $self->parse_bedgraph_file($self->file); | |||||||||||||||||
| 78 | 0 | return $self->data; | |||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | elsif ($self->filetype =~ m/[Bb]ed6/){ | ||||||||||||||||||
| 81 | 0 | ▶ | if($self->instanceOf eq "Feature"){ | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 82 | #carp "INFO [$this_function] \$self->instanceOf is Feature\n"; | ||||||||||||||||||
| 83 | 0 | $type=0; # ArrayRef of individual Feature objects | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | elsif ($self->instanceOf eq "FeatureChain"){ | ||||||||||||||||||
| 86 | #carp "INFO [$this_function] \$self->instanceOf is FeatureChain\n"; | ||||||||||||||||||
| 87 | 0 | $type=1; # ArrayRef of FeatureChain objects, one per Feature object | |||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | elsif ($self->instanceOf eq "FeatureChainBlock"){ | ||||||||||||||||||
| 90 | #carp "INFO [$this_function] \$self->instanceOf is FeatureChainBlock\n"; | ||||||||||||||||||
| 91 | 0 | $type=2; # ArrayRef of the entire block of Features (aka Bed12 from Bed6 block) | |||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | else{ | ||||||||||||||||||
| 94 | 0 | croak "ERROR [$this_function] Invalid type for \$self->instanceOf: $self->instanceOf"; | |||||||||||||||||
| 95 | } | ||||||||||||||||||
| 96 | 0 | $self->parse_bed6_file($self->file,$type); | |||||||||||||||||
| 97 | 0 | return $self->data; | |||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | elsif ($self->filetype =~ m/[Bb]ed12/){ | ||||||||||||||||||
| 100 | 0 | ▶ | if($self->instanceOf eq "Bed"){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | #carp "INFO [$this_function] \$self->instanceOf is Bed\n"; | ||||||||||||||||||
| 102 | 0 | $type=0; # ArrayRef of individual Bio::ViennaNGS::Bed objects | |||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | 0 | else {croak "ERROR [$this_function] currently only 'Bed' is a valid option for \$self->instance";} | |||||||||||||||||
| 105 | 0 | $self->parse_bed12_file($self->file,$type); | |||||||||||||||||
| 106 | 0 | return $self->data; | |||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | else{ | ||||||||||||||||||
| 109 | 0 | croak "ERROR [$this_function] Invalid type for \$self->filetyp: $self->filetype"; | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | 0 | $self->count_entries(); | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 0 | ▶ | sub count_entries { | ||||||||||||||||
|
Subroutine
count_entries: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||||||
| 116 | 0 | my $cnt = scalar @{$self->data}; | |||||||||||||||||
| 117 | 0 | $self->_entries($cnt); | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | # TODO ensure FeatureBase is handled correctly | ||||||||||||||||||
| 121 | 0 | ▶ | sub parse_bedgraph_file{ | ||||||||||||||||
|
Subroutine
parse_bedgraph_file: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 122 | 0 | my ($self,$filename) = @_; | |||||||||||||||||
| 123 | 0 | my $this_function = (caller(0))[3]; | |||||||||||||||||
| 124 | 0 | my ($file,$line,$entry,$chr,$start,$end,$val); | |||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | 0 | $file = read_file( $filename, array_ref => 1, chomp =>1 ) ; | |||||||||||||||||
| 127 | 0 | foreach $line (@$file){ | |||||||||||||||||
| 128 | 0 | ▶ | croak "ERROR [$this_function] cannot parse bedGraph input from $filename" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 129 | unless {$line =~ m/^([a-zA-Z0-9._]+)\t(\d+)\t(\d+)\t(-?\d+\.?\d*)$/}; | ||||||||||||||||||
| 130 | 0 | $chr = $1; $start = $2; $end = $3, $val = $4; | |||||||||||||||||
| 131 | #print "++ \$chr $chr ++ \$start $start ++ \$end $end ++ \$val $val\n"; | ||||||||||||||||||
| 132 | 0 | $entry = Bio::ViennaNGS::BedGraphEntry->new(chromosome => $chr, | |||||||||||||||||
| 133 | start => $start, | ||||||||||||||||||
| 134 | end => $end, | ||||||||||||||||||
| 135 | dataValue => $val); | ||||||||||||||||||
| 136 | 0 | push @{$self->data}, $entry; | |||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | } | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 0 | ▶ | sub parse_bed6_file{ | ||||||||||||||||
|
Subroutine
parse_bed6_file: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||
| 141 | 0 | my ($self,$file,$typ) = @_; | |||||||||||||||||
| 142 | 0 | my $this_function = (caller(0))[3]; | |||||||||||||||||
| 143 | 0 | my ($line,$feat,$fc); | |||||||||||||||||
| 144 | 0 | $file = read_file( $file, array_ref => 1, chomp =>1 ); | |||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | 0 | ▶ | if ($typ == 2){ # initialize an empty FeatureChain object | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 147 | 0 | $fc = Bio::ViennaNGS::FeatureChain->new(type => "feature", | |||||||||||||||||
| 148 | base => $self->base); | ||||||||||||||||||
| 149 | } | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | # print "********** in parse_bed6: typ= $typ ************\n"; | ||||||||||||||||||
| 152 | 0 | foreach $line (@$file){ | |||||||||||||||||
| 153 | 0 | my @feat = split /\t/,$line; | |||||||||||||||||
| 154 | 0 | $feat = Bio::ViennaNGS::Feature->new(chromosome=>$feat[0], | |||||||||||||||||
| 155 | start=>$feat[1], | ||||||||||||||||||
| 156 | end=>$feat[2], | ||||||||||||||||||
| 157 | name=>$feat[3], | ||||||||||||||||||
| 158 | score=>$feat[4], | ||||||||||||||||||
| 159 | strand=>$feat[5], | ||||||||||||||||||
| 160 | base=>$self->base); | ||||||||||||||||||
| 161 | 0 | ▶ | if($typ == 0){ # ArrayRef of individual Feature objects | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 162 | 0 | push @{$self->data}, $feat; | |||||||||||||||||
| 163 | } | ||||||||||||||||||
| 164 | elsif ($typ == 1) { # ArrayRef of FeatureChain objects, one per Feature object | ||||||||||||||||||
| 165 | 0 | $fc = Bio::ViennaNGS::FeatureChain->new(type => "feature", | |||||||||||||||||
| 166 | chain => [$feat], | ||||||||||||||||||
| 167 | base => $self->base); | ||||||||||||||||||
| 168 | 0 | push @{$self->data}, $fc; | |||||||||||||||||
| 169 | } | ||||||||||||||||||
| 170 | elsif($typ == 2){ | ||||||||||||||||||
| 171 | 0 | $fc->add($feat); | |||||||||||||||||
| 172 | 0 | $fc->count_entries(); | |||||||||||||||||
| 173 | } | ||||||||||||||||||
| 174 | else{ | ||||||||||||||||||
| 175 | 0 | croak "ERROR [$this_function] don't know how to handle type $typ"; | |||||||||||||||||
| 176 | } | ||||||||||||||||||
| 177 | } #end foreach | ||||||||||||||||||
| 178 | 0 | ▶ | if ($typ == 2) { push @{$self->data}, $fc; } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 179 | # print Dumper($self); | ||||||||||||||||||
| 180 | } | ||||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | 0 | ▶ | sub parse_bed12_file{ | ||||||||||||||||
|
Subroutine
parse_bed12_file: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 183 | 0 | my ($self,$file,$typ) = @_; | |||||||||||||||||
| 184 | 0 | my $this_function = (caller(0))[3]; | |||||||||||||||||
| 185 | 0 | my ($line,$feat,$fc); | |||||||||||||||||
| 186 | 0 | $file = read_file( $file, array_ref => 1, chomp =>1 ); | |||||||||||||||||
| 187 | 0 | foreach $line (@$file){ | |||||||||||||||||
| 188 | 0 | my @mcData = split /\t/,$line; | |||||||||||||||||
| 189 | 0 | ▶ | if ($typ == 0){ # ArrayRef of Bio::ViennaNGS::Bed objects | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 190 | 0 | my $bo = Bio::ViennaNGS::Bed->new(chromosome => $mcData[0], | |||||||||||||||||
| 191 | start => $mcData[1], | ||||||||||||||||||
| 192 | end => $mcData[2], | ||||||||||||||||||
| 193 | name => $mcData[3], | ||||||||||||||||||
| 194 | score => $mcData[4], | ||||||||||||||||||
| 195 | strand => $mcData[5], | ||||||||||||||||||
| 196 | thickStart => $mcData[6], | ||||||||||||||||||
| 197 | thickEnd => $mcData[7], | ||||||||||||||||||
| 198 | itemRgb => $mcData[8], | ||||||||||||||||||
| 199 | blockCount => $mcData[9], | ||||||||||||||||||
| 200 | blockSizes => $mcData[10], | ||||||||||||||||||
| 201 | blockStarts => $mcData[11], | ||||||||||||||||||
| 202 | ); | ||||||||||||||||||
| 203 | 0 | push @{$self->data}, $bo; | |||||||||||||||||
| 204 | } | ||||||||||||||||||
| 205 | } | ||||||||||||||||||
| 206 | |||||||||||||||||||
| 207 | } | ||||||||||||||||||
| 208 | |||||||||||||||||||
| 209 | 1 | ▶ | no Moose; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 210 | |||||||||||||||||||
| 211 | 1 | 1; | |||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | __END__ | ||||||||||||||||||