annotate tools/rgenetics/rgLDIndep.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 # oct 2009 - must make a map file in case later usage requires it...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # galaxy tool xml files can define a galaxy supplied output filename
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 # that must be passed to the tool and used to return output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 # here, the plink log file is copied to that file and removed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 # took a while to figure this out!
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 # use exec_before_job to give files sensible names
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 # ross april 14 2007
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 # plink cleanup script
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 # ross lazarus March 2007 for camp illumina whole genome data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 # note problems with multiple commands being ignored - eg --freq --missing --mendel
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 # only the first seems to get done...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 ##Summary statistics versus inclusion criteria
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 ##
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 ##Feature As summary statistic As inclusion criteria
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 ##Missingness per individual --missing --mind N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 ##Missingness per marker --missing --geno N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 ##Allele frequency --freq --maf N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 ##Hardy-Weinberg equilibrium --hardy --hwe N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 ##Mendel error rates --mendel --me N M
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 # this is rgLDIndep.py - main task is to decrease LD by filtering high LD pairs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 # remove that function from rgClean.py as it may not be needed.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 import sys,shutil,os,subprocess, glob, string, tempfile, time
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 from rgutils import plinke, timenow, galhtmlprefix
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 prog = os.path.split(sys.argv[0])[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 myversion = 'January 4 2010'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 def pruneld(plinktasks=[] ,cd='./',vclbase = []):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 plink blathers when doing pruning - ignore
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 Linkage disequilibrium based SNP pruning
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 if a million snps in 3 billion base pairs, have mean 3k spacing
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 assume 40-60k of ld in ceu, a window of 120k width is about 40 snps
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 so lots more is perhaps less efficient - each window computational cost is
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 ON^2 unless the code is smart enough to avoid unecessary computation where
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 allele frequencies make it impossible to see ld > the r^2 cutoff threshold
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 So, do a window and move forward 20?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 from the plink docs at http://pngu.mgh.harvard.edu/~purcell/plink/summary.shtml#prune
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 Sometimes it is useful to generate a pruned subset of SNPs that are in approximate linkage equilibrium with each other. This can be achieved via two commands: --indep which prunes based on the variance inflation factor (VIF), which recursively removes SNPs within a sliding window; second, --indep-pairwise which is similar, except it is based only on pairwise genotypic correlation.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 Hint The output of either of these commands is two lists of SNPs: those that are pruned out and those that are not. A separate command using the --extract or --exclude option is necessary to actually perform the pruning.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 The VIF pruning routine is performed:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 plink --file data --indep 50 5 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 will create files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 plink.prune.in
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 plink.prune.out
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 Each is a simlpe list of SNP IDs; both these files can subsequently be specified as the argument for
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 a --extract or --exclude command.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 The parameters for --indep are: window size in SNPs (e.g. 50), the number of SNPs to shift the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 window at each step (e.g. 5), the VIF threshold. The VIF is 1/(1-R^2) where R^2 is the multiple correlation coefficient for a SNP being regressed on all other SNPs simultaneously. That is, this considers the correlations between SNPs but also between linear combinations of SNPs. A VIF of 10 is often taken to represent near collinearity problems in standard multiple regression analyses (i.e. implies R^2 of 0.9). A VIF of 1 would imply that the SNP is completely independent of all other SNPs. Practically, values between 1.5 and 2 should probably be used; particularly in small samples, if this threshold is too low and/or the window size is too large, too many SNPs may be removed.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 The second procedure is performed:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 plink --file data --indep-pairwise 50 5 0.5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 This generates the same output files as the first version; the only difference is that a
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 simple pairwise threshold is used. The first two parameters (50 and 5) are the same as above (window size and step); the third parameter represents the r^2 threshold. Note: this represents the pairwise SNP-SNP metric now, not the multiple correlation coefficient; also note, this is based on the genotypic correlation, i.e. it does not involve phasing.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 To give a concrete example: the command above that specifies 50 5 0.5 would a) consider a
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 window of 50 SNPs, b) calculate LD between each pair of SNPs in the window, b) remove one of a pair of SNPs if the LD is greater than 0.5, c) shift the window 5 SNPs forward and repeat the procedure.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 To make a new, pruned file, then use something like (in this example, we also convert the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 standard PED fileset to a binary one):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 plink --file data --extract plink.prune.in --make-bed --out pruneddata
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 logres = ['## Rgenetics %s: http://rgenetics.org Galaxy Tools rgLDIndep.py Plink pruneLD runner\n' % myversion,]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 for task in plinktasks: # each is a list
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 fplog,plog = tempfile.mkstemp()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 sto = open(plog,'w') # to catch the blather
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 vcl = vclbase + task
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 s = '## ldindep now executing %s\n' % ' '.join(vcl)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 print s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 logres.append(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 x = subprocess.Popen(' '.join(vcl),shell=True,stdout=sto,stderr=sto,cwd=cd)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 retval = x.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 sto.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 sto = open(plog,'r') # read
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 lplog = sto.readlines()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 lplog = [x for x in lplog if x.find('Pruning SNP') == -1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 logres += lplog
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 logres.append('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 logres.append('### %s Strange - no std out from plink when running command line\n%s' % (timenow(),' '.join(vcl)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 sto.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 os.unlink(plog) # no longer needed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 return logres
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 def clean():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 if len(sys.argv) < 14:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 print >> sys.stdout, '## %s expected 14 params in sys.argv, got %d - %s' % (prog,len(sys.argv),sys.argv)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 print >> sys.stdout, """this script will filter a linkage format ped
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 and map file containing genotypes. It takes 14 parameters - the plink --f parameter and"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 a new filename root for the output clean data followed by the mind,geno,hwe,maf, mef and mei"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 documented in the plink docs plus the file to be returned to Galaxy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 Called as:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 <command interpreter="python">
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 rgLDIndep.py '$input_file.extra_files_path' '$input_file.metadata.base_name' '$title' '$mind'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 '$geno' '$hwe' '$maf' '$mef' '$mei' '$out_file1'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 '$out_file1.extra_files_path' '$window' '$step' '$r2'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 </command>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 plog = ['## Rgenetics: http://rgenetics.org Galaxy Tools rgLDIndep.py started %s\n' % timenow()]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 inpath = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 inbase = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 killme = string.punctuation + string.whitespace
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 trantab = string.maketrans(killme,'_'*len(killme))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 title = sys.argv[3].translate(trantab)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 mind = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 geno = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 hwe = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 maf = sys.argv[7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 me1 = sys.argv[8]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 me2 = sys.argv[9]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 outfname = sys.argv[10]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 outfpath = sys.argv[11]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 winsize = sys.argv[12]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 step = sys.argv[13]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 r2 = sys.argv[14]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 output = os.path.join(outfpath,outfname)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 outpath = os.path.join(outfpath,title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 outprunepath = os.path.join(outfpath,'ldprune_%s' % title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 os.makedirs(outfpath)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 bfile = os.path.join(inpath,inbase)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 filterout = os.path.join(outpath,'filtered_%s' % inbase)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 outf = file(outfname,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 outf.write(galhtmlprefix % prog)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 ldin = bfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 plinktasks = [['--bfile',ldin,'--indep-pairwise %s %s %s' % (winsize,step,r2),'--out',outpath,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 '--mind',mind,'--geno',geno,'--maf',maf,'--hwe',hwe,'--me',me1,me2,],
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 ['--bfile',ldin,'--extract %s.prune.in --make-bed --out %s' % (outpath,outpath)],
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 ['--bfile',outpath,'--recode --out',outpath]] # make map file - don't really need ped but...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 # subset of ld independent markers for eigenstrat and other requirements
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 vclbase = [plinke,'--noweb']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 prunelog = pruneld(plinktasks=plinktasks,cd=outfpath,vclbase = vclbase)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 """This generates the same output files as the first version;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 the only difference is that a simple pairwise threshold is used.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 The first two parameters (50 and 5) are the same as above (window size and step);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 the third parameter represents the r^2 threshold.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 Note: this represents the pairwise SNP-SNP metric now, not the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 multiple correlation coefficient; also note, this is based on the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 genotypic correlation, i.e. it does not involve phasing.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 plog += prunelog
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 flog = '%s.log' % outpath
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 flogf = open(flog,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 flogf.write(''.join(plog))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 flogf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 flogf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 globme = os.path.join(outfpath,'*')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 flist = glob.glob(globme)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 flist.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 for i, data in enumerate( flist ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 outf.write('<li><a href="%s">%s</a></li>\n' % (os.path.split(data)[-1],os.path.split(data)[-1]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 outf.write('</ol></div>\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 outf.write("</div></body></html>")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 outf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 clean()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182