Mercurial > repos > guerler > springsuite
annotate spring_package/Molecule.py @ 37:0be0af9e695d draft
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
author | guerler |
---|---|
date | Wed, 25 Nov 2020 14:35:35 +0000 |
parents | |
children | 172398348efd |
rev | line source |
---|---|
37
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
1 class Molecule: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
2 def __init__(self, fileName=None): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
3 self.calpha = dict() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
4 self.biomol = dict() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
5 self.atoms = list() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
6 if fileName is not None: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
7 self.fromFile(fileName) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
8 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
9 def fromFile(self, fileName): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
10 biomolNumber = 0 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
11 biomolChains = list() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
12 self.biomol[0] = None |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
13 with open(fileName) as file: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
14 for line in file: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
15 key = line[0:6].strip() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
16 if key == "ATOM": |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
17 atom = line[12:16] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
18 atomNumber = line[6:11] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
19 chainName = line[21:22] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
20 if chainName not in self.calpha: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
21 self.calpha[chainName] = dict() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
22 x = self.toFloat(line[30:38]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
23 y = self.toFloat(line[38:46]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
24 z = self.toFloat(line[46:54]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
25 occupancy = self.toFloat(line[54:60], optional=True) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
26 temperature = self.toFloat(line[54:60], optional=True) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
27 residue = line[17:20] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
28 residueNumber = self.toInt(line[22:26]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
29 atomNumber = self.toInt(line[6:11]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
30 atomName = line[12:16] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
31 atomDict = dict(x=x, y=y, z=z, |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
32 residue=residue, |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
33 occupancy=occupancy, |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
34 temperature=temperature, |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
35 atomNumber=atomNumber, |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
36 atomName=atomName, |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
37 residueNumber=residueNumber, |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
38 chainName=chainName) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
39 if atom.strip() == "CA": |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
40 self.calpha[chainName][residueNumber] = atomDict |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
41 self.atoms.append(atomDict) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
42 biokey = "REMARK 350 BIOMOLECULE:" |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
43 if line[0:len(biokey)] == biokey: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
44 biomolNumber = self.toInt(line[len(biokey):]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
45 if biomolNumber == 0: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
46 raise Exception("Invalid biomolecule identifier [0].") |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
47 biokey = "REMARK 350 APPLY THE FOLLOWING TO CHAINS:" |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
48 nextLine = next(file) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
49 while nextLine[:len(biokey)] != biokey: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
50 nextLine = next(file) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
51 biomolChains = nextLine[len(biokey):].split(",") |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
52 biomolChains = list(map(lambda x: x.strip(), biomolChains)) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
53 biokey = "REMARK 350 AND CHAINS:" |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
54 nextLine = next(file) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
55 while nextLine[:len(biokey)] == biokey: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
56 moreChains = nextLine[len(biokey):].split(",") |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
57 moreChains = list(map(lambda x: x.strip(), moreChains)) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
58 biomolChains = biomolChains + moreChains |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
59 nextLine = next(file) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
60 biokey = "REMARK 350 BIOMT" |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
61 if nextLine[:len(biokey)] == biokey: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
62 biomolMatId1, biomolMat1 = self.getFloats(nextLine) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
63 nextLine = next(file) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
64 biomolMatId2, biomolMat2 = self.getFloats(nextLine) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
65 nextLine = next(file) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
66 biomolMatId3, biomolMat3 = self.getFloats(nextLine) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
67 if biomolMatId1 != biomolMatId2 or biomolMatId1 != biomolMatId3: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
68 raise Exception("Invalid rotation matrix format [%s]." % biomolMatId1) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
69 matrix = [biomolMat1, biomolMat2, biomolMat3] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
70 biomolChains = [c for c in biomolChains if c] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
71 if biomolNumber not in self.biomol: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
72 self.biomol[biomolNumber] = list() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
73 self.biomol[biomolNumber].append(dict(chains=biomolChains, matrix=matrix)) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
74 removeChains = [] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
75 for chainName in self.calpha: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
76 if len(self.calpha[chainName]) == 0: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
77 removeChains.append(chainName) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
78 for chainName in removeChains: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
79 del self.calpha[chainName] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
80 if not self.calpha: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
81 raise Exception("Molecule has no atoms.") |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
82 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
83 def getFloats(self, nextLine): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
84 matId = self.toInt(nextLine[20:23]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
85 matLine = nextLine[23:].split() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
86 matLine = list(map(lambda x: self.toFloat(x), matLine)) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
87 return matId, matLine |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
88 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
89 def createUnit(self, biomolNumber=1): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
90 molecule = Molecule() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
91 chainCount = 0 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
92 for matrixDict in self.biomol[biomolNumber]: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
93 for chain in matrixDict["chains"]: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
94 if chain in self.calpha: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
95 chainCopy = dict() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
96 for residue in self.calpha[chain]: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
97 chainCopy[residue] = self.calpha[chain][residue].copy() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
98 for atomNumber in chainCopy: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
99 atom = chainCopy[atomNumber] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
100 rotmat = matrixDict["matrix"] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
101 self.applyMatrix(atom, rotmat) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
102 if chain in molecule.calpha: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
103 chainName = "%s%d" % (chain, chainCount) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
104 else: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
105 chainName = chain |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
106 molecule.calpha[chainName] = chainCopy |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
107 chainCount = chainCount + 1 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
108 return molecule |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
109 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
110 def getSequence(self, chainName): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
111 seq = "" |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
112 if chainName not in self.calpha: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
113 raise Exception("Chain identifier not found [%s]" % chainName) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
114 chainDict = self.calpha[chainName] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
115 for residueNumber in sorted(chainDict): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
116 seq = seq + self.toSingleAmino(chainDict[residueNumber]["residue"]) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
117 return seq |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
118 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
119 def applyMatrix(self, atom, rotmat): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
120 newx = atom["x"] * rotmat[0][0] + atom["y"] * rotmat[0][1] + atom["z"] * rotmat[0][2] + rotmat[0][3] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
121 newy = atom["x"] * rotmat[1][0] + atom["y"] * rotmat[1][1] + atom["z"] * rotmat[1][2] + rotmat[1][3] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
122 newz = atom["x"] * rotmat[2][0] + atom["y"] * rotmat[2][1] + atom["z"] * rotmat[2][2] + rotmat[2][3] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
123 atom["x"] = newx |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
124 atom["y"] = newy |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
125 atom["z"] = newz |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
126 return atom |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
127 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
128 def toFloat(self, x, optional=False): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
129 try: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
130 return float(x) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
131 except Exception: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
132 if not optional: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
133 raise Exception("Invalid float conversion [%s]." % x) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
134 return 0.0 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
135 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
136 def toInt(self, x): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
137 try: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
138 return int(x) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
139 except Exception: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
140 raise Exception("Invalid integer conversion [%s]." % x) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
141 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
142 def toSingleAmino(self, seq): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
143 code = dict(GLY="G", ALA="A", VAL="V", LEU="L", ILE="I", MET="M", PHE="F", PRO="P", TYR="Y", TRP="W", |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
144 LYS="K", SER="S", CYS="C", ASN="N", GLN="Q", HIS="H", THR="T", GLU="E", ASP="D", ARG="R") |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
145 return code[seq] if seq in code else "X" |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
146 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
147 def saveChain(self, chainName, outputName): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
148 print("Writing PDB file to %s." % outputName) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
149 f = open(outputName, "w") |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
150 for residueNumber in sorted(self.calpha[chainName].keys()): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
151 ca = self.calpha[chainName][residueNumber] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
152 if ca["residue"] is not None: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
153 f.write(self.atomString(ca)) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
154 f.close() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
155 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
156 def save(self, outputName, append=False, chainName=None): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
157 print("Writing atoms to PDB file to %s." % outputName) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
158 fileFlag = "+a" if append else "w" |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
159 f = open(outputName, fileFlag) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
160 for atom in self.atoms: |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
161 atom["chainName"] = chainName if chainName else atom["chainName"] |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
162 f.write(self.atomString(atom)) |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
163 f.write("TER\n") |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
164 f.close() |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
165 |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
166 def atomString(self, atom): |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
167 return "ATOM %5d %s %s %1s%4d %8.3f%8.3f%8.3f%6.2f%6.2f\n" % (atom["atomNumber"], atom["atomName"], atom["residue"], atom["chainName"], atom["residueNumber"], |
0be0af9e695d
"planemo upload commit c716195a2cc1ed30ff8c4936621091296a93b2fc"
guerler
parents:
diff
changeset
|
168 atom["x"], atom["y"], atom["z"], atom["occupancy"], atom["temperature"]) |