Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndsimons
    Junior Member
    • Dec 2022
    • 2

    #1

    count reads spanning multiple variants from BAM file

    Hello, I have a BAM and a phased VCF for a sample and my goal is to count how many individual reads (not one variant covered by a read and other variant covered by mate pair, but both variants covered by an individual read) confirm the phased genotypes for a pair of variant sites.

    For example, if I have two sites in a phase block at coordinates chr11:128839353 and chr11:128839368, with genotypes 0|1 and 0|1 respectively, I want to know how many individual reads that have the reference allele at the first site also have the reference allele at the second site.

    Any help is much appreciated!
  • ndsimons
    Junior Member
    • Dec 2022
    • 2

    #2
    For posterity:
    import argparse
    import pysam
    import os

    # Define the command-line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("bam_file", help="Path to the BAM file")
    parser.add_argument("nucleotide1", help="The nucleotide to count at position 1 (e.g., T)")
    parser.add_argument("pos1", help="Position 1 (e.g., chr11:128839353)")
    parser.add_argument("nucleotide2", help="The nucleotide to count at position 2 (e.g., C)")
    parser.add_argument("pos2", help="Position 2 (e.g., chr11:128839368)")
    args = parser.parse_args()

    # Split the positions into chromosome and position
    pos1_chrom, pos1_pos = args.pos1.split(":")
    pos2_chrom, pos2_pos = args.pos2.split(":")

    # Convert the position values to integers
    pos1_pos = int(pos1_pos)
    pos2_pos = int(pos2_pos)

    # Open the BAM file
    bam_file = pysam.AlignmentFile(args.bam_file, "rb")

    # Initialize a counter for the number of reads
    count = 0

    # Iterate through the reads in the BAM file
    for read in bam_file.fetch():
    # Check if the read is on the same chromosome as pos1 and pos2
    if read.reference_name == pos1_chrom and read.reference_name == pos2_chrom:
    # Check if the read overlaps with either pos1 or pos2
    if (read.reference_start <= pos1_pos < read.reference_end) or (read.reference_start <= pos2_pos < read.reference_end):
    # Get the positions of the bases in the reference genome that correspond to the bases in the read
    ref_positions = read.get_reference_positions(full_length=True)
    # Initialize a flag for whether the read has the specified nucleotides at both positions
    has_nucleotides_at_both_positions = True
    # Check if the read has the specified nucleotide1 at pos1
    if pos1_pos in ref_positions:
    has_nucleotides_at_both_positions = has_nucleotides_at_both_positions and (read.query_sequence[ref_positions.index(pos1_pos)] == args.nucleotide1)
    else:
    has_nucleotides_at_both_positions = False
    # Check if the read has the specified nucleotide2 at pos2
    if pos2_pos in ref_positions:
    has_nucleotides_at_both_positions = has_nucleotides_at_both_positions and (read.query_sequence[ref_positions.index(pos2_pos)] == args.nucleotide2)
    else:
    has_nucleotides_at_both_positions = False
    # If the read has the specified nucleotides at both positions, increment the counter
    if has_nucleotides_at_both_positions:
    count += 1

    # Print the count
    print(count)

    # get read depth
    read_depth = bam_file.count(pos1_chrom, pos1_pos, pos1_pos+1)

    # print read depth
    print(read_depth)

    # Close the BAM file
    bam_file.close()


    To run:
    python phaseCount.py test.bam C chr11:128839352 C chr11:128839367
    where test.bam is subset to include reads overlapping BOTH sites (can be generate with bedtools intersect -wa -a original.bam -b sites.bed -F 1.0 > test.bam)

    Comment

    Latest Articles

    Collapse

    • SEQadmin2
      Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
      by SEQadmin2



      CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

      Despite this, “CRISPR helped turn genome editing from a specialized technique into
      ...
      07-31-2026, 11:01 AM
    • SEQadmin2
      Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
      by SEQadmin2


      Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

      The systematic characterization of the human proteome has
      ...
      07-20-2026, 11:48 AM

    ad_right_rmr

    Collapse

    News

    Collapse

    Topics Statistics Last Post
    Started by SEQadmin2, 08-06-2026, 07:41 AM
    0 responses
    17 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 08-03-2026, 10:13 AM
    0 responses
    33 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 07-31-2026, 02:55 AM
    0 responses
    42 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 07-24-2026, 12:17 PM
    0 responses
    26 views
    0 reactions
    Last Post SEQadmin2  
    Working...