This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools sort: truncated file. Aborting

Hi, I have a bam file. Then, to extract specific regions of that file, I used the command

cat chrs.txt | xargs -n 1 samtools view -@ 10 -b -h bd.bam > bd2.bam

where chrs.txt is the file containing the regions to be exacted, like

chr1

chr1_random

chr2

Then, after got the bd2.bam file containing the extracted regions. I wanted to sort and index it. But an error happened here

samtools sort: truncated file. Aborting

I didn't know the reason, but on the other hand, if I generated another regional bam file using command like

samtools view -@ 10 -b -h bd.bam 'chrY'> bd3.bam

This time the bd3.bam file could be sorted normally. I don't know why encounter such an error. Does anyone know the reason?

Thank you.

The header information is

@HD     VN:1.4  SO:coordinate
@SQ     SN:BK000964     LN:45957
@SQ     SN:chr10        LN:129993255
@SQ     SN:chr11        LN:121843856
@SQ     SN:chr12        LN:121257530
@SQ     SN:chr13        LN:120284312
@SQ     SN:chr13_random LN:400311
@SQ     SN:chr14        LN:125194864
@SQ     SN:chr15        LN:103494974
@SQ     SN:chr16        LN:98319150
@SQ     SN:chr16_random LN:3994
@SQ     SN:chr17        LN:95272651
@SQ     SN:chr17_random LN:628739
@SQ     SN:chr18        LN:90772031
@SQ     SN:chr19        LN:61342430
@SQ     SN:chr1 LN:197195432
@SQ     SN:chr1_random  LN:1231697
@SQ     SN:chr2 LN:181748087
@SQ     SN:chr3 LN:159599783
@SQ     SN:chr3_random  LN:41899
@SQ     SN:chr4 LN:155630120
@SQ     SN:chr4_random  LN:160594
@SQ     SN:chr5 LN:152537259
@SQ     SN:chr5_random  LN:357350
@SQ     SN:chr6 LN:149517037
@SQ     SN:chr7 LN:152524553
@SQ     SN:chr7_random  LN:362490
@SQ     SN:chr8 LN:131738871
@SQ     SN:chr8_random  LN:849593
@SQ     SN:chr9 LN:124076172
@SQ     SN:chr9_random  LN:449403
@SQ     SN:chrM LN:16299
@SQ     SN:chrUn_random LN:5900358
@SQ     SN:chrX LN:166650296
@SQ     SN:chrX_random  LN:1785075
@SQ     SN:chrY LN:15902555
@SQ     SN:chrY_random  LN:58682461
@PG     ID:STAR PN:STAR VN:STAR_2.5.3a  CL:STAR   ...
@CO     user command line: STAR ...
samtools truncated file

1 answer

Clearly your xargs command is not doing what you think it should. samtools view can take a bed file as input to filter a bam file, why don't you use the built-in command instead of trying to make your own?

I think this should work:

samtools view -@ 10 -b -h bd.bam <(cat chrs.txt) > bd2.bam

Thank you so much for reply. Because I wanted to extract reads on some whole chromosomes, and just use their name to select the whole without the start and end coordinates in the bed file, I used xargs instead of the -L flag of samtools. I have found the issue is that the header of the bam file makes xargs doesn't act as I want, so I have changed to the following commands and the problem has been solved. Thank you. samtools view -@ 10 -H bd.bam > H.sam cat chrs.txt | xargs -n 1 samtools view -@ 10 bd.bam >> H.sam samtools view -@ 10 -S -b -o bd2.bam H.sam rm bd2.bam

Log in to answer this question.