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

feat: overloaded 'SeqDict.from_sam()' method to support SAM files (#176) #183

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

TimD1
Copy link
Contributor

@TimD1 TimD1 commented Sep 4, 2024

  • in addition to pysam SAM headers and SAM header dicts, SeqDicts can now be initialized from SAM file paths and pysam SAM files
  • added basic tests for this new functionality

@TimD1 TimD1 requested review from nh13 and tfenne as code owners September 4, 2024 15:52
Copy link

codecov bot commented Sep 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.78%. Comparing base (b0b4227) to head (c69320f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #183      +/-   ##
==========================================
- Coverage   91.06%   90.78%   -0.28%     
==========================================
  Files          18       18              
  Lines        2283     2301      +18     
  Branches      337      339       +2     
==========================================
+ Hits         2079     2089      +10     
- Misses        133      142       +9     
+ Partials       71       70       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TimD1 TimD1 force-pushed the 176_td_add-read-seq-dict branch from 2f19451 to bdf1284 Compare September 4, 2024 17:19
fgpyo/fasta/sequence_dictionary.py Outdated Show resolved Hide resolved
fgpyo/fasta/sequence_dictionary.py Outdated Show resolved Hide resolved
fgpyo/fasta/sequence_dictionary.py Outdated Show resolved Hide resolved
tests/fgpyo/fasta/data/sequence.dict Outdated Show resolved Hide resolved
tests/fgpyo/fasta/test_sequence_dictionary.py Outdated Show resolved Hide resolved
@TimD1 TimD1 force-pushed the 176_td_add-read-seq-dict branch from bdf1284 to 45f7eca Compare January 14, 2025 19:48
@TimD1 TimD1 requested a review from clintval as a code owner January 14, 2025 19:48
Copy link
Contributor

coderabbitai bot commented Jan 14, 2025

Walkthrough

The pull request introduces changes to the SequenceDictionary class in sequence_dictionary.py, specifically modifying the from_sam method. The method's signature is updated to accept a wider array of input types, including Path, pysam.AlignmentFile, pysam.AlignmentHeader, and lists of dictionaries, enhancing its versatility for creating sequence dictionaries from various SAM file sources.

Additionally, the pyproject.toml file is updated with a new coverage reporting section that specifies exclusion rules for certain line types. The test file test_sequence_dictionary.py is also revised to validate the new input handling capabilities, incorporating additional assertions to ensure the correct functionality of the from_sam method with SAM file operations.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 45f7eca and c69320f.

📒 Files selected for processing (1)
  • tests/fgpyo/fasta/test_sequence_dictionary.py (3 hunks)
🔇 Additional comments (3)
tests/fgpyo/fasta/test_sequence_dictionary.py (3)

15-16: LGTM!

Clean import additions.


335-335: Add .sam extension to the test file.

Missing file extension makes the purpose unclear.

-    samfile: Path = builder.SamBuilder(sd=mapping).to_path()
+    samfile: Path = builder.SamBuilder(sd=mapping).to_path().with_suffix('.sam')

335-343: LGTM! Comprehensive test coverage.

Tests all input types and error cases.

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b0b4227 and 45f7eca.

📒 Files selected for processing (3)
  • fgpyo/fasta/sequence_dictionary.py (4 hunks)
  • pyproject.toml (1 hunks)
  • tests/fgpyo/fasta/test_sequence_dictionary.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: testing (3.10)
  • GitHub Check: testing (3.9)
🔇 Additional comments (3)
fgpyo/fasta/sequence_dictionary.py (1)

481-499: from_sam method implementation effectively handles multiple input types.

The use of an if-elif-else chain improves readability and accommodates various data inputs as specified.

tests/fgpyo/fasta/test_sequence_dictionary.py (1)

335-338: Test cases validate new from_sam functionality.

The added tests ensure that the method correctly processes Path and pysam.AlignmentFile inputs.

pyproject.toml (1)

92-96: Excluding @overload annotations from coverage improves report accuracy.

Adjusting the coverage configuration aligns with best practices for handling type stubs.

Comment on lines +496 to +497
except Exception as e:
raise ValueError(f"Could not parse sequence information from data: {data}") from e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid including raw data in exception messages.

Including the entire data object in the error message may lead to verbose logs or expose sensitive information. Consider providing a more general error message.

Apply this diff to adjust the error message:

-except Exception as e:
-    raise ValueError(f"Could not parse sequence information from data: {data}") from e
+except Exception as e:
+    raise ValueError("Could not parse sequence information from provided data.") from e
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
except Exception as e:
raise ValueError(f"Could not parse sequence information from data: {data}") from e
except Exception as e:
raise ValueError("Could not parse sequence information from provided data.") from e

@TimD1 TimD1 requested a review from msto January 14, 2025 20:06
Copy link
Member

@nh13 nh13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking really good, nice work! Could you fix up the one TODO and the few other very minor things?

A `SequenceDictionary` mapping refrence names to their metadata.
"""
seq_dict: SequenceDictionary
if isinstance(data, pysam.AlignmentHeader):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is begging for a structural pattern (class pattern) but I think our min python version is 3.9 :/


return seq_dict

# TODO: mypy doesn't like these
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME

@@ -88,3 +88,9 @@ warn_unused_configs = true
warn_unused_ignores = true
enable_error_code = "ignore-without-code"
exclude = ["site/", "docs/"]

[tool.coverage.report]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@@ -333,10 +332,15 @@ def test_sequence_dictionary_to_and_from_sam() -> None:
header = pysam.AlignmentHeader.from_dict(
header_dict={"HD": {"VN": "1.5"}, "SQ": mapping, "RG": [{"ID": "foo"}]}
)

samfile: Path = builder.SamBuilder(sd=mapping).to_path()
alignment: pysam.AlignmentFile = reader(samfile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use with a with statement so it automatically closes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a class/module method to read in a .dict file to a SequenceDictionary
4 participants