How to Separate a SAM File Containing Forward and Reverse Reads into Two Separate Files
2
0
Entering edit mode
7.6 years ago
kdevakan ▴ 20

Hi,

I have several sam output files, where each file contains forward AND reverse reads for an experimental sample. These output files were generated using bowtie2, where I created an index of all my negative controls and aligned this index with each sample. The reads found in my negative controls where though to be background noise/contamination. Any sample's reads which aligned to the index reads where then omitted, and bowtie2 generated an output file of all the unaligned sample reads (an output file for each sample). This output file contains forward and reverse reads.

I am looking for a method to separate this file into two; one file for forward reads and one file for reverse reads. However, this method has to be smart enough to only include matching pairs of forward and reverse reads.

Any help would be greatly appreciated! Thanks!

sequence alignment sequencing • 3.1k views
ADD COMMENT
0
Entering edit mode
7.6 years ago

Sort the reads by name with samtools. Then in python, using pysam:

import pysam

samfile = pysam.AlignmentFile("mysam.sam")
out1 = pysam.AlignmentFile("read1s.sam", "w", template=samfile)
out2 = pysam.AlignmentFile("read2s.sam", "w", template=samfile)

reads = samfile.fetch(until_eof=True)

last_read = samfile.next()

for current_read in samfile:

    if not current_read.query_name == last_read.query_name:
        last_read = current_read
        continue

    if last_read.is_read1 and current_read.is_read2:
        out1.write(last_read)
        out2.write(current_read)
    elif last_read.is_read2 and current_read.is_read1:
        out2.write(last_read)
        out1.write(current_read)

    last_read = current_read

I strongly suspect implementations of the same logic are possible in perl, R or probably even awk.

ADD COMMENT
0
Entering edit mode
7.6 years ago
GenoMax 141k

Using splitsam.sh from BBMap suite.

splitsam.sh mapped.sam forward.sam reverse.sam
ADD COMMENT

Login before adding your answer.

Traffic: 3176 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6