-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfss
executable file
·144 lines (116 loc) · 2.76 KB
/
fss
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
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
fss
=head1 SYNOPSIS
fss [options] <gt-file>
-h help
-s number of sub samples to be generated
-n size of sub sample
-p pseudo bootstrap (default is off)
-d minimum intermarker distance
gt-file gt-file
example: fss -s 100 -n 20 pscalare.gt
fss -s 1 -n 10 pscalare.gt
fss -s 1 -n 10 -p pscalare.gt
Samples from a gt-file.
Subsamples are named s<no>-<gt-file>.
Pseudo bootstrap allows subsamples to have overlap SNPs.
=head1 DESCRIPTION
=cut
#option variables
my $verbose;
my $debug;
my $help;
my $sampleNo;
my $sampleSize;
my $pseudoBootstrap;
my $gtFile;
my $outFile;
my $headerProcessed;
my %label2Column;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help,
's=i'=>\$sampleNo,
'n=i'=>\$sampleSize,
'p'=>\$pseudoBootstrap)
|| !defined($sampleNo)
|| !defined($sampleSize)
|| $sampleSize<1
|| $sampleNo<1
|| scalar(@ARGV)!=1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$gtFile = $ARGV[0];
#checks validity of genotype file
#isGt($gtFile) || die "$gtFile not a gt-file";
my $colNo;
my $colElementNo;
#counts number of columns and column elements
open(IN, $gtFile) || die "Cannot open $gtFile";
$_ = <IN>;
s/\r?\n?$//;
$colNo = s/\t/\t/g + 1;
$colElementNo = $colNo-1;
close(IN);
#checks if you can indeed sample s samples of size n from the genotype file
if((!$pseudoBootstrap && $sampleSize*$sampleNo>$colElementNo) || ($pseudoBootstrap && $sampleNo>$colElementNo))
{
die "You cannot sample $sampleNo samples of size $sampleSize from $colElementNo elements";
}
#subsamples the elements
my @SELECTED_COLUMNS;
my @COL_ELEMENTS = (1 .. $colElementNo);
for my $i (1 .. $sampleSize*$sampleNo)
{
my $selectedIndex = rand(scalar(@COL_ELEMENTS));
push (@SELECTED_COLUMNS, $COL_ELEMENTS[$selectedIndex]);
splice(@COL_ELEMENTS, $selectedIndex, 1);
#adds back all the samples
if ($pseudoBootstrap && $i%$sampleSize==0)
{
@COL_ELEMENTS = (1 .. $colElementNo);
}
}
#prints out subsamples
my @FH;
my ($name, $dir, $ext) = fileparse($gtFile, '\..*');
$outFile = "$name$ext";
for my $i (1..$sampleNo)
{
open($FH[$i-1], ">s$i-$outFile") || die "Cannot open s$i-$outFile";
}
open(IN, $gtFile) || die "Cannot open $gtFile";
while(<IN>)
{
s/\r?\n?$//;
my @fields = split('\t', $_, $colNo);
for my $i (0 .. $#FH)
{
print {$FH[$i]} "$fields[0]";
for my $j ($i*$sampleSize .. ($i+1)*$sampleSize-1)
{
print {$FH[$i]} "\t$fields[$SELECTED_COLUMNS[$j]]";
}
print {$FH[$i]} "\n";
}
}
close(IN);
for my $i (0 .. $#FH)
{
close($FH[$i]);
}