phylogenie 2.1.28__py3-none-any.whl → 2.1.29__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of phylogenie might be problematic. Click here for more details.

phylogenie/io/fasta.py CHANGED
@@ -1,10 +1,11 @@
1
+ from pathlib import Path
1
2
  from typing import Callable
2
3
 
3
4
  from phylogenie.msa import MSA, Sequence
4
5
 
5
6
 
6
7
  def load_fasta(
7
- fasta_file: str, extract_time_from_id: Callable[[str], float] | None = None
8
+ fasta_file: str | Path, extract_time_from_id: Callable[[str], float] | None = None
8
9
  ) -> MSA:
9
10
  sequences: list[Sequence] = []
10
11
  with open(fasta_file, "r") as f:
phylogenie/io/newick.py CHANGED
@@ -1,4 +1,5 @@
1
1
  import re
2
+ from pathlib import Path
2
3
 
3
4
  from phylogenie.tree import Tree
4
5
 
@@ -66,7 +67,7 @@ def parse_newick(newick: str, translations: dict[str, str] | None = None) -> Tre
66
67
  i += 1
67
68
 
68
69
 
69
- def load_newick(filepath: str) -> Tree | list[Tree]:
70
+ def load_newick(filepath: str | Path) -> Tree | list[Tree]:
70
71
  with open(filepath, "r") as file:
71
72
  trees = [parse_newick(newick) for newick in file]
72
73
  return trees[0] if len(trees) == 1 else trees
@@ -95,7 +96,7 @@ def to_newick(tree: Tree) -> str:
95
96
  return newick
96
97
 
97
98
 
98
- def dump_newick(trees: Tree | list[Tree], filepath: str) -> None:
99
+ def dump_newick(trees: Tree | list[Tree], filepath: str | Path) -> None:
99
100
  if isinstance(trees, Tree):
100
101
  trees = [trees]
101
102
  with open(filepath, "w") as file:
phylogenie/io/nexus.py CHANGED
@@ -1,5 +1,6 @@
1
1
  import re
2
2
  from collections.abc import Iterator
3
+ from pathlib import Path
3
4
 
4
5
  from phylogenie.io.newick import parse_newick
5
6
  from phylogenie.tree import Tree
@@ -35,7 +36,7 @@ def _parse_trees_block(lines: Iterator[str]) -> dict[str, Tree]:
35
36
  return trees
36
37
 
37
38
 
38
- def load_nexus(nexus_file: str) -> dict[str, Tree]:
39
+ def load_nexus(nexus_file: str | Path) -> dict[str, Tree]:
39
40
  with open(nexus_file, "r") as f:
40
41
  for line in f:
41
42
  if line.strip().upper() == "BEGIN TREES;":
@@ -1,6 +1,6 @@
1
- import os
2
1
  import time
3
2
  from collections.abc import Iterable, Sequence
3
+ from pathlib import Path
4
4
  from typing import Any
5
5
 
6
6
  import joblib
