| 1 | package Bio::GMOD::Blast::Graph::ScientificNotation; | ||
| 2 | BEGIN { | ||
| 3 | $Bio::GMOD::Blast::Graph::ScientificNotation::AUTHORITY = 'cpan:RBUELS'; | ||
| 4 | } | ||
| 5 | BEGIN { | ||
| 6 | $Bio::GMOD::Blast::Graph::ScientificNotation::VERSION = '0.06'; | ||
| 7 | } | ||
| 8 | ##################################################################### | ||
| 9 | # | ||
| 10 | # Cared for by Shuai Weng <shuai@genome.stanford.edu> | ||
| 11 | # | ||
| 12 | # Originally created by John Slenk <jces@genome.stanford.edu> | ||
| 13 | # | ||
| 14 | # You may distribute this module under the same terms as perl itself | ||
| 15 | #----------------------------------------------------------------- | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | |||
| 19 | use Bio::GMOD::Blast::Graph::MyDebug qw( dmsg debugP ); | ||
| 20 | |||
| 21 | my( $kZero ) = '0.0e0'; | ||
| 22 | my( $kOne ) = '1.0e0'; | ||
| 23 | |||
| 24 | # some day i should make a really strong set | ||
| 25 | # of regexps to make sure i'm handling valid numbers. | ||
| 26 | my( $kAllowedGlyphsRegexp ) = '[\de\.-]'; | ||
| 27 | my( $kNotAllowedGlyphsRegexp ) = '[^\de\.-]'; | ||
| 28 | |||
| 29 | debugP( 0 ); | ||
| 30 | |||
| 31 | # i want a zero p value to be the first thing in the list - we're really | ||
| 32 | # trying to sort from most to least significant. gross. | ||
| 33 | |||
| 34 | sub cmp | ||
| 35 | { | ||
| 36 | my( $a, $b ) = @_; | ||
| 37 | |||
| 38 | my( $asn, $am, $ae ); | ||
| 39 | my( $bsn, $bm, $be ); | ||
| 40 | my( $cmp ); | ||
| 41 | |||
| 42 | $asn = toScientificNotation( $a ); | ||
| 43 | $am = getMantissa( $asn ); | ||
| 44 | $ae = getExponent( $asn ); | ||
| 45 | |||
| 46 | $bsn = toScientificNotation( $b ); | ||
| 47 | $bm = getMantissa( $bsn ); | ||
| 48 | $be = getExponent( $bsn ); | ||
| 49 | |||
| 50 | # jmc debugging | ||
| 51 | |||
| 52 | if ($asn eq "" || $bsn eq "" ) { | ||
| 53 | print STDERR "debug ScientificNotation a = $a, b = $b, asn = $asn, bsn = $bsn\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | if( isZero( $asn ) && ! isZero( $bsn ) ) | ||
| 57 | { | ||
| 58 | $cmp = -1; | ||
| 59 | } | ||
| 60 | elsif( isZero( $bsn ) && ! isZero( $asn ) ) | ||
| 61 | { | ||
| 62 | $cmp = 1; | ||
| 63 | } | ||
| 64 | elsif( $ae == $be ) | ||
| 65 | { | ||
| 66 | $cmp = $am <=> $bm; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | $cmp = $ae <=> $be; | ||
| 71 | } | ||
| 72 | |||
| 73 | return( $cmp ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub isZero | ||
| 77 | { | ||
| 78 | my( $num ) = shift; | ||
| 79 | my( $zP ); | ||
| 80 | |||
| 81 | $num = toScientificNotation( $num ); | ||
| 82 | |||
| 83 | if( $num eq $kZero ) | ||
| 84 | { | ||
| 85 | $zP = 1; | ||
| 86 | } | ||
| 87 | else | ||
| 88 | { | ||
| 89 | $zP = 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | return( $zP ); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub isOne | ||
| 96 | { | ||
| 97 | my( $num ) = shift; | ||
| 98 | my( $zP ); | ||
| 99 | |||
| 100 | dmsg( "isOne(): before = $num" ); | ||
| 101 | $num = toScientificNotation( $num ); | ||
| 102 | dmsg( "isOne(): after = $num" ); | ||
| 103 | |||
| 104 | if( $num eq $kOne ) | ||
| 105 | { | ||
| 106 | $zP = 1; | ||
| 107 | } | ||
| 108 | else | ||
| 109 | { | ||
| 110 | $zP = 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | return( $zP ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub getMantissa | ||
| 117 | { | ||
| 118 | my( $num ) = shift; | ||
| 119 | my( $man ); | ||
| 120 | |||
| 121 | $num = toScientificNotation( $num ); | ||
| 122 | $man = $num; | ||
| 123 | $man =~ s/(^[^e]*)e.*/$1/; | ||
| 124 | |||
| 125 | # jmc debugging | ||
| 126 | if ($man eq "") { $man = 0; } | ||
| 127 | |||
| 128 | return( $man ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub getExponent | ||
| 132 | { | ||
| 133 | my( $num ) = shift; | ||
| 134 | my( $exp ); | ||
| 135 | |||
| 136 | $num = toScientificNotation( $num ); | ||
| 137 | $exp = $num; | ||
| 138 | $exp =~ s/^[^e]*e(.*)/$1/; | ||
| 139 | |||
| 140 | # jmc debugging | ||
| 141 | if ($exp eq "") { $exp = 0; } | ||
| 142 | |||
| 143 | return( $exp ); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub toScientificNotation | ||
| 147 | { | ||
| 148 | my( $num ) = shift; | ||
| 149 | my( $sn ); | ||
| 150 | |||
| 151 | dmsg( "toScientificNotation( $num )" ); | ||
| 152 | |||
| 153 | $num = cleanNumber( $num ); | ||
| 154 | |||
| 155 | if( $num eq "" ) | ||
| 156 | { | ||
| 157 | $sn = $kZero; | ||
| 158 | } | ||
| 159 | elsif( $num =~ m/([^e]*)e([^e]*)/ ) | ||
| 160 | { | ||
| 161 | $sn = $num; | ||
| 162 | } | ||
| 163 | elsif( $num =~ m/(\d*)\.(\d*)/ ) | ||
| 164 | { | ||
| 165 | my( $oldPre, $newPre ); | ||
| 166 | my( $oldPost, $newPost ); | ||
| 167 | my( $lenPre ); | ||
| 168 | |||
| 169 | $oldPre = $1; | ||
| 170 | $oldPost = $2; | ||
| 171 | $lenPre = length( $oldPre ); | ||
| 172 | |||
| 173 | # first case: \d{2,}\.\d* | ||
| 174 | if( $lenPre > 1 ) | ||
| 175 | { | ||
| 176 | my( $exp ); | ||
| 177 | |||
| 178 | $newPre = substr( $oldPre, 0, 1 ); | ||
| 179 | $newPost = substr( $oldPre, 1 ); | ||
| 180 | $newPost .= $oldPost; | ||
| 181 | $exp = $lenPre-1; | ||
| 182 | $sn = "$newPre.$newPost" . 'e' . $exp; | ||
| 183 | $sn = addSignPrefix( $sn, $num ); | ||
| 184 | } | ||
| 185 | # second case: [1-9]\.\d* | ||
| 186 | elsif( $lenPre == 1 && $oldPre != 0 ) | ||
| 187 | { | ||
| 188 | $sn = "$oldPre.$oldPost" . 'e0'; | ||
| 189 | } | ||
| 190 | # last case: 0\.\d* | ||
| 191 | else | ||
| 192 | { | ||
| 193 | my( $zcount ); | ||
| 194 | my( $exp ); | ||
| 195 | |||
| 196 | if( $oldPost =~ m/^(0+)/ ) | ||
| 197 | { | ||
| 198 | $zcount = length( $1 ); | ||
| 199 | } | ||
| 200 | else | ||
| 201 | { | ||
| 202 | $zcount = 0; | ||
| 203 | } | ||
| 204 | $exp = $zcount + 1; | ||
| 205 | |||
| 206 | # we resuse ourself to set the decmial | ||
| 207 | # and then reset the bogus exponent value | ||
| 208 | # to the correct one we just computed. | ||
| 209 | dmsg( "toScientificNotation(): before $oldPost" ); | ||
| 210 | if ($oldPost) { | ||
| 211 | $sn = toScientificNotation( $oldPost ); | ||
| 212 | } else { | ||
| 213 | $sn = 0; | ||
| 214 | } | ||
| 215 | dmsg( "toScientificNotation(): after $sn" ); | ||
| 216 | |||
| 217 | $sn =~ s/e.*//; | ||
| 218 | $sn .= "e-$exp"; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | dmsg( "toScientificNotation(): $sn" ); | ||
| 223 | |||
| 224 | return( $sn ); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub cleanNumber | ||
| 228 | { | ||
| 229 | my( $str ) = shift; | ||
| 230 | |||
| 231 | dmsg( "cleanNumber( $str )" ); | ||
| 232 | |||
| 233 | if( ! numberP( $str ) ) | ||
| 234 | { | ||
| 235 | die( "cleanNumber(): can't parse $str" ); | ||
| 236 | } | ||
| 237 | |||
| 238 | # remove any leading zeros since | ||
| 239 | # they only confuse things. | ||
| 240 | $str =~ s/^0+(.*)/$1/; | ||
| 241 | |||
| 242 | dmsg( "cleanNumber(): 1 $str" ); | ||
| 243 | |||
| 244 | # but we'd like to have a single | ||
| 245 | # leading zero before the decimal | ||
| 246 | # point if all we have is a fraction. | ||
| 247 | if( $str =~ m/^\./ ) | ||
| 248 | { | ||
| 249 | $str = "0$str"; | ||
| 250 | } | ||
| 251 | |||
| 252 | dmsg( "cleanNumber(): 2 $str" ); | ||
| 253 | |||
| 254 | # remove any trailing zeros. | ||
| 255 | if( $str =~ m/(.*e\d)0+$/ ) | ||
| 256 | { | ||
| 257 | $str = $1; | ||
| 258 | } | ||
| 259 | |||
| 260 | dmsg( "cleanNumber(): 3 $str" ); | ||
| 261 | |||
| 262 | # we want to make sure there | ||
| 263 | # is a decmial in there somewhere, | ||
| 264 | # because that's what our converter | ||
| 265 | # is looking for. | ||
| 266 | if( $str !~ m/\./ ) | ||
| 267 | { | ||
| 268 | $str = "$str.0"; | ||
| 269 | } | ||
| 270 | |||
| 271 | dmsg( "cleanNumber(): 4 $str" ); | ||
| 272 | |||
| 273 | # make sure there's something | ||
| 274 | # after the decimal, too. | ||
| 275 | if( $str =~ m/\.$/ ) | ||
| 276 | { | ||
| 277 | $str .= "0"; | ||
| 278 | } | ||
| 279 | |||
| 280 | dmsg( "cleanNumber(): final output = $str" ); | ||
| 281 | |||
| 282 | return( $str ); | ||
| 283 | } | ||
| 284 | |||
| 285 | sub addSignPrefix | ||
| 286 | { | ||
| 287 | my( $str ) = shift; | ||
| 288 | my( $num ) = shift; | ||
| 289 | my( $negP ); | ||
| 290 | |||
| 291 | $negP = negativeP( $num ); | ||
| 292 | if( $negP ) | ||
| 293 | { | ||
| 294 | $str = "-$str"; | ||
| 295 | } | ||
| 296 | |||
| 297 | return( $str ); | ||
| 298 | } | ||
| 299 | |||
| 300 | sub negativeP | ||
| 301 | { | ||
| 302 | my( $num ) = shift; | ||
| 303 | my( $negP ); | ||
| 304 | |||
| 305 | if( $num >= 0 ) | ||
| 306 | { | ||
| 307 | $negP = 0; | ||
| 308 | } | ||
| 309 | else | ||
| 310 | { | ||
| 311 | $negP = 1; | ||
| 312 | } | ||
| 313 | |||
| 314 | return( $negP ); | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | sub numberP | ||
| 319 | { | ||
| 320 | my( $str ) = shift; | ||
| 321 | my( $numP ); | ||
| 322 | |||
| 323 | # if anything other than acceptable | ||
| 324 | # items are found, then it's not | ||
| 325 | # a number. | ||
| 326 | |||
| 327 | if( !defined( $str ) ) | ||
| 328 | { | ||
| 329 | confess( "undef value not allowed" ); | ||
| 330 | } | ||
| 331 | elsif( $str !~ m/[^\s]+/ ) | ||
| 332 | { | ||
| 333 | $numP = 0; | ||
| 334 | } | ||
| 335 | elsif( $str =~ m/$kNotAllowedGlyphsRegexp/ ) | ||
| 336 | { | ||
| 337 | $numP = 0; | ||
| 338 | } | ||
| 339 | else | ||
| 340 | { | ||
| 341 | $numP = 1; | ||
| 342 | } | ||
| 343 | |||
| 344 | return( $numP ); | ||
| 345 | } | ||
| 346 | |||
| 347 | 1; | ||
| 348 | |||
| 349 | __END__ |