Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cli stand-alone version #7

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Using pip / pip3:
```bash
pip install tajimas_d
```

Using conda:
```bash
conda install -c bioconda tajimas_d
```

Or by source:
```bash
git clone [email protected]:not-a-feature/tajimas_d.git
Expand All @@ -56,13 +62,10 @@ theta_w = watterson_estimator(sequences)


## Standalone version

Clone the repository and cd into it. Intall the requirements (miniFasta >= 2.2, for fasta reading).

```
usage: tajimas-d.py [-h] -f PATH [-p] [-t] [-w]
usage: tajimas_d [-h] -f PATH [-p] [-t] [-w]

tajimas-d: Compute Tajima's D, the Pi- or Watterson-Estimator for multiple
tajimas_d: Compute Tajima's D, the Pi- or Watterson-Estimator for multiple
sequences.

optional arguments:
Expand Down
9 changes: 7 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = tajimas_d
version = 1.0.5
version = 2.0.0
description = Computes Tajimas D, the Pi- or Watterson-Estimator for multiple sequences.
long_description = file: README.md
long_description_content_type=text/markdown
Expand Down Expand Up @@ -31,8 +31,13 @@ testing =
tox>=3.20
miniFasta>=2.2

[options.entry_points]
console_scripts =
tajimas_d = tajimas_d._tajimas_d:run_cli


[options.package_data]
frameshift_aware_alignment = py.typed
tajimas_d = py.typed

[pytest]
xfail_strict=true
5 changes: 3 additions & 2 deletions src/tajimas_d/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._tajimas_d import pi_estimator, watterson_estimator, tajimas_d
"""Init of the tajimas_d package."""
from ._tajimas_d import pi_estimator, watterson_estimator, tajimas_d, _main_cli

__all__ = ["pi_estimator", "watterson_estimator", "tajimas_d"]
__all__ = ["pi_estimator", "watterson_estimator", "tajimas_d", "_main_cli"]
95 changes: 95 additions & 0 deletions src/tajimas_d/_tajimas_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

from itertools import combinations
from typing import List
import argparse
import miniFasta
from sys import argv


def __check(sequences: List[str]) -> None:
Expand Down Expand Up @@ -144,3 +147,95 @@ def tajimas_d(sequences: List[str]) -> float:
delta_Theta = theta_pi - (seg_sites / harmonic)
tD = delta_Theta / (((e1 * seg_sites) + (e2 * seg_sites * (seg_sites - 1))) ** 0.5) # Ref 27
return float(tD)


def parse_args(args):
"""Parse command line parameters.

Args:
args (List[str]): command line parameters as list of strings
(for example ``["--help"]``).

Returns:
:obj:`argparse.Namespace`: command line parameters namespace
"""
# Parse arguments
parser = argparse.ArgumentParser(
description="""tajimas_d: Compute Tajima's D, the Pi- or Watterson-Estimator for multiple sequences."""
)

parser.add_argument(
"-f",
"--file",
action="store",
dest="path",
help="Path to fasta file with all sequences.",
required=True,
)

parser.add_argument(
"-p",
"--pi",
action="store_true",
dest="pi",
help="Compute the Pi-Estimator score. (default)",
required=False,
)

parser.add_argument(
"-t",
"--tajima",
action="store_true",
dest="tajima",
help="Compute Tajima's D",
required=False,
)

parser.add_argument(
"-w",
"--watterson",
action="store_true",
dest="watterson",
help="Compute the Watterson-Estimator score.",
required=False,
)

return parser.parse_args()


def _main_cli(args):
args = parse_args(args)
# Load sequences
sequences = [mf.body for mf in miniFasta.read(args.path)]

# Compute
if args.tajima or not (args.pi or args.watterson):
print(f"Tajima's D score:\t\t{tajimas_d(sequences)}")

if args.pi:
print(f"Pi-Estimator score:\t\t{pi_estimator(sequences)}")

if args.watterson:
print(f"Watterson-Estimator score:\t{watterson_estimator(sequences)}")


def run_cli():
"""Calls :func:`_main_cli` passing the CLI arguments extracted from :obj:`sys.argv`.

This function is used as entry point to create a console script with setuptools.
"""
_main_cli(argv[1:])


if __name__ == "__main__":
# ^ This is a guard statement that will prevent the following code from
# being executed in the case someone imports this file instead of
# executing it as a script.
# https://docs.python.org/3/library/__main__.html

# After installing your project with pip, users can also run your Python
# modules as scripts via the ``-m`` flag, as defined in PEP 338::
#
# python -m tajimas_d.tajimas_d [ARGS]
#
run_cli()
73 changes: 0 additions & 73 deletions tajimas-d.py

This file was deleted.