Repository navigation
Expand file tree
/
Copy pathgemini_query.py
More file actions
executable file
·129 lines (104 loc) · 3.87 KB
/
Copy pathgemini_query.py
File metadata and controls
executable file
·129 lines (104 loc) · 3.87 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
#!/usr/bin/env python
import os
import sys
from itertools import tee, ifilterfalse
# 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
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:
predicates.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)
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)
gq = GeminiQuery.GeminiQuery(args.db, out_format=formatter)
gq.run(args.query, args.gt_filter, args.show_variant_samples,
args.sample_delim, predicates, needs_genotypes(args))
if args.use_header and gq.header:
print gq.header
for row in gq:
print row
def query(parser, args):
if (args.db is None):
parser.print_help()
if os.path.exists(args.db):
run_query(args)
if __name__ == "__main__":
main()