-
Notifications
You must be signed in to change notification settings - Fork 41
/
uni2pandas.py
executable file
·143 lines (129 loc) · 4.47 KB
/
uni2pandas.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
#!/usr/bin/env python
import click as ck
import numpy as np
import pandas as pd
import gzip
import logging
from utils import Ontology, is_exp_code, is_cafa_target, FUNC_DICT
logging.basicConfig(level=logging.INFO)
ORGS = set(['HUMAN', 'MOUSE', ])
@ck.command()
@ck.option(
'--go-file', '-gf', default='data/go.obo',
help='Gene Ontology file in OBO Format')
@ck.option(
'--swissprot-file', '-sf', default='data/uniprot_sprot.dat.gz',
help='UniProt/SwissProt knowledgebase file in text format (archived)')
@ck.option(
'--out-file', '-o', default='data/swissprot.pkl',
help='Result file with a list of proteins, sequences and annotations')
def main(go_file, swissprot_file, out_file):
go = Ontology(go_file, with_rels=True)
proteins, accessions, sequences, annotations, interpros, orgs = load_data(swissprot_file)
df = pd.DataFrame({
'proteins': proteins,
'accessions': accessions,
'sequences': sequences,
'annotations': annotations,
'interpros': interpros,
'orgs': orgs
})
logging.info('Filtering proteins with experimental annotations')
index = []
annotations = []
for i, row in enumerate(df.itertuples()):
annots = []
for annot in row.annotations:
go_id, code = annot.split('|')
if is_exp_code(code):
annots.append(go_id)
# Ignore proteins without experimental annotations
if len(annots) == 0:
continue
index.append(i)
annotations.append(annots)
df = df.iloc[index]
df = df.reset_index()
df['exp_annotations'] = annotations
prop_annotations = []
for i, row in df.iterrows():
# Propagate annotations
annot_set = set()
annots = row['exp_annotations']
for go_id in annots:
annot_set |= go.get_anchestors(go_id)
annots = list(annot_set)
prop_annotations.append(annots)
df['prop_annotations'] = prop_annotations
cafa_target = []
for i, row in enumerate(df.itertuples()):
if is_cafa_target(row.orgs):
cafa_target.append(True)
else:
cafa_target.append(False)
df['cafa_target'] = cafa_target
df.to_pickle(out_file)
logging.info('Successfully saved %d proteins' % (len(df),) )
def load_data(swissprot_file):
proteins = list()
accessions = list()
sequences = list()
annotations = list()
interpros = list()
orgs = list()
with gzip.open(swissprot_file, 'rt') as f:
prot_id = ''
prot_ac = ''
seq = ''
org = ''
annots = list()
ipros = list()
for line in f:
items = line.strip().split(' ')
if items[0] == 'ID' and len(items) > 1:
if prot_id != '':
proteins.append(prot_id)
accessions.append(prot_ac)
sequences.append(seq)
annotations.append(annots)
interpros.append(ipros)
orgs.append(org)
prot_id = items[1]
annots = list()
ipros = list()
seq = ''
elif items[0] == 'AC' and len(items) > 1:
prot_ac = items[1]
elif items[0] == 'OX' and len(items) > 1:
if items[1].startswith('NCBI_TaxID='):
org = items[1][11:]
end = org.find(' ')
org = org[:end]
else:
org = ''
elif items[0] == 'DR' and len(items) > 1:
items = items[1].split('; ')
if items[0] == 'GO':
go_id = items[1]
code = items[3].split(':')[0]
annots.append(go_id + '|' + code)
if items[0] == 'InterPro':
ipro_id = items[1]
ipros.append(ipro_id)
elif items[0] == 'SQ':
seq = next(f).strip().replace(' ', '')
while True:
sq = next(f).strip().replace(' ', '')
if sq == '//':
break
else:
seq += sq
proteins.append(prot_id)
accessions.append(prot_ac)
sequences.append(seq)
annotations.append(annots)
interpros.append(ipros)
orgs.append(org)
return proteins, accessions, sequences, annotations, interpros, orgs
if __name__ == '__main__':
main()