Skip to content

Commit 9a1e8c2

Browse files
authored
Merge pull request #6
Add property setters and deleters
2 parents ec20447 + 253cce3 commit 9a1e8c2

File tree

11 files changed

+263
-23
lines changed

11 files changed

+263
-23
lines changed

.conda/fastaparser/meta.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# conda package recipe
22

33
{% set name = "fastaparser" %}
4-
{% set version = "1.0" %}
4+
{% set version = "1.1" %}
55

66
package:
77
name: "{{ name|lower }}"
@@ -34,7 +34,7 @@ about:
3434
license: "GNU General Public v3 (GPLv3)"
3535
license_family: GPL3
3636
license_file: LICENSE
37-
summary: "A Python FASTA file Parser and Writer"
37+
summary: "A Python FASTA file Parser and Writer."
3838
doc_url: https://fastaparser.readthedocs.io/en/latest/
3939
dev_url: https://github.com/Kronopt/FastaParser
4040

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ assignees: ''
1212
- Python version:
1313
- Python interpreter: [Cpython (default), Jython, PyPy, etc]
1414
- Python packages installed:
15-
- Install method: [pip, conda, other]
15+
- FastaParser install method: [pip, conda, other]
16+
- FastaParser version:
1617

1718
**Describe the bug**
1819
Describe the bug as precisely as possible.

.github/workflows/workflow.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: CI
22
on: [push]
3-
43
jobs:
5-
64
tests:
75
runs-on: ubuntu-latest
86
strategy:

docs/api_fastasequence.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ Instances of the FastaSequence class have the following attributes
2525

2626
| Attribute | Type / Value | Editable | Description |
2727
|:---:|:---:|:---:|---|
28-
| id | str | No | ID portion of the definition line (header). Can be empty |
29-
| description | str | No | Description portion of the definition line (header). Can be empty |
28+
| id | str | Yes | ID portion of the definition line (header). Can be empty |
29+
| description | str | Yes | Description portion of the definition line (header). Can be empty |
3030
| sequence | list([LetterCode](api_lettercode.md)) | No | Sequence |
3131
| sequence_type | 'nucleotide', 'aminoacid' or None | Yes | Indicates the sequence type. Can be `None` if not known |
3232
| inferred_type | bool | No | `True` if `FastaSequence` inferred the sequence type, `False` otherwise.
3333

34+
Editable attributes can be set by standard variable assignment and deleted/reset with the del keyword:
35+
```Python
36+
fastasequence_object.id = 'new_id'
37+
del fastasequence_object.description
38+
```
39+
3440
## Methods
3541
Instances of the FastaSequence class have the following methods
3642

docs/api_lettercode.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Instances of the LetterCode class have the following attributes
2929
| supported | bool | No | Indicates if letter code is supported or not (ie, if `letter_type` is provided and letter code is defined in the FASTA specification). |
3030
| in_fasta_spec | bool | No | Indicates if letter code is defined in the FASTA specification. |
3131

32+
Editable attributes can be set by standard variable assignment and deleted/reset with the del keyword:
33+
```Python
34+
lettercode_object.letter_type = 'nucleotide'
35+
del lettercode_object.letter_type
36+
```
37+
3238
## Methods
3339
Instances of the LetterCode class have the following method
3440

docs/history.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# History
22

3+
### 1.1 (13-02-2020)
4+
* Added property setters for:
5+
* FastaSequence.id
6+
* FastaSequence.description
7+
* Added property deleters for:
8+
* FastaSequence.id
9+
* FastaSequence.description
10+
* FastaSequence.sequence_type
11+
* LetterCode.letter_type
12+
313
### 1.0 (27-01-2020)
414
* First release on PyPI and Anaconda Cloud
515
* Reader, Writer, FastaSequence and LetterCode classes

fastaparser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
__author__ = 'Pedro HC David, https://github.com/Kronopt'
3434
__credits__ = ['Pedro HC David']
35-
__version__ = '1.0'
35+
__version__ = '1.1'
3636
__license__ = 'GPLv3'
3737

3838

fastaparser/fastasequence.py

Lines changed: 110 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class FastaSequence:
6161
TypeError
6262
When calling __init__, if sequence, id_, description, sequence_type or infer_type are of the wrong type.
6363
When calling from_fastasequence(), if fastasequence is of the wrong type.
64+
When setting id, if id_value is not str.
65+
When setting description, if description_value is not str.
6466
When setting sequence_type, if sequence_type_value is of the wrong type.
6567
When calling complement(), if sequence_type is 'aminoacid' or reverse is not bool.
6668
When calling gc_content(), if sequence_type is 'aminoacid' or as_percentage is not bool.
@@ -99,19 +101,8 @@ def __init__(self, sequence, id_='', description='', sequence_type=None, infer_t
99101
TypeError
100102
If sequence, id_, description, sequence_type or infer_type are of the wrong type.
101103
"""
102-
if isinstance(id_, str):
103-
id_ = ''.join(id_.strip().replace(' ', '_').split()) # remove spaces and newlines
104-
if id_.startswith('>'): # remove '>' if any
105-
id_ = id_[1:]
106-
self._id = id_
107-
else:
108-
raise TypeError('id_ must be str')
109-
110-
if isinstance(description, str):
111-
self._description = ' '.join(description.strip().split()) # remove extra spaces and newlines
112-
else:
113-
raise TypeError('description must be str')
114-
104+
self._update_id(id_)
105+
self._update_description(description)
115106
self._update_sequence_type(sequence_type, update_letter_code_objects=False)
116107

117108
if isinstance(sequence, str) and len(sequence) > 0:
@@ -165,11 +156,63 @@ def id(self):
165156
"""return id."""
166157
return self._id
167158

159+
@id.setter
160+
def id(self, id_value):
161+
"""
162+
Sets id.
163+
'>' and newlines will be removed, if any. Spaces will be converted to '_'.
164+
Can be an empty string.
165+
166+
Parameters
167+
----------
168+
id_value : str
169+
ID portion of the definition line (header).
170+
171+
Raises
172+
------
173+
TypeError
174+
If id_value is not str.
175+
"""
176+
self._update_id(id_value)
177+
178+
@id.deleter
179+
def id(self):
180+
"""
181+
Sets id to the default value ('').
182+
"""
183+
self._update_id('')
184+
168185
@property
169186
def description(self):
170187
"""return description."""
171188
return self._description
172189

190+
@description.setter
191+
def description(self, description_value):
192+
"""
193+
Sets description.
194+
Newlines will be removed, if any.
195+
Can be an empty string.
196+
197+
Parameters
198+
----------
199+
description_value : str
200+
Description portion of the definition line (header).
201+
202+
Raises
203+
------
204+
TypeError
205+
If description_value is not str.
206+
"""
207+
self._update_description(description_value)
208+
209+
@description.deleter
210+
def description(self):
211+
"""
212+
Sets description to the default value ('').
213+
"""
214+
self._update_description('')
215+
173216
@property
174217
def sequence(self):
175218
"""return sequence."""
@@ -197,6 +240,13 @@ def sequence_type(self, sequence_type_value):
197240
"""
198241
self._update_sequence_type(sequence_type_value)
199242

243+
@sequence_type.deleter
244+
def sequence_type(self):
245+
"""
246+
Sets sequence_type to the default value (None) and updates all other relevant properties as needed.
247+
"""
248+
self._update_sequence_type(None)
249+
200250
@property
201251
def inferred_type(self):
202252
"""return inferred_type."""
@@ -374,7 +424,7 @@ def count_letter_codes_degenerate(self):
374424
return {letter: counts for letter, counts in self._counts.items()
375425
if letter in LETTER_CODES[self._sequence_type][1]}
376426
raise TypeError('To count degenerate letter codes the sequence_type must be '
377-
'explicitly \'%s\'' % ('\' or \''.join(LETTER_CODES),))
427+
'explicitly \'%s\'' % '\' or \''.join(LETTER_CODES))
378428

379429
def formatted_definition_line(self):
380430
"""
@@ -457,6 +507,51 @@ def reverse(self):
457507
"""
458508
return self.__reversed__()
459509

510+
def _update_id(self, id_):
511+
"""
512+
Updates ID portion of the definition line (header).
513+
'>' and newlines will be removed, if any. Spaces will be converted to '_'.
514+
Can be an empty string.
515+
516+
Parameters
517+
----------
518+
id_ : str
519+
ID portion of the definition line (header).
520+
521+
Raises
522+
------
523+
TypeError
524+
If id_ is not str.
525+
"""
526+
if isinstance(id_, str):
527+
id_ = ''.join(id_.strip().replace(' ', '_').split()) # remove spaces and newlines
528+
if id_.startswith('>'): # remove '>' if any
529+
id_ = id_[1:]
530+
self._id = id_
531+
else:
532+
raise TypeError('id_ must be str')
533+
534+
def _update_description(self, description):
535+
"""
536+
Updates description portion of the definition line (header).
537+
Newlines will be removed, if any.
538+
Can be an empty string.
539+
540+
Parameters
541+
----------
542+
description : str
543+
Description portion of the definition line (header).
544+
545+
Raises
546+
------
547+
TypeError
548+
If description is not str.
549+
"""
550+
if isinstance(description, str):
551+
self._description = ' '.join(description.strip().split()) # remove extra spaces and newlines
552+
else:
553+
raise TypeError('description must be str')
554+
460555
def _update_sequence_type(self, sequence_type, update_letter_code_objects=True):
461556
"""
462557
Updates sequence_type and all other relevant properties as needed.
@@ -477,7 +572,7 @@ def _update_sequence_type(self, sequence_type, update_letter_code_objects=True):
477572
self._sequence_type = sequence_type
478573
self._inferred_type = False
479574
else:
480-
raise TypeError('sequence_type must be one of: %s or None' % LETTER_CODES)
575+
raise TypeError('sequence_type must be one of: \'%s\' or None' % '\', \''.join(LETTER_CODES))
481576
if isinstance(update_letter_code_objects, bool):
482577
if update_letter_code_objects:
483578
for letter_code_object in self._sequence: # update LetterCode objects

fastaparser/lettercode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def letter_type(self, letter_type_value):
124124
"""
125125
self._update_letter_type(letter_type_value)
126126

127+
@letter_type.deleter
128+
def letter_type(self):
129+
"""
130+
Sets letter_type to the default value (None) and updates all other relevant properties as needed.
131+
"""
132+
self._update_letter_type(None)
133+
127134
@property
128135
def description(self):
129136
"""return description."""

0 commit comments

Comments
 (0)