Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- no warnings 'experimental::smartmatch';
- use feature 'say';
- sub generate_function {
- my $file = shift;
- my $bed = [];
- open my $f, "< $file" or die;
- while (<$f>) {
- push @$bed, [map { $_ + 0 } (split)[1, 2, 4]];
- }
- close $f;
- return sub {
- my $v = shift;
- my $total = 0;
- for my $i (@$bed) {
- my ($start, $stop, $score) = @$i;
- my $exp = [$start..$stop];
- my $exp2 = [$v..($v+6)];
- if ($v ~~ $exp and ($v + 6) ~~ $exp) {
- $total = 6 * $score;
- last;
- } elsif ($v ~~ $exp) {
- $total = ($stop - $v) * $score;
- next;
- } elsif (($v + 6) ~~ $exp) {
- $total += ($v + 6 - $start) * $score;
- last;
- } elsif ($start ~~ $exp2 and $stop ~~ $exp2) {
- $total += ($stop - $start) * $score;
- }
- }
- return $total;
- };
- }
- my $f1 = shift || 'test.bedgraph';
- my $f2 = shift || 'test.bed';
- my $total_score = generate_function $f1;
- open my $f, "< $f2" or die;
- while (<$f>) {
- my $v = (split)[1] + 0;
- say $total_score->($v);
- }
- close $f;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement