I have two paired-end FASQ files named fq1.fastq and fq2.fastq. Then I use BWA MEM to map these two files with the paired-end mode, like this:
bwa mem reference.fasta fq1.fastq fq2.fastq > result.sam
Then I transfer this sam file to bam format and sort it by coordinates with samtools:
samtools view result.sam > result.bam && samtools sort result.bam -o result_sort.bam
Now I want to restore these two fastq files from result_sort.bam. I know that I can use picard SamToFastq to do this. But reads in fastq files not remain sorted like the result_sort.bam. So, is there any other way that I can restore fastq files with reads sorted? I want to do this, because when I get fq1.fastq and fq2.fastq from result_sort.bam and use BWA to map then I again, I can get a sam file within that many reads are sorted. Any reply will be much appreciated!
samtools sort
accept also a sam file as input and can write to sam file. But why do you need a sam file instead of bam?fin swimmer
Either sam or bam format is OK for me
May I ask why you need this?
I want to get the sorted fastq files, so I can get the sorted sam file directly when I run BWA MEM with sorted fastq files. But it seems very difficult!
Ok, what about reads getting assigned to random positions due to poor mapping quality, they will not end up in the same position? And to re-iterate my initial question, may I ask why you need this?