Header Leaderboard Ad


Help with sorting sam files in python!



No announcement yet.
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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):


    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

  • #2
    I am not familiar with Python, but why do you need it? This forum seems to work with PHP and I know it a little bit so if you will need PHP help, I can do it for you. Just let me know.


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

      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:

      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".

      Jamal Shah


      Latest Articles


      • seqadmin
        A Brief Overview and Common Challenges in Single-cell Sequencing Analysis
        by seqadmin

        ​​​​​​The introduction of single-cell sequencing has advanced the ability to study cell-to-cell heterogeneity. Its use has improved our understanding of somatic mutations1, cell lineages2, cellular diversity and regulation3, and development in multicellular organisms4. Single-cell sequencing encompasses hundreds of techniques with different approaches to studying the genomes, transcriptomes, epigenomes, and other omics of individual cells. The analysis of single-cell sequencing data i...

        01-24-2023, 01:19 PM
      • seqadmin
        Introduction to Single-Cell Sequencing
        by seqadmin
        Single-cell sequencing is a technique used to investigate the genome, transcriptome, epigenome, and other omics of individual cells using high-throughput sequencing. This technology has provided many scientific breakthroughs and continues to be applied across many fields, including microbiology, oncology, immunology, neurobiology, precision medicine, and stem cell research.

        The advancement of single-cell sequencing began in 2009 when Tang et al. investigated the single-cell transcriptomes
        01-09-2023, 03:10 PM