Entering edit mode
3.4 years ago
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 ....
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?
https://pysam.readthedocs.io/en/latest/api.html