annotate pgSnp2gd_snp.pl @ 0:d281062566f9 draft

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