annotate SNP_density/CalculateSlidingWindowsSNPdensitiesFromVCF.pl @ 14:d15869b3731a draft

Deleted selected files
author dereeper
date Tue, 08 Jan 2019 08:46:57 -0500
parents 98c37a5d67f4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
1 #!/usr/bin/perl
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
2
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
3 use strict;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
4 use Getopt::Long;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
5 use Bio::SeqIO;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
6
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
7 my $usage = qq~Usage:$0 <args> [<opts>]
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
8 where <args> are:
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
9 -i, --input <VCF/Hapmap input>
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
10 -o, --out <output in tabular format>
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
11 -s, --step <step (in bp)>
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
12 ~;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
13 $usage .= "\n";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
14
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
15 my ($input,$out,$step);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
16
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
17 GetOptions(
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
18 "input=s" => \$input,
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
19 "out=s" => \$out,
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
20 "step=s" => \$step,
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
21 );
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
22
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
23
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
24 die $usage
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
25 if ( !$input || !$step || !$out );
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
26
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
27 my $max_chr_num = 100;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
28
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
29 my %counts;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
30 my %counts_by_ind;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
31
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
32 my $VCFTOOLS_EXE = "vcftools";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
33
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
34 my $is_vcf = `head -4000 $input | grep -c CHROM`;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
35 my $is_bcf = 0;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
36 if ($input =~/\.bcf/){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
37 $is_bcf = 1;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
38 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
39
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
40
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
41 my $IN;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
42 my $headers;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
43 my $start_indiv_retrieval = 12;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
44 my $chrom_retrieval = 2;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
45 my $pos_retrieval = 3;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
46 if ($is_vcf or $is_bcf){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
47 $start_indiv_retrieval = 9;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
48 $chrom_retrieval = 0;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
49 $pos_retrieval = 1;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
50 if ($is_vcf){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
51 $headers = `grep '#CHROM' $input`;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
52 open($IN,$input);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
53 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
54 elsif ($is_bcf){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
55 my $cmd = "$VCFTOOLS_EXE --bcf $input --stdout --recode | head -4000 | grep CHROM";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
56 $headers = `$cmd`;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
57 my $cmd2 = "$VCFTOOLS_EXE --bcf $input --stdout --recode";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
58 open $IN, '-|' , "$cmd2" or die "Can not run Vcftools";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
59 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
60 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
61 else{
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
62 $headers= <$IN>;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
63 open($IN,$input);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
64 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
65 $headers=~s/\n//g;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
66 $headers=~s/\r//g;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
67 my @ind_names = split(/\t/,$headers);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
68 my @individual_names;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
69 for (my $i = $start_indiv_retrieval; $i <= $#ind_names; $i++)
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
70 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
71 push(@individual_names,$ind_names[$i]);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
72 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
73 my %maximums;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
74 while(<$IN>)
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
75 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
76 my $line = $_;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
77 $line=~s/\n//g;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
78 $line=~s/\r//g;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
79 my @infos = split(/\t/,$line);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
80 if (scalar @infos > 8 && !/#CHROM/){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
81 my $chrom = $infos[$chrom_retrieval];
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
82 my $position = $infos[$pos_retrieval];
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
83 if ($position > $maximums{$chrom}){$maximums{$chrom}=$position;}
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
84 my $classe_position = int($position/$step);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
85 $counts{$chrom}{$classe_position}++;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
86
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
87 my $ref_allele = $infos[11];
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
88 if ($is_vcf or $is_bcf){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
89 $ref_allele = "0/0";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
90 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
91 for (my $i = $start_indiv_retrieval; $i <= $#infos; $i++){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
92 if (!$counts_by_ind{$chrom}{$classe_position}{$i}){$counts_by_ind{$chrom}{$classe_position}{$i} = 0;}
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
93 if ($infos[$i] ne $ref_allele){
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
94 $counts_by_ind{$chrom}{$classe_position}{$i}++;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
95 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
96 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
97 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
98 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
99 close($IN);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
100
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
101 #######################################################
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
102 # global
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
103 #######################################################
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
104 open(my $OUT,">$out");
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
105 print $OUT "Chromosome Position SNPs\n";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
106 my $chr_num = 0;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
107 foreach my $chrom(sort keys(%counts))
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
108 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
109 $chr_num++;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
110 my $ref_counts = $counts{$chrom};
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
111 my %final_counts = %$ref_counts;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
112 my $x = 0;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
113 #foreach my $classe_position(sort {$a<=>$b} keys(%final_counts))
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
114 for (my $classe_position = 0; $classe_position <= $maximums{$chrom}/$step;$classe_position++)
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
115 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
116 my $nb = 0;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
117 if ($counts{$chrom}{$classe_position})
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
118 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
119 $nb = $counts{$chrom}{$classe_position};
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
120 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
121 $x += $step;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
122 print $OUT "$chrom $x $nb\n";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
123 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
124 if ($chr_num >= $max_chr_num){last;}
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
125 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
126 close($OUT);
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
127
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
128 #######################################################
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
129 # For each individual
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
130 #######################################################
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
131 open(my $OUT2,">$out.by_sample");
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
132 $chr_num = 0;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
133 print $OUT2 "Chromosome ".join("\t",@individual_names) . "\n";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
134 foreach my $chrom(sort keys(%counts_by_ind))
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
135 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
136 $chr_num++;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
137 my $ref_counts = $counts_by_ind{$chrom};
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
138 my %final_counts = %$ref_counts;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
139 for (my $classe_position = 0; $classe_position <= $maximums{$chrom}/$step;$classe_position++)
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
140 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
141 print $OUT2 "$chrom";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
142 my $num_ind = $start_indiv_retrieval;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
143 foreach my $indiv(@individual_names)
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
144 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
145 my $val = 0;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
146
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
147 if ($counts_by_ind{$chrom}{$classe_position}{$num_ind})
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
148 {
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
149 $val = $counts_by_ind{$chrom}{$classe_position}{$num_ind};
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
150 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
151 print $OUT2 " $val";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
152 $num_ind++;
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
153 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
154 print $OUT2 "\n";
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
155 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
156 if ($chr_num >= $max_chr_num){last;}
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
157 }
98c37a5d67f4 Uploaded
dereeper
parents:
diff changeset
158 close($OUT2);