Skip to content

Commit

Permalink
make names more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
kedhammar committed Jun 14, 2024
1 parent fcc034a commit c67cfb9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
8 changes: 5 additions & 3 deletions anglerfish/anglerfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Alignment,
categorize_matches,
cluster_matches,
parse_reads_alns_from_paf,
map_reads_to_alns,
run_minimap2,
write_demuxedfastq,
)
Expand Down Expand Up @@ -139,12 +139,14 @@ def run_demux(args):
for i in f:
num_fq += 1
num_fq = int(num_fq / 4)
reads_alns: dict[str, list[Alignment]] = parse_reads_alns_from_paf(align_path)
reads_to_alns: dict[str, list[Alignment]] = map_reads_to_alns(align_path)

# Make stats
log.info(f" Searching for adaptor hits in {adaptor_bc_name}")
fragments, singletons, concats, unknowns = categorize_matches(
adaptor_name + "_i5", adaptor_name + "_i7", reads_alns
i5_name=f"{adaptor_name}_i5",
i7_name=f"{adaptor_name}_i7",
reads_to_alns=reads_to_alns,
)
stats = AlignmentStat(adaptor_bc_name)
stats.compute_pafstats(num_fq, fragments, singletons, concats, unknowns)
Expand Down
22 changes: 11 additions & 11 deletions anglerfish/demux/demux.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ def __init__(self, paf_line: str):
self.sample: str | None = None


def parse_reads_alns_from_paf(
def map_reads_to_alns(
paf_path: str, min_qual: int = 1, complex_identifier: bool = False
) -> dict[str, list[Alignment]]:
"""
Parse a .paf file into a dict, mapping reads to their respective alignment objects.
Outputs:
reads_alns = {
reads_to_alns = {
"read1": [
aln_read1_adaptor1_i5,
aln_read1_adaptor1_i7,
Expand All @@ -124,12 +124,12 @@ def parse_reads_alns_from_paf(
}
complex_identifier = False (default)
--> The keys will be on the form "{read}".
--> The keys will be on the form "{read_name}".
complex_identifier = True
--> The keys will be on the form "{read}_{i5_or_i7}_{strand_str}".
--> The keys will be on the form "{read_name}_{i5_or_i7}_{strand_str}".
"""
reads_alns: dict = {}
reads_to_alns: dict = {}
with open(paf_path) as paf:
for paf_line in paf:
try:
Expand All @@ -153,16 +153,16 @@ def parse_reads_alns_from_paf(
log.debug(f"Low quality alignment: {aln.read_name}")
continue

if key in reads_alns.keys():
reads_alns[key].append(aln)
if key in reads_to_alns.keys():
reads_to_alns[key].append(aln)
else:
reads_alns[key] = [aln]
reads_to_alns[key] = [aln]

return reads_alns
return reads_to_alns


def categorize_matches(
i5_name: str, i7_name: str, reads_alns: dict[str, list[Alignment]]
i5_name: str, i7_name: str, reads_to_alns: dict[str, list[Alignment]]
) -> tuple[
dict[str, list[Alignment]],
dict[str, list[Alignment]],
Expand All @@ -183,7 +183,7 @@ def categorize_matches(
singletons = {}
concats = {}
unknowns = {}
for read, alns in reads_alns.items():
for read, alns in reads_to_alns.items():
sorted_alns = []
for i in range(len(alns) - 1):
aln_i: Alignment = alns[i]
Expand Down
4 changes: 2 additions & 2 deletions anglerfish/explore/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pandas as pd

from anglerfish.demux.adaptor import Adaptor, load_adaptors
from anglerfish.demux.demux import Alignment, parse_reads_alns_from_paf, run_minimap2
from anglerfish.demux.demux import Alignment, map_reads_to_alns, run_minimap2
from anglerfish.explore.entropy import calculate_relative_entropy

logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -72,7 +72,7 @@ def run_explore(
adaptors_included = []
for adaptor, aln_path in adaptors_and_aln_paths:
log.info(f"Parsing {adaptor.name}")
reads_alns: dict[str, list[Alignment]] = parse_reads_alns_from_paf(
reads_alns: dict[str, list[Alignment]] = map_reads_to_alns(
aln_path, complex_identifier=True
)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_anglerfish/test_demux/test_demux.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_run_minimap2(fixture):


def test_parse_alns_from_path(fixture):
reads_alns = to_test.parse_reads_alns_from_paf(fixture["paf_single"])
reads_alns = to_test.map_reads_to_alns(fixture["paf_single"])

for read_name, alns in reads_alns.items():
assert read_name == "0ad8bdb6-e009-43c5-95b1-d381e699f983"
Expand Down Expand Up @@ -165,10 +165,10 @@ def test_parse_cs(fixture):
def test_layout_matches(fixture):
i5_name = "truseq_i5"
i7_name = "truseq_i7"
reads_alns = to_test.parse_reads_alns_from_paf(fixture["paf_multiple"])
reads_alns = to_test.map_reads_to_alns(fixture["paf_multiple"])

layout = to_test.categorize_matches(
i5_name=i5_name, i7_name=i7_name, reads_alns=reads_alns
i5_name=i5_name, i7_name=i7_name, reads_to_alns=reads_alns
)
fragments, singletons, concats, unknowns = layout

Expand Down

0 comments on commit c67cfb9

Please sign in to comment.