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

Refactor CLI to handle help command and following clig.dev guidelines #64

Merged
merged 1 commit into from
Nov 29, 2024
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ aphylogeo/bin/*.fasta

#Output
*.dnd

#Ignore process files
datasets/example/geneticTrees.json
result.csv
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
aPhylogeo:
python aphylogeo/main.py
python aphylogeo/main.py run

.PHONY: clean

Expand Down
35 changes: 26 additions & 9 deletions aphylogeo/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import pandas as pd
import time
from aphylogeo.alignement import AlignSequences
Expand All @@ -21,17 +23,23 @@
\/__/
""" # https://patorjk.com/software/taag/#p=display&f=Larry%203D&t=aphylogeo%20

app = typer.Typer(invoke_without_command=True)
app = typer.Typer(
invoke_without_command=False,
help="A tool for processing climatic and genetic data to generate phylogenetic trees.",
callback=lambda: typer.echo(typer.style(titleCard, fg=typer.colors.GREEN))
)

@app.command()
def climate_pipeline(
file_name: str = typer.Option(Params.PARAMETER_KEYS["file_name"], help="The name of the file containing the climatic data."),
output: str = typer.Option("./datasets/example/climaticTrees.nwk", help="The name of the file to save the climatic trees."),
):
"""
This function is used to run the climatic pipeline that creates the climatic trees.
Run the climatic pipeline that process the climatic trees.

Args:
file_name (str): The name of the file containing the climatic data.
output (str): The name of the file to save the climatic trees.
"""
Params.load_from_file()

Expand All @@ -49,9 +57,11 @@ def genetic_pipeline(
output: str = typer.Option("./datasets/example/geneticTrees.json", help="The name of the file to save the genetic trees."),
):
"""
This function is used to run the genetic pipeline that creates the genetic trees.
Run the genetic pipeline that process the genetic trees.

Args:
reference_gene_filepath (str): The path to the reference gene file.
output (str): The name of the file to save the genetic trees.
"""
Params.load_from_file()

Expand All @@ -72,12 +82,20 @@ def genetic_pipeline(
except Exception as e:
print(f"Error saving the file: {e}")

@app.callback()
def main(
@app.command()
def run(
climatic_tree: str = typer.Option(None, help="The name of the file containing the climatic trees."),
genetic_tree: str = typer.Option(None, help="The name of the file containing the genetic trees."),
output: str = typer.Option("./results/output.csv", help="The name of the file to save the output."),
):
"""
Run the pipelines and process the trees and phylogeographic analyses.

Args:
climatic_tree (str): The name of the file containing the climatic trees.
genetic_tree (str): The name of the file containing the genetic trees.
output (str): The name of the file to save the output.
"""
# geneticTrees = GeneticTrees.load_trees_from_file("./results/geneticTreesTest.json")
# loaded_seq_alignment = Alignment.load_from_json("./results/aligned_sequences.json")

Expand All @@ -93,8 +111,6 @@ def main(
trees = GeneticTrees(trees_dict=geneticTrees, format="newick")
else:

typer.echo(typer.style(titleCard, fg=typer.colors.GREEN))

sequenceFile = utils.loadSequenceFile(Params.reference_gene_filepath)
align_sequence = AlignSequences(sequenceFile)

Expand All @@ -115,14 +131,15 @@ def main(
climatic_data = pd.read_csv(Params.file_name)
climaticTrees = utils.climaticPipeline(climatic_data)

utils.filterResults(climaticTrees, geneticTrees, climatic_data)
filtered_results = utils.filterResults(climaticTrees, geneticTrees, climatic_data)

utils.writeOutputFile(filtered_results, output)

# save results
if alignements is not None:
alignements.save_to_json(f"./results/aligned_{Params.reference_gene_file}.json")

trees.save_trees_to_json("./results/geneticTrees.json")


if __name__ == "__main__":
app()
18 changes: 10 additions & 8 deletions aphylogeo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,19 @@ def getData(leavesName, dist, index, climaticList, bootstrap, genetic, csv_data,
return [reference_gene_filename, climaticList[index], leave, genetic, str(bootstrap), str(round(dist, 2))]


def writeOutputFile(data):
def writeOutputFile(data, output_file):
"""
Write the datas from data list into a new csv file

:param data: The list containing the final data.
:param output_file: Output csv filepath for the results.
:type data: list
:type output_file: String
"""
print("Writing the output file")
directory = os.path.abspath("./results")
os.makedirs(directory, exist_ok=True)
with open("./results/output.csv", "w", encoding="UTF8") as f:
with open(output_file, "w", encoding="UTF8") as f:
writer = csv_writer(f)
writer.writerow(header())
for i in range(len(data)):
Expand All @@ -498,8 +500,8 @@ def filterResults(
:param create_file: Whether to create a file or not. Default is True.
:type create_file: bool

:return: The final data in a dictionary format.
:rtype: dict
:return: The final data in a list format.
:rtype: list
"""

# Create a list of the tree if the bootstrap is superior to the
Expand Down Expand Up @@ -606,10 +608,10 @@ def filterResults(
else:
raise ValueError("Invalid distance method")

if create_file:
# We write the datas into an output csv file
writeOutputFile(data)
return format_to_csv(data)
# if create_file:
# # We write the datas into an output csv file
# writeOutputFile(data, Params.output_file)
return data


def format_to_csv(data):
Expand Down