Skip to content

Commit

Permalink
Further changes to convert bam files not generated by fastq2bam
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofonseca committed Feb 11, 2019
1 parent 2fc2f21 commit 56b0504
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/bam2fastq.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ char *get_tag(bam1_t *aln,const char tagname[2]) {
return(s2);
}

void QWRITE(gzFile fd,FILE_LOC type, char*s1,char*s2,char*s3) {
void QWRITE(gzFile fd,FILE_LOC type, char*s1,char*s2,char*s3,int add_suffix) {
char rn_suf[4]="";
if (type!=SE) // add a suffix
if (add_suffix) // add a suffix
sprintf(&rn_suf[0],"/%u",(short)type+1);
GZ_WRITE(fd,"@");
GZ_WRITE(fd,s1);
Expand Down Expand Up @@ -214,6 +214,7 @@ int main(int argc, char *argv[])
// ORIG_RN_TAG missing
// BAM was not generated with fastq2bam
char *hdr=bam1_qname(aln);
qual=get_qual(aln,&qual_buf[0]);
if ( qual == NULL ) {
// create a dummy sequence to ensure that the fastq file is valid
unsigned int i,len = aln->core.l_qseq;
Expand All @@ -234,34 +235,35 @@ int main(int argc, char *argv[])
} else if ( (aln->core.flag==(BAM_FUNMAP|BAM_FMUNMAP|BAM_FPAIRED|BAM_FREAD1)) || aln->core.flag & BAM_FUNMAP ) {
write_to=R1;
}
QWRITE(get_fp(fd, write_to , out_file_prefix),write_to, hdr,seq,qual);
QWRITE(get_fp(fd, write_to , out_file_prefix),write_to, hdr,seq,qual,TRUE);
} else {
// bam generated by fastq2bam
// TODO: add checks?
if ( is_pe == -1 ) {
fprintf(stderr,"processing fastq2bam file...\n");
if (aln->core.flag & BAM_FUNMAP ) { // SE
is_pe=FALSE;
} else { // PE
if (aln->core.flag & BAM_FPAIRED ) {
is_pe=TRUE;
}
}
// TODO: add checks?

is_pe=FALSE;
if (aln->core.flag & BAM_FPAIRED ) {
is_pe=TRUE;
}
if ( (aln->core.flag==(BAM_FUNMAP|BAM_FMUNMAP|BAM_FPAIRED|BAM_FREAD1)) || aln->core.flag & BAM_FUNMAP ) {
// read1
QWRITE(get_fp(fd, R1 , out_file_prefix),R1, hdr,seq,qual);

if ( !is_pe ||
(aln->core.flag & BAM_FREAD1 ) ) {
//
short write_to=R1;
if ( ! is_pe )
write_to=SE;

QWRITE(get_fp(fd, write_to , out_file_prefix),R1, hdr,seq,qual,FALSE);
// cell
if (get_tag(aln,CELL_TAG)!=NULL)
QWRITE(get_fp(fd, CELL, out_file_prefix),CELL, hdr,get_tag(aln,CELL_TAG),get_tag(aln,CELL_QUAL_TAG));
QWRITE(get_fp(fd, CELL, out_file_prefix),CELL, hdr,get_tag(aln,CELL_TAG),get_tag(aln,CELL_QUAL_TAG),FALSE);
// umi
if (get_tag(aln,UMI_TAG)!=NULL)
QWRITE(get_fp(fd, UMI, out_file_prefix),UMI,hdr,get_tag(aln,UMI_TAG),get_tag(aln,UMI_QUAL_TAG));
QWRITE(get_fp(fd, UMI, out_file_prefix),UMI,hdr,get_tag(aln,UMI_TAG),get_tag(aln,UMI_QUAL_TAG),FALSE);
// sample
if (get_tag(aln,SAMPLE_TAG)!=NULL)
QWRITE(get_fp(fd, SAMPLE, out_file_prefix),SAMPLE,hdr,get_tag(aln,SAMPLE_TAG),get_tag(aln,SAMPLE_QUAL_TAG));
QWRITE(get_fp(fd, SAMPLE, out_file_prefix),SAMPLE,hdr,get_tag(aln,SAMPLE_TAG),get_tag(aln,SAMPLE_QUAL_TAG),FALSE);
} else { //R2
QWRITE(get_fp(fd, R2 , out_file_prefix),R2,hdr,seq,qual);
QWRITE(get_fp(fd, R2 , out_file_prefix),R2,hdr,seq,qual,FALSE);
}
}
}
Expand Down

0 comments on commit 56b0504

Please sign in to comment.