annotate DC_Genotyper.pl @ 3:61b6d523acd9 draft

syntax error in perl script
author geert-vandeweyer
date Thu, 25 Sep 2014 05:36:54 -0400
parents e8a32d824f39
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
1 #!/usr/bin/perl
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
2 #############################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
3 ## DEEP COVERAGE GENOTYPER ##
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
4 #############################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
5 # version : 0.0.1
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
6 # Principle:
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
7 # 1. get allele counts on all positions in specified targets (bed) using igvtools. Only SNPs !!
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
8 # 2. remove known dbsnp positions (bcf file)
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
9 # 3. Get distribution of background noise (pcr/sequencing errors), by modelling allele fractions as normal distributions.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
10 # 4. Based on these distributions, check each position for significant change from the reference allele (based on allele fraction)
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
11 # 5. For abberant positions, check each alternate allele to see if it passes the background signal.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
12 # 6. Generate VCF file.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
13
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
14
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
15 ##################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
16 ## LOAD MODULES ##
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
17 ##################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
18 use threads;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
19 use threads::shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
20 use Thread::Queue;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
21 use Getopt::Std;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
22
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
23 ####################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
24 ## get paramaters ##
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
25 ####################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
26 # t: target file
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
27 # b: bam file
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
28 # R: 2bit version of reference fasta.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
29 # p: number of threads.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
30 # s: dbsnp file
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
31 # m: minimal coverage (defaults 400x)
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
32 # P: ploidy
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
33 # a: outfile for allele distributions
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
34 # v: vcf file output.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
35 getopts('t:b:R:p:s:m:P:v:a:', \%opts) ;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
36
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
37 ## variables
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
38 my $twobit :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
39 my $igvgenome :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
40 if (!defined($opts{'R'})) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
41 die("Reference Genomes not specified\n");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
42 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
43 my @refgenomes = split(",",$opts{'r'});
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
44 if (!-e $refgenomes[0]) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
45 die("'$refgenomes[0]' is not a valid file path.");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
46 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
47 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
48 $twobit = $refgenomes[0];
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
49 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
50 if (!-e $refgenomes[1]) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
51 die("'$refgenomes[1]' is not a valid file path.");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
52 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
53 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
54 $igvgenome = $refgenomes[1];
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
55 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
56
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
57
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
58 my $mincov :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
59 $mincov = 320;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
60 if (defined($opts{'m'})) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
61 $mincov = $opts{'m'};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
62 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
63
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
64 my $ploidy :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
65 if (defined($opts{'P'}) && $opts{'P'} =~ m/^\d+$/) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
66 $ploidy = $opts{'P'};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
67 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
68 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
69 die("Ploidy (-P) was not specified or not an integer\n");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
70 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
71
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
72
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
73 if (defined($opts{'v'})) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
74 $outfile = $opts{'v'};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
75 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
76 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
77 die("No output vcf-file specified.\n");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
78 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
79 if (!defined($opts{'a'})) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
80 die("No output file specified for distribution details\n");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
81 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
82 ## create working dir.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
83 my $rand = int(rand(10000));
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
84 while (-d "/tmp/DC_Genotyper_$rand") {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
85 $rand = int(rand(10000));
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
86 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
87 my $wd :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
88 $wd = "/tmp/DC_Genotyper_$rand";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
89 system("mkdir '$wd'");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
90
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
91
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
92 my $snpfile :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
93 my $hassnp :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
94 $hassnp = 'NoDbSNP';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
95 $snpfile = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
96 if (defined($opts{'s'})) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
97 $snpfile = $opts{'s'};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
98 if (!-e $snpfile) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
99 die("'$snpfile' is not a valid file path.");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
100 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
101
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
102 my $mime = `file $snpfile`;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
103 if ($mime !~ m/compressed/) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
104 print "$snpfile is not in compressed format. compressing & indexing the file now.\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
105 #print "... this takes a while\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
106 system("bgzip -c $snpfile > $wd/dbSNP.vcf.bgz");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
107 system("cd $wd/ && tabix -p vcf dbSNP.vcf.bgz");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
108 $snpfile = "$wd/dbSNP.vcf.bgz";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
109 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
110 elsif (!-e "$snpfile.tbi") {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
111 print "tabix index file is missing for '$snpfile'. creating now.\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
112 ## check if I can write it out for future use
3
61b6d523acd9 syntax error in perl script
geert-vandeweyer
parents: 0
diff changeset
113 $snpfile =~ m/(.*)([^\/]+)$/;
0
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
114 my $d = $1;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
115 if (-w $d) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
116 open OUT, ">$d/lock";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
117 flock(OUT,2);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
118 system("cd $d && tabix -p vcf $snpfile");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
119 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
120 system("rm $d/lock");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
121 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
122 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
123 system("cp $snpfile /$wd/dbSNP.vcf.bgz");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
124 system("cd $wd/ && tabix -p vcf dbSNP.vcf.bgz");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
125 $snpfile = "$wd/dbSNP.vcf.bgz";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
126 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
127 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
128 $hassnp = 'WithDbSNP';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
129 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
130
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
131
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
132 ## 1. Get FASTA and prepare output hashes:
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
133 my $targets_one = Thread::Queue->new();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
134 my $targets_two = Thread::Queue->new();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
135 my $targets_three = Thread::Queue->new();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
136 open IN, $opts{'t'} or die("Could not open $opts{'t'} file for reading");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
137 if (-d "$wd/Fasta/") {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
138 system("rm $wd/Fasta/*");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
139 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
140 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
141 system("mkdir $wd/Fasta");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
142 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
143 ## create the threads.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
144 for (my $i = 1; $i<= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
145 ${'thr'.$i} = threads->create('FetchFasta');
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
146 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
147
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
148 ## enqueue the targets.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
149 my %thash;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
150 while (<IN>) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
151 chomp;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
152 my ($chr,$start,$stop,$name,$score,$strand) = split(/\t/,$_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
153 $targets_one->enqueue($_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
154 $targets_two->enqueue($_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
155 $targets_three->enqueue($_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
156 $thash{$chr}{$start} = $stop;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
157 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
158 close IN;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
159
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
160 ## end the threads.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
161 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
162 $targets_one->enqueue(undef);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
163 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
164
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
165 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
166 ${'thr'.$i}->join();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
167 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
168
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
169 ## load dbSNP inside target regions into shared structure.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
170 ##########################################################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
171 my %dbsnp :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
172 if ($snpfile ne '') {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
173 my $bcf = `which bcftools`;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
174 chomp($bcf);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
175 if ($bcf ne '') {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
176 my $command = "bcftools query -f '\%CHROM\\t\%POS\\t\%REF\\t\%ALT\\t\%ID\\n' -R '".$opts{'t'}."' '$snpfile' > $wd/dbsnp.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
177 system("$command");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
178 open IN, "$wd/dbsnp.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
179 while (<IN>) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
180 chomp;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
181 my @p = split(/\t/,$_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
182 $dbsnp{$p[0].'-'.$p[1]} = $p[2].'-'.$p[3].'-'.$p[4];
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
183 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
184 close IN;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
185 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
186 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
187 print "WARNING: BCFtools is not in the path. Skipping snp handling.\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
188 $snpfile = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
189 system("touch $wd/dbsnp.txt");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
190 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
191 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
192 else {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
193 system("touch $wd/dbsnp.txt");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
194 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
195
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
196 ## now process the bam file.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
197 mkdir "$wd/WIGS/";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
198 my $bam :shared;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
199 $bam = $opts{'b'};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
200 my $bai = $bam.".bai";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
201 if (!-e $bai) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
202 print "BAI ($bai) missing for $bam : creating\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
203 system("samtools index $bam");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
204 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
205
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
206 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
207 ${'thr'.$i} = threads->create('CountAlleles');
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
208 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
209 ## end the threads.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
210 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
211 $targets_two->enqueue(undef);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
212 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
213
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
214 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
215 ${'thr'.$i}->join();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
216 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
217
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
218 ## generate the distributions.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
219 ##############################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
220 my $alleles = Thread::Queue->new();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
221 my %all = ('A' => 1,'C' => 2,'G' => 3, 'T' => 4);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
222 foreach(keys(%all)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
223 $alleles->enqueue($_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
224 my $a = $_;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
225 foreach(keys(%all)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
226 if ($_ eq $a) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
227 next;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
228 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
229 $alleles->enqueue($a.'-'.$_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
230 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
231 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
232 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
233 ${'thr'.$i} = threads->create('GetDistribution');
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
234 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
235 ## end the threads.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
236 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
237 $alleles->enqueue(undef);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
238 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
239
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
240 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
241 ${'thr'.$i}->join();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
242 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
243
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
244 ## group distributions into one file
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
245 ####################################
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
246 my %map =('A' => 2,'C' => 3,'G' => 4, 'T' => 5);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
247 open OUT, ">".$opts{'a'};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
248 print OUT "allele\tavg\tsd\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
249 foreach(keys(%map)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
250 my $r = $_;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
251 my $f = "$wd/model.$r.$mincov"."x.$hassnp.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
252 open IN, "$f";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
253 my $a = <IN>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
254 chomp($a);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
255 #$dists{$r}{'avg'} = $a;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
256 my $s = <IN>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
257 chomp($s);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
258 #$dists{$r}{'sd'} = $s;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
259 close IN;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
260 print OUT "$r\t$a\t$s\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
261 foreach(keys(%map)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
262 if ($_ eq $r) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
263 next;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
264 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
265 my $f = "$wd/model.$r-$_.$mincov"."x.$hassnp.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
266 open IN, "$f";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
267 my $a = <IN>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
268 chomp($a);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
269 my $s = <IN>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
270 chomp($s);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
271 close IN;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
272 print OUT "$r-$_\t$a\t$s\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
273 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
274 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
275 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
276
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
277 ## CALL SNPs
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
278 ############
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
279 # create the R script.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
280 open R, ">$wd/CallSNPs.R";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
281 print R "args <- commandArgs(trailingOnly = TRUE)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
282 print R "counts <- read.table(file=args[1],header=FALSE, as.is=TRUE)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
283 print R "ploidy <- as.integer(args[3])\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
284 print R "chr <- args[2]\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
285 print R "snps <- read.table(file=args[5],header=FALSE,as.is=TRUE)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
286 print R "colnames(snps) <- c('chr','pos','ref','alt','id')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
287 print R "colnames(counts) <- c('pos','ref','A','C','G','T','TotalDepth')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
288 print R "dists <- read.table(file='$wd/allelic_distributions.txt',header=TRUE,as.is=TRUE)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
289 print R 'rownames(dists) = dists$allele'."\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
290 print R 'dists <- dists[,-1]'."\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
291 print R "vcf <- c()\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
292 print R "lower <- c()\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
293 print R "higher <- c()\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
294 print R "for (i in 1:(ploidy)) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
295 print R " lower[length(lower)+1] <- (2*i-1)/(2*ploidy)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
296 print R " higher[length(higher)+1] <- (2*i+1)/(2*ploidy)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
297 print R "}\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
298 print R "for (i in 1:nrow(counts)) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
299 print R " if (counts[i,'TotalDepth'] == 0) next\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
300 print R " # significantly different from reference?\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
301 print R " z <- ((counts[i,counts[i,'ref']]/counts[i,'TotalDepth']) - dists[counts[i,'ref'],'avg']) / dists[counts[i,'ref'],'sd']\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
302 print R " if (abs(z) > 3) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
303 print R " # test all alterate alleles to see which one is significant.\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
304 print R " for (j in c('A','C','G','T')) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
305 print R " if (j == counts[i,'ref']) next\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
306 print R " z <- ((counts[i,j]/counts[i,'TotalDepth']) - dists[paste(counts[i,'ref'],'-',j,sep=''),'avg']) / dists[paste(counts[i,'ref'],'-',j,sep=''),'sd']\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
307 print R " if (abs(z) > 3){\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
308 print R " filter <- 'PASS'\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
309 print R " phred <- round(-10*log(pnorm(-abs(z))),digits=0)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
310 print R " if (phred > 9999) phred <- 9999\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
311 print R " frac <- counts[i,j]/counts[i,'TotalDepth']\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
312 print R " for (k in 1:ploidy) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
313 print R " if (frac >= lower[k] && frac < higher[k]) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
314 print R " sample <- paste(paste(paste(rep(0,(ploidy-k)),sep='',collapse='/'),paste(rep(1,k),sep='',collapse='/'),sep='/',collapse=''),':',counts[i,counts[i,'ref']],',',counts[i,j],sep='',collapse='')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
315 print R " af <- k/ploidy\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
316 print R " break\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
317 print R " }\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
318 print R " }\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
319 print R " if (frac < lower[1]) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
320 print R " sample <- paste(paste(paste(rep(0,(ploidy-1)),sep='',collapse='/'),paste(rep(1,1),sep='',collapse='/'),sep='/',collapse=''),':',counts[i,counts[i,'ref']],',',counts[i,j],sep='',collapse='')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
321 print R " af <- 1/ploidy\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
322 print R " filter <- 'LowFraction'\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
323 print R " }\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
324 print R " if (counts[i,'TotalDepth'] < $mincov) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
325 print R " filter <- 'LowCoverage'\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
326 print R " }\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
327 print R " info <- paste('DP=',counts[i,'TotalDepth'],';AF=',round(af,digits=5),';AR=',round(frac,digits=5),sep='')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
328 print R " snpids <- which(snps\$chr == chr & snps\$pos == counts[i,'pos'])\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
329 print R " id <- '.'\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
330 print R " if (length(snpids) > 0) id <- snps[snpids[1],'id']\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
331 print R " vcf[length(vcf)+1] <- paste(chr,counts[i,'pos'],id,counts[i,'ref'],j,phred,filter,info,'GT:AD',sample,sep='\\t',collapse='')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
332 print R " }\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
333 print R " }\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
334 print R " }\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
335 print R "}\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
336 print R "if (length(vcf) > 0) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
337 print R " write(file=args[4],paste(vcf,sep='\\n'))\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
338 print R "}\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
339 close R;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
340 system("mkdir $wd/VCF/");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
341 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
342 ${'thr'.$i} = threads->create('CallSNPs');
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
343 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
344 ## end the threads.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
345 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
346 $targets_three->enqueue(undef);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
347 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
348
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
349 for (my $i = 1; $i <= $opts{'p'}; $i++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
350 ${'thr'.$i}->join();
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
351 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
352
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
353 ## BUILD FINAL VCF
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
354 open OUT, ">$outfile";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
355 print OUT "##fileformat=VCFv4.1\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
356 print OUT "##source=High_Ploidy_Genotyper_v.0.1\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
357 print OUT "##genome_reference=$twobit\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
358 if ($snpfile ne '') {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
359 print OUT "##SNP_file=$snpfile\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
360 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
361 foreach(keys(%thash)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
362 print OUT "##contig=<ID=$_,assembly=hg19,species=\"Homo Sapiens\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
363 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
364 print OUT "##INFO=<ID=DP,Number=1,Type=Integer,Description=\"Total Depth\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
365 print OUT "##INFO=<ID=AF,Number=1,Type=Float,Description=\"Allele Frequency\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
366 print OUT "##INFO=<ID=AR,Number=1,Type=Float,Description=\"Allelic Ratio\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
367 print OUT "##FILTER=<ID=LowFraction,Description=\"Allelic Fraction under 1/2*$ploidy\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
368 print OUT "##FILTER=<ID=LowCoverage,Description=\"Total Depth is lower than threshold of $mincov\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
369 print OUT "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
370 print OUT "##FORMAT=<ID=AD,Number=2,type=Integer,Description,\"Allelic Depth\">\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
371 print OUT "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tSAMPLE\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
372 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
373 @i = ( 1 .. 22,'X','Y','M' );
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
374 foreach(@i) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
375 my $chr = "chr$_";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
376 foreach(sort {$a <=> $b} keys(%{$thash{$chr}})) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
377 my $v = "$wd/VCF/$chr.$_-".$thash{$chr}{$_}.".vcf";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
378 if (-e $v) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
379 system("cat '$v' >> '$outfile'");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
380 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
381 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
382 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
383
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
384 ## clean up
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
385 system("rm -Rf '$wd'");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
386
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
387 sub FetchFasta {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
388 while(defined(my $line = $targets_one->dequeue())) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
389 my ($chr,$start,$stop,$name,$score,$strand) = split(/\t/,$line);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
390 # 2bit is zero based, non-including => decrease start by one
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
391 $startposition = $start - 1;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
392 my $command = "twoBitToFa -seq=$chr -start=$startposition -end=$stop -noMask $twobit $wd/Fasta/$chr-$start-$stop.fasta";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
393 system($command);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
394 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
395 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
396
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
397 sub CountAlleles {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
398 # local version of hashes
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
399 my $snp = \%dbsnp;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
400 my %counts;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
401 $counts{'A'} = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
402 $counts{'C'} = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
403 $counts{'G'} = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
404 $counts{'T'} = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
405 my %map =('A' => 1,'C' => 2,'G' => 3, 'T' => 4);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
406 my %options;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
407 foreach(keys(%map)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
408 my $r = $_;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
409 foreach(keys(%map)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
410 if ($_ eq $r) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
411 next;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
412 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
413 $options{$r.'-'.$_} = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
414 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
415 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
416 while (defined(my $line = $targets_two->dequeue())) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
417 $out = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
418 my ($chr,$start,$stop,$name,$score,$strand) = split(/\t/,$line);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
419 ## get reference alleles
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
420 my %ref_alleles;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
421 open FASTA, "$wd/Fasta/$chr-$start-$stop.fasta";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
422 my $head = <FASTA>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
423 my $seq = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
424 while (<FASTA>) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
425 chomp;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
426 $seq .= $_;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
427 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
428 close FASTA;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
429 # this generates a hash of the reference alleles once, instead of substr-calls in every bam, on every iteration.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
430 for (my $pos = 0; $pos < length($seq); $pos++) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
431 $ref_alleles{($pos+$start)} = substr($seq,$pos,1);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
432 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
433 ## get counts.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
434 my $target = "$chr:$start-$stop";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
435 my $command = "igvtools count -w 1 --bases --query '$target' '$bam' '$wd/WIGS/$chr-$start-$stop.wig' '$igvgenome' > /dev/null 2>&1";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
436 system($command);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
437 open WIG, "$wd/WIGS/$chr-$start-$stop.wig";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
438 my $h = <WIG>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
439 $h = <WIG>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
440 $h = <WIG>;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
441 my $target_counts = '';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
442 while (<WIG>) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
443 chomp;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
444 #my ($pos, $a, $c, $g, $t , $n) = split(/\t/,$_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
445 my @p = split(/\t/,$_);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
446 my $s = $p[1] + $p[2] + $p[3] + $p[4];
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
447 $target_counts .= "$p[0]\t$ref_alleles{$p[0]}\t$p[1]\t$p[2]\t$p[3]\t$p[4]\t$s\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
448 ## skip positions with coverage < minimal coverage, and positions in dbsnp if specified (if not specified, snp hash is empty).
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
449 if ($s > $mincov && !defined($snp->{$chr.'-'.$p[0]})) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
450 ## for model of 'non-reference'
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
451 my $frac = $p[$map{$ref_alleles{$p[0]}}] / $s;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
452 $counts{$ref_alleles{$p[0]}} .= $frac.',';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
453 $out .= "$target\t$p[0]\t$ref_alleles{$p[0]}\t$p[1]\t$p[2]\t$p[3]\t$p[4]\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
454 ## for each of the options background models
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
455 foreach(keys(%map)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
456 if ($_ eq $ref_alleles{$p[0]}) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
457 next;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
458 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
459 $options{$ref_alleles{$p[0]}.'-'.$_} .= ($p[$map{$_}] / $s) .',';
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
460 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
461
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
462 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
463 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
464 close WIG;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
465 open OUT, ">>$wd/allcounts.$mincov"."x.$hassnp.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
466 flock(OUT, 2);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
467 print OUT $out;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
468 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
469 open OUT, ">$wd/WIGS/$chr.$start-$stop.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
470 print OUT $target_counts;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
471 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
472
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
473 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
474 foreach(keys(%counts)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
475 open OUT, ">>$wd/counts_$_.$mincov"."x.$hassnp.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
476 flock(OUT,2);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
477 print OUT $counts{$_};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
478 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
479 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
480 foreach(keys(%options)) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
481 open OUT, ">>$wd/counts_$_.$mincov"."x.$hassnp.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
482 flock(OUT,2);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
483 print OUT $options{$_};
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
484 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
485 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
486 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
487
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
488 sub GetDistribution {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
489 while (defined(my $allele = $alleles->dequeue())) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
490 system("sed -i 's/.\$//' '$wd/counts_$allele.$mincov"."x.$hassnp.txt'");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
491 open OUT, ">$wd/GetDistribution.$allele.R";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
492 print OUT "sample <- '$bam'\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
493 print OUT "nt <- '$allele'\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
494 #print OUT "pdf(file='$wd/Distribution.$allele.$mincov"."x.$hassnp.pdf',paper='a4')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
495 print OUT "data <- scan(file='$wd/counts_$allele.$mincov"."x.$hassnp.txt',sep=',')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
496 print OUT "nr <- length(data)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
497 print OUT "avg <- mean(data)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
498 print OUT "sdd <- sd(data)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
499 #print OUT "if (avg > 0.5) {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
500 #print OUT " x <- seq(0.8,1,length=1000)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
501 #print OUT " y <- dnorm(x,mean=avg,sd=sdd)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
502 #print OUT " plot(x,y,main='Distribution in sample $bam for nt $allele',xlab='Allelic Ratio',type='l',lwd=1)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
503 #print OUT " abline(v=(avg-3*sdd),col='red')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
504 #print OUT " text(0.81,max(y-0.5),paste(c('avg: ',avg,'\\nsd: ',sdd,'\\nnrDataPoints:', nr,'\\n$hassnp\\nMin.Cov:',$mincov),sep=' ',collapse=''),adj=c(0,1))\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
505 #print OUT "} else {\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
506 #print OUT " x <- seq(0,0.3,length=1000)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
507 #print OUT " y <- dnorm(x,mean=avg,sd=sdd)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
508 #print OUT " plot(x,y,main='Distribution in sample $bam for nt $allele',xlab='Allelic Ratio',type='l',lwd=1)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
509 #print OUT " abline(v=(avg+3*sdd),col='red')\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
510 #print OUT " text(0.2,max(y-0.5),paste(c('avg: ',avg,'\\nsd: ',sdd,'\\nnrDataPoints:', nr,'\\n$hassnp\\nMin.Cov:',$mincov),sep=' ',collapse=''),adj=c(0,1))\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
511 #print OUT "}\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
512 #print OUT "dev.off()\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
513 print OUT "write(c(avg,sdd),file='$wd/model.$allele.$mincov"."x.$hassnp.txt',ncolumns=1)\n";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
514 close OUT;
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
515 system("cd $wd && Rscript GetDistribution.$allele.R >/dev/null 2>&1");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
516 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
517 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
518
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
519
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
520 sub CallSNPs {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
521 while (defined(my $line = $targets_three->dequeue())) {
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
522 # split.
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
523 my ($chr,$start,$stop,$name,$score,$strand) = split(/\t/,$line);
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
524 my $file = "$wd/WIGS/$chr.$start-$stop.txt";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
525 my $ofile = "$wd/VCF/$chr.$start-$stop.vcf";
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
526 system("cd $wd && Rscript CallSNPs.R '$file' '$chr' '$ploidy' '$ofile' '$wd/dbsnp.txt'");
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
527 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
528
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
529 }
e8a32d824f39 Uploaded
geert-vandeweyer
parents:
diff changeset
530