-
Notifications
You must be signed in to change notification settings - Fork 4
/
uni2pandas.py
executable file
·135 lines (123 loc) · 4.39 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
#!/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(
'--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')
@ck.option(
'--org-id', '-org', default='',
help='Organism NCBI taxonomy ID. If absent then it will consider all the organisms')
def main(swissprot_file, out_file, org_id):
go = Ontology('data/go.obo', with_rels=True)
proteins, accessions, sequences, annotations, string_ids, orgs = load_data(swissprot_file, org_id)
df = pd.DataFrame({
'proteins': proteins,
'accessions': accessions,
'sequences': sequences,
'annotations': annotations,
'string_ids': string_ids,
'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
df.to_pickle(out_file)
logging.info('Successfully saved %d proteins' % (len(df),) )
def load_data(swissprot_file, org_id):
proteins = list()
accessions = list()
sequences = list()
annotations = list()
string_ids = list()
orgs = list()
with gzip.open(swissprot_file, 'rt') as f:
prot_id = ''
prot_ac = ''
seq = ''
org = ''
annots = list()
strs = list()
for line in f:
items = line.strip().split(' ')
if items[0] == 'ID' and len(items) > 1:
if prot_id != '' and (org_id == "" or org == org_id):
proteins.append(prot_id)
accessions.append(prot_ac)
sequences.append(seq)
annotations.append(annots)
string_ids.append(strs)
orgs.append(org)
prot_id = items[1]
annots = list()
strs = 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] == 'STRING':
str_id = items[1]
strs.append(str_id)
elif items[0] == 'SQ':
seq = next(f).strip().replace(' ', '')
while True:
sq = next(f).strip().replace(' ', '')
if sq == '//':
break
else:
seq += sq
if org_id == "" or org == org_id:
proteins.append(prot_id)
accessions.append(prot_ac)
sequences.append(seq)
annotations.append(annots)
string_ids.append(strs)
orgs.append(org)
return proteins, accessions, sequences, annotations, string_ids, orgs
if __name__ == '__main__':
main()