ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBam.h
(Generate patch)
# Line 163 | Line 163
163     char* fname;
164   public:
165     void bopen(const char* filename) {
166 <      //TODO: must detect file type first and accordingly
167 <      FILE* f=fopen(filename, "b");
168 <      
169 <      bam_file=samopen(filename, "rb", 0);
170 <      //for .sam files:
171 <      //bam_file=samopen(fname, "r", 0);
166 >      if (strcmp(filename, "-")==0) {
167 >        //if stdin was given, we HAVE to assume it's text SAM format, sorry
168 >        bam_file=samopen(filename, "r", 0);
169 >        }
170 >      else {
171 >      //BAM files have the zlib signature bytes at the beginning: 1F 8B 08
172 >      //if that's not present, we assume sam file
173 >      FILE* f=fopen(filename, "rb");
174 >      if (f==NULL) GError("Error opening file %s!\n", filename);
175 >      byte fsig[3];
176 >      size_t rd=fread(fsig, 1, 3, f);
177 >      if (rd<3) GError("Error reading from file %s!\n",filename);
178 >      if ((fsig[0]==0x1F && fsig[1]==0x8B && fsig[2]==0x08) ||
179 >          (fsig[0]=='B' && fsig[1]=='A' && fsig[2]=='M')) {
180 >          bam_file=samopen(filename, "rb", 0); //BAM or uncompressed BAM
181 >          }
182 >        else { //assume text SAM file
183 >          bam_file=samopen(filename, "r", 0);
184 >          }
185 >        }
186        if (bam_file==NULL)
187           GError("Error: could not open SAM file %s!\n",filename);
188        fname=Gstrdup(filename);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines