Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • elviszboncak
    Junior Member
    • Aug 2022
    • 1

    Help with sorting sam files in python!

    Hello All!

    I am new to coding. I have written a python script to analyze a bunch of fastq files from RNASeq analysis. I am using the subprocess module to call bowtie2 in order to index the genome and align the unpaired short reads to the genome.My script generates a sam file which needs to be sorted before running the cuffdiff module. The linux sort command works perfectly on my mac, however, in spite of installing the Unxupdates on my windows at work, I am unable to make my script work. I am hoping for some help/advice here to sort my sam file in python. Here is the script I tried.

    #!/usr/bin/env python2.7

    import os, sys

    inputFile = open(‘Outputfile1.txt’, 'r')

    lineList = inputFile.readlines()

    for line in sorted(lineList):

    print(line.rstrip());

    With this script, the stdout is written to the screen. But this recognizes only the text file. Sam files are not recognized.

    Any help is much appreciated.

    Thanks in advance
  • jamalshah888
    Junior Member
    • Jan 2023
    • 1

    #2
    Here's an example of how you can sort a SAM file in Python using the pysam library:

    code:
    import pysam # Open the input SAM file samfile = pysam.AlignmentFile("input.sam", "r") # Create an output file for the sorted SAM data sorted_samfile = pysam.AlignmentFile("sorted.sam", "wb", template=samfile) # Sort the input SAM file by read name for read in samfile.fetch(until_eof=True): sorted_samfile.write(read) # Close the input and output files samfile.close() sorted_samfile.close()
    This code will open an input SAM file called "input.sam", sort the reads by read name, and write the sorted data to an output file called "sorted.sam".


    You can also sort the SAM file by other criteria such as reference name, position, etc. by using the appropriate sort function from pysam. For example, to sort the SAM file by reference name and position, you can use the following code:


    code:
    sorted_samfile = pysam.AlignmentFile("sorted.sam", "wb", template=samfile) sorted_samfile.sort("-o", "sorted.sam", "input.sam") sorted_samfile.close()
    This code will use the pysam sort function to sort the input SAM file by reference name and position and write the sorted data to the output file "sorted.sam".

    Regards
    Jamal Shah

    Comment

    Latest Articles

    Collapse

    ad_right_rmr

    Collapse

    News

    Collapse

    Topics Statistics Last Post
    Started by SEQadmin2, Yesterday, 06:09 AM
    0 responses
    16 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 06-09-2026, 11:58 AM
    0 responses
    34 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 06-05-2026, 10:09 AM
    0 responses
    41 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 06-04-2026, 08:59 AM
    0 responses
    48 views
    0 reactions
    Last Post SEQadmin2  
    Working...