-
Notifications
You must be signed in to change notification settings - Fork 2
/
fplot
executable file
·176 lines (152 loc) · 3.89 KB
/
fplot
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
#!/usr/bin/perl
use warnings;
use strict;
use Cwd;
use Cwd 'abs_path';
use File::Basename;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
fplot
=head1 SYNOPSIS
fplot [options] file
-o output file name
Default output file is plot-<file>.pdf
-x x header name
-y y header name
-t Title
-u plot [0,1] on both axes
-p Point Character: 0-25 (Default 1)
http://www.phaget4.org/R/plot.html
-c Colour : black, red, blue, green (Default black)
sample-call-file sample call rate file
a)<x-header-name>
b)<y-header-name>
example: fplot -x paltum -y peimekki pscalare.txt -t "paltum vs peimekki"
fplot -x paltum -y peimekki pscalare.txt -t "paltum vs peimekki" -c blue -p 4
Makes a plot of 2 variables.
=head1 DESCRIPTION
=cut
## Global variables
my $outputFileName;
my $inputFile;
my $inputDir;
my %COL_HEADERS;
my $title;
my $xLabel;
my $yLabel;
## Option variables
my $help;
my $axesAreUnity;
my $pointCharacter = 1;
my $colour = 'black';
## Main Script
# initialize options
Getopt::Long::Configure('bundling');
if(!GetOptions ('h'=>\$help,
'u'=>\$axesAreUnity,
'o=s'=>\$outputFileName,
'p=i'=>\$pointCharacter,
'c=s'=>\$colour,
't=s'=>\$title,
'x=s'=>\$xLabel,
'y=s'=>\$yLabel)
|| scalar(@ARGV)!=1
|| !defined($xLabel)
|| !defined($yLabel)
|| $pointCharacter!~/^([12]?[0-5]|1?[6-9])$/
|| $colour!~/^(black|blue|green|red)$/)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$inputFile = $ARGV[0];
if(!defined($outputFileName))
{
my($filename, $directories, $suffix) = fileparse($inputFile, '\..*');
$outputFileName = "plot-$filename.pdf";
$inputDir = abs_path($directories);
}
$outputFileName .= '.pdf' unless($outputFileName =~ /\.pdf$/i);
%COL_HEADERS = ($xLabel => -1, $yLabel => -1);
# check file correctness
open(iFILE, "< $inputFile") or die "Can't open sample call rate file - $inputFile :: $! \n";
my $header = <iFILE>;
chomp($header);
my @fields = split(/\t/, $header);
for my $i (0 .. $#fields)
{
if(exists $COL_HEADERS{$fields[$i]})
{
$COL_HEADERS{$fields[$i]} = $i;
}
}
close iFILE;
for my $key (keys %COL_HEADERS)
{
die "Can't find required header - $key - in $inputFile \n" if($COL_HEADERS{$key} == -1);
}
# Load inputfile to R to generate plot
my $currDir = cwd();
my $rTransformedXLabel = $xLabel;
$rTransformedXLabel =~ s/\W/./g;
my $rTransformedYLabel = $yLabel;
$rTransformedYLabel =~ s/\W/./g;
open(oFILE, "> $currDir/R.input") or die "Can't create temp R input file :: $! \n";
if($axesAreUnity)
{
print oFILE <<RSCRIPT;
data <- read.table("$inputDir/$inputFile", header=1 );
pdf("$currDir/$outputFileName");
plot( data\$$rTransformedXLabel,
data\$$rTransformedYLabel,
main="$title",
xlab="$xLabel",
ylab="$yLabel",
xlim=c(0,1),
ylim=c(0,1),
pch=$pointCharacter,
col="$colour");
dev.off();
q();
RSCRIPT
}
else
{
print oFILE <<RSCRIPT;
data <- read.table("$inputDir/$inputFile", header=1 );
pdf("$currDir/$outputFileName");
plot( data\$$rTransformedXLabel,
data\$$rTransformedYLabel,
main="$title",
xlab="$xLabel",
ylab="$yLabel",
pch=$pointCharacter,
col="$colour");
dev.off();
q();
RSCRIPT
}
close oFILE;
system("R --vanilla <$currDir/R.input &>$currDir/R.log") == 0 || die "Plotting failed, please check R.input and R.log";
if ($? == -1)
{
warn "failed to execute: $!\n";
}
elsif
($? & 127)
{
printf STDERR "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
}
else
{
unlink("$currDir/R.input");
unlink("$currDir/R.log");
}
__END__