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

fix: catch when FORMAT field is present in truth #114

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

Conversation

famosab
Copy link
Collaborator

@famosab famosab commented Feb 25, 2025

stratify_truth currently fails with:

Warning: different number of samples: 1 vs 2
Error: Invalid record in file /dev/fd/63. Record is 
1       3387427 .       A       <DEL>   .       PASS    MTD=bwa_gatk3   GT      1|0     0/1 

This fix might work - but maybe a second rule to check for the GT field first is needed.

Summary by CodeRabbit

  • New Features

    • Added a new rule to check for the presence of FORMAT fields in BCF files, enhancing variant processing capabilities.
    • Introduced new functions for conditional command generation based on FORMAT field presence, improving workflow flexibility.
    • Created a new resource file containing reference data.
  • Chores

    • Updated input parameters for existing rules to streamline command execution and enhance clarity.

Copy link
Contributor

coderabbitai bot commented Feb 25, 2025

Walkthrough

A new file (truth.txt) containing the text "truth" has been added. Additionally, the add_format_field rule in workflow/rules/eval.smk has been modified to use named input parameters, and the shell command has been simplified to utilize a new parameterized command. Furthermore, a new rule named has_format_field has been introduced in workflow/rules/download.smk to check for the presence of a FORMAT field in a BCF file. Two new functions have also been added to support these changes.

Changes

File Path Change Summary
workflow/resources/truth.txt Created a new file containing a single line with the text "truth".
workflow/rules/eval.smk Updated input parameters to use named parameters (bcf, truth_name, format_field_file), and simplified the shell command to use format_field_cmd.
workflow/rules/common.smk Added two new functions: has_format_field to check for a specific format field, and get_format_field_command to generate command strings based on the format field's presence.
workflow/rules/download.smk Introduced a new rule has_format_field to check if the input BCF file contains a FORMAT field, outputting the result to a text file.

Sequence Diagram(s)

sequenceDiagram
    participant Rule as add_format_field Rule
    participant VCF as vcf-genotype-annotator
    participant Fallback as bcftools reheader & conversion

    Rule->>VCF: Execute vcf-genotype-annotator
    alt Success
        VCF-->>Rule: Return VCF with added FORMAT field
    else Failure
        VCF-->>Rule: Error (GT field exists)
        Rule->>Fallback: Execute bcftools reheader & convert BCF to VCF using truth.txt
        Fallback-->>Rule: Return modified VCF
    end
Loading

Poem

I'm a rabbit on a merry quest,
Hopping through code with joyful zest,
New truth unfurls in a little file,
And smart commands now go the extra mile.
In workflows neat, our hops are true—
For every change, I celebrate anew!
🐇🌟


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: 3

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e89ed7d and 23d70c0.

📒 Files selected for processing (3)
  • workflow/rules/common.smk (1 hunks)
  • workflow/rules/download.smk (1 hunks)
  • workflow/rules/eval.smk (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Formatting
🔇 Additional comments (2)
workflow/rules/download.smk (1)

81-97: Good addition of a new rule to check for FORMAT field

The new has_format_field rule is a well-structured solution to detect FORMAT fields in BCF files. This addresses the core issue mentioned in the PR objectives, providing a reliable way to check if a FORMAT field exists in the truth data.

The implementation using bcftools view -h | grep -q FORMAT is clean and concise, writing "true" or "false" to the output file accordingly.

workflow/rules/eval.smk (1)

65-76: Good refactoring of the add_format_field rule

The changes to this rule improve the structure and flexibility of how the FORMAT field is handled. The use of named input parameters makes the code more maintainable, and the integration with the new functions provides a cleaner solution to the FORMAT field issue.

The use of format_field_cmd parameterization allows for dynamic handling based on the presence of the FORMAT field, which directly addresses the problem described in the PR objectives.

Comment on lines 285 to 293
def has_format_field(wildcards):
file_path = input.format_field_file
with open(file_path, "r") as infile:
content = infile.read().strip()
if content == "true":
return True
else:
return False

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix input parameter handling in has_format_field function

The function assumes input.format_field_file is defined, but this variable isn't passed as a parameter to the function. This could cause runtime errors when the function is called outside the context where input is defined.

Apply this fix to properly handle the input parameter:

-def has_format_field(wildcards):
-    file_path = input.format_field_file
+def has_format_field(wildcards, input=None):
+    file_path = input.format_field_file if input else f"resources/variants/{wildcards.genome}/has-format-field.txt"
     with open(file_path, "r") as infile:
         content = infile.read().strip()
         if content == "true":
             return True
         else:
             return False
📝 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
def has_format_field(wildcards):
file_path = input.format_field_file
with open(file_path, "r") as infile:
content = infile.read().strip()
if content == "true":
return True
else:
return False
def has_format_field(wildcards, input=None):
file_path = input.format_field_file if input else f"resources/variants/{wildcards.genome}/has-format-field.txt"
with open(file_path, "r") as infile:
content = infile.read().strip()
if content == "true":
return True
else:
return False

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

♻️ Duplicate comments (2)
workflow/rules/common.smk (2)

285-292: ⚠️ Potential issue

Fix input parameter handling in has_format_field function

The function assumes input.format_field_file is defined, but this variable might not be available when the function is called outside the context where input is defined.

Apply this fix to properly handle the input parameter:

-def has_format_field(wildcards, input):
-    file_path = input.format_field_file
+def has_format_field(wildcards, input=None):
+    if input is None or not hasattr(input, "format_field_file"):
+        file_path = f"resources/variants/{wildcards.genome}/has-format-field.txt"
+    else:
+        file_path = input.format_field_file
     with open(file_path, "r") as infile:
         content = infile.read().strip()
         if content == "true":
             return True
         else:
             return False

295-302: ⚠️ Potential issue

Fix parameter handling in get_format_field_command function

The function assumes params.format_field is defined, but this might not be available in all contexts where the function is called.

Apply this fix to handle cases where params might not be defined:

-def get_format_field_command(wildcards, params):
+def get_format_field_command(wildcards, params=None):
+    has_format = params.format_field if params and hasattr(params, "format_field") else has_format_field(wildcards)
     if params.format_field:
         # bcftools reheader changes sample name which is associated with GT field to truth
         return "bcftools reheader -s {input.truth_name} {input.bcf} | bcftools view -Oz > {output}"
     else:
         # bcftools convert makes sure that input for vcf-genotype-annotator is in vcf format
         # adds FORMAT field with GT field and sample name 'truth'
         return "vcf-genotype-annotator <(bcftools convert -Ov {input.bcf}) truth 0/1 -o {output} &> {log}"
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 66c7910 and b5e1395.

📒 Files selected for processing (2)
  • workflow/rules/common.smk (1 hunks)
  • workflow/rules/eval.smk (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Formatting
🔇 Additional comments (2)
workflow/rules/eval.smk (2)

65-69: Good improvement: Named inputs for clarity

The change to use named inputs (bcf, truth_name, format_field_file) instead of a single string input makes the code more readable and maintainable.


79-79: Parameter reference is correct, but dependent on function fixes

The shell command now correctly uses {params.format_field_cmd} instead of hardcoded logic, which is a good improvement. However, the function generating this parameter needs to be fixed as noted in the common.smk review.

Comment on lines +73 to 76
params:
format_field=has_format_field,
format_field_cmd=get_format_field_command,
conda:
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

Check that function parameter handling is fixed

The added parameters properly connect to the new functions, but make sure the issues with parameter handling in those functions are fixed first, as noted in the common.smk review.

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.

1 participant