annotate pgSnp2gd_snp.pl @ 0:d189d06d23cf draft

Imported from capsule None
author devteam
date Mon, 28 Jul 2014 11:30:09 -0400
parents
children 57c5ac41f22c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/perl -w
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
2 use strict;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
3
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
4 #convert from pgSnp file to snp table (Webb format?)
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
5
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
6 #snp table format:
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
7 #1. chr
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
8 #2. position (0 based)
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
9 #3. ref allele
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
10 #4. second allele
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
11 #5. overall quality
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
12 #foreach individual (6-9, 10-13, ...)
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
13 #a. count of allele in 3
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
14 #b. count of allele in 4
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
15 #c. genotype call (-1, or count of ref allele)
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
16 #d. quality of genotype call (quality of non-ref allele from masterVar)
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
17
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
18 if (!@ARGV) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
19 print "usage: pgSnp2gd_snp.pl file.pgSnp[.gz|.bz2] [-tab=snpTable.txt -addColsOnly -build=hg19 -name=na -ref=#1based -chr=#1based ] > newSnpTable.txt\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
20 exit;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
21 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
22
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
23 my $in = shift @ARGV;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
24 my $tab;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
25 my $tabOnly;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
26 my $build;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
27 my $name;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
28 my $ref;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
29 my $binChr = 1; #position of chrom column, indicates if bin is added
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
30 foreach (@ARGV) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
31 if (/-tab=(.*)/) { $tab = $1; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
32 elsif (/-addColsOnly/) { $tabOnly = 1; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
33 elsif (/-build=(.*)/) { $build = $1; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
34 elsif (/-name=(.*)/) { $name = $1; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
35 elsif (/-ref=(\d+)/) { $ref = $1 - 1; } #go to index
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
36 elsif (/-chr=(\d+)/) { $binChr = $1; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
37 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
38
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
39 if ($binChr == 2 && $ref) { $ref--; } #shift over by 1, we will delete bin
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
40 if ((!$tab or !$tabOnly) && !$ref) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
41 print "Error the reference allele must be in a column in the file if not just adding to a previous SNP table.\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
42 exit;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
43 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
44
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
45 #WARNING loads snp table in memory, this could take > 1G ram
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
46 my %old;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
47 my $colcnt = 0;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
48 my @head;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
49 if ($tab) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
50 open(FH, $tab) or die "Couldn't open $tab, $!\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
51 while (<FH>) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
52 chomp;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
53 if (/^#/) { push(@head, $_); next; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
54 my @f = split(/\t/);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
55 $old{"$f[0]:$f[1]"} = join("\t", @f);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
56 $colcnt = scalar @f;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
57 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
58 close FH or die "Couldn't close $tab, $!\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
59 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
60
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
61 if ($in =~ /.gz$/) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
62 open(FH, "zcat $in |") or die "Couldn't open $in, $!\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
63 }elsif ($in =~ /.bz2$/) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
64 open(FH, "bzcat $in |") or die "Couldn't open $in, $!\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
65 }else {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
66 open(FH, $in) or die "Couldn't open $in, $!\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
67 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
68 prepHeader();
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
69 if (@head) { #keep old header, add new?
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
70 print join("\n", @head), "\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
71 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
72 while (<FH>) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
73 chomp;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
74 if (/^#/) { next; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
75 if (/^\s*$/) { next; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
76 my @f = split(/\t/);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
77 if ($binChr == 2) { #must have a bin column prepended on the beginning
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
78 shift @f; #delete it
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
79 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
80 if (!$f[3]) { next; } #WHAT? most likely still zipped?
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
81 if ($f[4] > 2) { next; } #can only do cases of 2 alleles
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
82 if ($f[2] == $f[1] or $f[2] - $f[1] != 1) { next; } #no indels
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
83 if ($f[3] =~ /-/) { next; } #no indels
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
84 #if creating a new table need the reference allele in a column
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
85 if (%old && $old{"$f[0]:$f[1]"}) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
86 my @o = split(/\t/, $old{"$f[0]:$f[1]"});
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
87 my $freq = 0;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
88 my $freq2 = 0;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
89 my $sc;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
90 my $g = 1; #genotype == ref allele count
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
91 if ($f[4] == 1) { #should be homozygous
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
92 if ($f[3] eq $o[2]) { $g = 2; $freq = $f[5]; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
93 elsif ($f[3] eq $o[3]) { $g = 0; $freq2 = $f[5]; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
94 else { next; } #doesn't match either allele, skip
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
95 $sc = $f[6];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
96 }else {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
97 my $a = 0; #index of a alleles, freq, scores
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
98 my $b = 1; #same for b
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
99 my @all = split(/\//, $f[3]);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
100 if ($o[2] ne $all[0] && $o[2] ne $all[1]) { next; } #must match one
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
101 if ($o[3] ne $all[0] && $o[3] ne $all[1]) { next; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
102 if ($o[2] eq $all[1]) { #switch indexes
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
103 $a = 1;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
104 $b = 0;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
105 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
106 my @fr = split(/,/, $f[5]);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
107 $freq = $fr[$a];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
108 $freq2 = $fr[$b];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
109 my @s = split(/,/, $f[6]);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
110 $sc = $s[$b];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
111 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
112 #print old
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
113 print $old{"$f[0]:$f[1]"};
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
114 #add new columns
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
115 print "\t$freq\t$freq2\t$g\t$sc\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
116 $old{"$f[0]:$f[1]"} = '';
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
117 }elsif (!$tabOnly) { #new table, or don't have this SNP
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
118 #need reference allele
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
119 if ($f[3] !~ /$f[$ref]/ && $f[4] == 2) { next; } #no reference allele
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
120 my $freq = 0;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
121 my $freq2 = 0;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
122 my $sc;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
123 my $g = 1; #genotype == ref allele count
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
124 my $alt;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
125 if ($f[4] == 1) { #should be homozygous
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
126 if ($f[3] eq $f[$ref]) { $g = 2; $freq = $f[5]; $alt = 'N'; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
127 else { $g = 0; $freq2 = $f[5]; $alt = $f[3]; } #matches alternate
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
128 $sc = $f[6];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
129 }else {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
130 my $a = 0; #index of a alleles, freq, scores
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
131 my $b = 1; #same for b
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
132 my @all = split(/\//, $f[3]);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
133 if ($f[$ref] ne $all[0] && $f[$ref] ne $all[1]) { next; } #must match one
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
134 if ($f[$ref] eq $all[1]) { #switch indexes
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
135 $a = 1;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
136 $b = 0;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
137 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
138 my @fr = split(/,/, $f[5]);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
139 $freq = $fr[$a];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
140 $freq2 = $fr[$b];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
141 my @s = split(/,/, $f[6]);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
142 $sc = $s[$b];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
143 $alt = $all[$b];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
144 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
145 #print initial columns
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
146 print "$f[0]\t$f[1]\t$f[$ref]\t$alt\t-1";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
147 #pad for other individuals if needed
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
148 my $i = 5;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
149 while ($i < $colcnt) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
150 print "\t-1\t-1\t-1\t-1";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
151 $i += 4;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
152 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
153 #add new columns
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
154 print "\t$freq\t$freq2\t$g\t$sc\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
155 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
156 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
157 close FH or die "Couldn't close $in, $!\n";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
158
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
159 #if adding to a snp table, now we need to finish those not in the latest set
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
160 foreach my $k (keys %old) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
161 if ($old{$k} ne '') { #not printed yet
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
162 print $old{$k}, "\t-1\t-1\t-1\t-1\n"; #plus blank for this one
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
163 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
164 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
165
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
166 exit;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
167
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
168 #parse old header and add or create new
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
169 sub prepHeader {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
170 if (!$build) { $build = 'hg19'; } #set default
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
171 my @cnames;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
172 my @ind;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
173 my $n;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
174 if (@head) { #parse previous header
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
175 my $h = join("", @head); #may split between lines
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
176 if ($h =~ /"column_names":\[(.*?)\]/) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
177 my @t = split(/,/, $1);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
178 foreach (@t) { s/"//g; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
179 @cnames = @t;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
180 $n = $cnames[$#cnames];
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
181 $n =~ s/Q//;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
182 $n++;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
183 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
184 if ($h =~ /"dbkey":"(.*?)"/) { $build = $1; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
185 if ($h =~ /"individuals":\[(.*)\]/) {
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
186 my $t = $1;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
187 $t =~ s/\]\].*/]/; #remove if there is more categories
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
188 @ind = split(/,/, $t);
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
189 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
190 }else { #start new header
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
191 @cnames = ("chr", "pos", "A", "B", "Q");
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
192 $n = 1;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
193 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
194 #add current
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
195 if (!$name) { $name= 'na'; }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
196 my $stcol = $colcnt + 1;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
197 if ($stcol == 1) { $stcol = 6; } #move past initial columns
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
198 push(@ind, "[\"$name\",$stcol]");
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
199 push(@cnames, "${n}A", "${n}B", "${n}G", "${n}Q");
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
200 #reassign head
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
201 undef @head;
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
202 foreach (@cnames) { $_ = "\"$_\""; } #quote name
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
203 $head[0] = "#{\"column_names\":[" . join(",", @cnames) . "],";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
204 $head[1] = "#\"individuals\":[" . join(",", @ind) . "],";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
205 $head[2] = "#\"dbkey\":\"$build\",\"pos\":2,\"rPos\":2,\"ref\":1,\"scaffold\":1,\"species\":\"$build\"}";
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
206 }
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
207 ####End
d189d06d23cf Imported from capsule None
devteam
parents:
diff changeset
208