How to sort a BAM file by read name ?
2
0
Entering edit mode
7.0 years ago
Jee2 ▴ 20

I ran suspenders on BAM files preprocessed by lapels & got a warning message that says - SO tag in input BAM files indicates not sorted by query_name.

I tried sorting BAM files by read name using:

samtools sort -o -n input.bam > Input_sorted.bam

From the header, it does appear that Input_sorted.bam is sorted by queryname

samtools view -H Input_sorted.bam

@HD VN:1.0 SO:queryname

@SQ SN:chr1 LN:195471971

@SQ SN:chr10 LN:130694993

@SQ SN:chr11 LN:122082543

@SQ SN:chr12 LN:120129022

@SQ SN:chr13 LN:120421639

@SQ SN:chr14 LN:124902244

@SQ SN:chr15 LN:104043685 I am using samtools 0.1.19

However, I get the same warning message when I run suspenders on Input_sorted.bam.

Can anyone tell what could be going wrong & how to get around this issue?

Also, samtools index cannot index this file.

RNA-Seq next-gen alignment • 5.3k views
ADD COMMENT
1
Entering edit mode

Which version of samtools are you using?

ADD REPLY
0
Entering edit mode

samtools 0.1.19-96b5f2294a

ADD REPLY
1
Entering edit mode

While this does not answer your question I wanted to see if you were using this pipeline. If yes, you could give pylapels a try. Perhaps that would do the trick.

Samtools 0.1.19 is deprecated. Current version is 1.4. I am not sure if it would make a difference in the sorting but that may be another option to explore.

ADD REPLY
0
Entering edit mode

I am following the pipeline you mentioned and it actually uses pylapels.

Thanks, I will give 1.4 a try.

ADD REPLY
1
Entering edit mode

Then run pylapels with the -n option as suggested, if you have not done so.

ADD REPLY
0
Entering edit mode

I have

pylapels -n -o lapels/Mat.bam WSBEiJ_b38_f.mod WSBalign /accepted_hits.bam

ADD REPLY
1
Entering edit mode

Do you still get that error? You should not need to use samtools if you already name sorted using pylapels.

ADD REPLY
0
Entering edit mode

I second you in that I should not need to use samtools. Indeed, samtools view -H shows that the Input.bam (output BAM file from lapels ) is already sorted by queryname.

But since I get an empty suspenders output file along with the warning message, I will have to get rid of the warning message somehow.

ADD REPLY
1
Entering edit mode

Warnings may be ok to ignore (some times, based on context) but since you are getting an empty output that would need to be looked at. Has this install of the pipeline been successfully used before?

ADD REPLY
0
Entering edit mode

Yes, It has been successfully used by other groups (PMID: 27999113, 27933528) apart from the team (PMID 27933528) that came up with it.

I have emailed authors couple days ago and as of this writing waiting for the response.

It is explicitly stated in the pipeline that for suspenders to work successfully both input BAM files need to be sorted by read name and not by coordinates (will produce a warning).

ADD REPLY
1
Entering edit mode

I was asking if your local software installation for this package/pipeline is known to work right.

ADD REPLY
0
Entering edit mode

I have installed this software locally & no one else around has ever dealt with this pipeline - now looking into the installation. Thanks

Adding -t argument to the pysuspender command gives traceback error ( in addition to the original SO tag error):

File "/home/local_dir/suspenders-0.2.5 py2.7.egg/MergeImprove/MergeImprove.py", line 1910, in mainRun pysam.sort(args.outMergedBam+'.tmp0.pileup.sorted.bam', args.outMergedBam+'.tmp.pileup_all')

P.S. samtools 1.4 did not make any difference.

ADD REPLY
1
Entering edit mode

If there is a test dataset included in the software then try to run through that to see if the install itself works. If it does then come back to this problem.

ADD REPLY
0
Entering edit mode

I have tried running their test lapels output files as input files for suspenders and got same errors.

ADD REPLY
1
Entering edit mode

That is a good clue that it is likely not your files but the software install (or some incompatibility there in). Check on @h.mon's suggestion.

ADD REPLY
0
Entering edit mode

Yes, it turned out that I have missed stuff during installation.

I thought I did not have to install pysam separately as it was already a part of suspenders package. But I see two different versions in there

pysam-0.11-py2.7-linux-x86_64.egg

pysam-0.7.6-py2.7-linux-x86_64.egg

But the suspenders seems to have been tested with pysam 0.7.4, to which I did not pay attention originally

https://github.com/holtjma/suspenders

This link also mentions that I also need to have matplotlib & argparse - I dont see them as part of the package so was supposed to install them - but where? Same directory? I can’t figure this out as they are not called in the suspenders command line.

Thanks a lot for your patience on this @ genomax2 & @h.mon

P.S. Lapels is installed in the same directory

ADD REPLY
1
Entering edit mode

Install in the same directory and start there (or at least make sure they are available in $PATH). Hopefully useful error messages will guide you through the rest.

ADD REPLY
1
Entering edit mode

I could easily install with virtualenv:

virtualenv ~/bin/suspenders/Suspenders-0.2.5
source ~/bin/suspenders/Suspenders-0.2.5/bin/activate
pip install suspenders
pip install lapels

pip takes care of python package dependencies, however, it did install pysam-0.11. To install an older version of pysam (0.7.4 is not available), install pysam before instaling suspenders / lapels:

virtualenv ~/bin/suspenders/Suspenders-0.2.5
source ~/bin/suspenders/Suspenders-0.2.5/bin/activate
pip install 'pysam==0.7.6'
pip install suspenders
pip install lapels

To use suspenders, you have to issue:

 source ~/bin/suspenders/Suspenders-0.2.5/bin/activate
ADD REPLY
1
Entering edit mode

Which version of pysam you have installed? There are some incompatible changes between some pysam versions.

ADD REPLY
3
Entering edit mode
7.0 years ago
Jee2 ▴ 20

I did following:

Installed anaconda and added pysam 0.8.3 package to it.

Installed suspenders in anaconda environment.

And it worked finally!

Thanks a lot for your help.

ADD COMMENT
1
Entering edit mode
7.0 years ago
h.mon 35k

Did you run lapels with -n? It should sort by name. Also, as far as I know, samtools index expects bam sorted by position, not by name, hence your problem.

ADD COMMENT
0
Entering edit mode

Yes, I used lapels with -n

pylapels -n -o lapels/Mat.bam WSBEiJ_b38_f.mod WSBalign /accepted_hits.bam

Thanks for your comment on indexing. I will remove its mention from my question as my requirement is just getting the Bam file sorted by query_name

ADD REPLY
1
Entering edit mode

I think you should not remove it from your question, as this (partially) defeats the purpose of a questions&answers forum. Besides, it will make me look a bit crazy, answering unasked questions.

ADD REPLY

Login before adding your answer.

Traffic: 2052 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