ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBam.h
(Generate patch)
# Line 5 | Line 5
5   #include "bam.h"
6   #include "sam.h"
7  
8
8   class GBamReader;
9   class GBamWriter;
10  
# Line 19 | Line 18
18     //   +seq  (4bit-encoded)
19     //    +qual
20     //     +aux
21 <   bool novel; //created from scratch, not from a given bam1_t* record
21 >   bool novel; //if true, will also free b on delete
22     bam_header_t* bam_header;
23     char tag[2];
24     uint8_t abuf[512];
25   public:
26 <   GVec<GSeg> exons;
26 >   GVec<GSeg> exons; //coordinates will be 1-based
27     //created from a reader:
28 <   GBamRecord(bam1_t* from_b=NULL, bam_header_t* b_header=NULL):exons(1) {
28 >   void bfree_on_delete(bool b_free=true) { novel=b_free; }
29 >   GBamRecord(bam1_t* from_b=NULL, bam_header_t* b_header=NULL, bool b_free=true):exons(1) {
30        bam_header=NULL;
31        if (from_b==NULL) {
32             b=bam_init1();
33             novel=true;
34             }
35          else {
36 <           b=from_b; //it'll take over from_b but not free it when destroyed
37 <           novel=false;
36 >           b=from_b; //it'll take over from_b
37 >           novel=b_free;
38             }
39 +
40        bam_header=b_header;
41 <      setupCoordinates();//sets start, end coordinate and populates exons array
41 >      setupCoordinates();//set 1-based coordinates (start, end and exons array)
42        }
43  
44     const GBamRecord& operator=(GBamRecord& r) {
# Line 55 | Line 56
56       void clear() {
57          if (novel) {
58             bam_destroy1(b);
59 <           novel=false;
59 >           //novel=false;
60             }
61          b=NULL;
62          exons.Clear();
# Line 168 | Line 169
169   char* cigar(); //returns text version of the CIGAR string; user must free
170   };
171  
172 + // from sam.c:
173 + #define FTYPE_BAM  1
174 + #define FTYPE_READ 2
175  
176   class GBamReader {
177     samfile_t* bam_file;
178     char* fname;
179 +   // from bam_import.c:
180 +   struct samtools_tamFile_t {
181 +        gzFile fp;
182 +        void *ks;
183 +        void *str;
184 +        uint64_t n_lines;
185 +        int is_first;
186 +   };
187 +
188   public:
189     void bopen(const char* filename) {
190        if (strcmp(filename, "-")==0) {
# Line 185 | Line 198
198        if (f==NULL) GError("Error opening file %s!\n", filename);
199        byte fsig[3];
200        size_t rd=fread(fsig, 1, 3, f);
201 +      fclose(f);
202        if (rd<3) GError("Error reading from file %s!\n",filename);
203        if ((fsig[0]==0x1F && fsig[1]==0x8B && fsig[2]==0x08) ||
204            (fsig[0]=='B' && fsig[1]=='A' && fsig[2]=='M')) {
# Line 217 | Line 231
231        bclose();
232        GFREE(fname);
233        }
234 +   int64_t fpos() {
235 +         if ( bam_file->type & FTYPE_BAM )
236 +           return bgzf_tell(bam_file->x.bam);
237 +         else
238 +                 return (int64_t)gztell(((samtools_tamFile_t*)(bam_file->x.tamr))->fp);
239 +   }
240 +   int64_t fseek(int64_t offs) {
241 +         if ( bam_file->type & FTYPE_BAM )
242 +                 return bgzf_seek(bam_file->x.bam, offs, SEEK_SET);
243 +         else
244 +                 return (int64_t)gzseek(((samtools_tamFile_t*)(bam_file->x.tamr))->fp, offs, SEEK_SET);
245 +   }
246     void rewind() {
247       if (fname==NULL) {
248         GMessage("Warning: GBamReader::rewind() called without a file name.\n");
# Line 233 | Line 259
259          GError("Warning: GBamReader::next() called with no open file.\n");
260        bam1_t* b = bam_init1();
261        if (samread(bam_file, b) >= 0) {
262 <        GBamRecord* bamrec=new GBamRecord(b, bam_file->header);
262 >        GBamRecord* bamrec=new GBamRecord(b, bam_file->header, true);
263          return bamrec;
264          }
265        else {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines