Sample an alignment to a fixed length#
Let’s load in an alignment of rodents to use in the examples.
from cogent3 import get_app
loader = get_app("load_aligned", moltype="protein", format="phylip")
aln = loader("data/abglobin_aa.phylip")
aln
NotCompleted(type=ERROR, origin=load_aligned, source="abglobin_aa.phylip", message="Traceback (most recent call last):
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/composable.py", line 401, in _call
result = self.main(val, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 330, in main
return _load_seqs(path, cogent3.make_aligned_seqs, self._parser, self.moltype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 286, in _load_seqs
data = _read_it(path)
^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 282, in _
return _read_it(Path(path))
^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 277, in _
return path.read_text()
^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1027, in read_text
with self.open(mode='r', encoding=encoding, errors=errors) as f:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1013, in open
return io.open(self, mode, buffering, encoding, errors, newline)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/cogent3.github.io/cogent3.github.io/c3org/doc/data/abglobin_aa.phylip'
")
How to sample the first n
positions of an alignment#
We can use the fixed_length
app to sample an alignment to a fixed length. By default, it will sample from the beginning of an alignment, the argument length=20
specifies how many positions to sample.
from cogent3 import get_app
first_20 = get_app("fixed_length", length=20)
first_20(aln)
NotCompleted(type=ERROR, origin=load_aligned, source="abglobin_aa.phylip", message="Traceback (most recent call last):
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/composable.py", line 401, in _call
result = self.main(val, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 330, in main
return _load_seqs(path, cogent3.make_aligned_seqs, self._parser, self.moltype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 286, in _load_seqs
data = _read_it(path)
^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 282, in _
return _read_it(Path(path))
^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 277, in _
return path.read_text()
^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1027, in read_text
with self.open(mode='r', encoding=encoding, errors=errors) as f:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1013, in open
return io.open(self, mode, buffering, encoding, errors, newline)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/cogent3.github.io/cogent3.github.io/c3org/doc/data/abglobin_aa.phylip'
")
How to sample n
positions from within an alignment#
Creating the fixed_length
app with the argument start=x
specifies that the sampled sequence should begin x
positions into the alignment.
from cogent3 import get_app
skip_10_take_20 = get_app("fixed_length", length=20, start=10)
skip_10_take_20(aln)
NotCompleted(type=ERROR, origin=load_aligned, source="abglobin_aa.phylip", message="Traceback (most recent call last):
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/composable.py", line 401, in _call
result = self.main(val, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 330, in main
return _load_seqs(path, cogent3.make_aligned_seqs, self._parser, self.moltype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 286, in _load_seqs
data = _read_it(path)
^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 282, in _
return _read_it(Path(path))
^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 277, in _
return path.read_text()
^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1027, in read_text
with self.open(mode='r', encoding=encoding, errors=errors) as f:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1013, in open
return io.open(self, mode, buffering, encoding, errors, newline)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/cogent3.github.io/cogent3.github.io/c3org/doc/data/abglobin_aa.phylip'
")
How to sample n
positions randomly from within an alignment#
The start position can be selected at random with random=True
. An optional seed
can be provided to ensure the same start position is used when the app is called.
from cogent3 import get_app
random_20 = get_app("fixed_length", length=20, random=True)
random_20(aln)
NotCompleted(type=ERROR, origin=load_aligned, source="abglobin_aa.phylip", message="Traceback (most recent call last):
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/composable.py", line 401, in _call
result = self.main(val, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 330, in main
return _load_seqs(path, cogent3.make_aligned_seqs, self._parser, self.moltype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 286, in _load_seqs
data = _read_it(path)
^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 282, in _
return _read_it(Path(path))
^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.12/site-packages/cogent3/app/io.py", line 277, in _
return path.read_text()
^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1027, in read_text
with self.open(mode='r', encoding=encoding, errors=errors) as f:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.8/x64/lib/python3.12/pathlib.py", line 1013, in open
return io.open(self, mode, buffering, encoding, errors, newline)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/cogent3.github.io/cogent3.github.io/c3org/doc/data/abglobin_aa.phylip'
")