@@ -106,7 +106,7 @@ def simulate_tree(
106
106
 
107
107
 
108
108
  def generate_trees(
109
- output_dir: str,
109
+ output_dir: str | Path,
110
110
  n_trees: int,
111
111
  events: Sequence[Event],
112
112
  min_tips: int = 1,
@@ -119,7 +119,13 @@ def generate_trees(
119
119
  n_jobs: int = -1,
120
120
  timeout: float = np.inf,
121
121
  ) -> pd.DataFrame:
122
- def _simulate_tree(seed: int) -> tuple[Tree, dict[str, Any]]:
122
+ if isinstance(output_dir, str):
123
+ output_dir = Path(output_dir)
124
+ if output_dir.exists():
125
+ raise FileExistsError(f"Output directory {output_dir} already exists")
126
+ output_dir.mkdir(parents=True)
127
+
128
+ def _simulate_tree(i: int, seed: int) -> dict[str, Any]:
123
129
  while True:
124
130
  try:
125
131
  tree, metadata = simulate_tree(
@@ -132,27 +138,21 @@ def generate_trees(
132
138
  seed=seed,
133
139
  timeout=timeout,
134
140
  )
141
+ metadata["file_id"] = i
135
142
  if node_features is not None:
136
143
  set_features(tree, node_features)
137
- return (tree, metadata)
144
+ dump_newick(tree, output_dir / f"{i}.nwk")
145
+ return metadata
138
146
  except TimeoutError:
139
147
  print("Simulation timed out, retrying with a different seed...")
140
148
  seed += 1
141
149
 
142
- if os.path.exists(output_dir):
143
- raise FileExistsError(f"Output directory {output_dir} already exists")
144
- os.makedirs(output_dir)
145
-
146
150
  rng = default_rng(seed)
147
151
  jobs = joblib.Parallel(n_jobs=n_jobs, return_as="generator_unordered")(
148
- joblib.delayed(_simulate_tree)(seed=int(rng.integers(2**32)))
149
- for _ in range(n_trees)
152
+ joblib.delayed(_simulate_tree)(i=i, seed=int(rng.integers(2**32)))
153
+ for i in range(n_trees)
150
154
  )
151
155
 
152
- df: list[dict[str, Any]] = []
153
- for i, (tree, metadata) in tqdm(
154
- enumerate(jobs), total=n_trees, desc=f"Generating trees in {output_dir}..."
155
- ):
156
- df.append({"file_id": i} | metadata)
157
- dump_newick(tree, os.path.join(output_dir, f"{i}.nwk"))
158
- return pd.DataFrame(df)
156
+ return pd.DataFrame(
157
+ [md for md in tqdm(jobs, f"Generating trees in {output_dir}...", n_trees)]
158
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: phylogenie
3
- Version: 2.1.28
3
+ Version: 2.1.29
4
4
  Summary: Generate phylogenetic datasets with minimal setup effort
5
5
  Author: Gabriele Marino
6
6
  Author-email: gabmarino.8601@gmail.com
@@ -8,9 +8,9 @@ phylogenie/generators/factories.py,sha256=qSz-s7ltQwUf7R6TPZ-5xFyOnnSHkKn0u5Iuke
8
8
  phylogenie/generators/trees.py,sha256=zsmL3_YUUwtn-_NrGSvwZVSGIm9kpf8SPHxoQCIT_hE,10168
9
9
  phylogenie/generators/typeguards.py,sha256=yj4VkhOaUXJ2OrY-6zhOeY9C4yKIQxjZtk2d-vIxttQ,828
10
10
  phylogenie/io/__init__.py,sha256=gtRYtDdZSTlWCj3I4vmMJSAs93jdz5RySkCakD3sxlQ,214
11
- phylogenie/io/fasta.py,sha256=IWtNb_RQLR6kvS0G826wB9SodkCGfugddoUHx78Yrec,837
12
- phylogenie/io/newick.py,sha256=vw0fafh1LL0SXQifIIa1TQ7g5KgTCIAX6vzq-bUOrKE,3396
13
- phylogenie/io/nexus.py,sha256=IKbV8lJ_Q053iYJ7JzVQPCUqSkSfmiRpUchFTrLHZuE,1551
11
+ phylogenie/io/fasta.py,sha256=CUFO06m7wClprarsMheZojM4Os2NQf3ALYUXSWzfNL0,869
12
+ phylogenie/io/newick.py,sha256=4j7m9B11erVJijye0GfpbWomk2ttlBXdyHNoBc_fvIg,3435
13
+ phylogenie/io/nexus.py,sha256=KGf0rH8sAVAuPnmhokj2XbQTBsiuhPujJflRixUunGY,1583
14
14
  phylogenie/main.py,sha256=vtvSpQxBNlYABoFQ25czl-l3fIr4QRo3svWVd-jcArw,1170
15
15
  phylogenie/models.py,sha256=pCg9ob0RpLUHwM49x4knKxL4FNPr3-EU_6zMXsvxtAg,370
16
16
  phylogenie/msa.py,sha256=JDGyZUsAq6-m-SQjoCDjAkAZIxfgyl_PDIhdYn5HOow,2064
@@ -27,13 +27,13 @@ phylogenie/treesimulator/events/contact_tracing.py,sha256=QqinLfQmBBi6qF3nFE_Qsi
27
27
  phylogenie/treesimulator/events/core.py,sha256=bhgQgi5L-oaHsoWJmUOsTTzWxi0POYxVLoF-KrC8AGQ,8179
28
28
  phylogenie/treesimulator/events/mutations.py,sha256=fGxxeXhiFrMAaS-s_vd42aVBZ6XQiRjpW9uJUATNk1E,3752
29
29
  phylogenie/treesimulator/features.py,sha256=nNfv0kPudAZnrnufIVuFCR_VEJSP-6fZ2L-VG5_-Fqo,1366
30
- phylogenie/treesimulator/gillespie.py,sha256=_O1TqK-LOjH6k8ZSos-CbVQ7wJZPzf1Awv_1DXt0NYE,5730
30
+ phylogenie/treesimulator/gillespie.py,sha256=3xPeqkSitP71Qa2a8d8vMkdV4CCdLy1rRCdDsNctAug,5732
31
31
  phylogenie/treesimulator/model.py,sha256=lhDwmBFQ8Qh8qVGZPgED0vehtPC3DE7_CgCV_8rPB-A,4641
32
32
  phylogenie/typeguards.py,sha256=JtqmbEWJZBRHbWgCvcl6nrWm3VcBfzRbklbTBYHItn0,1325
33
33
  phylogenie/typings.py,sha256=p694PBe_tk25A6N8vGGWxuqoDtt3nHFUsIYJrwR_76Y,494
34
34
  phylogenie/utils.py,sha256=ehVk_2kvjW8Q_EyM2kxBPHYiK-KlPmZQx7JeVN6Fh-E,5419
35
- phylogenie-2.1.28.dist-info/LICENSE.txt,sha256=NUrDqElK-eD3I0WqC004CJsy6cs0JgsAoebDv_42-pw,1071
36
- phylogenie-2.1.28.dist-info/METADATA,sha256=tC9r3osM37OauXzzqD0P3K1-HsOIQjYx7_tWiUfn6i4,5477
37
- phylogenie-2.1.28.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
38
- phylogenie-2.1.28.dist-info/entry_points.txt,sha256=Rt6_usN0FkBX1ZfiqCirjMN9FKOgFLG8rydcQ8kugeE,51
39
- phylogenie-2.1.28.dist-info/RECORD,,
35
+ phylogenie-2.1.29.dist-info/LICENSE.txt,sha256=NUrDqElK-eD3I0WqC004CJsy6cs0JgsAoebDv_42-pw,1071
36
+ phylogenie-2.1.29.dist-info/METADATA,sha256=mVsGaofekWG_o0Mu2CqYu7RZsEL7MKsY3m5w7wv8hAw,5477
37
+ phylogenie-2.1.29.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
38
+ phylogenie-2.1.29.dist-info/entry_points.txt,sha256=Rt6_usN0FkBX1ZfiqCirjMN9FKOgFLG8rydcQ8kugeE,51
39
+ phylogenie-2.1.29.dist-info/RECORD,,