View Single Post
Old 05-08-2012, 12:15 PM   #2
sdriscoll
I like code
 
Location: San Diego, CA, USA

Join Date: Sep 2009
Posts: 438
Default

to really do it right one should also edit the alignment column but since that column which might specify different things at each alignment (continuous alignments, gapped, spliced etc) but if you just want to cut the reads and quality strings down (cutting from the right side and keeping left side) you could do this (all in one line):

Code:
myfile="some_file.bam";mylen=20;cat <(samtools view -H $myfile) <(samtools view $myfile | while read line; do cutRead=`echo "$line" | cut -f10 | cut -c1-$mylen`; cutQual=`echo "$line" | cut -f10 | cut -c1-$mylen`;  echo -e "$line" | awk -v cRead=$cutRead -v cQual=$cutQual 'BEGIN {FS="\t";OFS="\t"} {$10=cRead;$11=cQual;print $0}; done)
just set myfile and mylen to your bam file and the length you want the reads to be post-trimming. the above will produce SAM output to stdout. samtools will not allow you to turn this back into a BAM file, however, since the alignment CIGAR column won't match the sequence lengths.

of course i 100% do not recommend using the output of this for any downstream analysis using software the accepts SAM files because of the fact that the alignment column (CIGAR annotation) is not edited so as far as any tools are concerted the alignments will still be full length. note that editing that value would require more code to interpret the alignment and edit it based on the trimming.

is there any chance you can trim the reads prior to alignment? that would be much less messy.
sdriscoll is offline   Reply With Quote