This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Trim bam file reads to the same length

Hi,

I have a bam file with vary reads length. I want to trim all the reads to the length of the shorter read in the file. I don't have access to to the fastq files, and the bam files went through many filters, so I prefer to do the trimming on the bam. Any idea how cam I do it?

Thanks!

rna-seq genome sequencing next-gen

2 answers

You could try using reformat.sh from BBMap suite. This is untested but may work.

reformat.sh in=your.bam out=new.bam maxlength=number_you_want

Hi @genomax, please help check the following error. I have installed the bbmap and don't remove any files from the installion directory

reformat.sh in=test.bam out=test_short.bam maxlength=75
/histor/kang/yangpc/soft/01.NGS_data_process/bbmap//calcmem.sh: line 75: [: -v: unary operator expected
java -ea -Xms300m -cp /histor/kang/yangpc/soft/01.NGS_data_process/bbmap/current/ jgi.ReformatReads in=test.bam out=test_short.bam maxlength=75
Executing jgi.ReformatReads [in=test.bam, out=test_short.bam, maxlength=75]

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.Process.isAlive()Z
        at dna.Data.testExecute(Data.java:1828)
        at dna.Data.SAMBAMBA(Data.java:1710)
        at fileIO.ReadWrite.SAMBAMBA(ReadWrite.java:1898)
        at fileIO.ReadWrite.getInputStream(ReadWrite.java:840)
        at fileIO.ByteFile1.open(ByteFile1.java:330)
        at fileIO.ByteFile1.<init>(ByteFile1.java:100)
        at fileIO.ByteFile2$BF1Thread.<init>(ByteFile2.java:237)
        at fileIO.ByteFile2.open(ByteFile2.java:215)
        at fileIO.ByteFile2.<init>(ByteFile2.java:86)
        at fileIO.ByteFile.makeByteFile(ByteFile.java:35)
        at fileIO.ByteFile.makeByteFile(ByteFile.java:27)
        at stream.SamReadInputStream.<init>(SamReadInputStream.java:39)
        at stream.ConcurrentReadInputStream.getReadInputStream(ConcurrentReadInputStream.java:158)
        at stream.ConcurrentReadInputStream.getReadInputStream(ConcurrentReadInputStream.java:69)
        at jgi.ReformatReads.process(ReformatReads.java:480)
        at jgi.ReformatReads.main(ReformatReads.java:52)

Can you try?

reformat.sh -Xmx10g in=test.bam out=test_short.bam maxlength=75

Hi @genomax, it turned out to be the java version's problem. jdk1.8 work, but 1.7 failed. I modified the line 228 in the file bbmap/reformat.sh as following: local CMD="/path/to/java/jdk1.8.0_211/bin/java $EA $EOOM $z $z2 -cp $CP jgi.ReformatReads $@"

From the help maxlength=0 If nonzero, reads longer than this after trimming will be discarded.

From what i understand it does not want to remove the reads but trim supernumary nucleotide after a certain length

Using forcetrimright from bbmap it is possible to trim the reads to a choosen length

Log in to answer this question.