GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — glide-scripts/meta_analysis.py at master · BorgwardtLab/glide-scripts

Expand file tree

/

meta_analysis.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
131 lines (105 loc) · 4.54 KB
/

meta_analysis.py

Copy path

File metadata and controls

131 lines (105 loc) · 4.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# meta-analysis.py --- Run a meta analysis of the pairs of SNPs
# selected by running GLIDE on 4 different cohorts.
# contact: [email protected]
import sys
import numpy as np
import tables
from utils import testPair_logistic_intermediate as testPair
dataDirRoot = 'Clinical_studies/'
rDir = 'Results'
def main(args):
usage = """python %s <intermediate phenotye>
Run a meta analysis of the top-ranked pairs for the different cohorts.
Save results in %s/meta_<intermediate phenotype>.sortedpvals.
""" % (args[0], rDir)
if len(args) == 2:
phenotypeName = args[1]
else:
print usage
sys.exit(0)
# Get the list of pairs to run
threshold = 1e-12
sigPairsList = set([])
for cohort in ['cohort1', 'cohort2', 'cohort3', 'cohort4']:
with open("%s/%s_%s.sortedpvals" % (rDir, cohort, phenotypeName)) as f:
f.readline()
for line in f:
ls = line.split()
if float(ls[-1]) > threshold:
break
pair = ['%s_%s_%s' % (ls[0], ls[1], ls[2]),
'%s_%s_%s' % (ls[3], ls[4], ls[5])]
pair.sort()
pair = "/".join(pair)
sigPairsList.add(pair)
f.close()
sigPairsList = list(sigPairsList)
# Only keep pairs of SNPs that appear in all studies
for cohort in ['cohort1', 'cohort2', 'cohort3', 'cohort4']:
f = open('%s/%s/%s_clean_%s.bim' % (dataDirRoot, cohort, cohort, phenotypeName))
if not allSnps:
allSnps = set([line.split()[1] for line in f])
else:
allSnps = allSnps.intersection(set([line.split()[1] for line in f]))
f.close()
print len(allSnps), "SNPs appear in all studies"
sigPairsList = [x for x in sigPairsList if \
((x.split("/")[0].split("_")[0] in allSnps) \
and (x.split("/")[1].split("_")[0] in allSnps))]
print len(sigPairsList), "pairs to run."
# Test pairs for each study
sigPairsDict = {pair:[] for pair in sigPairsList}
for cohort in ['cohort1', 'cohort2', 'cohort3', 'cohort4']:
print "Testing for %s" % cohort
dataDir = '%s/%s' % (dataDirRoot, cohort)
phenoF = '%s/%s_clean_%s.phenoGlide' % (dataDir, cohort, phenotypeName)
bimF = '%s/%s_clean_%s.bim' % (dataDir, cohort, phenotypeName)
h5fname = '%s/%s_clean_%s.h5' % (dataDir, cohort, phenotypeName)
pheno = np.loadtxt(phenoF)
with open(bimF) as f:
snpsDict = {('%s_%s_%s' % \
(line.split()[1], line.split()[0], line.split()[3])):idx \
for (idx, line) in enumerate(f)}
f.close()
with tables.openFile(h5fname) as h5f:
for pair in sigPairsList:
pairSplit = pair.split("/")
snp1idx = snpsDict[pairSplit[0]]
snp2idx = snpsDict[pairSplit[1]]
snp1x = np.ma.masked_values(h5f.root.genotype[snp1idx], 3)
snp2x = np.ma.masked_values(h5f.root.genotype[snp2idx], 3)
sigPairsDict[pair].append(testPair(snp1x, snp2x, pheno))
h5f.close()
# Combine into a meta-analysis
pvalsDict = {}
for pair, outputs in sigPairsDict.iteritems():
betas = np.array([x[0] for x in outputs])
sesqs = np.array([x[1] for x in outputs])
betas[np.where(betas==0)] = 1e-10
sesqs[np.where(sesqs<=0)] = 1e-10
sesqinvs = 1./sesqs
betam = np.sum(betas*sesqinvs)/np.sum(sesqinvs)
sesqm = np.sqrt(1./np.sum(sesqinvs))
zmeta = betam/sesqm
pval = 2 * st.norm.sf(zmeta) # two-sided
try:
pvalsDict[pval].append([pair, zmeta])
except KeyError:
pvalsDict[pval] = [[pair, zmeta]]
# sort p-values and save
pvals = pvalsDict.keys()
pvals.sort()
with open('%s/meta_logistic_%s.sortedpvals' % (rDir, phenotypeName), 'w') as f:
f.write("SNP1 & chr & pos & SNP2 & chr & pos & meta z-score & meta pval \n")
for pval in pvals:
for [pair, zmeta] in pvalsDict[pval]:
[snp1idx, snp2idx] = pair.split("/")
snp1 = snp1idx
snp2 = snp2idx
f.write("%s & %s & %.2e & %.2e\n" % \
(" & ".join(snp1.split("_")),
" & ".join(snp2.split("_")),
zmeta, pval))
f.close()
if __name__ == "__main__":
main(sys.argv)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.