| 1 | package XMS::WeightMatrix; | ||
| 2 | |||
| 3 | use 5.008008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | |||
| 11 | # This allows declaration use XMS ':all'; | ||
| 12 | |||
| 13 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 14 | |||
| 15 | ) ] ); | ||
| 16 | |||
| 17 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 18 | |||
| 19 | our @EXPORT = qw( | ||
| 20 | |||
| 21 | ); | ||
| 22 | |||
| 23 | our $VERSION = '0.01'; | ||
| 24 | |||
| 25 | use XML::Writer; | ||
| 26 | use XML::Writer::String; | ||
| 27 | use IO::File; | ||
| 28 | use UNIVERSAL qw(isa); | ||
| 29 | use XML::Reader; | ||
| 30 | |||
| 31 | |||
| 32 | sub new { | ||
| 33 | |||
| 34 | my $class = shift; | ||
| 35 | my $self = {}; | ||
| 36 | my(@name) = @_; | ||
| 37 | $self->{alphabet} = "DNA"; | ||
| 38 | |||
| 39 | $self->{cols} = \@name; | ||
| 40 | |||
| 41 | $self->{xmlString} = ""; | ||
| 42 | $self->{output} = XML::Writer::String->new(); | ||
| 43 | $self->{writer} = new XML::Writer(OUTPUT => $self->{output}, DATA_MODE => 'TRUE', DATA_INDENT=>3); | ||
| 44 | |||
| 45 | $self->{rawstring} = ""; | ||
| 46 | |||
| 47 | |||
| 48 | bless($self,$class); | ||
| 49 | |||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | sub toXML { | ||
| 55 | |||
| 56 | my $self = shift; | ||
| 57 | my $useLocalXMLWriter = 1; | ||
| 58 | my $xmlString = $self->{xmlString}; | ||
| 59 | |||
| 60 | if($xmlString eq ""){ | ||
| 61 | |||
| 62 | my $writer = $self->{writer}; | ||
| 63 | my $output = $self->{output}; | ||
| 64 | |||
| 65 | my ($wr) = @_; | ||
| 66 | if( @_ && (ref($wr) eq "XML::Writer")){ | ||
| 67 | $writer = $wr; | ||
| 68 | $useLocalXMLWriter = 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | my $alphabet = $self->{alphabet}; | ||
| 72 | my @columns = @{$self->{cols}}; | ||
| 73 | |||
| 74 | my $length = @columns; | ||
| 75 | |||
| 76 | $writer->startTag("weightmatrix", 'alphabet'=>$alphabet, 'columns'=>$length); | ||
| 77 | |||
| 78 | my $symbol; | ||
| 79 | |||
| 80 | for my $c (0 .. $#columns){ | ||
| 81 | |||
| 82 | $writer->startTag("column", 'pos'=>$c); | ||
| 83 | |||
| 84 | for (my $s=0;$s<@{$columns[$c]};$s++){ | ||
| 85 | |||
| 86 | if($s==0){ | ||
| 87 | $symbol = "adenine"; | ||
| 88 | $writer->startTag("weight", 'symbol'=>$symbol); | ||
| 89 | $writer->characters($columns[$c][$s]); | ||
| 90 | $writer->endTag("weight"); | ||
| 91 | }elsif($s==1){ | ||
| 92 | $symbol = "cytosine"; | ||
| 93 | $writer->startTag("weight", 'symbol'=>$symbol); | ||
| 94 | $writer->characters($columns[$c][$s]); | ||
| 95 | $writer->endTag("weight"); | ||
| 96 | }elsif($s==2){ | ||
| 97 | $symbol = "guanine"; | ||
| 98 | $writer->startTag("weight", 'symbol'=>$symbol); | ||
| 99 | $writer->characters($columns[$c][$s]); | ||
| 100 | $writer->endTag("weight"); | ||
| 101 | }elsif($s==3){ | ||
| 102 | $symbol = "thymine"; | ||
| 103 | $writer->startTag("weight", 'symbol'=>$symbol); | ||
| 104 | $writer->characters($columns[$c][$s]); | ||
| 105 | $writer->endTag("weight"); | ||
| 106 | } | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | $writer->endTag("column"); | ||
| 111 | } | ||
| 112 | |||
| 113 | $writer->endTag("weightmatrix"); | ||
| 114 | |||
| 115 | if($useLocalXMLWriter == 1 ) { | ||
| 116 | $writer->end(); | ||
| 117 | $xmlString = $self->{output}->value(); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | return $xmlString; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | sub toString { | ||
| 125 | |||
| 126 | my $self = shift; | ||
| 127 | my $rawstring = $self->{rawstring}; | ||
| 128 | |||
| 129 | if($rawstring eq ""){ | ||
| 130 | |||
| 131 | my @columns = @{$self->{cols}}; | ||
| 132 | |||
| 133 | my $length = @columns; | ||
| 134 | my $sep = ""; | ||
| 135 | for my $c (0 .. $#columns){ | ||
| 136 | |||
| 137 | $rawstring = $rawstring . $sep. "@{$columns[$c]}"; | ||
| 138 | $sep = "\n"; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | return $rawstring; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | sub weightForSymbol { | ||
| 146 | |||
| 147 | my $self = shift; | ||
| 148 | my($symbol,$rowindex) = @_; | ||
| 149 | |||
| 150 | my @columns = @{$self->{cols}}; | ||
| 151 | my $weight; | ||
| 152 | |||
| 153 | |||
| 154 | for my $c (0 .. $#columns){ | ||
| 155 | if($c == $rowindex){ | ||
| 156 | |||
| 157 | my @dna = ("adenine", "cytosine", "guanine", "thymine"); | ||
| 158 | |||
| 159 | for (my $s=0;$s<@dna;$s++){ | ||
| 160 | if($dna[$s] eq "$symbol"){ | ||
| 161 | $weight = $columns[$c][$s]; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | return $weight; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | sub setWeightForSymbol { | ||
| 171 | |||
| 172 | my $self = shift; | ||
| 173 | my($key,$rowindex,$weight)=@_; | ||
| 174 | |||
| 175 | |||
| 176 | my @columns = @{$self->{cols}}; | ||
| 177 | |||
| 178 | for my $c (0 .. $#columns){ | ||
| 179 | |||
| 180 | if($c == $rowindex){ | ||
| 181 | my @dna = ("adenine", "cytosine", "guanine", "thymine"); | ||
| 182 | |||
| 183 | for (my $s=0;$s<@dna;$s++){ | ||
| 184 | if($dna[$s] eq "$key"){ | ||
| 185 | $columns[$c][$s]=$weight; | ||
| 186 | $self->{rawstring} = ""; | ||
| 187 | $self->{xmlString} = ""; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | 1; | ||
| 198 | __END__ |