| 1 | #!/usr/bin/env perl | ||
| 2 | # -*-CPerl-*- | ||
| 3 | # Last changed Time-stamp: <2015-06-01 16:40:16 mtw> | ||
| 4 | # | ||
| 5 | # Compute normalized expression data in TPM or RPKM from (raw) read | ||
| 6 | # counts in multicov. | ||
| 7 | # TPM reference: Wagner et al, Theory Biosci. 131(4), pp 281-85 (2012) | ||
| 8 | # | ||
| 9 | # usage: normalize_multicov.pl -i file.multicov -r 50 -o /output/path | ||
| 10 | # | ||
| 11 | # *********************************************************************** | ||
| 12 | # * Copyright notice | ||
| 13 | # * | ||
| 14 | # * Copyright 2015 Michael T. Wolfinger <michael@wolfinger.eu> | ||
| 15 | # * All rights reserved | ||
| 16 | # * | ||
| 17 | # * This program is free software: you can redistribute it and/or modify | ||
| 18 | # * it under the terms of the GNU General Public License as published by | ||
| 19 | # * the Free Software Foundation, either version 3 of the License, or | ||
| 20 | # * (at your option) any later version. | ||
| 21 | # * | ||
| 22 | # * This program is distributed in the hope that it will be useful, | ||
| 23 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 24 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 25 | # * GNU General Public License for more details. | ||
| 26 | # * | ||
| 27 | # * You should have received a copy of the GNU General Public License | ||
| 28 | # * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 29 | # * | ||
| 30 | # * This copyright notice MUST APPEAR in all copies of the script! | ||
| 31 | # *********************************************************************** | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use warnings; | ||
| 35 | use Getopt::Long; | ||
| 36 | use Data::Dumper; | ||
| 37 | use File::Basename; | ||
| 38 | use Pod::Usage; | ||
| 39 | use Cwd; | ||
| 40 | use Path::Class; | ||
| 41 | use Bio::ViennaNGS::Expression; | ||
| 42 | |||
| 43 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 44 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 45 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 46 | |||
| 47 | my ($infile,$FC,$FC_sample,$conds,$totreads,$i,$sample,$basename); | ||
| 48 | my ($dir,$ext,$cwd); | ||
| 49 | my $readlength = 100; | ||
| 50 | my $dest = getcwd(); | ||
| 51 | my $type = "TPM"; | ||
| 52 | |||
| 53 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 54 | #^^^^^^^^^^^^^^ Main ^^^^^^^^^^^^^# | ||
| 55 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 56 | Getopt::Long::config('no_ignore_case'); | ||
| 57 | pod2usage(-verbose => 1) unless GetOptions("i=s" => \$infile, | ||
| 58 | "readlength|r=s" => \$readlength, | ||
| 59 | "out|o=s" => \$dest, | ||
| 60 | "type|t=s" => \$type, | ||
| 61 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 62 | "help|h" => sub{pod2usage(1)} | ||
| 63 | ); | ||
| 64 | unless(-f $infile){ | ||
| 65 | warn "Could not find input multicov file provided via -i option"; | ||
| 66 | pod2usage(-verbose => 0); | ||
| 67 | } | ||
| 68 | |||
| 69 | unless ($type eq "TPM" || $type eq "RPKM"){ | ||
| 70 | warn "\"$type\" is an unknown normalization type, please specify \"TPM\" or \"RPKM\""; | ||
| 71 | pod2usage(-verbose => 0); | ||
| 72 | } | ||
| 73 | |||
| 74 | $cwd = getcwd(); | ||
| 75 | unless ($infile =~ /^\// || $infile =~ /^\.\//){$infile = file($cwd,$infile);} | ||
| 76 | unless ($dest =~ /\/$/){$dest .= "/";} | ||
| 77 | unless (-d $dest){mkdir $dest;} | ||
| 78 | ($basename,$dir,$ext) = fileparse($infile,qr/\.[^.]*/); | ||
| 79 | |||
| 80 | # parse multicov file | ||
| 81 | my $exp = Bio::ViennaNGS::Expression->new(); | ||
| 82 | |||
| 83 | $exp->parse_readcounts_bed12("$infile"); # stringified Path::Class object | ||
| 84 | #print Dumper($exp->nr_features); | ||
| 85 | #print Dumper($exp->data); | ||
| 86 | |||
| 87 | # compute TPM values for all genes in each condition | ||
| 88 | for ($i=0;$i<$exp->conds;$i++){ | ||
| 89 | if($type eq "TPM"){ | ||
| 90 | $exp->computeTPM($i, $readlength); | ||
| 91 | } | ||
| 92 | else{ | ||
| 93 | $exp->computeRPKM($i); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | # write extended BED12 file for each condition past column 12 | ||
| 98 | if($type eq "TPM"){ | ||
| 99 | $exp->write_expression_bed12("TPM", $dest, $basename); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $exp->write_expression_bed12("RPKM", $dest, $basename); | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | # compute RPKM from TPM for debugging purposes | ||
| 107 | #for ($i=0;$i<$exp->conds;$i++){ | ||
| 108 | # my $avRPKM = $exp->computeRPKMfromTPM($i,$readlength); | ||
| 109 | # warn "RPKMfromTPM (sample $i): avg RPKM $avRPKM"; | ||
| 110 | #} | ||
| 111 | #$exp->write_expression_bed12("RPKMfromTPM", $dest, $basename); | ||
| 112 | |||
| 113 | __END__ | ||
| 114 | |||
| 115 | |||
| 116 | =head1 NAME | ||
| 117 | |||
| 118 | normalize_multicov.pl - Compute normalized expression data from read | ||
| 119 | counts | ||
| 120 | |||
| 121 | =head1 SYNOPSIS | ||
| 122 | |||
| 123 | normalize_multicov.pl [-i I<FILE>] [-t RPKM|TPM] [--readlength I<INT>] [options] | ||
| 124 | |||
| 125 | =head1 DESCRIPTION | ||
| 126 | |||
| 127 | This program computes normalized expression values in 'Transcript per | ||
| 128 | Million' (TPM) or 'Reads per Megabase per Million' (RPKM) from read | ||
| 129 | counts. The latter must be provided in the format produced by the | ||
| 130 | 'bedtools multicov' utility, i.e. an extended BED12 file where each | ||
| 131 | colum past the 12th lists read counts for one sample/condition. | ||
| 132 | |||
| 133 | =head1 OPTIONS | ||
| 134 | |||
| 135 | =over | ||
| 136 | |||
| 137 | =item B<-i> | ||
| 138 | |||
| 139 | Input file in 'bedtools multicov' output format, i.e. an extended BED12 | ||
| 140 | file where each colum past the 12th lists the read counts for one | ||
| 141 | sample/condition. | ||
| 142 | |||
| 143 | =item B<--readlength -r> | ||
| 144 | |||
| 145 | Read length of the RNA-seq experiment. | ||
| 146 | |||
| 147 | =item B<--out -o> | ||
| 148 | |||
| 149 | Output folder. | ||
| 150 | |||
| 151 | =item B<--type -t> | ||
| 152 | |||
| 153 | Normalization method, either 'TPM' (default) or 'RPKM'. | ||
| 154 | |||
| 155 | =item B<--help -h> | ||
| 156 | |||
| 157 | Print short help | ||
| 158 | |||
| 159 | =item B<--man> | ||
| 160 | |||
| 161 | Prints the manual page and exits | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =head1 SEE ALSO | ||
| 166 | |||
| 167 | TPM reference: Wagner et al, Theory Biosci. 131(4), pp 281-85 (2012) | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 |