Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tom.Booker
    Junior Member
    • Oct 2014
    • 2

    #1

    Extracting insert size from .bam using pysam

    Hi there, this is my first time posting. I am fairly new to bioinformatics so I may make silly errors here and there.

    Anyway, I have been trying to extract the mate-pair distances from an indexed bam file of paired end reads. I have been using pysam to iterate through a a bam using the pysam.Samfile().fetch() methods. By manually inspecting my output I know that I have the reads that I am interested in. The problem is that pretty much all the reads extracted using pysam have in incorrect (or possibly missing!) value in the insert size (isize) field which I have confirmed by extracting a few corresponding reads from the sam file. All fields match up but for the overwhelming majority of reads the insert size field have an exact value of 101, which made me think that there could be another value taking the place of isize in the pysam.AlignedRead object.

    Does anyone with experience using pysam know what might be going on?

    Here is the code I've been using, if it helps:

    sam_file = pysam.Samfile(FILE_NAME, "rb")
    output_file = open("mate_pair_distances.tsv","w")
    iter= sam_file.fetch("REGION_OF_INTEREST",1,500)
    for x in iter:
    line = (str(x))
    cols = line.rstrip("\n").split("\t")
    print(cols)
    if cols[2] == "REGION_OF_INTEREST":
    mate_dist=int(cols[8])
    position = int(cols[3])
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #2
    Why are you converting the convenient to use AlignedRead class to a string? Just:
    Code:
    sam_file = pysam.Samfile(FILE_NAME, "rb")
    output_file = open("mate_pair_distances.tsv","w")
    for alignment in  sam_file.fetch("REGION_OF_INTEREST",1,500) :
        isize=alignment.tlen
        ...

    Comment

    • dpryan
      Devon Ryan
      • Jul 2011
      • 3478

      #3
      Note that you might want to toss an abs() in there, size the insert size will be negative for one of the mates.

      Comment

      • Tom.Booker
        Junior Member
        • Oct 2014
        • 2

        #4
        Thanks! That's cleared it right up.

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
          by SEQadmin2



          Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

          Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
          ...
          10-14-2026, 09:55 AM
        • SEQadmin2
          New Genomics Technologies Take Aim at Long-Standing Limits
          by SEQadmin2


          Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

          We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
          ...
          09-28-2026, 10:25 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, Yesterday, 11:13 AM
        0 responses
        21 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, Yesterday, 10:48 AM
        0 responses
        18 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-15-2026, 10:52 AM
        0 responses
        36 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-14-2026, 05:39 AM
        0 responses
        42 views
        0 reactions
        Last Post SEQadmin2  
        Working...