-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupedSurveys.pl
234 lines (199 loc) · 5.66 KB
/
groupedSurveys.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/perl
# calculate the number of false positives and negatives between two surveys.
use warnings;
use strict;
use IO::File;
use List::Util;
use utf8;
my $file1 = $ARGV[0] || die "Please pass an input file name to process $ARGV[1]\n";
my $outfile = $ARGV[1] || die "Please pass an output file name to process\n";
my $replicates = $ARGV[2] || die "Please pass the number of replicates\n";
my $numberToCombine = $ARGV[3] || die "Please pass the number of surveys to combine\n";
print "usage groupedSurveys surveysFile outputFileName #replicates #surveys\n";
my %results = ();
my @cutline;
my @surveys2;
my $numberOfSurveys;
my @random_set=();
my $totalNumberOfSpeciesFound = 0;
my $totalNumberOfFalsePositivesFound = 0;
my $totalNumberOfTruePositivesFound = 0;
my $totalOfTwoSurveys = 0; #combining two surveys just for the purpose of true positives
my $totalOfTwoSurveysRP = 0;
my $totalOfTwoSurveysFP = 0;
my $SumOfFalsePositivesFound = 0;
my $SumOfTruePositivesFound = 0;
my $rp = 0;
my $fp = 0;
my @results;
my @resultsFP; #False positives
my @resultsRP; #Real positives
my $count = 0;
my %seen;
my %h;
open(my $fhIn1, "<", $file1)
or die "cannot open < input1.txt: $!";
open(my $fhOut, ">", $outfile)
or die "cannot open > output.txt: $!";
my @surveys = <$fhIn1>;
#copy the list of surveys
@surveys2 = @surveys;
$numberOfSurveys = @surveys;
my @output = ("0") x 290;
my @outputFP = ("0") x 290;
my @outputRP = ("0") x 290;
for (my $i=0; $i <= $replicates-1; $i++){ #loop over the number of replicates
@random_set = unique_random_list(1,$numberOfSurveys,$numberToCombine);
for (my $k=0; $k <= $numberToCombine-1; $k++) { #loop over the chosen surveys
@cutline = split /\t/,$surveys[$random_set[$k]];
for (my $m=0; $m <= $#cutline; $m++) { #loop over each species
if($cutline[$m] =~ /^\d+$/){
#print "=$m - $cutline[$m],";
if($cutline[$m] != 0 && $cutline[$m] != 1 && $cutline[$m] != 2){
#this is just here to check the validity of the input files. The values should be 0, 1 or 2.
print $fhOut "shouldnt be here $cutline[$m]\n,";
}
if($cutline[$m] > 0){
$output[$m]= $output[$m] + 1;
}
else{
$output[$m] = $output[$m] + 0; #this just avoids ending up with some null values, otherwise it does nothing
}
if($cutline[$m] == 1){
$outputRP[$m]= $outputRP[$m] + 1;
}
else{
$outputRP[$m]= $outputRP[$m] + 0;
}
if($cutline[$m] == 2){
$outputFP[$m]= $outputFP[$m] + 1;
}
else{
$outputFP[$m]= $outputFP[$m] + 0;
}
}
else{
$output[$m]= $output[$m] . "," . $cutline[$m]; #if it is not a number it is a label, so stick it to the front of the string
}
}
} #end loop over the chosen surveys
#Count up the total number of species left by combining species
foreach (@output)
{
if($_ =~ /^\d+$/){
if($_ == $numberToCombine){
$totalNumberOfSpeciesFound++;
}
}
}
foreach (@output)
{
if($_ =~ /^\d+$/){
if($_ >= 2){
$totalOfTwoSurveys++;
}
}
}
#Count up the total number of real species in the combined surveys
foreach (@outputRP)
{
if($_ =~ /^\d+$/){
if($_ == $numberToCombine){
$totalNumberOfTruePositivesFound++;
}
}
}
foreach (@outputRP)
{
if($_ =~ /^\d+$/){
if($_ >= 2){
$totalOfTwoSurveysRP++;
}
}
}
foreach $rp (@outputRP)
{
if($rp =~ /^\d+$/){
if($rp >= 1){
$SumOfTruePositivesFound++;
}
}
}
#Count up the total number of false positive species in the combined surveys
foreach (@outputFP)
{
if($_ =~ /^\d+$/){
if($_ == $numberToCombine){
$totalNumberOfFalsePositivesFound++;
}
}
}
foreach (@outputFP)
{
if($_ =~ /^\d+$/){
if($_ >= 2){
$totalOfTwoSurveysFP++;
}
}
}
foreach $fp(@outputFP)
{
if($fp =~ /^\d+$/){
if($fp >= 1){
$SumOfFalsePositivesFound++;
}
}
}
@output = (0) x 290;
@outputRP = (0) x 290;
@outputFP = (0) x 290;
@random_set=();
push @results, "$totalNumberOfSpeciesFound,$totalNumberOfTruePositivesFound,$totalOfTwoSurveys, $totalNumberOfFalsePositivesFound, $totalOfTwoSurveysFP, $totalOfTwoSurveysRP, $SumOfTruePositivesFound, $SumOfFalsePositivesFound";
$totalNumberOfSpeciesFound = 0;
$totalNumberOfTruePositivesFound = 0;
$totalNumberOfFalsePositivesFound = 0;
$totalOfTwoSurveys = 0;
$totalOfTwoSurveysFP = 0;
$totalOfTwoSurveysRP = 0;
$SumOfFalsePositivesFound = 0;
$SumOfTruePositivesFound = 0;
}
foreach (@results)
{
$count++;
print $fhOut "\nTotal number of species found $count, $_";
}
# foreach (@resultsRP)
# {
# print $fhOut "\nTotal number of real species found: $_";
# }
# foreach (@resultsFP)
# {
# print $fhOut "\nTotal number of false positives found: $_";
# }
sub unique_random_list {
#Taken from happy.barney on Jul 28, 2011 at 10:52 UTC at http://www.perlmonks.org/
my ($from, $to, $count) = @_;
$count = List::Util::min ($count, $to - $from);
my @result;
my @queue = [ $from, $to - $from, $count ];
while (my $job = shift @queue) {
my ($from, $length, $count) = @$job;
if ($count == $length) {
push @result, $from .. $from + $length;
} elsif ($count == 1) {
push @result, $from + int rand $length;
} else {
my $split_length = int ($length / 2);
my $split_count = List::Util::min (int rand $count, $split_length);
my $pad_length = $length - $split_length;
my $pad_count = $count - $split_count;
$split_count += $pad_count - $pad_length
if $pad_count > $pad_length;
unshift @queue, grep $_->[2],
[ $from, $split_length, $split_count ],
[ $from + $split_length, $length - $split_length, $count - $split_count ];
}
}
@result;
}