Repository navigation
Expand file tree
/
Copy pathgemini_query.py
More file actions
executable file
·170 lines (139 loc) · 5.29 KB
/
Copy pathgemini_query.py
File metadata and controls
executable file
·170 lines (139 loc) · 5.29 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import os
import sys
from itertools import tee, ifilterfalse
from collections import defaultdict
# gemini imports
import GeminiQuery
from GeminiQuery import select_formatter
from gemini_constants import *
from gemini_region import add_region_to_query
from gemini_subjects import (Subject, get_subjects, get_subjects_in_family,
get_family_dict)
from gemini_utils import itersubclasses
from dgidb import query_dgidb
def all_samples_predicate(args):
""" returns a predicate that returns True if, for a variant,
the only samples that have the variant have a given phenotype
"""
subjects = get_subjects(args).values()
return select_subjects_predicate(subjects, args)
def family_wise_predicate(args):
formatter = select_formatter(args)
families = get_family_dict(args)
gq = GeminiQuery.GeminiQuery(args.db, out_format=formatter)
predicates = []
for f in families.values():
family_names = [x.name for x in f]
subjects = get_subjects_in_family(args, f).values()
predicates.append(select_subjects_predicate(subjects, args,
family_names))
def predicate(row):
return sum([p(row) for p in predicates]) >= args.min_kindreds
return predicate
def select_subjects_predicate(subjects, args, subset=None):
subjects = set([s.name for s in subjects])
predicates = []
if "all" in args.in_subject:
predicates.append(variant_in_all_subjects(subjects))
if "none" in args.in_subject:
predicates.append(variant_not_in_subjects(subjects))
if "only" in args.in_subject:
predicates.append(variant_only_in_subjects(subjects, subset))
if "any" in args.in_subject:
pr
8000
edicates.append(variant_in_any_subject(subjects))
def predicate(row):
return all([p(row) for p in predicates])
return predicate
def variant_in_any_subject(subjects):
def predicate(row):
return subjects.intersection(samples_with_variant(row)) != set()
return predicate
def variant_in_all_subjects(subjects):
def predicate(row):
return subjects.issubset(samples_with_variant(row))
return predicate
def variant_only_in_subjects(subjects, subset=None):
def predicate(row):
if subset:
check = set(subset).intersection(samples_with_variant(row))
else:
check = samples_with_variant(row)
return check and subjects.issuperset(check)
return predicate
def variant_not_in_subjects(subjects):
def predicate(row):
return subjects.intersection(samples_with_variant(row)) == set()
return predicate
def samples_with_variant(row):
return row['variant_samples']
def queries_variants(query):
return "variants" in query.lower()
def get_row_predicates(args):
"""
generate a list of predicates a row must pass in order to be
returned from a query
"""
predicates = []
if args.family_wise:
predicates.append(family_wise_predicate(args))
elif args.sample_filter:
predicates.append(all_samples_predicate(args))
return predicates
def needs_genotypes(args):
return (args.show_variant_samples or
args.family_wise or
args.sample_filter or
args.carrier_summary or
args.show_families or
args.gt_filter)
def needs_gene(args):
return (args.dgidb)
def add_required_columns_to_query(args):
if args.region:
add_region_to_query(args)
def run_query(args):
predicates = get_row_predicates(args)
add_required_columns_to_query(args)
formatter = select_formatter(args)
genotypes_needed = needs_genotypes(args)
gene_needed = needs_gene(args)
if args.dbtype == "sqlite":
gq = GeminiQuery.GeminiQuery(args.db, out_format=formatter)
elif args.dbtype == "postgresql":
gq = GeminiQuery.GeminiQueryPostgres(args.db, out_format=formatter)
gq.run(args.query, args.gt_filter, args.show_variant_samples,
args.sample_delim, predicates, genotypes_needed,
gene_needed, args.show_families)
if args.use_header and gq.header:
print gq.header
if not args.dgidb:
for row in gq:
print row
else:
# collect a list of all the genes that need to be queried
# from DGIdb
genes = defaultdict()
for row in gq:
genes[row['gene']] = True
# collect info from DGIdb
dgidb_info = query_dgidb(genes)
# rerun the query (the cursor is now consumed)
if args.dbtype == "sqlite":
gq = GeminiQuery.GeminiQuery(args.db, out_format=formatter)
elif args.dbtype == "postgresql":
gq = GeminiQuery.GeminiQueryPostgres(args.db, out_format=formatter)
gq.run(args.query, args.gt_filter, args.show_variant_samples,
args.sample_delim, predicates, genotypes_needed,
gene_needed, args.show_families)
# report the query results with DGIdb info added at the end.
for row in gq:
print str(row) + "\t" + str(dgidb_info[row['gene']])
def query(parser, args):
if (args.db is None):
parser.print_help()
if (args.dbtype == "sqlite" and os.path.exists(args.db)) \
or args.dbtype == "postgresql":
run_query(args)
if __name__ == "__main__":
main()