ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBam.h
(Generate patch)
# Line 82 | Line 82
82        b->core.flag=flags;
83        }
84  
85
86
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 121 | 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 149 | Line 163
163     char* fname;
164   public:
165     void bopen(const char* filename) {
166 <      bam_file=samopen(filename, "rb", 0);
167 <      //for .sam files:
168 <      //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);
189        }
190 <   GBamReader(const char* fname) {
190 >   GBamReader(const char* fn) {
191        bam_file=NULL;
192        fname=NULL;
193 <      bopen(fname);
193 >      bopen(fn);
194        }
195  
196     bam_header_t* header() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines