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

        • kinpict.com
          Reply to 16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
          by kinpict.com
          The part about this topic feels surprisingly grounded, and the details on the page is the bit that made me slow down. I’d keep <a href="https://kinpict.com/">ai family photo generator</a> bookmarked for later.
          Today, 06:37 PM
        • SEQadmin2
          16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
          by SEQadmin2





          NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification,...
          10-30-2026, 06:46 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, 11-12-2026, 07:59 AM
        0 responses
        27 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 11-05-2026, 09:25 AM
        0 responses
        35 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 11-03-2026, 11:54 AM
        2 responses
        56 views
        0 reactions
        Last Post kinpict.com  
        Started by SEQadmin2, 10-28-2026, 09:57 AM
        0 responses
        175 views
        0 reactions
        Last Post SEQadmin2  
        Working...