name-sorted BAM if it is paired-end.
For testing purposes, I'm interested into work with a small limited number of reads that align well to a given genomic region, also small, so computation time and memory requirements are as low as possible.
This could be done (I guess) if from the alignment BAM file one could associate the aligned reads with specific reads in the original FASTQ file.
I don't know if this is even possible. So far, I've found nothing.
2 answers
Ok, I got a way to do it. You can find it in the following link: https://seqome.com/convert-bam-file-fastq/.
There it describes how bedtools bamtofastq can deal with the task. It will only require a sorted BAM file as input, eg:
bedtools bamtofastq -i input.bam -fq output.fq -fq2
You can pipe them out using samtools and the sam flags 64 and 128 for read 1 and read 2, and tune the extractables more carefully with convoluted combinations of the SAM flags.
Log in to answer this question.
Yes, reads can be extracted using e.g.
samtools fastq. It is recommended to randomize the BAM before doing that as many alignment tools expect random fastq order (for paired-end data). Could be don withsamtools collateorsamtools sort -nfollowed bysamtools fastq. For other threads on this please use the search function, there are many.Two questions:
What will
samtools fastqdo with hard-clipped reads/secondary alignments? Output shortened reads? I assume some kind of filtering should be applied before.Do you have any reference for the random fastq order requirement? I have not heard of this before. That would be good to know for certain applications...
Edit: formatting
So you would like to convert bam to fastq? Have you googled for that?
Not with that words, my mistake (I'm not native speaker). Now I 've found a way with
bedtools, thank youDon't worry. Most here aren't. Neither WouterDeCoster nor me are.
Yes. You can filter your BAM file to separate aligned reads and then convert them back to fastq.
You can first filter your BAM with
samtools view regionto get the region you need.Then use
reformat.shfrom BBMap suite to retrieve reads:If you have single end data then just use
out=read.fq.gzinstead.where you able to retrieve your original reads file frombam files? Can you share the processs and commands? I really need help on it I mixed up my data and need to retrieve from bam files the raw reads.
There are multiple suggestions on how to do this in this thread. Suggest you pick one and try it.