-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsa2hscplot
executable file
·155 lines (127 loc) · 4.01 KB
/
sa2hscplot
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
#!/usr/bin/perl
use warnings;
use strict;
use warnings;
use Cwd;
use Cwd 'abs_path';
use File::Basename;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
sa2hscplot
=head1 SYNOPSIS
sa2hscplot [options] <sample-call-file>
-o output file name
Default output file is <sample-call-file>-hscplot.pdf
-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)sample-id
b)A_A
c)A_B
d)B_B
e)sample-call-rate
example: sa2hscplot samples.txt
Saves a plot of sample heterozygosity vs sample call rate in pdf format.
=head1 DESCRIPTION
Saves a plot of sample heterozygosity vs sample call rate in pdf format
=cut
## Global variables
my $outputFileName = '';
my $inputFile = '';
my $inputDir = '';
my %COL_HEADERS = (
'sample-id' => -1,
'A_A' => -1,
'A_B' => -1,
'B_B' => -1,
'sample-call-rate' => -1);
## 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)
|| scalar(@ARGV)!=1
|| $pointCharacter!~/^([12]?[0-5]|1?[6-9])$/
|| $colour!~/^(black|blue|green|red)$/) {
if ($help) {
pod2usage(-verbose => 2);
}
else {
pod2usage(1);
}
}
$inputFile = $ARGV[0];
if($outputFileName eq '') {
my($filename, $directories, $suffix) = fileparse($inputFile, '\..*');
$outputFileName = $filename . '-hscplot.pdf';
$inputDir = abs_path($directories);
}
$outputFileName .= '.pdf' unless($outputFileName =~ /\.pdf$/i);
# 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);
foreach my $i (0 .. $#fields) {
if(exists $COL_HEADERS{$fields[$i]}) {
$COL_HEADERS{$fields[$i]} = $i;
}
}
close iFILE;
foreach 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();
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 );
samples <- transform( data, miss.rate=1-data\$sample.call.rate, hetero.rate=data\$A_B/(data\$A_A+data\$A_B+data\$B_B) );
pdf(\"$currDir/$outputFileName\");
plot( samples\$sample.call.rate,
samples\$hetero.rate,
main="Sample Heterozygosity vs Sample Call Rate",
xlab="Sample Call Rate",
ylab="Sample Heterozygosity Rate",
xlim=c(0,1),
ylim=c(0,1),
pch=$pointCharacter,
col=\"$colour\");
dev.off();
q();
RSCRIPT
}
else
{
print oFILE "data <- read.table(\"$inputDir/$inputFile\", header=1 ); \n";
print oFILE 'samples <- transform( data, miss.rate=1-data$sample.call.rate, hetero.rate=data$A_B/(data$A_A+data$A_B+data$B_B) );', "\n";
print oFILE "pdf(\"$currDir/$outputFileName\"); \n";
print oFILE 'plot( samples$sample.call.rate, samples$hetero.rate, main="Sample Heterozygosity vs Sample Call Rate", xlab="Sample Call Rate", ylab="Sample Heterozygosity Rate",', " pch=$pointCharacter, col=\"$colour\");\n";
print oFILE "dev.off(); \n";
print oFILE "q(); \n";
}
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__