-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnormalize.pl
executable file
·175 lines (150 loc) · 3.81 KB
/
normalize.pl
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
#!/bin/env perl
$|=1;
use Getopt::Long;
use lib ".";
use Normalizer;
$verbose = 0;
$base = 2;
$infile = "sample.data.gz";
$format = "simple";
$code = "";
$method = "net";
%opts = (
'verbose' => \$verbose,
'base=i' => \$base,
'infile=s' => \$infile,
'format=s' => \$format,
'help' => \$help,
'code=s' => \$code,
'outfile=s' => \$outfile,
'method=s' => \$method,
'guide=s' => \$guideGenes,
);
%defaults = (
'verbose' => $verbose,
'base' => $base,
'infile' => $infile,
'format' => $format,
'help' => $help,
'code' => $code,
'outfile' => $outfile,
'method' => $method,
);
GetOptions(%opts);
if ($help || !$base || !$infile) {
usage();
exit;
}
$logbase = log($base);
unless ($outfile) {
($outfile) = $infile =~ /([^\.]+)/;
$outfile .= ".normalized";
}
$norm = new Normalizer;
$norm->set_param("tmp", ".");
$norm->set_param("verbose", 1) if $verbose;
$norm->set_param("persistent_id", $code);
$norm->set_param("method", $method);
$norm->set_param("fraction", 1);
print "Reading $infile ...\n" if $verbose;
if (-e $infile) {
if ($infile =~ /\.gz$/) {
open DATA, "gunzip -c $infile |";
} else {
open DATA, $infile;
}
$_ = <DATA>;
chomp;
@samples = split /\t/; #first line names the samples
if ($format eq "simple") {
shift @samples; #...except for the first column
} else {
#...except for the first two columns
foreach my $i (0..1) {
shift @samples;
}
}
print scalar @samples, " samples\n" if $verbose;
while (<DATA>) {
my(@values, $transcript, $gene, $class, $length);
chomp;
if ($format eq "simple") {
($transcript, @values) = split /\t/;
} else {
($gene, $length, @values) = split /\t/;
($gene, $transcript) = split /:/, $gene;
$transcript = "$gene:$transcript";
$gene{$transcript} = $gene;
$class{$gene} = $class;
$length{$transcript} = $length;
}
foreach my $i (0..$#samples) {
if ((my $v = $values[$i]) > 0) {
$counts{$transcript}{$samples[$i]} = $v;
$nonzero++;
}
}
}
close DATA;
print "nonzero: $nonzero\n" if $verbose;
} else {
die "I don't know $infile\n";
}
$norm->set_data(\%counts, \%length);
$sampleCount = $norm->param("sample_count");
print "samples: $sampleCount, genes: ", scalar keys %counts, ", nonzero values: $nonzero\n" if $verbose;
if ($method ne 'quantile' && $method ne "none") {
if ($method eq 'guide' || $method eq 'genorm') {
open GUIDES, $guideGenes;
while (<GUIDES>) {
chomp;
my($gene, $weight) = split /\t/;
$weight{$gene} = ($weight || 1);
}
close GUIDES;
die "Method '$method' requires a file of guide gene weights, specified via the 'guide' parameter.\n" unless %weight;
$norm->set_gene_weights(%weight);
} elsif ($method eq "total") {
$norm->set_param("total_size_for_normalizing", 0);
} elsif ($method eq "cpm") {
$norm->set_param("total_size_for_normalizing", 1e6);
} else {
$ubiquitous = $norm->datarestore("ubiquitous_genes") || $norm->identify_ubiquitous_genes();
foreach my $gene (@{$ubiquitous}) {
$isUbiquitous{$gene} = 1;
}
}
$fact = $norm->compute_scaling_factors();
print "\nComputed scaling factors per sample:\n" if $verbose;
foreach my $sample (@samples) {
print join("\t", $sample, sprintf("%.5f", $fact->{$sample})), "\n";
}
}
$normalized = $norm->normalize();
if ($outfile) {
print "Saving $outfile ...\n" if $verbose;
open OUTF, ">$outfile";
print OUTF join("\t", "genes\\samples", @samples), "\n";
while (my($gene) = each %counts) {
$_ = $gene;
s/\t/;/g;
print OUTF $_;
foreach my $sample (@samples) {
print OUTF "\t";
if (my $v = $normalized->{$gene}{$sample}) {
print OUTF sprintf("%.3f", $v);
}
}
print OUTF "\n";
}
close OUTF;
}
#######
sub usage {
print "Usage: normalize.pl [options]\n";
print "\tOption\tDefault\n";
print "\t------\t-------\n";
foreach my $opt (keys %defaults) {
print join("\t", "", $opt, $defaults{$opt}), "\n";
}
}