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: add vaf calculation for strelka results #109

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

Conversation

famosab
Copy link
Collaborator

@famosab famosab commented Jan 9, 2025

Closes #100

This is a work in progress. The following steps still have to be solved:

  • add calculated VAF to investigated VCF
  • write function to determine whether VAF needs to be calculated or not and only execute rule when this resolves to true
  • set the vaf_status flag correctly for files that have calculated VAF

Summary by CodeRabbit

Release Notes

  • New Features

    • Added functionality to calculate Variant Allele Frequency (VAF) for SNVs and INDELs.
    • Introduced new workflow rule for VAF calculation.
  • Configuration Updates

    • Updated Conda environment configuration for bioinformatics package support.
    • Modified .gitignore to exclude test and report directories.
  • Workflow Improvements

    • Enhanced VAF status determination in workflow rules.
    • Added new script for VAF calculation processing.

Copy link

coderabbitai bot commented Jan 9, 2025

Walkthrough

The pull request introduces functionality for calculating Variant Allele Frequency (VAF) in a bioinformatics workflow. A new Conda environment for cyvcf2 is created, and modifications are made to the workflow rules and scripts to support VAF calculation. The changes include adding a new rule to process variant files, implementing functions to calculate VAF for SNVs and INDELs, and updating the VAF status checking logic.

Changes

