forked from bernhardcl/coot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.py
171 lines (126 loc) · 4.8 KB
/
chain.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
"""A class for storing chains of nucleotides."""
# Copyright 2010, 2011 Kevin Keating
#
# Licensed under the Educational Community License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.osedu.org/licenses/ECL-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS"
# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
from suite import Suite
class Chain:
"""A class for storing chains of nucleotides (which must all be instances of the nucleotide class)."""
def __init__(self, nucs = None):
"""Initialize a Chain object.
OPTIONAL ARGUMENTS:
nucs - a list of nucleotide objects to add to the chain
If not given, then an empty chain is created
RETURNS:
an initialized Chain object
"""
#self.__nucNums = []
self.__resNumDict = {}
if nucs is not None:
for (curSeqNum, curNuc) in enumerate(nucs):
curNuc.chain = self
curNuc.seqNum = curSeqNum
self.__resNumDict[str(curNuc.resNum)] = curSeqNum
#self.__nucNums.append(str(curNuc.resNum))
else:
nucs = []
self.nucleotides = nucs
@property
def nucs(self):
"""A synonym for Chain.nucleotides"""
return self.nucleotides
def addNuc(self, nuc):
"""Add a nucleotide to the end of the chain
ARGUMENTS:
nuc - a nucleotide object to add to the chain
RETURNS:
None
EFFECTS:
Adds nuc to the chain
"""
nuc.chain = self
nuc.seqNum = self.numNucs()
self.nucleotides.append(nuc)
self.__resNumDict[str(nuc.resNum)] = nuc.seqNum
#self.__nucNums.append(str(nuc.resNum))
def addNuc5p(self, nuc):
"""Add a nucleotide to the start (i.e. 5' end) of the chain
ARGUMENTS:
nuc - a nucleotide object to add to the chain
RETURNS:
None
EFFECTS:
Adds nuc to the chain
"""
nuc.chain = self
nuc.seqNum = 0
#update the sequence number of the nucleotides
for curNuc in self.nucleotides:
curNuc.seqNum += 1
#update the residue number lookup dictionary
for (curKey, curVal) in self.__resNumDict.iteritems():
self.__resNumDict[curKey] = curVal + 1
self.nucleotides.insert(0, nuc)
def suites(self):
"""A generator for suites in the chain
ARGUMENTS:
None
YIELDS:
The next suite of the chain
"""
#if the first nucleotide doesn't have a C1', then assume it's not a complete nucleotide and skip it
if self.nucleotides[0].hasAtom("C1'"):
startingNuc = 0
else:
startingNuc = 1
for i in xrange(startingNuc, len(self.nucleotides)-2):
yield Suite(self.nucleotides[i], self.nucleotides[i+1])
def len(self):
"""The length of the chain in nucleotides
ARGUMENTS:
None
RETURNS:
The length of the chain in nucleotides
"""
return len(self.nucleotides)
def numNucs(self):
"""The length of the chain in nucleotides
ARGUMENTS:
None
RETURNS:
The length of the chain in nucleotides
"""
return len(self.nucleotides)
def numSuites(self):
"""The length of the chain in suites
ARGUMENTS:
None
RETURNS:
The length of the chain in suites
"""
return len(self.nucleotides) - 1
def resIndex(self, resNum):
"""Find the index (i.e. position in the chain) of a given residue number
ARGUMENTS:
resNum - the residue number to find (which may include an insertion code)
RETURNS:
the index of the specified residue number in this chain
"""
return self.__resNumDict[str(resNum)]
def resNumList(self):
return [nuc.resNum for nuc in self.nucleotides]
def resTypeList(self):
return [nuc.type for nuc in self.nucleotides]
def __str__(self):
"""Create a string representation of the object"""
return "\n\n".join(map(str, self.nucleotides))