| 1 |
#! /usr/bin/perl |
| 2 |
# |
| 3 |
# Given a list file with 3-tuples for each series to plot |
| 4 |
# create a pdf of a ROC curve. |
| 5 |
# |
| 6 |
# Format of list file: |
| 7 |
# <series_name> <root> <color> |
| 8 |
# |
| 9 |
# <root>/<root>.em<key>.xy, <root>/<root>.em<key>.mer and <root>/<root>.time must exist. |
| 10 |
# |
| 11 |
# <key> is the second command line argument. |
| 12 |
# |
| 13 |
# Usage: perl rmark-Rroc.pl <listfile> <key: E-value multiplier (often 1)> <name for pdf> <1/0 yes/no draw error-bars> <plot title> |
| 14 |
# Example usage: |
| 15 |
# > perl rmark-Rroc.pl -R rmark-2.list 1 rmark-2.pdf 0 RMARK3_results |
| 16 |
use Getopt::Std; |
| 17 |
getopts('RPLM:'); |
| 18 |
if (defined $opt_R) { $replace_underscores = 1; } |
| 19 |
if (defined $opt_P) { $paper_mode = 1; } |
| 20 |
if (defined $opt_L) { $lty_mode = 1; } |
| 21 |
if (defined $opt_M) { $perMb_mode = 1; $numMb = $opt_M; } |
| 22 |
|
| 23 |
my $usage = "Usage: perl rmark-Rroc.pl [OPTIONS] <listfile> <key (e.g. 1E04)> <pdfname> <1/0 yes/no draw error-bars> <plot title>\n"; |
| 24 |
$usage .= "\nFormat of list file:\n\t<series_name> <root> <color/linetype>\n\n"; |
| 25 |
$usage .= "\nExample:\n\tinf1p02-df r2-i1p02 green\n\n"; |
| 26 |
$usage .= "<root>/<root>.em$key.xy, <root>/<root>.em$key.mer and <root>/<root>.time must exist\n\n"; |
| 27 |
$usage .= "Options:\n\t"; |
| 28 |
$usage .= "-R : replace underscores in <list file> with spaces\n\t"; |
| 29 |
$usage .= "-P : run in 'paper' mode, create a smaller PDF for a Bioinformatics Applications Note\n\t"; |
| 30 |
$usage .= "-L : list file includes line types instead of colors\n\t"; |
| 31 |
$usage .= "-M <f> : modify X-axis to FPs/query/Mb for a database of <f> Mb\n\n"; |
| 32 |
|
| 33 |
if(scalar(@ARGV) != 5) { printf("$usage"); exit(1); } |
| 34 |
($listfile, $key, $pdf, $do_errorbars, $main) = @ARGV; |
| 35 |
$n = 0; |
| 36 |
|
| 37 |
if($do_errorbars && $lty_mode) { die "ERROR -L is incompatible with error bars"; } |
| 38 |
|
| 39 |
if($replace_underscores) { $main =~ s/\_/ /g; } |
| 40 |
|
| 41 |
@R = (); |
| 42 |
|
| 43 |
$xlabel = "errors per query"; |
| 44 |
$ylabel = "fractional coverage of positives"; |
| 45 |
|
| 46 |
#push(@R, "xlimit<-c(0.001,20)\n"); |
| 47 |
if($perMb_mode) { |
| 48 |
push(@R, "xlimit<-c(0.001,1.0)\n"); |
| 49 |
} |
| 50 |
push(@R, "ylimit<-c(0,1)\n"); |
| 51 |
if($paper_mode) { |
| 52 |
push(@R, "postscript(\"$pdf\", height=5.5, width=9.3)\n"); |
| 53 |
} |
| 54 |
else { |
| 55 |
push(@R, "pdf(\"$pdf\", height=8.5, width=11)\n"); |
| 56 |
} |
| 57 |
open(LIST, $listfile) || die "ERROR, could not open $listfile"; |
| 58 |
@nametimemerA = (); |
| 59 |
while(<LIST>) { |
| 60 |
chomp $_; |
| 61 |
if(m/\S+/) { |
| 62 |
@fields = split(' ', $_, 3); |
| 63 |
if(scalar(@fields) != 3) { die "ERROR, format of $listfile invalid"; } |
| 64 |
($name, $root, $color_or_lty) = @fields; |
| 65 |
$n++; |
| 66 |
@xA = (); |
| 67 |
@yA = (); |
| 68 |
@dy1A = (); |
| 69 |
@dy2A = (); |
| 70 |
$xyfile = $root . ".em" . $key . ".xy"; |
| 71 |
$merfile = $root . ".em" . $key . ".mer"; |
| 72 |
$sumfile = $root . ".sum"; |
| 73 |
$timefile = $root . ".time"; |
| 74 |
$sumfile = $root . ".em" . $key . ".sum"; |
| 75 |
if(! -e $xyfile) { |
| 76 |
$xyfile = $root . "/" . $xyfile; |
| 77 |
if(! -e $xyfile) { |
| 78 |
die "ERROR, $xyfile does not exist"; |
| 79 |
} |
| 80 |
} |
| 81 |
if(! -e $merfile) { |
| 82 |
$merfile = $root . "/" . $merfile; |
| 83 |
if(! -e $merfile) { |
| 84 |
die "ERROR, $merfile does not exist"; |
| 85 |
} |
| 86 |
} |
| 87 |
if(! -e $sumfile) { |
| 88 |
$sumfile = $root . "/" . $sumfile; |
| 89 |
if(! -e $sumfile) { |
| 90 |
die "ERROR, $sumfile does not exist"; |
| 91 |
} |
| 92 |
} |
| 93 |
if(! -e $timefile) { |
| 94 |
$timefile = $root . "/" . $timefile; |
| 95 |
if(! -e $timefile) { |
| 96 |
die "ERROR, $timefile does not exist"; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
# process time file |
| 101 |
open(TIME, $timefile) || die "ERROR, could not open time file $timefile"; |
| 102 |
while($line = <TIME>) { |
| 103 |
chomp $line; |
| 104 |
if($line =~ s/^total\s+//) { |
| 105 |
$line =~ s/\s+.+$//; |
| 106 |
$time = $line; |
| 107 |
} |
| 108 |
} |
| 109 |
close(TIME); |
| 110 |
|
| 111 |
# process mer file |
| 112 |
open(MER, $merfile) || die "ERROR, could not open mer file $merfile"; |
| 113 |
while($mer = <MER>) { |
| 114 |
if($mer =~ s/^\s*\*summary\*\s+//) { |
| 115 |
$mer =~ s/\s+$//; |
| 116 |
#$legstring = sprintf("\"%-25s %-10s MER: %4d\"", $name, $time . "h", $mer); |
| 117 |
#push(@nametimemerA, $legstring); |
| 118 |
#push(@nametimemerA, "\"" . $name . " " . $time . "h MER: " . $mer . "\""); |
| 119 |
} |
| 120 |
} |
| 121 |
close(MER); |
| 122 |
|
| 123 |
# process sum file |
| 124 |
open(SUM, $sumfile) || die "ERROR, could not open sum file $sumfile"; |
| 125 |
$sum = <SUM>; |
| 126 |
chomp $sum; |
| 127 |
$summer = $sum; |
| 128 |
($sumname, $trash1, $trash2, $summer, $sumfn, $sumfp, $summerthr, $trash3, $trash4, $trash5, $time) = split(/\s+/, $sum); |
| 129 |
$legstring = sprintf("\"%-25s %-10s MER: %5.1f\"", $name, $time, $summer); |
| 130 |
push(@nametimemerA, $legstring); |
| 131 |
close(SUM); |
| 132 |
|
| 133 |
if($lty_mode) { |
| 134 |
push(@ltyA, "$color_or_lty"); |
| 135 |
$col_or_lty_str = "lty=$color_or_lty"; |
| 136 |
} |
| 137 |
else { |
| 138 |
push(@colorA, "\"$color_or_lty\""); |
| 139 |
$col_or_lty_str = "col=\"$color_or_lty\""; |
| 140 |
} |
| 141 |
open(XY, $xyfile) || die "ERROR, could not open xy file $root.xy"; |
| 142 |
while(<XY>) { |
| 143 |
if(/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { |
| 144 |
($x, $y, $dy1, $dy2) = ($1, $2, $3, $4); |
| 145 |
if($perMb_mode) { $x /= $numMb; } |
| 146 |
push(@xA, $x); |
| 147 |
push(@yA, $y); |
| 148 |
push(@dy1A, $y + $dy1); |
| 149 |
push(@dy2A, $y - $dy2); |
| 150 |
$have_errorbars = 1; |
| 151 |
} |
| 152 |
elsif(/(\S+)\s+(\S+)/) { |
| 153 |
if($perMb_mode) { $x /= $numMb; } |
| 154 |
($x, $y) = ($1, $2, $3, $4); |
| 155 |
push(@xA, $x); |
| 156 |
push(@yA, $y); |
| 157 |
if($do_errorbars) { die "ERROR, confidence intervals not read, but you want error bars"; } |
| 158 |
$have_errorbars = 0; |
| 159 |
} |
| 160 |
} |
| 161 |
push(@R, "x" . $n . "<-c" . return_vec_line(\@xA) . "\n"); |
| 162 |
push(@R, "y" . $n . "<-c" . return_vec_line(\@yA) . "\n"); |
| 163 |
if($have_errorbars) { |
| 164 |
push(@R, "dy1" . $n . "<-c" . return_vec_line(\@dy1A) . "\n"); |
| 165 |
push(@R, "dy2" . $n . "<-c" . return_vec_line(\@dy2A) . "\n"); |
| 166 |
} |
| 167 |
if($n == 1) { |
| 168 |
push(@R, "plot(x$n, y$n, type=\"l\", lwd=2, log=\"x\", ylim=ylimit, $col_or_lty_str, main=\"$main\", xlab=\"$xlabel\", ylab=\"$ylabel\")\n"); |
| 169 |
if($paper_mode) { |
| 170 |
push(@R, "points(x$n, y$n, type=\"p\", $col_or_lty_str)\n"); |
| 171 |
} |
| 172 |
} |
| 173 |
else { |
| 174 |
push(@R, "points(x$n, y$n, type=\"l\", lwd=2, $col_or_lty_str)\n"); |
| 175 |
if($paper_mode) { |
| 176 |
push(@R, "points(x$n, y$n, type=\"p\", $col_or_lty_str)\n"); |
| 177 |
} |
| 178 |
} |
| 179 |
if($do_errorbars) { |
| 180 |
push(@R, "points(x$n, dy1$n, type=\"l\", lty=2, lwd=0.4, $col_or_lty_str)\n"); |
| 181 |
push(@R, "points(x$n, dy2$n, type=\"l\", lty=2, lwd=0.4, $col_or_lty_str)\n"); |
| 182 |
} |
| 183 |
if($paper_mode) { |
| 184 |
push(@R, "axis(2, labels=FALSE, at=c(0.2,0.4,0.6,0.8,1.0))\n"); |
| 185 |
push(@R, "axis(4, labels=FALSE, at=c(0.2,0.4,0.6,0.8,1.0))\n"); |
| 186 |
} |
| 187 |
else { |
| 188 |
push(@R, "axis(2, labels=FALSE, at=c(0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.50,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95,1.0))\n"); |
| 189 |
push(@R, "axis(4, labels=FALSE, at=c(0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.50,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95,1.0))\n"); |
| 190 |
} |
| 191 |
close(XY); |
| 192 |
} |
| 193 |
} |
| 194 |
close(LIST); |
| 195 |
|
| 196 |
if($lty_mode) { |
| 197 |
if($perMb_mode) { |
| 198 |
push(@R, "legend(0.05, 0.25, c" . return_vec_line(\@nametimemerA) . ", cex=0.8, lty=c" . return_vec_line(\@ltyA) . ")\n"); |
| 199 |
} |
| 200 |
else { |
| 201 |
push(@R, "legend(0.5, 0.25, c" . return_vec_line(\@nametimemerA) . ", cex=0.8, lty=c" . return_vec_line(\@ltyA) . ")\n"); |
| 202 |
} |
| 203 |
} |
| 204 |
else { |
| 205 |
push(@R, "legend(0.5, 0.25, c" . return_vec_line(\@nametimemerA) . ", lty=1, cex=0.8, col=c" . return_vec_line(\@colorA) . ", text.col=c" . return_vec_line(\@colorA) . ")\n"); |
| 206 |
} |
| 207 |
|
| 208 |
push(@R, "dev.off()\n"); |
| 209 |
|
| 210 |
$Rinput = join("", @R); |
| 211 |
open(OUT, ">rmark-Rroc.tmp") || die "ERROR, couldn't open rmark-Rroc.tmp for writing"; |
| 212 |
print OUT $Rinput; |
| 213 |
close(OUT); |
| 214 |
|
| 215 |
$status = system("cat rmark-Rroc.tmp | R --vanilla"); |
| 216 |
if ($status != 0) { die "FAILED: cat rmark-Rroc.tmp | R --vanilla"; } |
| 217 |
#unlink "rmark-Rroc.tmp"; |
| 218 |
|
| 219 |
sub return_vec_line |
| 220 |
{ |
| 221 |
($arr_ref) = $_[0]; |
| 222 |
$return_line = "("; |
| 223 |
for($i = 0; $i < (scalar(@{$arr_ref})-1); $i++) { $return_line .= $arr_ref->[$i] . ", "; } |
| 224 |
$return_line .= $arr_ref->[(scalar(@{$arr_ref}-1))] . ")"; |
| 225 |
return $return_line; |
| 226 |
} |