ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/tophat_cpp/bam2fastx.cpp
(Generate patch)
# Line 10 | Line 10
10   bool sam_input=false; //default is BAM
11   bool all_reads=false;
12   bool mapped_only=false;
13 + char outfname[1024];
14  
15   #define USAGE "Usage: bam2fastx [--fasta|-a|--fastq|-q] [--sam|-s|-t] \n\
16 <        [-M|--mapped-only|-A|--all] <in.bam>|<in.sam> \n"
16 >        [-M|--mapped-only|-A|--all] [-o <outfile>] <in.bam>|<in.sam> \n"
17  
18  
19  
20   char qseq[2048];
21  
22 < const char *short_options = "aqstMU";
22 > const char *short_options = "o:aqstMUA";
23  
24   enum {
25     OPT_FASTA = 127,
# Line 67 | Line 68
68         case OPT_ALL:
69           all_reads = true;
70           break;
71 +       case 'o':
72 +         strcpy(outfname, optarg);
73 +         break;
74         default:
75           return 1;
76         }
# Line 80 | Line 84
84  
85   #define bam_unmapped(b) (((b)->core.flag & BAM_FUNMAP) != 0)
86  
87 < void showfastq(const bam1_t *b, samfile_t* fp) {
87 > void showfastq(const bam1_t *b, samfile_t* fp, FILE* fout) {
88    char *name  = bam1_qname(b);
89    char *qual  = (char*)bam1_qual(b);
90    char *s    = (char*)bam1_seq(b);
# Line 95 | Line 99
99      }
100    qseq[i] = 0;
101    
102 <  printf("@%s\n%s\n",name, qseq);
102 >  fprintf(fout, "@%s\n%s\n",name, qseq);
103    for(i=0;i<(b->core.l_qseq);i++) {
104      qseq[i]=qual[i]+33;
105      }
106    qseq[i]=0;
107 <  printf("+\n%s\n",qseq);
107 >  fprintf(fout, "+\n%s\n",qseq);
108   }
109  
110 < void showfasta(const bam1_t *b, samfile_t* fp) {
110 > void showfasta(const bam1_t *b, samfile_t* fp, FILE* fout) {
111    char *name  = bam1_qname(b);
112    char *s    = (char*)bam1_seq(b);
113    int i;
# Line 112 | Line 116
116      qseq[i] = bam_nt16_rev_table[v];
117    }
118    qseq[i] = 0;
119 <  printf(">%s\n%s\n",name, qseq);
119 >  fprintf(fout, ">%s\n%s\n",name, qseq);
120   }
121  
122  
# Line 120 | Line 124
124   int main(int argc, char *argv[])
125   {
126      samfile_t *fp;
127 +    outfname[0]=0;
128      char* fname=NULL;
129  
130      if (parse_options(argc, argv) || optind>=argc) {
# Line 140 | Line 145
145          fprintf(stderr, "Error: bam2fastx failed to open BAM file %s\n", fname);
146          return 1;
147          }
148 <    
148 >    FILE* fout=stdout;
149 >    if (outfname[0]) {
150 >       fout=fopen(outfname, "w");
151 >       if (fout==NULL) {
152 >           fprintf(stderr, "Error creating output file %s\n", outfname);
153 >           return 2;
154 >           }
155 >       }
156      bam1_t *b = bam_init1();
157      if (is_fastq) {
158 <        while (samread(fp, b) >= 0) showfastq(b, fp);
158 >        while (samread(fp, b) >= 0) showfastq(b, fp, fout);
159          }
160        else {
161 <        while (samread(fp, b) >= 0) showfasta(b, fp);
161 >        while (samread(fp, b) >= 0) showfasta(b, fp, fout);
162          }
163      
164      bam_destroy1(b);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines