/
parse_cellmesh_worm.py
182 lines (155 loc) · 5.94 KB
/
parse_cellmesh_worm.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
from collections import Counter
import json
import sqlite3
import os
import traceback
"""
path = 'data_c_elegans'
# 1. open db
conn_db = sqlite3.connect(os.path.join(path, 'corpus.db'))
cursor_db = conn_db.cursor()
# 1.5. load gene and mesh info from corpus.db
cursor_db.execute('SELECT * FROM gene_dict')
gene_list = cursor_db.fetchall()
gene_dict = {x[2]: x for x in gene_list}
cursor_db.execute('SELECT * FROM cell_dict')
cell_list = cursor_db.fetchall()
cell_dict = {x[1]: x for x in cell_list}
# 2. open json file
with open(os.path.join(path, 'CoCount_gene2pubmed_(C_elegans_protein_coding)_MeSH_cell.json')) as f:
gene_mesh_pubmed = json.load(f)
import numpy as np
data_dict = np.zeros((len(cell_list), len(gene_list)))
total_counts_dict = Counter()
for key, item in gene_mesh_pubmed.items():
s1 = key.split(',')
taxid = s1[0]
s2 = s1[1].split(':')
gene_id, mesh_id = s2
count = item['cnt']
cell_record = cell_dict[mesh_id]
gene_record = gene_dict[gene_id]
cell_id = cell_record[1]
gene = gene_record[3]
total_counts_dict[gene] += count
data_dict[cell_dict[cell_id][0], gene_dict[gene_id][0]] += count
from sklearn.feature_extraction.text import TfidfTransformer
tfidf = TfidfTransformer()
data_tfidf = tfidf.fit_transform(data_dict).toarray()
# 3. create new db
output_db = sqlite3.connect('cellmesh/data/cellmesh.db')
output_cursor = output_db.cursor()
# create a table representing a MeSH ID - gene mapping.
try:
# pmids is a comma-separated string of ints
#output_cursor.execute('CREATE TABLE cell_gene(cellID text, gene text, count integer, pmids text, taxid text)')
# iterate over nonzero entries of corpus
for key, item in gene_mesh_pubmed.items():
s1 = key.split(',')
taxid = s1[0]
s2 = s1[1].split(':')
gene_id, mesh_id = s2
count = item['cnt']
cell_record = cell_dict[mesh_id]
gene_record = gene_dict[gene_id]
cell_id = cell_record[1]
gene = gene_record[3]
taxid = gene_record[1]
pmids = ','.join(item['place'])
tfidf_val = data_tfidf[cell_dict[cell_id][0], gene_dict[gene_id][0]]
output_cursor.execute('INSERT INTO cell_gene VALUES (?, ?, ?, ?, ?, ?)', (cell_id, gene, count, tfidf_val, pmids, taxid))
except Exception as e:
text = traceback.format_exc()
print(text)
# update the gene symbol - gene info mapping
try:
existing_genes = output_cursor.execute('SELECT geneID FROM gene_info')
existing_genes = set([x[0] for x in existing_genes])
for i, gene_record in gene_dict.items():
gene = gene_record[3]
taxid = gene_record[1]
gene_id = gene_record[2]
if gene_id not in existing_genes:
total_counts = total_counts_dict[i]
output_cursor.execute('INSERT INTO gene_info VALUES (?, ?, ?, ?)', (gene, gene_id, total_counts, taxid))
except Exception as e:
text = traceback.format_exc()
print(text)
output_cursor.execute('VACUUM')
output_db.commit()
output_db.close()
"""
##################################################################################3
# add data to cellmesh-anatomy db
path = 'data_c_elegans/anatomy'
# 1. open db
conn_db = sqlite3.connect(os.path.join(path, 'corpus.db'))
cursor_db = conn_db.cursor()
# 1.5. load gene and mesh info from corpus.db
cursor_db.execute('SELECT * FROM gene_dict')
gene_list = cursor_db.fetchall()
gene_dict = {x[2]: x for x in gene_list}
cursor_db.execute('SELECT * FROM cell_dict')
cell_list = cursor_db.fetchall()
cell_dict = {x[1]: x for x in cell_list}
# 2. open json file
with open(os.path.join(path, 'CoCount_gene2pubmed_(C_elegans_protein_coding)_MeSH_anatomy.json')) as f:
gene_mesh_pubmed = json.load(f)
import numpy as np
data_dict = np.zeros((len(cell_list), len(gene_list)))
total_counts_dict = Counter()
for key, item in gene_mesh_pubmed.items():
s1 = key.split(',')
taxid = s1[0]
s2 = s1[1].split(':')
gene_id, mesh_id = s2
count = item['cnt']
cell_record = cell_dict[mesh_id]
gene_record = gene_dict[gene_id]
cell_id = cell_record[1]
gene = gene_record[3]
total_counts_dict[gene] += count
data_dict[cell_dict[cell_id][0], gene_dict[gene_id][0]] += count
from sklearn.feature_extraction.text import TfidfTransformer
tfidf = TfidfTransformer()
data_tfidf = tfidf.fit_transform(data_dict).toarray()
output_db = sqlite3.connect('cellmesh/data/anatomy_mesh.db')
output_cursor = output_db.cursor()
# create a table representing a MeSH ID - gene mapping.
try:
# pmids is a comma-separated string of ints
#output_cursor.execute('CREATE TABLE cell_gene(cellID text, gene text, count integer, pmids text, taxid text)')
# iterate over nonzero entries of corpus
for key, item in gene_mesh_pubmed.items():
s1 = key.split(',')
taxid = s1[0]
s2 = s1[1].split(':')
gene_id, mesh_id = s2
count = item['cnt']
cell_record = cell_dict[mesh_id]
gene_record = gene_dict[gene_id]
cell_id = cell_record[1]
gene = gene_record[3]
taxid = gene_record[1]
pmids = ','.join(item['place'])
tfidf_val = data_tfidf[cell_dict[cell_id][0], gene_dict[gene_id][0]]
output_cursor.execute('INSERT INTO cell_gene VALUES (?, ?, ?, ?, ?, ?)', (cell_id, gene, count, tfidf_val, pmids, taxid))
except Exception as e:
text = traceback.format_exc()
print(text)
# update the gene symbol - gene info mapping
try:
existing_genes = output_cursor.execute('SELECT geneID FROM gene_info')
existing_genes = set([x[0] for x in existing_genes])
for i, gene_record in gene_dict.items():
gene = gene_record[3]
taxid = gene_record[1]
gene_id = gene_record[2]
if gene_id not in existing_genes:
total_counts = total_counts_dict[i]
output_cursor.execute('INSERT INTO gene_info VALUES (?, ?, ?, ?)', (gene, gene_id, total_counts, taxid))
except Exception as e:
text = traceback.format_exc()
print(text)
output_db.commit()
output_db.close()