ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBam.h
(Generate patch)
# Line 19 | Line 19
19     //   +seq  (4bit-encoded)
20     //    +qual
21     //     +aux
22 <   bool novel;
22 >   //bool novel; //created from scratch, not from a given bam1_t* record
23     bam_header_t* bam_header;
24  
25   public:
26 <   GPVec<GSeg> exons;
26 >   GVec<GSeg> exons;
27 >   //created from a reader:
28     GBamRecord(bam1_t* from_b=NULL, bam_header_t* b_header=NULL):exons(1) {
29        bam_header=NULL;
30        if (from_b==NULL) {
31             b=bam_init1();
32 <           novel=true;
32 >           //novel=true;
33             }
34          else {
35 <           b=from_b;
36 <           novel=false;
35 >           b=from_b; //it'll take over from_b and FREE it when destroyed
36 >           //novel=false;
37             }
38        bam_header=b_header;
39        setupCoordinates();//sets start, end coordinate and populates exons array
40        }
41 +
42 +   const GBamRecord& operator=(GBamRecord& r) {
43 +      //make a new copy of the bam1_t record etc.
44 +      clear();
45 +      b=bam_dup1(r.b);
46 +      //novel=true; //will also free b when destroyed
47 +      start=r.start;
48 +      end=r.end;
49 +      exons = r.exons;
50 +      return *this;
51 +      }
52 +
53       void setupCoordinates();
54       void clear() {
55 <        if (novel) {
56 <            bam_destroy1(b);
57 <            }
58 <        novel=true;
59 <        b=bam_init1();
55 >        //if (novel) {
56 >        bam_destroy1(b);
57 >        //   novel=false;
58 >        //   }
59 >        b=NULL;
60 >        exons.Clear();
61 >        bam_header=NULL;
62          }
63  
64      ~GBamRecord() {
65 <       if (novel) {  bam_destroy1(b); }
65 >       clear();
66         }
67  
68      void parse_error(const char* s) {
# Line 133 | Line 148
148     samfile_t* bam_file;
149     char* fname;
150   public:
151 <   void open(const char* filename) {
151 >   void bopen(const char* filename) {
152        bam_file=samopen(filename, "rb", 0);
153        //for .sam files:
154        //bam_file=samopen(fname, "r", 0);
# Line 141 | Line 156
156           GError("Error: could not open SAM file %s!\n",filename);
157        fname=Gstrdup(filename);
158        }
144
159     GBamReader(const char* fname) {
160 +      bam_file=NULL;
161        fname=NULL;
162 <      open(fname);
162 >      bopen(fname);
163        }
164  
165 +   bam_header_t* header() {
166 +      return bam_file? bam_file->header : NULL;
167 +      }
168 +   void bclose() {
169 +      if (bam_file) {
170 +        samclose(bam_file);
171 +        bam_file=NULL;
172 +        }
173 +      }
174     ~GBamReader() {
175 <      samclose(bam_file);
175 >      bclose();
176        GFREE(fname);
177        }
178 +   void rewind() {
179 +     if (fname==NULL) {
180 +       GMessage("Warning: GBamReader::rewind() called without a file name.\n");
181 +       return;
182 +       }
183 +     bclose();
184 +     char* ifname=fname;
185 +     bopen(ifname);
186 +     GFREE(ifname);
187 +     }
188  
189 +   GBamRecord* next() {
190 +      if (bam_file==NULL)
191 +        GError("Warning: GBamReader::next() called with no open file.\n");
192 +      bam1_t* b = bam_init1();
193 +      if (samread(bam_file, b) >= 0) {
194 +        GBamRecord* bamrec=new GBamRecord(b, bam_file->header);
195 +        return bamrec;
196 +        }
197 +      else {
198 +        bam_destroy1(b);
199 +        return NULL;
200 +        }
201 +      }
202   };
203  
204  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines