perfect - thanks. The -l option of samtools mpileup takes BED file to output specified regions.
I want to convert bam file to pileup format. Is there a way to split into smaller jobs (by chromosomes for example) and re-merge to get the consolidated pileup file?
right now I am using "Pileup" tool from GATK but can use samtools too.
java -Xmx4g -jar ~/software/gatk/GenomeAnalysisTK/GenomeAnalysisTK.jar -I $bam -R $ref -T Pileup
2 answers
Both samtools view and samtools pileup can take a region as input in the command line, and will only output the region specified. Personally, I think the pileup from samtools looks nicer than the one from GATK. It's apparent immediately when the reads don't match the consensus. And samtools includes where reads start and stop, and which direction reads were going, and puts the coverage right there, so you don't have to count.
another option is to use -r REGION_STR
You may use simple command-line tools like 'awk' to split the sam alignments before converting to bam files. There is no real utilities to split bam, but you may write your own scripts using Bio::DB::Sam modules to access the bam files and grab the part of data you want.
Log in to answer this question.
Yes, you can split into smaller jobs using the 'bamtools split' command. In my case, I was using samtools mpileup. I found that for big jobs, such as those on multiple large bam files from many samples splitting is much faster. I think you could probably consolidate the resulting pileups with a short ad-hoc script. However, in my case, I further processed the pileups separately using variant calling software (VarScan) and then consolidated the smaller output files from that sofrtware and then removed the pileups.