File Change Summary
.gitignore Added entries for test/** and report/** directories
workflow/envs/cyvcf.yaml New Conda environment configuration with cyvcf2 package
workflow/rules/common.smk Updated get_vaf_status function to handle "tbc" VAF benchmark
workflow/rules/eval.smk Added new calculate_vaf rule for processing variant files
workflow/scripts/calc-vaf.py Added get_snv_allele_freq and get_indel_allele_freq functions

Sequence Diagram

sequenceDiagram
    participant Input as Filtered Variants
    participant VAFCalc as VAF Calculation Script
    participant Output as VAF-Added Variants
    
    Input->>VAFCalc: Process BCF file
    VAFCalc->>VAFCalc: Calculate SNV VAF
    VAFCalc->>VAFCalc: Calculate INDEL VAF
    VAFCalc->>Output: Generate annotated BCF
Loading

Assessment against linked issues

Objective Addressed Explanation
Calculate VAF for callers without VAF reporting [#100]

Poem

🐰 In the realm of variants wild and free,
VAF calculations dance with glee
SNVs and INDELs, now understood
Our benchmark's power, forever stood!
Hopping through data with scientific might 🧬


📜 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 4bc016d and e78b133.

📒 Files selected for processing (1)
  • workflow/rules/eval.smk (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • workflow/rules/eval.smk

Finishing Touches

  • 📝 Generate Docstrings

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

@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: 2

🧹 Nitpick comments (4)
workflow/scripts/calc-vaf.py (1)

3-4: Remove hardcoded file paths from comments.

The commented file paths appear to be local development paths. Remove them to avoid confusion.

-#vcf = "/Users/famke/01-pm4onco/osf-download/pipeline-results-of-imgag-data/qbic/strelka/tumor_5perc_vs_normal_5perc.strelka.somatic_snvs_VEP.ann.vcf" #snakemake.input
-#indel = "/Users/famke/01-pm4onco/osf-download/pipeline-results-of-imgag-data/qbic/strelka/tumor_5perc_vs_normal_5perc.strelka.somatic_indels_VEP.ann.vcf.gz"
workflow/rules/eval.smk (1)

94-105: Enhance the calculate_vaf rule configuration.

The rule needs some improvements:

  1. Add resources section for memory management
  2. Fix the log path to include {wildcards.callset}
  3. Add benchmark section for performance tracking

Here's a suggested implementation:

 rule calculate_vaf:
     input:
         "results/filtered-variants/{callset}.bcf"
     output:
         "results/calculate-vaf/{callset}.added-vaf.bcf"
     log:
-        "logs/calculate-vaf/"
+        "logs/calculate-vaf/{callset}.log"
+    benchmark:
+        "benchmarks/calculate-vaf/{callset}.tsv"
+    resources:
+        mem_mb=4000
     conda:
         "../envs/cyvcf.yaml"
     script:
         "../scripts/calc-vaf.py"
workflow/rules/common.smk (1)

458-459: Document the "tbc" condition in get_vaf_status.

Add a comment explaining what "tbc" means and why it triggers a True return value.

     if vaf_benchmark is None:
         return False
+    # Return True for "tbc" (to be calculated) to enable VAF calculation
+    # for variants that don't have pre-calculated VAF values
     if vaf_benchmark == "tbc":
         return True
workflow/envs/cyvcf.yaml (1)

1-5: Enhance the conda environment configuration.

The configuration needs improvements:

  1. Pin the cyvcf2 version for reproducibility
  2. Add a newline at the end of file

Here's a suggested implementation:

 channels:
   - conda-forge
   - bioconda
 dependencies:
-  - cyvcf2
+  - cyvcf2=0.30.22
+
🧰 Tools
🪛 yamllint (1.35.1)

[error] 5-5: no new line character at the end of file

(new-line-at-end-of-file)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0464700 and 4bc016d.

📒 Files selected for processing (5)
  • .gitignore (1 hunks)
  • workflow/envs/cyvcf.yaml (1 hunks)
  • workflow/rules/common.smk (1 hunks)
  • workflow/rules/eval.smk (1 hunks)
  • workflow/scripts/calc-vaf.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .gitignore
🧰 Additional context used
🪛 yamllint (1.35.1)
workflow/envs/cyvcf.yaml

[error] 5-5: no new line character at the end of file

(new-line-at-end-of-file)

Comment on lines +29 to +36
def get_indel_allele_freq(vcf):
for variant in VCF(vcf):
tier1RefCounts = variant.format("TAR")[0,0]
tier1AltCounts = variant.format("TIR")[0,0]

vaf = tier1AltCounts / (tier1AltCounts + tier1RefCounts)

print(vaf)
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Apply similar improvements to INDEL VAF calculation.

The INDEL function needs the same enhancements as the SNV function.

Here's a suggested implementation:

 def get_indel_allele_freq(vcf):
+    """Calculate Variant Allele Frequency (VAF) for INDELs.
+    
+    Args:
+        vcf: Path to the VCF file containing INDEL variants
+        
+    Returns:
+        List of tuples containing (variant_id, vaf)
+    """
+    results = []
     for variant in VCF(vcf):
         tier1RefCounts = variant.format("TAR")[0,0]
         tier1AltCounts = variant.format("TIR")[0,0]
 
-        vaf = tier1AltCounts / (tier1AltCounts + tier1RefCounts)
+        total_counts = tier1AltCounts + tier1RefCounts
+        if total_counts == 0:
+            vaf = 0.0
+        else:
+            vaf = tier1AltCounts / total_counts
 
-        print(vaf)
+        results.append((variant.ID or f"{variant.CHROM}:{variant.POS}", vaf))
+    return results
📝 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 get_indel_allele_freq(vcf):
for variant in VCF(vcf):
tier1RefCounts = variant.format("TAR")[0,0]
tier1AltCounts = variant.format("TIR")[0,0]
vaf = tier1AltCounts / (tier1AltCounts + tier1RefCounts)
print(vaf)
def get_indel_allele_freq(vcf):
"""Calculate Variant Allele Frequency (VAF) for INDELs.
Args:
vcf: Path to the VCF file containing INDEL variants
Returns:
List of tuples containing (variant_id, vaf)
"""
results = []
for variant in VCF(vcf):
tier1RefCounts = variant.format("TAR")[0,0]
tier1AltCounts = variant.format("TIR")[0,0]
total_counts = tier1AltCounts + tier1RefCounts
if total_counts == 0:
vaf = 0.0
else:
vaf = tier1AltCounts / total_counts
results.append((variant.ID or f"{variant.CHROM}:{variant.POS}", vaf))
return results

Comment on lines +10 to +21
def get_snv_allele_freq(vcf):
for variant in VCF(vcf):
refCounts = variant.format(variant.REF + "U")
altCounts = variant.format(variant.ALT[0] + "U")

# TODO: check which value is the correct one from the matrix (this leads to many zero VAF)
tier1RefCounts = refCounts[0, 0]
tier1AltCounts = altCounts[0, 0]

vaf = tier1AltCounts / (tier1AltCounts + tier1RefCounts)

print(vaf)
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance the SNV VAF calculation implementation.

The current implementation needs improvements:

  1. Add error handling for division by zero
  2. Add docstring explaining the function
  3. Store results instead of just printing
  4. Validate the matrix values as noted in TODO

Here's a suggested implementation:

 def get_snv_allele_freq(vcf):
+    """Calculate Variant Allele Frequency (VAF) for SNVs.
+    
+    Args:
+        vcf: Path to the VCF file containing SNV variants
+        
+    Returns:
+        List of tuples containing (variant_id, vaf)
+    """
+    results = []
     for variant in VCF(vcf):
         refCounts = variant.format(variant.REF + "U")
         altCounts = variant.format(variant.ALT[0] + "U")
 
-        # TODO: check which value is the correct one from the matrix (this leads to many zero VAF)
         tier1RefCounts = refCounts[0, 0]
         tier1AltCounts = altCounts[0, 0]
 
-        vaf = tier1AltCounts / (tier1AltCounts + tier1RefCounts)
+        total_counts = tier1AltCounts + tier1RefCounts
+        if total_counts == 0:
+            vaf = 0.0
+        else:
+            vaf = tier1AltCounts / total_counts
 
-        print(vaf)
+        results.append((variant.ID or f"{variant.CHROM}:{variant.POS}", vaf))
+    return results
📝 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 get_snv_allele_freq(vcf):
for variant in VCF(vcf):
refCounts = variant.format(variant.REF + "U")
altCounts = variant.format(variant.ALT[0] + "U")
# TODO: check which value is the correct one from the matrix (this leads to many zero VAF)
tier1RefCounts = refCounts[0, 0]
tier1AltCounts = altCounts[0, 0]
vaf = tier1AltCounts / (tier1AltCounts + tier1RefCounts)
print(vaf)
def get_snv_allele_freq(vcf):
"""Calculate Variant Allele Frequency (VAF) for SNVs.
Args:
vcf: Path to the VCF file containing SNV variants
Returns:
List of tuples containing (variant_id, vaf)
"""
results = []
for variant in VCF(vcf):
refCounts = variant.format(variant.REF + "U")
altCounts = variant.format(variant.ALT[0] + "U")
tier1RefCounts = refCounts[0, 0]
tier1AltCounts = altCounts[0, 0]
total_counts = tier1AltCounts + tier1RefCounts
if total_counts == 0:
vaf = 0.0
else:
vaf = tier1AltCounts / total_counts
results.append((variant.ID or f"{variant.CHROM}:{variant.POS}", vaf))
return results

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.

Somatic Benchmark: Calculate VAF for callers that do not report VAF
1 participant