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 67 | Line 82
82        b->core.flag=flags;
83        }
84  
70
71
85      //creates a new record from 1-based alignment coordinate
86      //quals should be given as Phred33
87      //Warning: pos and mate_pos must be given 1-based!
# Line 106 | Line 119
119   bool isMapped() { return ((b->core.flag & BAM_FUNMAP) == 0); }
120   bool isPaired() { return ((b->core.flag & BAM_FPAIRED) != 0); }
121   const char* name() { return bam1_qname(b); }
122 < int mateNum() {
122 > int pairOrder() {
123 >    //which read in the pair: 0 = unpaired, 1=first read, 2=second read
124      int r=0;
125      if ((b->core.flag & BAM_FREAD1) != 0) r=1;
126      else if ((b->core.flag & BAM_FREAD2) != 0) r=2;
127      return r;
128      }
129 < bool revStrand() { return ((b->core.flag & BAM_FREVERSE) != 0); }
130 < const char* refName() {
131 <     return (bam_header!=NULL) ? bam_header->target_name[b->core.tid] : NULL;
132 <     }
129 > bool revStrand() {
130 >   //this is the raw alignment strand, NOT the transcription (XS) strand
131 >   return ((b->core.flag & BAM_FREVERSE) != 0);
132 >   }
133 > const char* refName() {
134 >   return (bam_header!=NULL) ?
135 >         ((b->core.tid<0) ? "*" : bam_header->target_name[b->core.tid]) : NULL;
136 >   }
137   int32_t refId() { return b->core.tid; }
138 + int32_t mate_refId() { return b->core.mtid; }
139 + const char* mate_refName() {
140 +    return (bam_header!=NULL) ?
141 +       ((b->core.mtid<0) ? "*" : bam_header->target_name[b->core.mtid]) : NULL;
142 +    }
143 + int32_t insertSize() { return b->core.isize; }
144 + int32_t mate_start() { return b->core.mpos<0? 0 : b->core.mpos+1; }
145  
146   int find_tag(const char tag[2], uint8_t* & s, char& tag_type);
147 + //position s at the beginning of tag data, tag_type is set to the found tag type
148 + //returns length of tag data, or 0 if tag not found
149 +
150   char* tag_str(const char tag[2]); //return tag value for tag type 'Z'
151   int tag_int(const char tag[2]); //return numeric value of tag (for numeric types)
152   char tag_char(const char tag[2]); //return char value of tag (for type 'A')
153   char spliceStrand(); // '+', '-' from the XS tag, or '.' if no XS tag
154 < char* sequence(); //user must free this after use
155 < char* qualities();//user must free this after use
156 < char* cigar(); //returns text version of the CIGAR string; must be freed by user
154 >
155 > char* sequence(); //user should free after use
156 > char* qualities();//user should free after use
157 > char* cigar(); //returns text version of the CIGAR string; user must free
158   };
159  
160  
# Line 133 | Line 162
162     samfile_t* bam_file;
163     char* fname;
164   public:
165 <   void open(const char* filename) {
165 >   void bopen(const char* filename) {
166        bam_file=samopen(filename, "rb", 0);
167        //for .sam files:
168        //bam_file=samopen(fname, "r", 0);
# Line 141 | Line 170
170           GError("Error: could not open SAM file %s!\n",filename);
171        fname=Gstrdup(filename);
172        }
173 <
174 <   GBamReader(const char* fname) {
173 >   GBamReader(const char* fn) {
174 >      bam_file=NULL;
175        fname=NULL;
176 <      open(fname);
176 >      bopen(fn);
177        }
178  
179 +   bam_header_t* header() {
180 +      return bam_file? bam_file->header : NULL;
181 +      }
182 +   void bclose() {
183 +      if (bam_file) {
184 +        samclose(bam_file);
185 +        bam_file=NULL;
186 +        }
187 +      }
188     ~GBamReader() {
189 <      samclose(bam_file);
189 >      bclose();
190        GFREE(fname);
191        }
192 +   void rewind() {
193 +     if (fname==NULL) {
194 +       GMessage("Warning: GBamReader::rewind() called without a file name.\n");
195 +       return;
196 +       }
197 +     bclose();
198 +     char* ifname=fname;
199 +     bopen(ifname);
200 +     GFREE(ifname);
201 +     }
202  
203 +   GBamRecord* next() {
204 +      if (bam_file==NULL)
205 +        GError("Warning: GBamReader::next() called with no open file.\n");
206 +      bam1_t* b = bam_init1();
207 +      if (samread(bam_file, b) >= 0) {
208 +        GBamRecord* bamrec=new GBamRecord(b, bam_file->header);
209 +        return bamrec;
210 +        }
211 +      else {
212 +        bam_destroy1(b);
213 +        return NULL;
214 +        }
215 +      }
216   };
217  
218  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines