Skip to content

Commit

Permalink
improve docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
not-a-feature committed Mar 12, 2024
1 parent 4d28189 commit bdb3c76
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 36 deletions.
Empty file modified src/miniFasta/__init__.py
100755 → 100644
Empty file.
95 changes: 64 additions & 31 deletions src/miniFasta/_miniFasta.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@author: Jules Kreuer / not_a_feature
License: GPL-3.0
"""

from typing import Iterator
from dataclasses import dataclass

Expand Down Expand Up @@ -108,16 +109,20 @@ class fasta_object:
def __init__(self, head: str, body: str, stype: str = "any"):
"""
Object to keep a fasta entry.
Input:
head: str, head of fasta entry.
body: str, body of fasta entry.
stype: str, type of the sequence.
Check with self.valid() if the body consits of valid characters.
One of NA: Allows all Nucleic Acid Codes (DNA & RNA)
DNA: Allows all IUPAC DNA Codes
RNA: Allows all IUPAC DNA Codes
PROT: Allows all IUPAC Aminoacid Codes
ANY: Allows all characters [default]
Parameters
----------
head: str
Head of fasta entry.
body: str
Body of fasta entry.
stype: str
Type of the sequence.
Check with self.valid() if the body consits of valid characters.
One of NA: Allows all Nucleic Acid Codes (DNA & RNA)
DNA: Allows all IUPAC DNA Codes
RNA: Allows all IUPAC DNA Codes
PROT: Allows all IUPAC Aminoacid Codes
ANY: Allows all characters [default]
"""
if head.startswith(">"):
self.head = head
Expand Down Expand Up @@ -180,8 +185,10 @@ def valid(self, allowedChars: str = "") -> bool:
"""
Checks if this fasta_object is valid.
stype of fasta_object needs to be set in order to check for illegal characters in its body.
Input:
allowedChars: str, optional to overwrite default settings.
Parameters
----------
allowedChars: str
Optional to overwrite default settings.
"""
if 250000 <= len(self.body):
return False
Expand All @@ -207,17 +214,23 @@ def toAmino(self, d=translation_dict) -> None:
Translates the dna sequence of a fasta_object to amino-acids.
Reading frame starts at position 0, tailing bases will be ignored.
Attention: Will replace triplet with ~ if not found.
Input:
d: dict, dictionary of translation.
Parameters
----------
d: dict
Translation dictionary.
"""
self.body = translate_seq(self.body, d)

def toRevComp(self, d=complement_dict) -> None:
"""
Reverses complement of sequence.
If no complement was found, the nucleotide remains unchanged.
Input:
d: dict, dictionary of complement.
Parameters
----------
d: dict
Comlement dictionary.
"""
self.body = reverse_comp(self.body, d)

Expand All @@ -238,12 +251,20 @@ def print_fasta(fasta) -> None:
def __maybeFind(key, d, alt):
"""
Tries to find key in dict but has a fallback.
Input:
key: hashable, key to find.
d: dict, dictionary to search.
alt: alternative if key not found.
Returns:
v: found value or alt
Parameters
----------
key: hashable
Key to find.
d: dict
Dictionary to search.
alt: any
Alternative if key not found.
Returns
-------
v: any
Found value or alt.
"""
try:
return d[key]
Expand All @@ -259,11 +280,16 @@ def translate_seq(seq: str, d=translation_dict) -> str:
To translate a fasta_object use object.toAmino()
Input:
seq: String, sequence to translate.
d: dict, dictionary of translation.
Parameters
----------
seq: str
Sequence to translate.
d: dict
Translation dictionary.
Returns:
translated: String, translated sequence.
translated: str
Translated sequence.
"""

translated = ""
Expand All @@ -279,11 +305,18 @@ def reverse_comp(seq: str, d=complement_dict) -> str:
"""
Reverses complement of sequence.
If no complement was found, the nucleotides remains unchanged.
Input:
seq: String, sequence to compute the reverse complement.
d: dict, dictionary of complement.
Returns:
rev: String, translated sequence
Parameters
----------
seq: str
Sequence to compute the reverse complement.
d: dict
Complement dictionary.
Returns
-------
rev: str
Translated sequence
"""
if seq == "":
return ""
Expand Down
15 changes: 10 additions & 5 deletions src/miniFasta/_reader.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ def read(
Zip, tar, gz, tar.gz files are supported.
Attention: Encoding characters (backslash) will work under certain conditions.
Input:
file_path: str, path to folder / file.
upper: bool, cast sequences to upper-case letters.
seq: bool, return only the sequences.
Parameters
----------
file_path: str
Path to folder / file.
upper: bool, default: True
Cast sequences to upper-case letters.
seq: bool, default: False
Return only the sequences.
Returns:
fasta_objects: Iterator of fasta_object or Iterator of strings.
fasta_objects: Iterator
Iterator of fasta_object or Iterator of strings.
"""

if not path.isfile(file_path):
Expand Down
7 changes: 7 additions & 0 deletions src/miniFasta/_writer.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def write(fasta_pairs, file_path: str, mode="w") -> None:
"""
Writes a list of fasta_objects or a single one to a file.
Takes fasta_objects as input.
Parameters
----------
fasta_pairs: List[fasta_object] or fasta_object
List or single fasta_object to write.
file_path: str
String or FilePath to file.
"""

if not isinstance(fasta_pairs, list):
Expand Down

0 comments on commit bdb3c76

Please sign in to comment.