| 1 | #!/usr/bin/env perl | ||
| 2 | # -*-CPerl-*- | ||
| 3 | # Last changed Time-stamp: <2016-10-11 15:55:03 mtw> | ||
| 4 | # | ||
| 5 | # Find peaks/enriched regions of certain size in RNA-seq data | ||
| 6 | # | ||
| 7 | # usage: rnaseq_peakfinder --bgpos pos.bg --bgneg neg.pg | ||
| 8 | # | ||
| 9 | # *********************************************************************** | ||
| 10 | # * Copyright notice | ||
| 11 | # * | ||
| 12 | # * Copyright 2015 Michael T. Wolfinger <michael@wolfinger.eu> | ||
| 13 | # * All rights reserved | ||
| 14 | # * | ||
| 15 | # * This program is free software: you can redistribute it and/or modify | ||
| 16 | # * it under the terms of the GNU General Public License as published by | ||
| 17 | # * the Free Software Foundation, either version 3 of the License, or | ||
| 18 | # * (at your option) any later version. | ||
| 19 | # * | ||
| 20 | # * This program is distributed in the hope that it will be useful, | ||
| 21 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | # * GNU General Public License for more details. | ||
| 24 | # * | ||
| 25 | # * You should have received a copy of the GNU General Public License | ||
| 26 | # * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 27 | # * | ||
| 28 | # * This copyright notice MUST APPEAR in all copies of the script! | ||
| 29 | # *********************************************************************** | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 34 | use Pod::Usage; | ||
| 35 | use Data::Dumper; | ||
| 36 | use Cwd; | ||
| 37 | use Path::Class; | ||
| 38 | use Bio::ViennaNGS::FeatureIO; | ||
| 39 | use Bio::ViennaNGS::Peak; | ||
| 40 | use Bio::ViennaNGS::Util qw(mkdircheck); | ||
| 41 | |||
| 42 | |||
| 43 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 44 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 45 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 46 | my ($infile1, $infile2, $lf); | ||
| 47 | my $logname = "rnaseq_peakfinder.log.txt"; | ||
| 48 | my $fn_rawpeaks = "rawpeaks.bed"; | ||
| 49 | my $fn_candidatepeaks = "candidatepeaks.bed"; | ||
| 50 | my $winsize = 15; # sliding window size | ||
| 51 | my $wininterval = 1; # windows sliding interval | ||
| 52 | my $mincov = 100; # minimum coverage | ||
| 53 | my $maxlen = 100; # maximum peak length | ||
| 54 | my $threshold = 0.1; # cut off threhsold for peak end detection | ||
| 55 | my $debug = 0; # debug switch | ||
| 56 | my $dest = getcwd(); | ||
| 57 | |||
| 58 | |||
| 59 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 60 | #^^^^^^^^^^^^^^ Main ^^^^^^^^^^^^^# | ||
| 61 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 62 | Getopt::Long::config('no_ignore_case'); | ||
| 63 | pod2usage(-verbose => 1) unless GetOptions("bgpos=s" => \$infile1, | ||
| 64 | "bgneg=s" => \$infile2, | ||
| 65 | "w|winsize=i" => \$winsize, | ||
| 66 | "i|interval=i" => \$wininterval, | ||
| 67 | "m|mincov=i" => \$mincov, | ||
| 68 | "l|length=i" => \$maxlen, | ||
| 69 | "t|threshold=f" => \$threshold, | ||
| 70 | "out|o=s" => \$dest, | ||
| 71 | "d|debug" => sub{$debug=1}, | ||
| 72 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 73 | "help|h" => sub{pod2usage(1)} | ||
| 74 | ); | ||
| 75 | |||
| 76 | unless(-f $infile1){ | ||
| 77 | warn "Could not find input bedGraph file for [+] strand provided via ---bgpos option"; | ||
| 78 | pod2usage(-verbose => 0); | ||
| 79 | } | ||
| 80 | unless(-f $infile2){ | ||
| 81 | warn "Could not find input bedGraph file for [-] strand provided via ---bgneg option"; | ||
| 82 | pod2usage(-verbose => 0); | ||
| 83 | } | ||
| 84 | my $cwd = getcwd(); | ||
| 85 | unless ($infile1 =~ /^\// || $infile1 =~ /^\.\//){$infile1 = file($cwd,$infile1);} | ||
| 86 | unless ($infile2 =~ /^\// || $infile2 =~ /^\.\//){$infile2 = file($cwd,$infile2);} | ||
| 87 | unless (-d $dest){mkdircheck($dest);} | ||
| 88 | |||
| 89 | $lf = file($dest,$logname); | ||
| 90 | |||
| 91 | # parse input files | ||
| 92 | my $io_pos = Bio::ViennaNGS::FeatureIO->new(file => "$infile1", # [+] strand | ||
| 93 | filetype => "BedGraph", | ||
| 94 | instanceOf => "BedGraphEntry", | ||
| 95 | ); | ||
| 96 | |||
| 97 | my $io_neg = Bio::ViennaNGS::FeatureIO->new(file => "$infile2", # [-] strand | ||
| 98 | filetype => "BedGraph", | ||
| 99 | instanceOf => "BedGraphEntry", | ||
| 100 | ); | ||
| 101 | |||
| 102 | # get an instance of Bio::ViennaNGS::Peak | ||
| 103 | my $peaks = Bio::ViennaNGS::Peak->new(winsize => $winsize, | ||
| 104 | interval => $wininterval, | ||
| 105 | mincov => $mincov, | ||
| 106 | length => $maxlen, | ||
| 107 | threshold => $threshold, | ||
| 108 | ); | ||
| 109 | |||
| 110 | # parse BedGraph coverage data from FeatureIO into the Peak object | ||
| 111 | $peaks->populate_data($io_pos, $io_neg); | ||
| 112 | |||
| 113 | # identify covered regions | ||
| 114 | $peaks->raw_peaks($dest, "rnaseq_peakfinder", $lf); | ||
| 115 | |||
| 116 | # characterize peaks | ||
| 117 | $peaks->final_peaks($dest, "rnaseq_peakfinder", $lf); | ||
| 118 | |||
| 119 | |||
| 120 | __END__ |