annotate DC_Genotyper.pl @ 21:36e1ba3b4d1b draft

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