This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Sort Sam file by chromosome and position in python

Hi, I'm trying to read a sam file and write another sam file sorted by chromosome and position.

The problem is that there are some sam files that have no alignment like

seq1 16 cr1 100 ....
seq1 16 cr2 100 ....
seq2 4  *   0   ....

So when I try to sort with this

df = pd.read_csv('file.sam', sep="\t", header=None).to_numpy()
list1 = sorted(df, key=operator.itemgetter(1, 2))

Returns this

 seq2 4  *   0  ....
seq1 16 cr1 100 ....
seq1 16 cr2 100 ....
sam python genome alignment

Check pysam rather than coming up with costom solutions, it is the swiss-army knife for sequencing data manipulation in python.

So how would you sort the sam file by chromosome and position using pysam?

0 answers

No answers yet.

Log in to answer this question.