| 1 | #!/usr/bin/perl -w | ||
| 2 | use BIE::App::PacBio; | ||
| 3 | use PDL; | ||
| 4 | use PDL::Graphics::PLplot; | ||
| 5 | use v5.10; | ||
| 6 | |||
| 7 | die "Usage: $0 PacBio_bas.h5\n" | ||
| 8 | unless -e $ARGV[0]; | ||
| 9 | |||
| 10 | # Define constants for the paths. | ||
| 11 | use constant PG => 'PulseData/'; | ||
| 12 | use constant BG => PG . 'BaseCalls/'; | ||
| 13 | use constant BZG => BG . 'ZMW/'; | ||
| 14 | use constant BZMG => BG . 'ZMWMetrics/'; | ||
| 15 | use constant CBG => PG . 'ConsensusBaseCalls/'; | ||
| 16 | use constant CBZG => CBG . 'ZMW/'; | ||
| 17 | use constant CBPG => CBG . 'Passes/'; | ||
| 18 | # Define a function to create hash for datasets. | ||
| 19 | # It accepts a string array to return a path hash. | ||
| 20 | our %Bases = map {$_ => BG . $_} qw( | ||
| 21 | Basecall | ||
| 22 | DeletionQV | ||
| 23 | DeletionTag | ||
| 24 | InsertionQV | ||
| 25 | PreBaseFrames | ||
| 26 | PulseIndex | ||
| 27 | QualityValue | ||
| 28 | SubstitutionQV | ||
| 29 | SubstitutionTag | ||
| 30 | WidthInFrames | ||
| 31 | ); | ||
| 32 | |||
| 33 | our %ZMWs = map {$_ => BZG . $_} qw( | ||
| 34 | HoleNumber | ||
| 35 | HoleStatus | ||
| 36 | HoleXY | ||
| 37 | NumEvent | ||
| 38 | ); | ||
| 39 | |||
| 40 | our %ZMWMetrics = map {$_ => BZMG . $_} qw( | ||
| 41 | BaseFraction | ||
| 42 | BaseIpd | ||
| 43 | BaseRate | ||
| 44 | BaseWidth | ||
| 45 | CmBasQv | ||
| 46 | CmDelQv | ||
| 47 | CmInsQv | ||
| 48 | CmSubQv | ||
| 49 | DarkBaseRate | ||
| 50 | HQRegionEndTime | ||
| 51 | HQRegionSNR | ||
| 52 | HQRegionStartTime | ||
| 53 | LocalBaseRate | ||
| 54 | Productivity | ||
| 55 | ReadScore | ||
| 56 | RmBasQv | ||
| 57 | RmDelQv | ||
| 58 | RmInsQv | ||
| 59 | RmSubQv | ||
| 60 | ); | ||
| 61 | |||
| 62 | our %CCSBases = map {$_ => CBG . $_} qw( | ||
| 63 | Basecall | ||
| 64 | DeletionQV | ||
| 65 | DeletionTag | ||
| 66 | InsertionQV | ||
| 67 | QualityValue | ||
| 68 | SubstitutionQV | ||
| 69 | SubstitutionTag | ||
| 70 | ); | ||
| 71 | |||
| 72 | our %CCSPasses = map {$_ => CBPG . $_} qw( | ||
| 73 | AdapterHitAfter | ||
| 74 | AdapterHitBefore | ||
| 75 | NumPasses | ||
| 76 | PassDirection | ||
| 77 | PassNumBases | ||
| 78 | PassStartBase | ||
| 79 | ); | ||
| 80 | |||
| 81 | our %CCSZMWs = map {$_ => CBZG . $_} qw( | ||
| 82 | HoleNumber | ||
| 83 | HoleStatus | ||
| 84 | HoleXY | ||
| 85 | NumEvent | ||
| 86 | ); | ||
| 87 | |||
| 88 | my $pb = BIE::App::PacBio->new(file => $ARGV[0]); | ||
| 89 | (my $output = $ARGV[0]) =~ s/\..+$//; | ||
| 90 | |||
| 91 | ################################################## | ||
| 92 | #kinds of QC | ||
| 93 | ################################################## | ||
| 94 | &ccsQC(); | ||
| 95 | #&clrQC(); | ||
| 96 | |||
| 97 | sub clrQC { | ||
| 98 | $pb->lens($pb->read($ZMWs{NumEvent})); | ||
| 99 | my $prefix = 'CLR'; | ||
| 100 | my $qv = &preSplit($Bases{QualityValue}); | ||
| 101 | my $meanQV = &preStat(\&getAve, $qv); | ||
| 102 | &plotQVbyPos($prefix, $qv); | ||
| 103 | |||
| 104 | ################################################## | ||
| 105 | #plot quality value correlation | ||
| 106 | for (qw(DeletionQV InsertionQV SubstitutionQV)) { | ||
| 107 | (my $tag = $_) =~ s/QV//; | ||
| 108 | &plotQV($prefix.$tag, $Bases{$_}, $meanQV); | ||
| 109 | } | ||
| 110 | #plot base tags --- bargraph | ||
| 111 | for (qw(Basecall DeletionTag SubstitutionTag)) { | ||
| 112 | &plotBarGraph($prefix.$_, $Bases{$_}, {A=>65,C=>67,G=>71,T=>84}); | ||
| 113 | } | ||
| 114 | #plot length distribution | ||
| 115 | &plotLenDist($prefix, $pb); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub ccsQC { | ||
| 119 | open my $outFH, '>', $output . '.CCSQC'; | ||
| 120 | my @master; | ||
| 121 | $pb->lens($pb->read($CCSZMWs{NumEvent})); | ||
| 122 | my $prefix = 'CCS'; | ||
| 123 | my $qv = &preSplit($CCSBases{QualityValue}); | ||
| 124 | my $meanQV = &preStat(\&getAve, $qv); | ||
| 125 | push @master, $meanQV, $pb->lens, &plotQVLenCycle($pb, $meanQV); | ||
| 126 | &plotQVbyPos($prefix, $qv); | ||
| 127 | |||
| 128 | ################################################## | ||
| 129 | #plot quality value correlation | ||
| 130 | for (qw(DeletionQV InsertionQV SubstitutionQV)) { | ||
| 131 | (my $tag = $_) =~ s/QV//; | ||
| 132 | push @master, &plotQV($prefix.$tag, $CCSBases{$_}, $meanQV); | ||
| 133 | } | ||
| 134 | #plot base tags --- bargraph | ||
| 135 | for (qw(Basecall DeletionTag SubstitutionTag)) { | ||
| 136 | &plotBarGraph($prefix.$_, $CCSBases{$_}, {A=>65,C=>67,G=>71,T=>84}); | ||
| 137 | } | ||
| 138 | |||
| 139 | #plot length distribution | ||
| 140 | &plotLenDist($prefix, $pb); | ||
| 141 | |||
| 142 | for (qw(PassNumBases PassStartBase)) { | ||
| 143 | &plotHist($prefix.$_, $CCSPasses{$_}); | ||
| 144 | } | ||
| 145 | |||
| 146 | &plotBarGraph('CCSPassDirection', $CCSPasses{PassDirection}, | ||
| 147 | { | ||
| 148 | Pos=>1, Neg=>0}); | ||
| 149 | |||
| 150 | say $outFH join "\t", qw(idx avgQV Length Cycles avgDelQV avgInsQV avgSubQV); | ||
| 151 | for (0..$#{$master[0]}) { | ||
| 152 | my $idx = $_; | ||
| 153 | say $outFH join "\t", $idx, map {$_->[$idx]} @master; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | ################################################## | ||
| 158 | #functions for QC | ||
| 159 | ################################################## | ||
| 160 | |||
| 161 | sub plotQVbyPos { | ||
| 162 | my ($tag, $qv) = @_; | ||
| 163 | my $vmeanQV = &preStat(\&getVave, $qv); | ||
| 164 | &pbview( | ||
| 165 | 'qv'.$tag.'ByPos', | ||
| 166 | [[1..($#$vmeanQV+1)], | ||
| 167 | $vmeanQV], | ||
| 168 | 'QV by Position on Read', | ||
| 169 | 'Read Position', | ||
| 170 | 'Mean QV' | ||
| 171 | ); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub plotQVLenCycle { | ||
| 175 | my ($pb, $meanQV) = @_; | ||
| 176 | my $pass = &preSel($CCSPasses{NumPasses}); | ||
| 177 | &pbview( | ||
| 178 | 'qvLenCycle', | ||
| 179 | [$pb->lens, | ||
| 180 | $meanQV, | ||
| 181 | $pass], | ||
| 182 | 'QV by Length and Cycle Number', | ||
| 183 | 'Read Length', | ||
| 184 | 'Mean QV by Read', | ||
| 185 | 'Cycles', | ||
| 186 | ); | ||
| 187 | say "Quality vs. Length colored by Cycle Number: Done"; | ||
| 188 | return $pass; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub plotBarGraph { | ||
| 192 | # $lbs is a hash ref of label info. | ||
| 193 | my ($tag, $path, $lbs) = @_; | ||
| 194 | my $data = $pb->read($path); | ||
| 195 | &pbview( | ||
| 196 | 'qv'.$tag.'Bar', | ||
| 197 | [], | ||
| 198 | # [$data, {A=>65,C=>67,G=>71,T=>84}], | ||
| 199 | [$data, $lbs], | ||
| 200 | $tag . ' Bar', | ||
| 201 | $tag, | ||
| 202 | ); | ||
| 203 | say $tag . " Distribution: Done"; | ||
| 204 | return $data; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub plotLenDist { | ||
| 208 | my ($tag, $pb) = @_; | ||
| 209 | &pbview( | ||
| 210 | 'qv'.$tag.'LenDist', | ||
| 211 | [$pb->lens], | ||
| 212 | 100, #bin number | ||
| 213 | 'Distribution of Read Length', | ||
| 214 | 'Read Length' | ||
| 215 | ); | ||
| 216 | say $tag . ' Read Length Distribution: Done'; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub plotHist { | ||
| 220 | my ($tag, $path) = @_; | ||
| 221 | my $d = $pb->read($path); | ||
| 222 | &pbview( | ||
| 223 | 'qv'. $tag, | ||
| 224 | [$d], | ||
| 225 | 100, #bin number | ||
| 226 | 'Distribution of '.$tag, | ||
| 227 | $tag, | ||
| 228 | ); | ||
| 229 | say $tag . " Distribution: Done"; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub plotQV { | ||
| 233 | my ($tag, $path, $meanQV) = @_; | ||
| 234 | my $d = &preSplit($path); | ||
| 235 | my $meanD = &preStat(\&getAve, $d); | ||
| 236 | &pbview( | ||
| 237 | 'qv'.$tag.'Corr', | ||
| 238 | [$meanQV, | ||
| 239 | $meanD | ||
| 240 | ], | ||
| 241 | 'Correlation between QV and '.$tag.'QV', | ||
| 242 | 'Quality Value by Read', | ||
| 243 | $tag . ' Quality by Read', | ||
| 244 | ); | ||
| 245 | say 'Correlation between ' . $tag . 'QV and quality value: Done'; | ||
| 246 | return $meanD; | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | ################################################## | ||
| 251 | #functions for plotting # | ||
| 252 | ################################################## | ||
| 253 | |||
| 254 | #functions to preprocess data | ||
| 255 | #disgard nan data | ||
| 256 | sub preSel { | ||
| 257 | my $data = $pb->read(shift); | ||
| 258 | my $hit = [@{$data}[@{$pb->hitIdx}]]; | ||
| 259 | } | ||
| 260 | #split by read | ||
| 261 | sub preSplit { | ||
| 262 | my $data = $pb->split(shift); | ||
| 263 | } | ||
| 264 | sub preStat { | ||
| 265 | my $func = shift; | ||
| 266 | my $data = shift; | ||
| 267 | &{$func}($data); | ||
| 268 | } | ||
| 269 | #params: ArrRef of data | ||
| 270 | # plot dimensions: 2d or 3d? | ||
| 271 | sub pbview { | ||
| 272 | my $chart = PDL::Graphics::PLplot->new( | ||
| 273 | DEV => 'png', | ||
| 274 | FILE => shift . '.png', | ||
| 275 | ); | ||
| 276 | my ($xyz, @plotParams) = @_; | ||
| 277 | |||
| 278 | &{'pbplot'.($#$xyz+1).'d'}($chart, @$xyz, @plotParams); | ||
| 279 | $chart->close; | ||
| 280 | } | ||
| 281 | #one param: ArrRef[ArrRef] | ||
| 282 | sub getAve { | ||
| 283 | $PDL::undefval=-999; | ||
| 284 | my $data = pdl shift; | ||
| 285 | $data->inplace->setvaltobad($PDL::undefval); | ||
| 286 | return [ | ||
| 287 | list $data->average | ||
| 288 | ]; | ||
| 289 | } | ||
| 290 | sub getVave { | ||
| 291 | $PDL::undefval=-999; | ||
| 292 | my $data = pdl shift; | ||
| 293 | $data->inplace->setvaltobad($PDL::undefval); | ||
| 294 | return [ | ||
| 295 | list $data->transpose->average | ||
| 296 | ]; | ||
| 297 | } | ||
| 298 | |||
| 299 | #need params: title, x label, and x data. | ||
| 300 | sub pbplot0d { | ||
| 301 | my $chart = shift; | ||
| 302 | my ($data, $title, $xlab) = @_; | ||
| 303 | my $d = pdl $data->[0]; | ||
| 304 | my $lbl = $data->[1]; | ||
| 305 | my @labels = sort keys %$lbl; | ||
| 306 | my @vals; | ||
| 307 | for (0..$#labels) { | ||
| 308 | $vals[$_] = $d->where($d==$lbl->{$labels[$_]})->nelem; | ||
| 309 | } | ||
| 310 | my $v = pdl @vals; | ||
| 311 | $chart->bargraph( | ||
| 312 | \@labels, | ||
| 313 | $v, | ||
| 314 | XLAB => $xlab, | ||
| 315 | TITLE => $title, | ||
| 316 | BOX => [0, scalar(@labels), 0, $v->max * 1.1], | ||
| 317 | COLOR => 'BLUE', | ||
| 318 | ); | ||
| 319 | } | ||
| 320 | #need params: title, x label, and x data. | ||
| 321 | sub pbplot1d { | ||
| 322 | my $chart = shift; | ||
| 323 | my $x = pdl $_[0]; | ||
| 324 | my (undef, $binNumber, $title, $xlab) = @_; | ||
| 325 | my ($hx, $hy) = hist($x, $x->minmax, ($x->max - $x->min)/$binNumber); | ||
| 326 | $chart->histogram( | ||
| 327 | $x,$binNumber, | ||
| 328 | XLAB => $xlab, | ||
| 329 | TITLE => $title, | ||
| 330 | BOX => [$x->minmax, 0, $hy->max * 1.1], | ||
| 331 | ); | ||
| 332 | } | ||
| 333 | #need params: title, x label, ylabel and x y data. | ||
| 334 | sub pbplot2d { | ||
| 335 | my $chart = shift; | ||
| 336 | my ($x, $y) = map {pdl $_} @_[0,1]; | ||
| 337 | my (undef, undef, $title, $xlab, $ylab) = @_; | ||
| 338 | $chart->xyplot( | ||
| 339 | $x, $y, | ||
| 340 | PLOTTYPE=>'POINTS', | ||
| 341 | XLAB => $xlab, | ||
| 342 | YLAB => $ylab, | ||
| 343 | TITLE => $title, | ||
| 344 | ); | ||
| 345 | } | ||
| 346 | #need params: title, x label, y label, z label and x y z data. | ||
| 347 | #z would be denoted by color key. | ||
| 348 | sub pbplot3d { | ||
| 349 | my $chart = shift; | ||
| 350 | my ($x, $y, $z) = map {pdl $_} @_[0..2]; | ||
| 351 | my (undef, undef, undef, $title, $xlab, $ylab, $zlab) = @_; | ||
| 352 | $chart->xyplot( | ||
| 353 | $x, $y, | ||
| 354 | PLOTTYPE=>'POINTS', | ||
| 355 | XLAB => $xlab, | ||
| 356 | YLAB => $ylab, | ||
| 357 | TITLE => $title, | ||
| 358 | PALETTE => 'REVERSERAINBOW', | ||
| 359 | COLORMAP => $z, | ||
| 360 | ); | ||
| 361 | $chart->setparm(CHARSIZE=>0.5); | ||
| 362 | $chart->colorkey($z, 'v', | ||
| 363 | VIEWPORT => [0.93, 0.96, 0.13, 0.85], | ||
| 364 | TITLE => '', | ||
| 365 | XLAB => $zlab, | ||
| 366 | YLAB => '', | ||
| 367 | ); | ||
| 368 | } |