MotifSet.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Motif.pm ↑ Summary
1 package XMS::MotifSet;
2
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 require Exporter;
8
9 our @ISA = qw(Exporter);
10
11
12 # This allows declaration use XMS ':all';
13
14
15 our %EXPORT_TAGS = ( 'all' => [ qw(
16
17 ) ] );
18
19 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
20
21 our @EXPORT = qw(
22
23 );
24
25 our $VERSION = '0.01';
26
27 0 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=2.6
28 use XML::Writer::String;
29 use IO::File;
30 use XMS::WeightMatrix;
31 use XMS::Motif;
32 use XML::DOM;
33
34
35
36 sub new {
37
38 my $class = shift;
39 my $self = {};
40 my(@motifs) = @_;
41 my ($temp) = @motifs;
42
43 if(ref($temp) eq "XMS::Motif"){
44 @{$self->{motifs}} = @motifs;
45
46 }else{
47
48 my $parser = new XML::DOM::Parser;
49 my $doc = $parser->parsefile($temp);
50 my $root = $doc->getDocumentElement();
51
52 my @motifnodes = $root->getElementsByTagName("motif");
53
54 if (scalar @motifnodes == 0){
55 die "Corrupt input XMS file";
56 }
57
58 my $m=0;
59 my @motifarray=();
60
61 foreach my $motif (@motifnodes){
62
63
64 my $motifname = $motif->getElementsByTagName("name")->item(0)->getFirstChild()->getData;
65
66 my $threshold = $motif->getElementsByTagName("threshold")->item(0)->getFirstChild->getData;
67
68 my @props = $motif->getElementsByTagName("prop");
69
70 ####### Begin reading annotation key value pairs ##########
71
72 my %annotations;
73 foreach my $prop (@props){
74 my ($keynode) = $prop->getElementsByTagName("key");
75 my $key="";
76 if ( $keynode->getFirstChild() ) {
77 $key = $keynode->getFirstChild()->getData;
78 }
79 my ($valuenode) = $prop->getElementsByTagName("value");
80 my $value="";
81 if ( $valuenode->getFirstChild() ) {
82 $value = $valuenode->getFirstChild()->getData;
83 }
84 if ( $key ne "" ){
85 $annotations{$key} = $value;
86 }
87 }
88
89 ####### End reading annotation key value pairs ##########
90
91 my @wmnodes = $motif->getElementsByTagName("weightmatrix");
92 my @columnsarray=();
93 foreach my $wmnode (@wmnodes) {
94
95 my @columns=$motif->getElementsByTagName("column");
96
97 my %columnhash;
98 foreach my $column (@columns){
99
100 my @weights=$column->getElementsByTagName("weight");
101
102 foreach my $weight (@weights){
103
104 my $weightsymbol = $weight->getAttributeNode("symbol");
105 my $symbolvalue = $weightsymbol->getValue;
106
107 my $weightvalue=$weight->getFirstChild->getData;
108
109 $columnhash{$symbolvalue} = $weightvalue;
110 }
111 my $count=0;
112 my @wmvalues=();
113 foreach my $key (sort keys %columnhash){
114 $wmvalues[$count]=$columnhash{$key};
115 $count++;
116 }
117 push(@columnsarray,[@wmvalues]);
118 }
119 }
120
121 my $wmobj = XMS::WeightMatrix->new(@columnsarray);
122 my $motifobj = XMS::Motif->new($wmobj,$motifname,$threshold,%annotations);
123 $motifarray[$m] = $motifobj;
124 $m++;
125 }
126 @{$self->{motifs}} = @motifarray;
127 }
128
129 $self->{output} = XML::Writer::String->new();
130 $self->{writer} = new XML::Writer(OUTPUT => $self->{output}, DATA_MODE => 'TRUE', DATA_INDENT=>3);
131
132 bless($self,$class);
133 return $self;
134
135 }
136
137
138 sub toXML {
139
140 my $self = shift;
141 my $writer = $self->{writer};
142 my $output = $self->{output};
143
144 my @motifs = @{$self->{motifs}};
145
146
147 $writer->startTag("motifset");
148 for(my $m=0;$m<@motifs;$m++){
149 $self->{motifs}[$m]->toXML($writer);
150 }
151
152 $writer->endTag("motifset");
153
154 $writer->end();
155 return $self->{output}->value();
156 }
157
158
159 sub toString {
160
161 my $self = shift;
162 my @motifs = @{$self->{motifs}};
163
164 my $rawstring = "";
165
166 for(my $m=0;$m<@motifs;$m++){
167 $rawstring = $rawstring.$self->{motifs}[$m]->toString();
168 if ($m<(@motifs-1)){
169 $rawstring = $rawstring."\n";
170 }
171 }
172 return $rawstring;
173 }
174
175
176 1;
177 __END__
« Motif.pm ↑ Summary