ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/tophat_cpp/bam2fastx.cpp
(Generate patch)
# Line 6 | Line 6
6   #include "bam/bam.h"
7   #include "bam/sam.h"
8  
9 < static bool is_fastq=true; //default is fastq
10 < static bool sam_input=false; //default is BAM
9 > bool is_fastq=true; //default is fastq
10 > bool sam_input=false; //default is BAM
11 > bool all_reads=false;
12 > bool mapped_only=false;
13  
14 < static char qseq[2048];
14 > #define USAGE "Usage: bam2fastx [--fasta|-a|--fastq|-q] [--sam|-s|-t] \n\
15 >        [-M|--mapped-only|-A|--all] <in.bam>|<in.sam> \n"
16  
17 < const char *short_options = "aq";
17 >
18 >
19 > char qseq[2048];
20 >
21 > const char *short_options = "aqstMU";
22  
23   enum {
24     OPT_FASTA = 127,
25     OPT_FASTQ,
26 <   OPT_SAM
26 >   OPT_SAM,
27 >   OPT_MAPPED_ONLY,
28 >   OPT_ALL
29     };
30    
31 < static struct option long_options[] = {
31 > struct option long_options[] = {
32    {"fasta", no_argument, 0, OPT_FASTA},
33    {"fastq", no_argument, 0, OPT_FASTQ},
34    {"sam", no_argument, 0, OPT_SAM},
35 +  {"mapped-only", no_argument, 0, OPT_MAPPED_ONLY},
36 +  {"all", no_argument, 0, OPT_ALL},
37    {0, 0, 0, 0} // terminator
38    };
39  
# Line 48 | Line 59
59         case OPT_SAM: //sam (text) input
60           sam_input = true;
61           break;
62 +       case 'M':
63 +       case OPT_MAPPED_ONLY:
64 +         mapped_only = true;
65 +         break;
66 +       case 'A':
67 +       case OPT_ALL:
68 +         all_reads = true;
69 +         break;
70         default:
71           return 1;
72         }
73     } while(next_option != -1);
74 <  
74 >  if (all_reads && mapped_only) {
75 >    fprintf(stderr, "Error: incompatible options !\n");
76 >    exit(2);
77 >    }
78    return 0;
79   }
80  
81 + #define bam_unmapped(b) (((b)->core.flag & BAM_FUNMAP) != 0)
82  
83   void showfastq(const bam1_t *b, samfile_t* fp) {
61  //uint32_t *cigar = bam1_cigar(b);
62  //const bam1_core_t *c = &b->core;
84    char *name  = bam1_qname(b);
85    char *qual  = (char*)bam1_qual(b);
86    char *s    = (char*)bam1_seq(b);
87    int i;
88 +  bool ismapped=((b->core.flag & BAM_FUNMAP) == 0);
89 +  if (ismapped && !all_reads) return;
90 +  if (mapped_only && !ismapped) return;
91 +  
92    for(i=0;i<(b->core.l_qseq);i++) {
93      int8_t v = bam1_seqi(s,i);
94      qseq[i] = bam_nt16_rev_table[v];
95 <  }
95 >    }
96    qseq[i] = 0;
97    
98    printf("@%s\n%s\n",name, qseq);
74  //print SAM text format of this BAM record:
75  //char * samtext=bam_format1(fp_in->header, b);
76  //printf("%s\n",samtext);
77
78  //printf("s cigar: %s\n",cigar);
79
80  //printf("qual : ");
99    for(i=0;i<(b->core.l_qseq);i++) {
100 <    //printf(" %d",qual[n]);
83 <    qseq[i]=qual[i]+33;        
100 >    qseq[i]=qual[i]+33;
101      }
102    qseq[i]=0;
103    printf("+\n%s\n",qseq);
# Line 99 | Line 116
116   }
117  
118  
102 #define USAGE "Usage: bam2fastx [--fasta|-a|--fastq|-q] [--sam|-s|-t] <in.bam>|<in.sam> \n"
119  
120   int main(int argc, char *argv[])
121   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines