-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdownsample_vcf.pl
164 lines (118 loc) · 3.4 KB
/
downsample_vcf.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
#!/usr/bin/perl -w
#
# downsample_vcf.pl -- generate certain number of loci by random.
#
# Author: Nowind
# Created: 2011-10-19
# Updated: 2014-03-11
# Version: 1.0.1
#
# Change logs:
# Version 1.0.0 14/03/05: The initial version.
# Version 1.0.1 14/03/11: Bug fixed in generate suffix of file names; add option
# "--pos-only".
use strict;
use Data::Dumper;
use Getopt::Long;
use File::Find::Rule;
use Statistics::Descriptive;
use Statistics::PointEstimation;
use Data::Random qw(:all);
use MyPerl::FileIO qw(:all);
use MyPerl::Statistics;
################### Main #################
my $CMDLINE = "perl $0 @ARGV";
my $VERSION = '1.0.1';
my $HEADER = "##$CMDLINE\n##Version: $VERSION\n";
my $SOURCE = (scalar localtime()) . " Version: $VERSION";
my %opts = ();
$opts{times} = 1;
$opts{number} = 100;
my ($out_pos_only);
GetOptions(
"input=s" => \$opts{input},
"output=s" => \$opts{prefix},
"times=i" => \$opts{times},
"size=i" => \$opts{size},
"pos-only" => \$out_pos_only,
);
unless( $opts{input} ) {
print <<EOF;
$0 -- random select SNP markers and count directions
Version: $VERSION
Usage: perl $0 [options]
Options:
-i, --input <filename>
file contains chromosome lengths
-o, --output <string>
output file prefix, default: rand[001.vcf ...]
-t, --times
random times [default: 1]
-s, --size
numbers of loci [default: 100]
-p, --pos-only
only output chromosome and positions
EOF
exit(0);
}
$|++;
print STDERR "# $0 v$VERSION\n# " . (scalar localtime()) . "\n";
unless ($opts{prefix}) {
$opts{prefix} = 'rand';
}
print STDERR ">> Start parsing $opts{input} ... ";
my $vcf_header = '';
my $sample_line = '';
my @records_all = ();
parse_vcf($opts{input});
print STDERR "\tdone!\n";
gen_random_vars($opts{times}, $opts{size});
print STDERR "# " . (scalar localtime()) . "\n";
######################### Sub #########################
sub parse_vcf
{
my ($in) = shift;
my $fh = getInputFilehandle($in);
while (<$fh>)
{
if (/\#\#/) {
$vcf_header .= $_;
}
elsif (/^\#CHROM/) {
if ($out_pos_only) {
$sample_line = "#CHROM\tPOS\n";
}
else {
$sample_line = $_;
}
}
next if (/\#/ || /^\s+$/);
if ($out_pos_only) {
my ($chrom, $pos) = (split /\s+/, $_)[0,1];
push @records_all, "$chrom\t$pos";
}
else {
chomp;
push @records_all, $_;
}
}
}
sub gen_random_vars
{
my ($rand_times, $rand_size) = @_;
my %rand_values = ();
my $i = 0;
my $tag = '0' x (length($rand_times));
while (++$i <= $rand_times)
{
print STDERR "\r>> Start random process ... duplicate $i\/$rand_times";
open (my $fh, "> $opts{prefix}.$tag.vcf") || die $!;
print {$fh} "$vcf_header";
print {$fh} "##source=$SOURCE $CMDLINE\n";
print {$fh} "$sample_line";
my @rand_vars = rand_set( set => [@records_all], size => $rand_size, shuffle => 0 );
print {$fh} (join "\n", @rand_vars);
$tag++;
}
print STDERR "\tdone!\n";
}