-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.randomForest_predict.pl
More file actions
executable file
·178 lines (137 loc) · 4.55 KB
/
csv.randomForest_predict.pl
File metadata and controls
executable file
·178 lines (137 loc) · 4.55 KB
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
#!/usr/bin/perl -w
sub printUsage {
print "Predict using the specified random model\n\n";
print "Usage: ~ [-l] [-m] [-g png|jpeg|pdf|postscript] -o \"randomForest options\" <rf.model> <pred.csv> [<label>] [<modelFldx:predFldx> ... <modelFldz:predFldz>] (<aucOout.csv>|<predOut.csv>)\n";
print " -l\tThere is not label field in <pred.csv>. If so, do not specify <label> nor <predFldx> at the command line.\n";
print " -m\tTo keep intermediate files. Default is to delete\n";
print " If a field name in <pred.csv> does not match that in the model file, rename it\n";
print "Remember proximity does not work for regression\n\n";
exit(1);
}
use Util;
use Flat;
use math;
use Getopt::Std;
my $cmdLine = Util::getCmdLine();
my(%options);
getopts("lmg:o:", \%options);
if(scalar(@ARGV) < 3) {
printUsage();
}
my $model = shift @ARGV;
my $predFile = shift @ARGV;
my $outFile = pop @ARGV;
if(!(-e $model)) {
print "Model file '$model' does not exist\n";
printUsage();
}
if(!(-e $predFile)) {
print "PredFile '$predFile' does not exist\n";
printUsage();
}
my $grf = "pdf";
if(exists $options{"g"}) {
$grf = $options{"g"};
}
my($opts) = "";
if(exists $options{"o"}) {
$opts = ", ".$options{"o"};
}
if(!(exists $options{"l"})) {
if(scalar(@ARGV) < 1) {
printUsage();
}
}
# else OK
# get the field names from the model
use RF;
my(@mdlFldNames) = @{RF::getFieldNamesFromModel($model)};
my $pred = Flat->new($predFile, 1);
my $labelField = "";
if(!(exists $options{"l"})) {
$labelField = $pred->getFieldName(shift @ARGV);
}
my @predFields;
my $fldIndex;
my @renamedFlds = @ARGV; # fields to be renamed
my %model2file2;
for(my($i) = 0; $i < scalar(@renamedFlds); $i++) {
my($f1, $f2) = split(/:/, $renamedFlds[$i]);
$model2file2{$1} = $2;
}
# check to see if the field names stored in the model exist in <pred.csv>
for(my($i) = 0; $i < scalar(@mdlFldNames); $i++) {
if(exists $model2file2{$mdlFldNames[$i]}) {
$fldIndex = $pred->getFieldIndex($model2file2{$mdlFldNames[$i]});
}
else {
$fldIndex = $pred->getFieldIndex($mdlFldNames[$i]);
}
if($fldIndex == -1) {
my @predFldNames = $pred->getFieldNames();
die "Field name '$mdlFldNames[$i]' is not in '$predFile'.\nFields in the model: @mdlFldNames\nFields in '$predFile': @predFldNames\n";
}
else {
$predFields[$i] = $pred->getFieldName($fldIndex);
}
}
my($dir, $stem, $suf) = Util::getDirStemSuffix($outFile);
# extract prediction related fields
#my $allFldsFile = "$outFile.allFlds";
my @allFlds;
if($labelField eq "") { # no label specified
@allFlds = @predFields;
}
else {
@allFlds = ($labelField, @predFields);
}
my $predRE = join("|", map { "$_" } @allFlds);
# remove rows empty values ( "NA" seems to be OK with a saved RF model)
my $ready = "$outFile.ready";
my $naRE = join(" ", map { "$_ '^\\s*\$|NA'" } @allFlds);
my $naRemoved = "$outFile.NARemoved";
Util::run("rmRows.pl $predFile /dev/null $naRemoved $naRE", 0);
Util::run("extractColumns.pl $naRemoved '$predRE' $ready", 0);
my $rscript = "$stem.R";
open SCRIPT, "+>$rscript" or die "Cannot open $rscript\n";
print SCRIPT <<R0a;
library(randomForest);
library(ROCR);
load("$model");
rfPrdData<-read.table("$ready", header=TRUE, fill=T);
predicted<-predict(rf, rfPrdData $opts);
write("PREDICTED", "$outFile.pred0", append=F);
write(predicted, \"$outFile.pred0\", append=T, sep="\\n");
# proximity if exists
if(!is.null(rf\$proximity)) {
write.table(cbind(rfData, rf\$proximity), "$outFile.proximity", sep="\\t");
}
R0a
close SCRIPT;
# run R script
Util::run("R --no-save --no-restore < $rscript >& /dev/null", 1);
# concatenate "$outFile.pred" & $predFile to form a single output file
Util::rmIfExists(["$outFile.pred"]);
# get the output
if($labelField eq "") { # label field not specified, no AUC computation
Util::run("catColumns.pl -o $outFile.pred0 $ready $outFile.combined", 1);
Util::run("mv $outFile.pred0 $outFile", 1);
}
else { # compute AUC
if(scalar(Flat->new1($naRemoved)->getUniqueValues($labelField)) == 2) {
Util::run("catColumns.pl -o $outFile.pred0 $naRemoved $outFile.pred", 1);
Util::run("getAUC.pl -g $grf $outFile.pred $labelField PREDICTED $outFile", 1);
}
else {
Util::run("catColumns.pl -o $outFile.pred0 $naRemoved $outFile.combined", 1);
Util::run("mv $outFile.pred0 $outFile", 1);
}
}
if(!(exists $options{"m"})) {
if($labelField eq "") {
Util::run("rm $rscript $naRemoved $ready", 1);
}
else {
Util::rmIfExists([$rscript, "$outFile.pred0", "$outFile.pred", "$ready"]);
}
}