harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0468harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0468Harder task: Talon module with enhanced alignment identity computation
Goal
Extend the talon module to compute alignment identity with strong performance and robust edge-case handling for very large inputs. The function must be capable of streaming large MD tag strings without constructing intermediate lists, while preserving correctness for a variety of inputs.
What to implement
- A Python module at /app/talon.py that exposes a top-level function:
- def compute_alignment_identity(md_tag: str, seq: str) -> float
- The function must be pure, stateless, and thread-safe.
Behavior
- md_tag handling:
- If md_tag starts with "MD:Z:", use the substring after this prefix as the MD body.
- Otherwise, treat the entire md_tag string as the MD body.
- Parsing goals (via a single pass over the MD body, with O(n) time and O(1) extra space):
- matches_count: sum of all decimal numbers found in the MD body (e.g., "10A3T0T10" -> 10 + 3 + 0 + 10 = 23).
- deletions_in_reference: total number of reference bases deleted, computed as the sum of lengths of all ^<letters> groups (e.g., "^A" -> 1, "^AC" -> 2).
- Alignment length:
- alignment_length = len(seq) + deletions_in_reference
- Identity:
- identity = matches_count / alignment_length
- Return type:
- Always return a float. If the alignment length is 0 (e.g., empty seq and zero deletions), return 0.0.
- Robustness:
- If the MD body cannot be parsed in any meaningful way (e.g., empty string), return 0.0.
- Non-numeric MD content is treated by summing only numeric tokens; non-numeric characters do not contribute to matches_count.
Parsing specifics
- MD body extraction:
- If md_tag starts with "MD:Z:", use the substring after this prefix.
- Otherwise, use md_tag as the MD body.
- Numeric runs:
- Sum all integer literals found in the MD body. For example, "10A3T0T10" yields 23.
- Deletions in reference:
- Detect all occurrences of ^<letters> and add the length of the letter sequence to deletions_in_reference. Use a non-regex, single-pass approach to avoid extra allocations for large inputs.
- Examples: ^A contributes 1; ^AC contributes 2.
Edge cases and constraints
- Empty MD body -> matches_count = 0, deletions = 0 -> identity = 0.0 (unless seq is empty, in which case 0.0 by policy).
- Empty seq and no deletions -> alignment_length = 0 -> return 0.0.
- Very large MD bodies and/or sequences should be handled without excessive memory usage or nested data structures.
- The function should gracefully handle unusual characters in the MD body (ignore non-digit and non-^ letters for counts; only ^<letters> contributes to deletions).
Location and packaging
- Implement the solution in /app/talon.py (module name talon).
- The tests import as TALON (import talon as TALON), so compute_alignment_identity must be defined at the module level and accessible as TALON.compute_alignment_identity.
Assumptions and rationale
- The numerator in tests corresponds to the sum of all numeric runs in the MD body.
- The denominator accounts for deletions in the reference by adding the total number of bases removed (via ^<letters>) to the read length.
- The function is designed to be fast and memory-efficient for large inputs, prioritizing a single-pass approach over regex-based solutions.
What you need to deliver
- A single Python module at /app/talon.py implementing compute_alignment_identity(md_tag: str, seq: str) -> float with the behavior described above.
- Include necessary imports (e.g., typing, no external dependencies) and a clear, comprehensive docstring.
- Ensure the function is importable as TALON.compute_alignment_identity when tests run.
Notes
- The implementation should be self-contained, dependency-free, and performant for large MD tags and long sequences.
- Focus on correctness, clarity, and robust handling of edge cases, while meeting the single-pass, low-memory constraint for parsing.