-
Notifications
You must be signed in to change notification settings - Fork 0
/
C-end.py
executable file
·157 lines (140 loc) · 6.32 KB
/
C-end.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
#!/anaconda3/bin/python
import xml.etree.cElementTree as et
import pandas as pd
import sys
import re
def IDTrans(IDList,From='ACC+ID',To='GENENAME'):
import urllib.parse
import urllib.request
url = 'https://www.uniprot.org/uploadlists/'
IDs=' '.join(IDList)
params = {
'from': From,
'to': To,
'format': 'tab',
'query': IDs,
}
data = urllib.parse.urlencode(params)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as f:
response = f.read()
Raw=response.decode('utf-8')
Use=[i.split('\t') for i in Raw.strip().split('\n')[1:]]
# RawID=[i[0] for i in Use]
# TransID=[i[1] for i in Use]
return Use
def Reader(XML):
result=[]
parsedXML = et.parse(XML)
for c in parsedXML.getroot():
t=c.tag
if t=='BlastOutput_iterations':
for sc in c.getchildren():
for s in sc.getchildren():
tt=s.tag
if tt=='Iteration_hits':
for h in s.getchildren():
for hs in h.getchildren():
if hs.tag=='Hit_id':
ID=hs.text
if hs.tag=='Hit_len':
P_length=hs.text
if hs.tag=='Hit_def':
Name=hs.text
if hs.tag=='Hit_hsps':
for hss in hs.getchildren():
for hspuse in hss.getchildren():
# print(hss.items)
if hspuse.tag=='Hsp_hit-from':
Hit_from=hspuse.text
if hspuse.tag=='Hsp_hit-to':
Hit_end=hspuse.text
if Hit_end==P_length:
result.append({'ID':ID,
'ID_FullVersion':Name,
'P_length':P_length,
'From':Hit_from,
'End':Hit_end})
Df=pd.DataFrame(result)
return Df
def IDprepare(ID,ID_FullVersion):
Pair={'gb':'EMBL','dbj':'EMBL','emb':'EMBL','ref':'P_REFSEQ_AC','pdb':'PDB_ID'}
F0,IDUse,NoteUse,Warning=None,None,'',0
temp=ID.strip('|').split('|')
temp_notelist=[]
FullVersionContain_sp=re.findall('\>sp\|(.*?)\|',ID_FullVersion)
if temp[0]=='sp':
IDFormat='ACC+ID'
IDUse=temp[1]
Warning=0
else:
if len(FullVersionContain_sp)==1:
IDFormat='ACC+ID'
IDUse=FullVersionContain_sp[0]
else:
if len(FullVersionContain_sp)>1:
Warning+=1
Note=':'.join(['More Than one ACC found!','|'.join(FullVersionContain_sp)])
temp_notelist.append(Note)
IDFormat='ACC+ID'
IDUse=FullVersionContain_sp[0]
else:
if len(temp)==2:
F0,IDUse=temp
else:
if len(temp)!=2:
if len(temp)>2:
F0,IDUse=temp[0],temp[1]
Warning+=1
Note=':'.join(['IDUse may not be correct, ID have more contains than expect!',ID])
temp_notelist.append(Note)
if F0 in Pair.keys():
IDFormat=Pair[F0]
else:
Warning+=1
Note=':'.join(['ID do not contain the needed format, search format among ID_FullVersion',ID])
temp_notelist.append(Note)
Patterns=[(k,'\>%s\|.*?\|',Pair[k]) % k for k in Pair.keys()]
# bug, if format if gb|111|1, id will be 111, without warning!
for f,p,IDFormat0 in Patterns:
R=re.findall(p,ID_FullVersion)
lR=len(R)
if lR>0:
if len(R)==1:
IDFormat=IDFormat0
IDUse=R[0]
else:
Warning+=1
Note=':'.join(["ID_FullVersion contain more than one '%s' format" % f,'|'.join(R)])
temp_notelist.append(Note)
else:
continue
NoteUse=';'.join(temp_notelist)
return {'RawFormat':IDFormat,'RawFormatID':IDUse,
'ID':ID,'ID_FullVersion':ID_FullVersion,
'IDPrepWarning':Warning,'IDPrepNote':NoteUse}
def Main(XMLPath,ResultPath):
Df0=Reader(XMLPath)
IDPreparedDf=pd.DataFrame([IDprepare(ID,Full) for ID,Full in Df0.loc[:,['ID','ID_FullVersion']].values])
FormatsRaw=set(IDPreparedDf.loc[:,'RawFormat'])-{'ACC+ID'}
Df1=IDPreparedDf.loc[IDPreparedDf.loc[:,'RawFormat']=='ACC+ID',:]
Df1.loc[:,'UniProtKB_ACC']=Df1.loc[:,'RawFormatID'].map(lambda x:x.split('.')[0])
for f in FormatsRaw:
Dftemp=IDPreparedDf.loc[IDPreparedDf.loc[:,'RawFormat']==f,:]
IDList=Dftemp.loc[:,'RawFormatID'].dropna().values
f2ACC=IDTrans(IDList=IDList,From=f,To='ACC')
Dftemp.loc[:,'UniProtKB_ACC']=Dftemp.loc[:,'RawFormatID'].map(dict(f2ACC))
Df1=pd.concat([Df1,Dftemp])
ACC2GENENAME=IDTrans(IDList=Df1.loc[:,'UniProtKB_ACC'].dropna().values,
From='ACC',To='GENENAME')
ACC2ENSEMBL_ID=IDTrans(IDList=Df1.loc[:,'UniProtKB_ACC'].dropna().values,
From='ACC',To='ENSEMBL_ID')
Df1.loc[:,'GENENAME']=Df1.loc[:,'UniProtKB_ACC'].map(dict(ACC2GENENAME))
Df1.loc[:,'ENSEMBL_ID']=Df1.loc[:,'UniProtKB_ACC'].map(dict(ACC2ENSEMBL_ID))
DfFinial=pd.merge(Df0,Df1,on=['ID','ID_FullVersion'],how='outer')
DfFinial.to_excel(ResultPath)
return DfFinial
XML=sys.argv[1]
ResultPath=sys.argv[2]
Main(XML,ResultPath)