Advertisement
cd62131

Calc summation score with BED

Mar 27th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.09 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental::smartmatch';
  5. use feature 'say';
  6. sub generate_function {
  7.   my $file = shift;
  8.   my $bed = [];
  9.   open my $f, "< $file" or die;
  10.   while (<$f>) {
  11.     push @$bed, [map { $_ + 0 } (split)[1, 2, 4]];
  12.   }
  13.   close $f;
  14.   return sub {
  15.     my $v = shift;
  16.     my $total = 0;
  17.     for my $i (@$bed) {
  18.       my ($start, $stop, $score) = @$i;
  19.       my $exp = [$start..$stop];
  20.       my $exp2 = [$v..($v+6)];
  21.       if ($v ~~ $exp and ($v + 6) ~~ $exp) {
  22.         $total = 6 * $score;
  23.         last;
  24.       } elsif ($v ~~ $exp) {
  25.         $total = ($stop - $v) * $score;
  26.         next;
  27.       } elsif (($v + 6) ~~ $exp) {
  28.         $total += ($v + 6 - $start) * $score;
  29.         last;
  30.       } elsif ($start ~~ $exp2 and $stop ~~ $exp2) {
  31.         $total += ($stop - $start) * $score;
  32.       }
  33.     }
  34.     return $total;
  35.   };
  36. }
  37. my $f1 = shift || 'test.bedgraph';
  38. my $f2 = shift || 'test.bed';
  39. my $total_score = generate_function $f1;
  40. open my $f, "< $f2" or die;
  41. while (<$f>) {
  42.   my $v = (split)[1] + 0;
  43.   say $total_score->($v);
  44. }
  45. close $f;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement