forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertTab2Pssm.py
executable file
·119 lines (88 loc) · 2.71 KB
/
convertTab2Pssm.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
#!/usr/bin/env python
#convertTab2Pssm.py
'''
Copyright 2010 Wu Albert Cheng <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from sys import *
def convertStrMatrixToFloatMatrix(M):
F=[]
for mrow in M:
Frow=[]
F.append(Frow)
for s in mrow:
Frow.append(float(s))
return F
def convertFloatMatrixToStrMatrix(F):
S=[]
for frow in F:
Srow=[]
S.append(Srow)
for f in frow:
Srow.append(str(f))
return S
def toPSSM(M):
ncols=len(M[0])
nrows=len(M)
for c in range(0,ncols):
csum=0.0
for r in range(0,nrows):
csum+=M[r][c]
for r in range(0,nrows):
M[r][c]/=csum
if __name__=='__main__':
programName=argv[0]
args=argv[1:]
try:
tabfile,=args
except:
print >> stderr,programName,"tabfile > pssmfile"
exit()
fil=open(tabfile)
lines=fil.readlines()
fil.close()
if len(lines)!=4:
print >> stderr, "error: tab file format error, line no!=4"
exit()
for i in range(0,len(lines)):
lines[i]=lines[i].strip().split("\t")
if i==0:
ncol=len(lines[i])
if ncol<=1:
print >> stderr,"error: tab file format error, number of col<=1"
exit()
else:
if ncol!=len(lines[i]):
print >> stderr,"error: tab file format error, col numbers not consistent thru-out all rows"
exit()
alphabets=['A','C','G','T']
hasAlphabet=False
try:
testA=float(lines[0][0])
except ValueError:
hasAlphabet=True
if hasAlphabet:
#first col is alphabet check!
for i in range(0,len(lines)):
if alphabets[i]!=lines[i][0]:
print >> stderr,"error: tab file format error, alphabet not in order"
del lines[i][0]
PSSM=convertStrMatrixToFloatMatrix(lines)
toPSSM(PSSM)
sPSSM=convertFloatMatrixToStrMatrix(PSSM)
for sPSSMrow in sPSSM:
print >> stdout," ".join(sPSSMrow)