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; //created from scratch, not from a given bam1_t* record
22 >   bool novel; //created from scratch, not from a given bam1_t* record
23     bam_header_t* bam_header;
24 <
24 >   char tag[2];
25 >   uint8_t abuf[512];
26   public:
27     GVec<GSeg> exons;
28     //created from a reader:
# Line 29 | Line 30
30        bam_header=NULL;
31        if (from_b==NULL) {
32             b=bam_init1();
33 <           //novel=true;
33 >           novel=true;
34             }
35          else {
36 <           b=from_b; //it'll take over from_b and FREE it when destroyed
37 <           //novel=false;
36 >           b=from_b; //it'll take over from_b but not free it when destroyed
37 >           novel=false;
38             }
39        bam_header=b_header;
40        setupCoordinates();//sets start, end coordinate and populates exons array
# Line 43 | Line 44
44        //make a new copy of the bam1_t record etc.
45        clear();
46        b=bam_dup1(r.b);
47 <      //novel=true; //will also free b when destroyed
47 >      novel=true; //will also free b when destroyed
48        start=r.start;
49        end=r.end;
50        exons = r.exons;
# Line 52 | Line 53
53  
54       void setupCoordinates();
55       void clear() {
56 <        //if (novel) {
57 <        bam_destroy1(b);
58 <        //   novel=false;
59 <        //   }
56 >        if (novel) {
57 >           bam_destroy1(b);
58 >           novel=false;
59 >           }
60          b=NULL;
61          exons.Clear();
62          bam_header=NULL;
# Line 97 | Line 98
98      void add_quals(const char* quals); //quality values string in Phred33 format
99      void add_aux(const char* str); //adds one aux field in plain SAM text format (e.g. "NM:i:1")
100      void add_aux(const char tag[2], char atype, int len, uint8_t *data) {
101 +      //IMPORTANT:  strings (Z,H) should include the terminal \0
102 +      int addz=0;
103 +      if ((atype=='Z' || atype=='H') && data[len-1]!=0) {
104 +        addz=1;
105 +        }
106        int ori_len = b->data_len;
107 <      b->data_len += 3 + len;
108 <      b->l_aux += 3 + len;
107 >      b->data_len += 3 + len + addz;
108 >      b->l_aux += 3 + len + addz;
109        if (b->m_data < b->data_len) {
110          b->m_data = b->data_len;
111          kroundup32(b->m_data);
# Line 107 | Line 113
113        }
114        b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
115        b->data[ori_len + 2] = atype;
116 +      if (addz) {
117 +        b->data[ori_len+len+4]=0;
118 +        }
119        memcpy(b->data + ori_len + 3, data, len);
120        }
121 +
122      void add_tag(const char tag[2], char atype, int len, uint8_t *data) {
123        //same with add_aux()
124        add_aux(tag,atype,len,data);
# Line 163 | Line 173
173     char* fname;
174   public:
175     void bopen(const char* filename) {
176 <      bam_file=samopen(filename, "rb", 0);
177 <      //for .sam files:
178 <      //bam_file=samopen(fname, "r", 0);
176 >      if (strcmp(filename, "-")==0) {
177 >        //if stdin was given, we HAVE to assume it's text SAM format, sorry
178 >        bam_file=samopen(filename, "r", 0);
179 >        }
180 >      else {
181 >      //BAM files have the zlib signature bytes at the beginning: 1F 8B 08
182 >      //if that's not present, we assume sam file
183 >      FILE* f=fopen(filename, "rb");
184 >      if (f==NULL) GError("Error opening file %s!\n", filename);
185 >      byte fsig[3];
186 >      size_t rd=fread(fsig, 1, 3, f);
187 >      if (rd<3) GError("Error reading from file %s!\n",filename);
188 >      if ((fsig[0]==0x1F && fsig[1]==0x8B && fsig[2]==0x08) ||
189 >          (fsig[0]=='B' && fsig[1]=='A' && fsig[2]=='M')) {
190 >          bam_file=samopen(filename, "rb", 0); //BAM or uncompressed BAM
191 >          }
192 >        else { //assume text SAM file
193 >          bam_file=samopen(filename, "r", 0);
194 >          }
195 >        }
196        if (bam_file==NULL)
197           GError("Error: could not open SAM file %s!\n",filename);
198        fname=Gstrdup(filename);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines