1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use Getopt::Long qw(:config no_ignore_case bundling);
use Cwd;
use File::Basename;
use lib ("/usr/lib/trinityrnaseq/PerlLib");
use Data::Dumper;
my $usage = <<__EOUSAGE__;
#################################################################################################
#
# Required:
#
# --matrix <string> matrix of raw read counts (not normalized!)
#
################################################################################################
__EOUSAGE__
;
my $matrix_file;
my $help_flag;
&GetOptions ( 'h' => \$help_flag,
'matrix=s' => \$matrix_file,
);
if ($help_flag) {
die $usage;
}
unless ($matrix_file) {
die $usage;
}
main: {
my $tmm_info_file = &run_TMM($matrix_file);
&write_normalized_file($matrix_file, $tmm_info_file);
exit(0);
}
####
sub run_TMM {
my ($counts_matrix_file) = @_;
my $tmm_norm_script = "$counts_matrix_file.runTMM.R";
open (my $ofh, ">$tmm_norm_script") or die "Error, cannot write to $tmm_norm_script";
print $ofh "library(edgeR)\n\n";
print $ofh "rnaseqMatrix = read.table(\"$counts_matrix_file\", header=T, row.names=1, com='', check.names=F)\n";
print $ofh "rnaseqMatrix = as.matrix(rnaseqMatrix)\n";
print $ofh "rnaseqMatrix = round(rnaseqMatrix)\n";
print $ofh "exp_study = DGEList(counts=rnaseqMatrix, group=factor(colnames(rnaseqMatrix)))\n";
print $ofh "exp_study = calcNormFactors(exp_study)\n";
print $ofh "exp_study\$samples\$eff.lib.size = exp_study\$samples\$lib.size * exp_study\$samples\$norm.factors\n";
print $ofh "write.table(exp_study\$samples, file=\"$counts_matrix_file.TMM_info.txt\", quote=F, sep=\"\\t\", row.names=F)\n";
close $ofh;
&process_cmd("R --no-save --no-restore --no-site-file --no-init-file -q < $tmm_norm_script 1>&2 ");
my $tmm_matrix = "$counts_matrix_file.TMM_info.txt";
unless (-s $tmm_matrix) {
confess "Error, TMM matrix $tmm_matrix was not generated. Be sure edgeR is installed and see additional error messages above for other helpful info";
}
return($tmm_matrix);
}
####
sub process_cmd {
my ($cmd) = @_;
print STDERR "CMD: $cmd\n";
my $ret = system($cmd);
if ($ret) {
die "Error, cmd: $cmd died with ret ($ret) ";
}
return;
}
####
sub write_normalized_file {
my ($matrix_file, $tmm_info_file) = @_;
my %col_to_eff_lib_size;
my %col_to_norm_factor;
open (my $fh, $tmm_info_file) or die "Error, cannot open file $tmm_info_file";
my %bloom_to_col;
my $header = <$fh>;
while (<$fh>) {
chomp;
my @x = split(/\t/);
my ($col, $norm_factor, $eff_lib_size) = ($x[0], $x[2], $x[3]);
$col =~ s/\"//g;
my $bloom = $col;
$bloom =~ s/\W/$;/g;
$col_to_eff_lib_size{$col} = $eff_lib_size;
$col_to_norm_factor{$col} = $norm_factor;
if ($bloom ne $col) {
if (exists $bloom_to_col{$bloom}) {
die "Error, already stored $bloom_to_col{$bloom} for $bloom, but trying to also store $col here... Ensure column names are unique according to non-word characters.";
}
$col_to_eff_lib_size{$bloom} = $eff_lib_size;
$col_to_norm_factor{$bloom} = $norm_factor;
}
}
close $fh;
open ($fh, $matrix_file);
$header = <$fh>;
chomp $header;
my @pos_to_col = split(/\t/, $header);
my $check_column_ordering_flag = 0;
while (<$fh>) {
chomp;
my @x = split(/\t/);
unless ($check_column_ordering_flag) {
if (scalar(@x) == scalar(@pos_to_col) + 1) {
## header is offset, as is acceptable by R
## not acceptable here. fix it:
unshift (@pos_to_col, "");
}
$check_column_ordering_flag = 1;
print join("\t", @pos_to_col) . "\n";
}
my $gene = $x[0];
print $gene;
for (my $i = 1; $i <= $#x; $i++) {
my $col = $pos_to_col[$i];
$col =~ s/\"//g;
my $adj_col = $col;
$adj_col =~ s/-/\./g;
my $bloom = $col;
$bloom =~ s/\W/$;/g;
my $eff_lib_size = $col_to_eff_lib_size{$col}
|| $col_to_eff_lib_size{$bloom}
|| $col_to_eff_lib_size{$adj_col}
|| $col_to_eff_lib_size{"X$col"}
|| die "Error, no eff lib size for [$col] or [$bloom] or [$adj_col] or [\"X$col\"]" . Dumper(\%col_to_eff_lib_size);
my $norm_factor = $col_to_norm_factor{$col}
|| $col_to_norm_factor{$bloom}
|| $col_to_norm_factor{$adj_col}
|| $col_to_norm_factor{"X$col"}
|| die "Error, no normalization scaling factor for $col" . Dumper(\%col_to_norm_factor);
my $read_count = $x[$i];
my $converted_val = sprintf("%.3f", $read_count * 1/$norm_factor);
print "\t$converted_val";
}
print "\n";
}
return;
}
|