-
Notifications
You must be signed in to change notification settings - Fork 1
/
zwd2rnacentral.py
202 lines (168 loc) · 6.2 KB
/
zwd2rnacentral.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
"""
Copyright [2009-present] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json
import glob
import os
import re
from Bio import AlignIO
def get_taxid_data(taxid_file):
taxids = {}
with open(taxid_file, 'r') as infile:
for line in infile.readlines():
accession, taxid = line.strip().split('\t')
taxids[accession] = taxid
return taxids
def get_stockholm_annotations(filename):
annotations = {}
with open(filename, 'r') as stockholm:
for line in stockholm.readlines():
if line.startswith('#=GF '):
(key, value) = line.replace('#=GF ', '').strip().split(' ', 1)
annotations[key] = value
if 'TP' not in annotations:
print('TP line not found %s' % filename)
return annotations
def get_papers_info(filename):
"""
104 https://www.ncbi.nlm.nih.gov/pubmed/20230605
"""
papers = {}
with open(filename, 'r') as infile:
for line in infile.readlines():
fields = line.strip().split('\t')
if 'https' in fields[1]:
papers[fields[0]] = fields[1].replace('https://www.ncbi.nlm.nih.gov/pubmed/', '').replace('https://pubmed.ncbi.nlm.nih.gov/', '')
return papers
def get_not_for_rfam_info(filename):
"""
104/aceE.sto
104/Bacteroides-2.sto
"""
excluded = []
with open(filename, 'r') as infile:
for line in infile.readlines():
excluded.append(line.strip())
return excluded
def get_obsolete_taxid_mapping(filename):
"""
Obsolete taxid Current taxid
279263 420662
469595 1639133
"""
mapping = {}
with open(filename, 'r') as infile:
for i, line in enumerate(infile.readlines()):
if i == 0:
continue
old, new = line.strip().split(' ')
mapping[old] = new
mapping['1217713'] = '1776741'
return mapping
def check_sequence(sequence):
return re.match(r'^[ACGTYRNSMWBKU]+$', sequence)
def get_folders():
return [
'104',
'22',
'224',
'hairpin',
'MoCo',
'NiCo',
'SAH',
'SAM-IV',
'SAM-SAH-NMR',
'SAM-VI',
'THF',
'c-di-GMP-II',
'exceptional',
'guanidine',
# 'hairpin',
# 'patches',
'preQ1-III',
'r-leader',
'ts',
'twister',
'variants',
]
def get_so_term(tp_line):
so_term = 'SO:0000655' # ncRNA
if tp_line == 'Cis-reg;':
so_term = 'SO:0005836' # regulatory_region
elif tp_line == 'Cis-reg; leader;':
so_term = 'SO:0000204' # five_prime_UTR
elif tp_line == 'Cis-reg; riboswitch;':
so_term = 'SO:0000035' # riboswitch
elif tp_line == 'Gene; antisense;':
so_term = 'SO:0000077' # antisense
elif tp_line in ['Gene; ribozyme;', 'Gene;ribozyme;']:
so_term = 'SO:0000374' # ribozyme
elif tp_line == 'Gene; sRNA;':
so_term = 'SO:0000370' # small_regulatory_ncRNA
return so_term
def main():
location = '/data/zashaweinbergdata/'
entries = []
taxid_data = get_taxid_data(os.path.join(location, 'sto-seqid-taxid.tab'))
papers_data = get_papers_info(os.path.join(location, 'PAPERS'))
excluded = get_not_for_rfam_info(os.path.join(location, 'not-for-Rfam'))
taxid_mapping = get_obsolete_taxid_mapping('taxid-mapping.tsv')
for folder in get_folders():
for filename in glob.glob(os.path.join(location, folder, '*.sto')):
rna_name = os.path.basename(filename).replace('.sto', '')
if os.path.join(folder, rna_name + '.sto') in excluded:
print('Not for Rfam: {}'.format(filename))
continue
else:
print(filename)
alignment = AlignIO.read(open(filename), 'stockholm')
annotations = get_stockholm_annotations(filename)
# if alignment.column_annotations['secondary_structure'].count('>') != alignment.column_annotations['secondary_structure'].count('<'):
# import pdb; pdb.set_trace()
for record in alignment:
sequence = str(record.seq.ungap('-')).upper().replace('U', 'T')
if not check_sequence(sequence):
'Check sequence: %s' % sequence
continue
if record.name in taxid_data:
taxid = taxid_data[record.name]
if taxid in taxid_mapping:
taxid = taxid_mapping[taxid]
else:
taxid = '12908' # unclassified sequences
entries.append({
'primaryId': 'ZWD:' + record.id,
'taxonId': 'NCBITaxon:{}'.format(taxid),
'soTermId': get_so_term(annotations['TP'] if 'TP' in annotations else ''),
'sequence': sequence,
'name': annotations['DE'] if 'DE' in annotations else '{} RNA'.format(rna_name),
'url': 'https://bitbucket.org/zashaw/zashaweinbergdata/src/ba3b34e5418b990a87595ba150865be4d1d0bd35/{}/{}.sto'.format(folder, rna_name),
'publications' : [
'PMID:{}'.format(papers_data[folder]),
],
})
data = {
'data': entries,
'metaData': {
"dateProduced": "2020-06-05T00:00:00+01:00",
'dataProvider': 'ZWD',
'release': '1.1',
'schemaVersion': '0.2.0',
'publications': [
'PMID:28977401',
],
}
}
with open('/data/rnacentral/zwd.json', 'w') as outfile:
json.dump(data, outfile)
if __name__ == '__main__':
main()