Dear Biostar community,
I have a bwa mapping of metagenomic sequencing reads on assembled contigs. I also have a list of IDs of "interesting" contigs. Now I would like to subset the mapping (bam/sam file) to obtain a mapping (bam/sam file) that only contains my desired contigs. Do you know of any simpel way (samtools, picard tools, R, Scala/Java) to do so? Note please that creating a mapping with a reduced contig.fasta file is not an option for me.
Thank you in advance!
Thank you both for your answers. :) May I add that you can just append the regions: So one could do samtools view -bh input.sorted.bam chr1 chr2 > output.bam or samtools view -bh input.sorted.bam
cat list_of_contigs.txt | tr "\n" " "
> output.bamThanks for the additional information. I didn't know that.
I know it's old but for new arrivers this is what worked for me : cat chromosome.list | tr "\n" " " | xargs samtools view -bh input.bam > output.bam
If you havent converted your sam files to bam then use :
egrep "^@HD|^@RG|^@PG|chr1|chr2" input.sam > output.sam (filtered)