-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgene_annotations.py
More file actions
194 lines (181 loc) · 4.88 KB
/
Copy pathgene_annotations.py
File metadata and controls
194 lines (181 loc) · 4.88 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import argparse
import logging
import sys
from collections import defaultdict
from flib.core.obo import OBO
from flib.core.gmt import GMT
from flib.core.idmap import IDMap
from six import iteritems
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
parser = argparse.ArgumentParser(
description='Generate propagated gene annotation lists from ontology and \
association files')
parser.add_argument(
"-o",
"--obo-file",
dest="obo",
required=True,
help="A obo file",
metavar="FILE")
parser.add_argument(
"-a",
"--association-file",
dest="ass",
help="A gene association file",
metavar="FILE")
parser.add_argument(
"-b",
dest="term_col",
type=int,
help="The column of the annotations file containing the term identifiers",
default=4)
parser.add_argument(
"-g",
dest="gcol",
type=int,
help="The column of the annotations file containing the desired \
identifiers",
default=1)
parser.add_argument(
"-G",
"--gmt-file",
dest="gmt",
help="GMT file of gene associations")
parser.add_argument(
"-d",
"--output-prefix",
dest="opref",
help="The prefix for output files",
metavar="string")
parser.add_argument(
"-f",
"--output-filename",
dest="ofile",
help="If given outputs all go term/gene annotation pairs to this file, \
file is created in the output prefix directory.",
metavar="string")
parser.add_argument(
"-M",
"--output-gmt",
dest="out_gmt",
default=False,
action="store_true",
help="If given output will be in GMT file format.")
parser.add_argument(
"-i",
"--id-file",
dest="idfile",
help="File to map existing gene ids to the desired identifiers in the \
format <gene id>\\t<desired id>\\n",
metavar="FILE")
parser.add_argument(
"-p",
action="store_true",
dest="propagate",
default=False,
help="Propagate gene annotations")
parser.add_argument(
"-t",
"--terms-file",
dest="terms",
help="File of terms to limit output",
metavar="FILE")
parser.add_argument(
"-n",
"--namespace",
dest="nspace",
help="limit the GO term output to the input namespace: \
(biological_process, cellular_component, molecular_function)",
metavar="STRING")
parser.add_argument(
"-A",
dest="assoc_format",
action="store_true",
default=False,
help="If we are printing to a file (-f), pass this to get a full \
association file back.")
parser.add_argument(
"-u",
dest="pub_filter",
action="store_true",
default=False,
help="Filter annotations from high-throughput publications \
(>50 annotations)")
parser.add_argument(
"-r",
dest="remote_file",
action="store_true",
default=False,
help="Gene assocation file is a remote location")
args = parser.parse_args()
if args.obo is None:
sys.stderr.write("--obo file is required.\n")
sys.exit()
if args.pub_filter and args.nspace is None:
sys.stderr.write(
"--When filtering by publication, must provide GO namespace.\n")
sys.exit()
gene_ontology = OBO(args.obo)
logger.info('Populating gene associations')
if args.ass:
gene_ontology.populate_annotations(
args.ass,
remote_location=args.remote_file,
gene_col=args.gcol,
term_col=args.term_col)
elif args.gmt:
gmt = GMT(args.gmt)
gene_ontology.populate_annotations_from_gmt(gmt)
else:
sys.stderr.write(
"--Provide gene annotations from an association file or a GMT file")
exit()
if args.pub_filter:
pub_counts = defaultdict(set)
for (term_id, term) in iteritems(gene_ontology.go_terms):
if term.namespace != args.nspace:
continue
for a in term.annotations:
pub_counts[a.ref].add((term, a))
for (ref, annots) in iteritems(pub_counts):
if len(annots) > 50:
logger.info(
'Removing %i annotations from: %s',
ref,
len(annots))
for (term, a) in annots:
term.remove_annotation(a)
if args.idfile is not None:
id_name = IDMap(args.idfile)
gene_ontology.map_genes(id_name)
if args.propagate:
logger.info('Propagating gene associations')
gene_ontology.propagate()
gterms = None
if args.terms:
f = open(args.terms, 'r')
gterms = []
for line in f:
termid = line.rstrip('\n')
gterms.append(termid)
f.close()
if args.ofile:
if args.out_gmt:
gene_ontology.print_to_gmt_file(
args.opref +
'/' +
args.ofile)
else:
gene_ontology.print_to_single_file(
args.opref +
'/' +
args.ofile,
gterms,
args.nspace,
args.assoc_format)
else:
gene_ontology.print_to_dir(args.opref,
gterms,
args.nspace)