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;
27 <   GBamRecord(bam1_t* from_b=NULL, bam_header_t* b_header=NULL):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 105 | Line 120
120   bool isUnmapped() { return ((b->core.flag & BAM_FUNMAP) != 0); }
121   bool isMapped() { return ((b->core.flag & BAM_FUNMAP) == 0); }
122   bool isPaired() { return ((b->core.flag & BAM_FPAIRED) != 0); }
123 + const char* name() { return bam1_qname(b); }
124   int mateNum() {
125      int r=0;
126      if ((b->core.flag & BAM_FREAD1) != 0) r=1;
127      else if ((b->core.flag & BAM_FREAD2) != 0) r=2;
128      return r;
129      }
130 < bool onRevStrand() { return ((b->core.flag & BAM_FREVERSE) != 0); }
130 > bool revStrand() { return ((b->core.flag & BAM_FREVERSE) != 0); }
131   const char* refName() {
132       return (bam_header!=NULL) ? bam_header->target_name[b->core.tid] : NULL;
133       }
134   int32_t refId() { return b->core.tid; }
135  
136 < uint8_t* tag(const char tag[2]); //retrieves tag data
137 < int tag_int(const char tag[2]); //get the numeric value of tag (if it's numeric)
138 < char spliceStrand(); // '+', '-' from the XS tag, or 0 if no XS tag
136 > int find_tag(const char tag[2], uint8_t* & s, char& tag_type);
137 > char* tag_str(const char tag[2]); //return tag value for tag type 'Z'
138 > int tag_int(const char tag[2]); //return numeric value of tag (for numeric types)
139 > char tag_char(const char tag[2]); //return char value of tag (for type 'A')
140 > char spliceStrand(); // '+', '-' from the XS tag, or '.' if no XS tag
141   char* sequence(); //user must free this after use
142   char* qualities();//user must free this after use
143   char* cigar(); //returns text version of the CIGAR string; must be freed by user
# Line 130 | 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 138 | Line 156
156           GError("Error: could not open SAM file %s!\n",filename);
157        fname=Gstrdup(filename);
158        }
141
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