-
Notifications
You must be signed in to change notification settings - Fork 5
/
hgnc_geneinfo.py
executable file
·219 lines (194 loc) · 7.4 KB
/
hgnc_geneinfo.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
# Index HGNC gene info files with Elasticsearch, MongoDB or PostgreSQL
import argparse
import gzip
import json
from pprint import pprint
from elasticsearch.helpers import streaming_bulk
from nosqlbiosets.dbutils import DBconnection
from pymongo.errors import BulkWriteError
from sqlalchemy import (Column, Integer, Date, create_engine)
from sqlalchemy import Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.types import ARRAY
# Default index name with Elascticsearch,
# database name with MongoDB and PostgreSQL
INDEX = "geneinfo"
# Default document type name for Elascticsearch index entries,
# collection name with MongoDB, and table name with PostgreSQL
DOCTYPE = 'hgncgeneinfo'
CHUNKSIZE = 64
SOURCEURL = "http://ftp.ebi.ac.uk/pub/databases/genenames/" \
"new/json/hgnc_complete_set.json"
# Read HGNC gene info file, index using the index function specified
def read_and_index_hgnc_file(infile, dbc, indexfunc):
if infile.endswith(".gz"):
f = gzip.open(infile, 'rt')
else:
f = open(infile, 'r')
genesinfo = json.load(f)
r = indexfunc(dbc, genesinfo["response"])
return r
# HGNC gene attributes information:
# https://www.genenames.org/help/statistics-downloads
def read_genes(l):
for gene in l['docs']:
# Following attributes are ignored until we implemented support
for attr in ['pseudogene.org', "homeodb", "kznf_gene_catalog",
"intermediate_filament_db", "bioparadigms_slc",
"mamit-trnadb", "horde_id", "snornabase"]:
if attr in gene:
del(gene[attr])
gene["_id"] = int(gene["hgnc_id"][5:]) # skip prefix "HGNC:"
if "iuphar" in gene:
gene["iuphar"] = int(gene["iuphar"][9:]) # skip prefix "objectId:"
del gene["uuid"], gene["_version_"]
gene["_id"] = int(gene["hgnc_id"][5:]) # skip prefix "HGNC:"
if "entrez_id" in gene:
gene["entrez_id"] = int(gene["entrez_id"])
yield gene
def es_index_genes(dbc, genes):
r = 0
for ok, result in streaming_bulk(
dbc.es,
read_genes(genes),
index=dbc.index,
doc_type='_doc',
chunk_size=CHUNKSIZE
):
action, result = result.popitem()
doc_id = '/%s/commits/%s' % (dbc.index, result['_id'])
if not ok:
print('Failed to %s document %s: %r' % (action, doc_id, result))
else:
r += 1
return r
def mongodb_index_genes(mdbc, genes):
entries = list()
try:
for entry in read_genes(genes):
entries.append(entry)
if len(entries) == CHUNKSIZE:
mdbc.insert_many(entries)
entries = list()
mdbc.insert_many(entries)
except BulkWriteError as bwe:
pprint(bwe.details)
return
Base = declarative_base()
class GeneInfo(Base):
__tablename__ = DOCTYPE
_id = Column(Integer, primary_key=True)
name = Column(Text)
symbol = Column(Text)
prev_name = Column(ARRAY(Text))
prev_symbol = Column(ARRAY(Text))
alias_name = Column(ARRAY(Text))
alias_symbol = Column(ARRAY(Text))
gene_family = Column(ARRAY(Text))
locus_type = Column(Text)
locus_group = Column(Text)
location = Column(Text)
location_sortable = Column(Text)
status = Column(Text)
date_modified = Column(Date)
date_approved_reserved = Column(Date)
date_symbol_changed = Column(Date)
date_name_changed = Column(Date)
ccds_id = Column(ARRAY(Text))
cd = Column(Text)
cosmic = Column(Text)
ena = Column(ARRAY(Text))
ensembl_gene_id = Column(Text)
entrez_id = Column(Integer)
enzyme_id = Column(ARRAY(Text))
gene_family_id = Column(Text)
gene_group = Column(ARRAY(Text))
gene_group_id = Column(ARRAY(Integer))
gtrnadb = Column(Text)
hgnc_id = Column(Text)
imgt = Column(Text)
iuphar = Column(Text)
lncipedia = Column(Text)
lncrnadb = Column(Text)
lsdb = Column(ARRAY(Text))
merops = Column(Text)
mgd_id = Column(Text)
mirbase = Column(Text)
omim_id = Column(Text)
orphanet = Column(Text)
pubmed_id = Column(Text)
refseq_accession = Column(Text)
rgd_id = Column(Text)
rna_central_id = Column(ARRAY(Text))
ucsc_id = Column(Text)
uniprot_ids = Column(ARRAY(Text))
vega_id = Column(Text)
def pgsql_connect(host, port, user, password, db=INDEX):
if port is None:
port = 5432
if host is None:
host = 'localhost'
url = 'postgresql://{}:{}@{}:{}/{}'
url = url.format(user, password, host, port, db)
con = create_engine(url, client_encoding='utf8', echo=False)
Base.metadata.drop_all(con)
Base.metadata.create_all(con)
sm = sessionmaker(con)
session = sm()
return session
def pgsql_index_genes(session, genes):
entries = list()
for entry in read_genes(genes):
entries.append(GeneInfo(**entry))
if len(entries) == CHUNKSIZE:
session.bulk_save_objects(entries)
session.commit()
entries = list()
session.bulk_save_objects(entries)
session.commit()
def main(db, infile, index, doctype,
user=None, password=None, host=None, port=None):
if db in ["Elasticsearch", "MongoDB"]:
dbc = DBconnection(db, index, collection=doctype, host=host, port=port,
recreateindex=True)
if dbc.db == "Elasticsearch":
read_and_index_hgnc_file(infile, dbc, es_index_genes)
dbc.es.indices.refresh(index=index)
elif dbc.db == "MongoDB":
read_and_index_hgnc_file(infile, dbc.mdbi[doctype],
mongodb_index_genes)
else:
session = pgsql_connect(host, port, user, password, index)
session.query(GeneInfo).delete()
read_and_index_hgnc_file(infile, session, pgsql_index_genes)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Index HGNC gene-info json file using Elasticsearch, '
'MongoDB or PostgreSQL, downloaded from ' + SOURCEURL)
parser.add_argument('--infile',
required=True,
help='Input HGNC file to index')
parser.add_argument('--index', default=INDEX,
help='Index name for Elasticsearch, '
'database name for MongoDB and PostgreSQL')
parser.add_argument('--doctype', default=DOCTYPE,
help='Collection name for MongoDB, '
'table name for PostgreSQL')
parser.add_argument('--host',
help='Hostname for the database server')
parser.add_argument('--port',
help="Port number of the database server")
parser.add_argument('--db', default='PostgreSQL',
help="Database: 'Elasticsearch', 'MongoDB',"
" or 'PostgreSQL'")
parser.add_argument('--user',
help="Database user name, "
"supported with PostgreSQL option only")
parser.add_argument('--password',
help="Password for the database user, "
" supported with PostgreSQL option only")
args = parser.parse_args()
main(args.db, args.infile, args.index, args.doctype,
args.user, args.password, args.host, args.port)