annotate VCFToolFilter/VCFToolsFilter.pl @ 12:a03f54c420f1 draft

Uploaded
author dereeper
date Fri, 20 Feb 2015 11:16:59 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
1
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
2 #!/usr/bin/perl
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
3
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
4 use strict;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
5 use Switch;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
6 use Getopt::Long;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
7 use Bio::SeqIO;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
8
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
9 my $usage = qq~Usage:$0 <args> [<opts>]
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
10
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
11 where <args> are:
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
12
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
13 -i, --input <VCF input>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
14 -o, --out <Output basename>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
15
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
16 <opts> are:
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
17
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
18 -s, --samples <Samples to be analyzed. Comma separated list>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
19 -c, --chromosomes <Chromosomes to be analyzed. Comma separated list>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
20 -e, --export <Output format (VCF/freq/plink. Default: VCF>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
21 -f, --frequency <Minimum MAF. Default: 0.001>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
22 -m, --max_freq <Maximum MAF. Default: 0.5>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
23 -a, --allow_missing <Allowed missing data proportion per site. Must be comprised between 0 and 1. Default: 0>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
24 -n, --nb_alleles <Accepted number of alleles (min,max). Default: 2,4>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
25 -t, --type <Type of polymorphisms to keep (ALL/SNP/INDEL). Default: ALL>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
26 -b, --bounds <Lower bound and upper bound for a range of sites to be processed (start,end). Default: 1, 100000000>
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
27 ~;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
28 $usage .= "\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
29
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
30 my ($input,$out);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
31
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
32
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
33 #my $indel_size_max = 500;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
34 #my $indel_size_min = 1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
35 my $frequency_max = 0.5;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
36 my $frequency_min = 0.001;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
37 my $pos_max = 100000000000;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
38 my $pos_min = 0;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
39 my $filter_snp_type = "all";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
40
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
41 my $missing_data = 0;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
42 my $export = "VCF";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
43 my $type = "ALL";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
44 my $nb_alleles;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
45 my $bounds;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
46 my $samples;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
47 my $chromosomes;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
48
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
49 GetOptions(
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
50 "input=s" => \$input,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
51 "out=s" => \$out,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
52 "samples=s" => \$samples,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
53 "chromosomes=s" => \$chromosomes,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
54 "frequency=s" => \$frequency_min,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
55 "max_freq=s" => \$frequency_max,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
56 "allow_missing=s"=> \$missing_data,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
57 "export=s" => \$export,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
58 "type=s" => \$type,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
59 "nb_alleles=s" => \$nb_alleles,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
60 "bounds=s" => \$bounds,
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
61 );
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
62
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
63
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
64 die $usage
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
65 if ( !$input || !$out);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
66
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
67 if ($samples && $samples =~/^([\w\,]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
68 $samples = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
69 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
70 elsif ($samples){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
71 die "Error: Samples must be a comma separated list of string\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
72 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
73 if ($chromosomes && $chromosomes =~/^([\w\,]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
74 $chromosomes = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
75 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
76 elsif($chromosomes){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
77 die "Error: Chromosomes must be a comma separated list of string\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
78 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
79 if ($bounds && $bounds =~/^([\d\,]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
80 $bounds = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
81 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
82 elsif($bounds){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
83 die "Error: Bounds must be a comma separated list of integers\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
84 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
85
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
86 if ($frequency_min && $frequency_min =~/^([\d\.]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
87 $frequency_min = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
88 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
89 elsif ($frequency_min){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
90 die "Error: frequency must be an integer\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
91 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
92 if ($frequency_max && $frequency_max =~/^([\d\.]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
93 $frequency_max = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
94 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
95 elsif($frequency_max){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
96 die "Error: frequency must be an integer\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
97 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
98 if ($missing_data && $missing_data =~/^([\d\.]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
99 $missing_data = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
100 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
101 elsif ($missing_data){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
102 die "Error: Missing data must be an integer\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
103 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
104 if ($nb_alleles && $nb_alleles =~/^([\d\.\,]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
105 $nb_alleles = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
106 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
107 elsif($nb_alleles){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
108 die "Error: Nb alleles must be two integers\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
109 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
110 if ($export && $export =~/^([\w]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
111 $export = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
112 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
113 elsif($export){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
114 die "Error: Export must be a string\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
115 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
116 if ($type && $type =~/^([\w]+)\s*$/){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
117 $type = $1;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
118 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
119 elsif($type){
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
120 die "Error: Type must be a string\n";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
121 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
122
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
123
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
124 my @dnasamples;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
125 if ($samples)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
126 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
127 @dnasamples = split(",",$samples);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
128 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
129 my @nalleles;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
130 if ($nb_alleles)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
131 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
132 @nalleles = split(",",$nb_alleles);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
133 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
134 my @boundaries;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
135 if ($bounds)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
136 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
137 @boundaries = split(",",$bounds);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
138 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
139 my @chromosomes_list;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
140 if ($chromosomes)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
141 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
142 @chromosomes_list = split(",",$chromosomes);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
143 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
144
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
145
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
146 my $experiment = "chromosomes";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
147 my $table = "";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
148 my %genes;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
149 my @snp_ids;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
150 my @snp_ids_and_positions;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
151 my @snp_ids_and_positions_all;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
152 my $gene;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
153 my $snp_num = 0;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
154 my %ref_sequences;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
155 my %snps_of_gene;
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
156
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
157
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
158
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
159
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
160 my $indiv_cmd = "";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
161 if (@dnasamples)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
162 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
163 $indiv_cmd = "--indv " . join(" --indv ",@dnasamples);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
164 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
165
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
166 my $chrom_cmd = "";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
167 if (@chromosomes_list)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
168 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
169 $chrom_cmd = "--chr " . join(" --chr ",@chromosomes_list);
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
170 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
171
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
172 my $export_cmd = "--recode";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
173 if ($export eq "freq")
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
174 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
175 $export_cmd = "--freq";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
176 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
177 if ($export eq "plink")
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
178 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
179 $export_cmd = "--plink";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
180 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
181
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
182
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
183
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
184 my $nb_alleles_cmd = "--min-alleles 1 --max-alleles 4";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
185 if (@nalleles)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
186 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
187 $nb_alleles_cmd = "--min-alleles $nalleles[0] --max-alleles $nalleles[1]";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
188 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
189 my $bounds_cmd = "--from-bp 1 --to-bp 100000000";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
190 if (@boundaries)
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
191 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
192 $bounds_cmd = "--from-bp $boundaries[0] --to-bp $boundaries[1]";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
193 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
194
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
195
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
196 my $type_cmd = "";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
197 if ($type eq "INDEL")
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
198 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
199 $type_cmd = "--keep-only-indels";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
200 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
201 if ($type eq "SNP")
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
202 {
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
203 $type_cmd = "--remove-indels";
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
204 }
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
205
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
206
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
207 system("vcftools --vcf $input --out $out --keep-INFO-all --remove-filtered-all $type_cmd $export_cmd $chrom_cmd $indiv_cmd $nb_alleles_cmd --maf $frequency_min --max-maf $frequency_max --max-missing $missing_data");
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
208
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
209
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
210
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
211
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
212
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
213
a03f54c420f1 Uploaded
dereeper
parents:
diff changeset
214