@@ -141,20 +141,27 @@ def __init__(self, sequences: list[SeqRecord],
141
141
142
142
self ._auto_align = auto_align
143
143
self ._prealigned = prealigned
144
- self .aligned_sequences = None # not needed here, included for clarity
145
- self ._amino_acids = None
144
+ self .aligned_sequences : list [SeqRecord ]
146
145
self .sequences = sequences # must be set here since it affects others
147
- self .pdb_structure = pdb_structure # relies on aligned_sequences
146
+ if pdb_structure is not None :
147
+ self .pdb_structure = pdb_structure # relies on aligned_sequences
148
148
149
149
def __setattr__ (self , name , value ) -> None :
150
150
"""Set aligned_sequences according to auto_align if sequences reset."""
151
+ # Prealigned implies immutability. The exception is when
152
+ # aligned_sequences is None, which means self is being
153
+ # initialized.
154
+ if self ._prealigned and self .aligned_sequences is not None :
155
+ raise ValueError ('aligned_sequences cannot be changed if '
156
+ 'prealigned.' )
157
+
151
158
super ().__setattr__ (name , value )
152
159
153
160
if name == 'sequences' :
154
161
# Prealigned implies immutability. The exception is when
155
162
# aligned_sequences is None, which means self is being
156
163
# initialized.
157
- if self ._prealigned and self . aligned_sequences is not None :
164
+ if self ._prealigned and hasattr ( self , 'aligned_sequences' ) :
158
165
raise ValueError ('sequences cannot be changed if prealigned.' )
159
166
160
167
# If sequences is changed, want to modify aligned_sequences.
@@ -166,30 +173,23 @@ def __setattr__(self, name, value) -> None:
166
173
self .aligned_sequences = self .sequences
167
174
else :
168
175
# Reset aligned_sequences and wait for align() call.
169
- self .aligned_sequences = None
176
+ del self .aligned_sequences
170
177
171
178
elif name == 'aligned_sequences' :
172
- # Prealigned implies immutability. The exception is when
173
- # aligned_sequences is None, which means self is being
174
- # initialized.
175
- if self ._prealigned and self .aligned_sequences is not None :
176
- raise ValueError ('aligned_sequences cannot be changed if '
177
- 'prealigned.' )
179
+ aln_seqs = [str (sr .seq ) for sr in self .aligned_sequences ]
180
+ self ._amino_acids = tuple (zip (* aln_seqs ))
181
+ if hasattr (self , 'pdb_structure' ) and \
182
+ self .pdb_structure is not None :
183
+ self .pdb_structure .renumber (self .aligned_sequences [0 ])
178
184
179
- if value is None :
180
- self ._amino_acids = None
181
- else :
182
- aln_seqs = [str (sr .seq ) for sr in self .aligned_sequences ]
183
- self ._amino_acids = tuple (zip (* aln_seqs ))
184
- if hasattr (self , 'pdb_structure' ) and \
185
- self .pdb_structure is not None :
186
- self .pdb_structure .renumber (self .aligned_sequences [0 ])
187
-
188
- elif name == 'pdb_structure' and value is not None \
189
- and self .aligned_sequences is not None :
185
+ elif name == 'pdb_structure' and hasattr (self , 'aligned_sequences' ):
190
186
# Want to renumber pdb if aligned.
191
187
self .pdb_structure .renumber (self .aligned_sequences [0 ])
192
188
189
+ def __delattr__ (self , name ):
190
+ if name == 'aligned_sequences' :
191
+ del self ._amino_acids
192
+
193
193
def __getitem__ (self , key ):
194
194
"""Amino acids at position key in the alignment.
195
195
@@ -357,13 +357,14 @@ def to_json(self) -> str:
357
357
aligned = True
358
358
359
359
# TODO: probably add a version?
360
- out_dict = {'seq_records' : [], 'aligned' : aligned ,
361
- 'auto_align' : self ._auto_align }
360
+ seq_records = []
362
361
# important to preserve order of sequences
363
362
for sr in seq_records :
364
363
sr_dict = {'seq' : str (sr .seq ), 'id' : sr .id , 'name' : sr .name ,
365
364
'description' : sr .description }
366
- out_dict ['seq_records' ].append (sr_dict )
365
+ seq_records .append (sr_dict )
366
+ out_dict = {'seq_records' : seq_records , 'aligned' : aligned ,
367
+ 'auto_align' : self ._auto_align }
367
368
return json .dumps (out_dict )
368
369
369
370
@classmethod
@@ -398,7 +399,7 @@ def from_json(cls, in_json: str) -> 'ParentAlignment':
398
399
new_instance ._auto_align = in_dict ['auto_align' ]
399
400
return new_instance
400
401
401
- def _check_identity (self , desired_identity : float ) -> float :
402
+ def _check_identity (self , desired_identity : Optional [ float ] ) -> float :
402
403
"""Validate the input desired_identity, set to default if None."""
403
404
if desired_identity is None :
404
405
# Follow default behavior: 0.7 if there's only one sequence.
0 commit comments