Skip to content

Commit

Permalink
Add small usage example to Alignment and fix some documentation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Aug 28, 2024
1 parent 24a3fe3 commit b256b4f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 56 deletions.
2 changes: 1 addition & 1 deletion docs/_static/json/switcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": "https://pyfamsa.readthedocs.io/en/v0.3.3/"
},
{
"name": "v0.3 (latest)",
"name": "v0.3",
"version": "0.3.2",
"url": "https://pyfamsa.readthedocs.io/en/v0.3.2/"
},
Expand Down
1 change: 1 addition & 0 deletions docs/api/alignment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Alignment

.. autoclass:: pyfamsa.Alignment
:inherited-members:
:special-members: __init__, __len__, __getitem__
:members:
9 changes: 9 additions & 0 deletions docs/api/gapped_sequence.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Gapped Sequence
===============

.. currentmodule:: pyfamsa

.. autoclass:: pyfamsa.GappedSequence
:inherited-members:
:special-members: __init__
:members:
35 changes: 11 additions & 24 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ API Reference

.. currentmodule:: pyfamsa

.. toctree::
:caption: API Reference
:hidden:

Aligner <aligner>
Sequence <sequence>
Gapped Sequence <sequence>
Alignment <alignment>
Guide Tree <guide_tree>


Aligner
-------

Expand All @@ -11,12 +22,6 @@ Aligner

Aligner

.. toctree::
:caption: Aligner
:hidden:

aligner <aligner>


Sequence
--------
Expand All @@ -27,12 +32,6 @@ Sequence
Sequence
GappedSequence

.. toctree::
:caption: Sequence
:hidden:

sequence <sequence>


Alignment
---------
Expand All @@ -42,12 +41,6 @@ Alignment

Alignment

.. toctree::
:caption: Alignment
:hidden:

alignment <alignment>


GuideTree
---------
Expand All @@ -56,9 +49,3 @@ GuideTree
:nosignatures:

GuideTree

.. toctree::
:caption: GuideTree
:hidden:

guide_tree <guide_tree>
5 changes: 1 addition & 4 deletions docs/api/sequence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ Sequence

.. autoclass:: pyfamsa.Sequence
:inherited-members:
:special-members: __init__
:members:

.. autoclass:: pyfamsa.GappedSequence
:inherited-members:
:members:
10 changes: 0 additions & 10 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@ has the following advantages:
Access all the features of the original CLI through the :doc:`Python API <api/index>`.


- **single dependency**: ``pyfamsa`` is distributed as a Python package, so you
can add it as a dependency to your project, and stop worrying about the
FAMSA binary being present on the end-user machine.
- **no intermediate files**: Everything happens in memory, in a Python object
you control, so you don't have to invoke the FAMSA CLI using a
sub-process and temporary files.
- **friendly interface**: The different guide tree build methods and
heuristics can be selected from the Python code with a simple keyword
argument when configuring a new `Aligner`.


Setup
-----
Expand Down
59 changes: 42 additions & 17 deletions pyfamsa/_famsa.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,23 @@ cdef class GappedSequence:

cdef class Alignment:
"""An alignment, stored as a list of `GappedSequence` objects.
An alignment can be created from an iterable of `GappedSequence` that
are all the same gapped size::
>>> s1 = GappedSequence(b"seq1", b"MA-WMRLLPL")
>>> s2 = GappedSequence(b"seq2", b"MALWTR-RPL")
>>> alignment = Alignment([s1, s2])
The individual rows of the alignment can be accessed using the usual
indexing operation, and the alignment can be sliced to select only
certain rows::
>>> alignment[0].id
b'seq1'
>>> len(alignment[:1])
1
"""

# --- Magic methods ------------------------------------------------------
Expand Down Expand Up @@ -386,25 +403,31 @@ cdef class Alignment:
return self._msa.size()

def __getitem__(self, object index):
cdef GappedSequence gapped
cdef ssize_t index_
cdef size_t length = self._msa.size()
cdef GappedSequence gapped
cdef CGappedSequence* gseq
cdef ssize_t index_
cdef Alignment ali
cdef size_t length = self._msa.size()

if isinstance(index, slice):
indices = range(*index.indices(length))
return self.__class__(self[i] for i in indices)

index_ = index
if index_ < 0:
index_ += self._msa.size()
if index_ < 0 or index_ >= <ssize_t> self._msa.size():
raise IndexError(index)

gapped = GappedSequence.__new__(GappedSequence)
gapped.alignment = self
gapped._owned = True
gapped._gseq = self._msa[index_]
return gapped
ali = Alignment.__new__(Alignment)
for i in indices:
gseq = new CGappedSequence(self._msa[i][0])
gseq.original_no = gseq.sequence_no = ali._msa.size()
ali._msa.push_back(gseq)
return ali
else:
index_ = index
if index_ < 0:
index_ += self._msa.size()
if index_ < 0 or index_ >= <ssize_t> self._msa.size():
raise IndexError(index)
gapped = GappedSequence.__new__(GappedSequence)
gapped.alignment = self
gapped._owned = True
gapped._gseq = self._msa[index_]
return gapped

# --- Methods ------------------------------------------------------------

Expand Down Expand Up @@ -452,7 +475,9 @@ cdef class Aligner:
object refine=None,
object scoring_matrix=None,
):
"""Create a new aligner with the given configuration.
"""__init__(self, *, threads=0, guide_tree="sl", tree_heuristic=None, medoid_threshold=0, n_refinements=100, keep_duplicates=False, refine=None, scoring_matrix=None)\n--\n
Create a new aligner with the given configuration.
Keyword Arguments:
threads (`int`): The number of threads to use for parallel
Expand Down

0 comments on commit b256b4f

Please sign in to comment.