ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/gff.h
(Generate patch)
# Line 1 | Line 1
1   #ifndef GFF_H
2   #define GFF_H
3  
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
4   #include "GBase.h"
5   #include "gdna.h"
6   #include "codons.h"
# Line 19 | Line 15
15   const byte exMskMinSpliceR = 0x08;
16   const byte exMskTag = 0x80;
17   */
22 //reserved Gffnames::feats entries
23 extern const int gff_fid_mRNA;
24 extern const int gff_fid_exon;
25 extern const int gff_fid_CDS;
26 extern bool gff_warns; //show parser warnings
18  
19 + //reserved Gffnames::feats entries -- basic feature types
20 + extern const int gff_fid_mRNA; // "mRNA" feature name
21 + extern const int gff_fid_transcript; // *RNA, *transcript feature name
22 + extern const int gff_fid_exon;
23 + extern const int gff_fid_CDS; //never really used, except for display only
24 +                              //use gff_fid_exon instead
25   extern const uint GFF_MAX_LOCUS;
26   extern const uint GFF_MAX_EXON;
27   extern const uint GFF_MAX_INTRON;
28  
29 + extern const uint gfo_flag_CHILDREN_PROMOTED;
30 + extern const uint gfo_flag_HAS_ERRORS;
31 + extern const uint gfo_flag_IS_GENE;
32 + extern const uint gfo_flag_FROM_GFF3; //parsed from GFF3 formatted record
33 + extern const uint gfo_flag_BY_EXON;  //created by subfeature (exon) directly
34 +                      //(GTF2 and some chado gff3 dumps with exons given before their mRNA)
35 + extern const uint gfo_flag_IS_TRANSCRIPT; //recognized as '*RNA' or '*transcript'
36 + extern const uint gfo_flag_DISCARDED; //should not be printed under the "transcriptsOnly" directive
37 + extern const uint gfo_flag_LST_KEEP; //GffObj from GffReader::gflst is to be kept (not deallocated)
38 +                                     //when GffReader is destroyed
39 + extern const uint gfo_flag_LEVEL_MSK; //hierarchical level: 0 = no parent
40 + extern const byte gfo_flagShift_LEVEL;
41 +
42 + extern bool gff_show_warnings;
43 +
44   #define GFF_LINELEN 2048
45   #define ERR_NULL_GFNAMES "Error: GffObj::%s requires a non-null GffNames* names!\n"
46  
47  
48   enum GffExonType {
49 <  exgffNone=0,
50 <  exgffStop, //from "stop_codon" feature
49 >  exgffNone=0,  //not a recognizable exon or CDS segment
50 >  exgffStart, //from "start_codon" feature (within CDS)
51 >  exgffStop, //from "stop_codon" feature (may be outside CDS)
52    exgffCDS,  //from "CDS" feature
53    exgffUTR,  //from "UTR" feature
54 +  exgffCDSUTR, //from a merge of UTR and CDS feature
55    exgffExon, //from "exon" feature
56   };
57  
58   class GffReader;
59  
60   class GffLine {
61 +    char* _parents; //stores a copy of the Parent attribute value,
62 +       //with commas replaced by \0
63 +    int _parents_len;
64   public:
65 <    char* line;
65 >    char* dupline; //duplicate of original line
66 >    char* line; //this will have tabs replaced by \0
67 >    int llen;
68      char* gseqname;
69      char* track;
70      char* ftype; //feature name: mRNA/gene/exon/CDS
71 +    char* info; //the last, attributes' field, unparsed
72      uint fstart;
73      uint fend;
74      uint qstart; //overlap coords on query, if available
# Line 57 | Line 77
77      double score;
78      char strand;
79      bool skip;
80 +    bool is_gff3; //if the line appears to be in GFF3 format
81      bool is_cds; //"cds" and "stop_codon" features
82      bool is_exon; //"exon" and "utr" features
83      char exontype; // gffExonType
84 <    bool is_mrna;
84 >    bool is_transcript; //if current feature is *RNA or *transcript
85 >    bool is_gene; //if current feature is *gene
86      char phase;  // '.' , '0', '1' or '2'
87 <    char* gname; //gene_id or Name= value (or an otherwise parsed gene denominator
88 <                 //(for grouping isoforms)
89 <    char* info; //the last, attributes' field, unparsed
87 >    // -- allocated strings:
88 >    char* gene_name; //value of gene_name attribute (GTF) if present or Name attribute of a gene feature (GFF3)
89 >    char* gene_id; //value of gene_id attribute (GTF) if present or ID attribute of a gene feature (GFF3)
90      //
91 <    char* Parent; // if a Parent=.. attribute was found
92 <    char* ID;     // if a ID=.. attribute was parsed
91 >    char** parents; //for GTF only parents[0] is used
92 >    int num_parents;
93 >    char* ID;     // if a ID=.. attribute was parsed, or a GTF with 'transcript' line (transcript_id)
94      GffLine(GffReader* reader, const char* l); //parse the line accordingly
95 +    void discardParent() {
96 +       GFREE(_parents);
97 +       _parents_len=0;
98 +       num_parents=0;
99 +       parents=NULL;
100 +       }
101 +    char* extractAttr(const char* pre, bool caseStrict=true, bool enforce_GTF2=false);
102 +    GffLine(GffLine* l) { //a copy constructor
103 +      memcpy((void*)this, (void*)l, sizeof(GffLine));
104 +      line=NULL;
105 +      GMALLOC(line, llen+1);
106 +      memcpy(line, l->line, llen+1);
107 +      GMALLOC(dupline, llen+1);
108 +      memcpy(dupline, l->dupline, llen+1);
109 +      //--offsets within line[]
110 +      gseqname=line+(l->gseqname-l->line);
111 +      track=line+(l->track-l->line);
112 +      ftype=line+(l->ftype-l->line);
113 +      info=line+(l->info-l->line);
114 +      //Parent=Gstrdup(l->Parent);
115 +      if (l->_parents_len>0) {
116 +         _parents_len=l->_parents_len;
117 +         GMALLOC(_parents, _parents_len);
118 +         memcpy(_parents, l->_parents, _parents_len);
119 +         num_parents=l->num_parents;
120 +         for (int i=0;i<num_parents;i++) {
121 +            parents[i]=_parents+(l->parents[i] - l->_parents);
122 +            }
123 +         }
124 +      //-- allocated string copies:
125 +      ID=Gstrdup(l->ID);
126 +      if (l->gene_name!=NULL)
127 +          gene_name=Gstrdup(l->gene_name);
128 +      if (l->gene_id!=NULL)
129 +          gene_id=Gstrdup(l->gene_id);
130 +      }
131      GffLine() {
132        line=NULL;
133 +      dupline=NULL;
134        gseqname=NULL;
135        track=NULL;
136        ftype=NULL;
137        fstart=0;
138        fend=0;
139        info=NULL;
140 <      Parent=NULL;
140 >      _parents=NULL;
141 >      _parents_len=0;
142 >      parents=NULL;
143 >      num_parents=0;
144        ID=NULL;
145 <      gname=NULL;
145 >      gene_name=NULL;
146 >      gene_id=NULL;
147        skip=true;
148        qstart=0;
149        qend=0;
150        qlen=0;
151        exontype=0;
152        is_cds=false;
153 <      is_mrna=false;
153 >      is_gff3=false;
154 >      is_transcript=false;
155 >      is_gene=false;
156        is_exon=false;
157        }
158      ~GffLine() {
159 +      GFREE(dupline);
160        GFREE(line);
161 <      GFREE(Parent);
161 >      GFREE(_parents);
162 >      GFREE(parents);
163        GFREE(ID);
164 <      GFREE(gname);
164 >      GFREE(gene_name);
165 >      GFREE(gene_id);
166       }
167   };
168  
# Line 104 | Line 173
173     GffAttr(int an_id, const char* av=NULL) {
174       attr_id=an_id;
175       attr_val=NULL;
176 <     if (av!=NULL) {
108 <       char* lastch = (char*)(av+strlen(av)-1);
109 <       //remove spaces at the end:
110 <       while (*lastch==' ' && lastch!=av) lastch--;
111 <       lastch[1]=0;
112 <       //practical use: if it doesn't have any spaces just strip those useless double quotes
113 <       if (av[0]=='"' && strpbrk(av+1," ;")==NULL) {
114 <               if (*lastch=='"') *lastch=0;
115 <               attr_val=Gstrdup(av+1);
116 <               }
117 <          else attr_val=Gstrdup(av);
118 <       }
119 <
176 >     setValue(av);
177       }
178    ~GffAttr() {
179       GFREE(attr_val);
180       }
181 +  void setValue(const char* av) {
182 +     if (attr_val!=NULL) {
183 +        GFREE(attr_val);
184 +        }
185 +     if (av==NULL || av[0]==0) return;
186 +     //trim spaces
187 +     const char* vstart=av;
188 +     while (*vstart==' ') av++;
189 +     const char* vend=vstart;
190 +     bool keep_dq=false;
191 +     while (vend[1]!=0) {
192 +        if (*vend==' ' && vend[1]!=' ') keep_dq=true;
193 +          else if (*vend==';') keep_dq=true;
194 +        vend++;
195 +        }
196 +     //remove spaces at the end:
197 +     while (*vend==' ' && vend!=vstart) vend--;
198 +     //practical clean-up: if it doesn't have any internal spaces just strip those useless double quotes
199 +     if (!keep_dq && *vstart=='"' && *vend=='"') {
200 +               vend--;
201 +               vstart++;
202 +               }
203 +     attr_val=Gstrdup(vstart, vend);
204 +     }
205    bool operator==(GffAttr& d){
206        return (this==&d);
207        }
# Line 139 | Line 220
220   class GffNameInfo {
221    friend class GffNameList;
222   protected:
142   //unsigned char shared;
223     int idx;
224   public:
225     char* name;
# Line 147 | Line 227
227     GffNameInfo(const char* n) {
228       name=Gstrdup(n);
229       }
150   /*
151   GffNameInfo(const char* n, bool strshared=false) {
152     idx=-1;
153     if (strshared) {
154        shared=1;
155        name=(char *)n;
156        }
157     else {
158        shared=0;
159        name = (n==NULL) ? NULL : Gstrdup(n);
160        }
161     }
162     */
230  
231     ~GffNameInfo() {
232 <      //if (shared==0)
233 <          GFREE(name);
167 <    }
232 >      GFREE(name);
233 >     }
234  
235     bool operator==(GffNameInfo& d){
236         return (strcmp(this->name, d.name)==0);
# Line 177 | Line 243
243       }
244   };
245  
180 /*
181 int compareNameId(const pointer item1, const pointer item2) {
182  GffNameInfo* ni1=(GffNameInfo*)item1;
183  GffNameInfo* ni2=(GffNameInfo*)item2;
184  return (ni1->id > ni2->id )? 1 : (ni1->id==ni2->id ? 0 : -1);
185 }
186 */
187
246   class GffNameList:public GList<GffNameInfo> {
247    friend class GffNameInfo;
248    friend class GffNames;
# Line 225 | Line 283
283     idlast=fidx;
284     return fidx;
285     }
286 +
287 + int addNewName(const char* tname) {
288 +    GffNameInfo* f=new GffNameInfo(tname);
289 +    int fidx=this->Add(f);
290 +    f->idx=fidx;
291 +    byName.shkAdd(f->name,f);
292 +    return fidx;
293 +    }
294 +
295   int getId(const char* tname) { //only returns a name id# if found
296      GffNameInfo* f=byName.Find(tname);
297      if (f==NULL) return -1;
298      return f->idx;
299      }
300 < int removeName(const char* tname) {
300 > int removeName() {
301     GError("Error: removing names from GffNameList not allowed!\n");
302     return -1;
303     }
# Line 242 | Line 309
309     GffNameList tracks;
310     GffNameList gseqs;
311     GffNameList attrs;
312 <   GffNameList feats; //feature names - anything except 'mRNA', 'exon', 'CDS' gets stored here
312 >   GffNameList feats; //feature names: 'mRNA', 'exon', 'CDS' etc.
313     GffNames():tracks(),gseqs(),attrs(), feats() {
314      numrefs=0;
315      //the order below is critical!
316      //has to match: gff_fid_mRNA, gff_fid_exon, gff_fid_CDS
317      feats.addStatic("mRNA");//index 0=gff_fid_mRNA
318 +    feats.addStatic("transcript");//index 1=gff_fid_transcript
319      feats.addStatic("exon");//index 1=gff_fid_exon
320      feats.addStatic("CDS"); //index 2=gff_fid_CDS
321      }
# Line 270 | Line 338
338   class GffAttrs:public GList<GffAttr> {
339    public:
340      GffAttrs():GList<GffAttr>(false,true,false) { }
341 +    void add_or_update(GffNames* names, const char* attrname, const char* val) {
342 +      int aid=names->attrs.getId(attrname);
343 +      if (aid>=0) {
344 +         //attribute found in the dictionary
345 +         for (int i=0;i<Count();i++) {
346 +            //do we have it?
347 +            if (aid==Get(i)->attr_id) {
348 +                //update the value
349 +                Get(i)->setValue(val);
350 +                return;
351 +                }
352 +            }
353 +         }
354 +        else {
355 +         aid=names->attrs.addNewName(attrname);
356 +         }
357 +      this->Add(new GffAttr(aid, val));
358 +      }
359 +      
360      char* getAttr(GffNames* names, const char* attrname) {
361        int aid=names->attrs.getId(attrname);
362        if (aid>=0)
# Line 277 | Line 364
364            if (aid==Get(i)->attr_id) return Get(i)->attr_val;
365        return NULL;
366        }
367 +    char* getAttr(int aid) {
368 +      if (aid>=0)
369 +        for (int i=0;i<Count();i++)
370 +          if (aid==Get(i)->attr_id) return Get(i)->attr_val;
371 +      return NULL;
372 +      }
373   };
374  
375  
376   class GffExon : public GSeg {
377   public:
378 <  void* uptr; //for later exensibility
378 >  void* uptr; //for later extensions
379    GffAttrs* attrs; //other attributes kept for this exon
380    double score; // gff score column
381    char phase; //GFF phase column - for CDS segments only
# Line 318 | Line 411
411     return attrs->getAttr(names, atrname);
412     }
413  
414 + char* getAttr(int aid) {
415 +   if (attrs==NULL) return NULL;
416 +   return attrs->getAttr(aid);
417 +   }
418 +
419   ~GffExon() { //destructor
420     if (attrs!=NULL) delete attrs;
421     }
# Line 344 | Line 442
442              //'-' : (start,end) are relative to the reverse complement of xstart..xend region
443     //--
444     char* gffID; // ID name for mRNA (parent) feature
445 <   char* gname; // value of Name attribute (if given)
445 >   char* gene_name; //value of gene_name attribute (GTF) if present or Name attribute of the parent gene feature (GFF3)
446 >   char* geneID; //value of gene_id attribute (GTF) if present or ID attribute of a parent gene feature (GFF3)
447 >   unsigned int flags;
448     //-- friends:
449     friend class GffReader;
450     friend class GffExon;
451   public:
452 <  bool hasErrors; //overlapping exons, or too short introns, etc.
353 <  static GffNames* names; // common string storage that holds the various attribute names etc.
452 >  static GffNames* names; // dictionary storage that holds the various attribute names etc.
453    int track_id; // index of track name in names->tracks
454    int gseq_id; // index of genomic sequence name in names->gseqs
455    int ftype_id; // index of this record's feature name in names->feats, or the special gff_fid_mRNA value
456 <  int subftype_id; //index of child subfeature name in names->feats (that subfeature stored in "exons")
456 >  int exon_ftype_id; //index of child subfeature name in names->feats (that subfeature stored in "exons")
457                     //if ftype_id==gff_fid_mRNA then this value is ignored
458    GList<GffExon> exons; //for non-mRNA entries, these can be any subfeature of type subftype_id
459 +  GPVec<GffObj> children;
460 +  GffObj* parent;
461    int udata; //user data, flags etc.
462    void* uptr; //user pointer (to a parent object, cluster, locus etc.)
463    GffObj* ulink; //link to another GffObj (user controlled field)
# Line 366 | Line 467
467    uint CDstart; //CDS start coord
468    uint CDend;   //CDS end coord
469    char CDphase; //initial phase for CDS start
470 +  bool hasErrors() { return ((flags & gfo_flag_HAS_ERRORS)!=0); }
471 +  void hasErrors(bool v) {
472 +      if (v) flags |= gfo_flag_HAS_ERRORS;
473 +        else flags &= ~gfo_flag_HAS_ERRORS;
474 +      }
475 +  bool fromGff3() { return ((flags & gfo_flag_FROM_GFF3)!=0); }
476 +  void fromGff3(bool v) {
477 +      if (v) flags |= gfo_flag_FROM_GFF3;
478 +        else flags &= ~gfo_flag_FROM_GFF3;
479 +      }
480 +  bool createdByExon() { return ((flags & gfo_flag_BY_EXON)!=0); }
481 +  void createdByExon(bool v) {
482 +      if (v) flags |= gfo_flag_BY_EXON;
483 +        else flags &= ~gfo_flag_BY_EXON;
484 +      }
485 +  bool isGene() { return ((flags & gfo_flag_IS_GENE)!=0); }
486 +  void isGene(bool v) {
487 +      if (v) flags |= gfo_flag_IS_GENE;
488 +        else flags &= ~gfo_flag_IS_GENE;
489 +      }
490 +  bool isDiscarded() { return ((flags & gfo_flag_DISCARDED)!=0); }
491 +  void isDiscarded(bool v) {
492 +      if (v) flags |= gfo_flag_DISCARDED;
493 +        else flags &= ~gfo_flag_DISCARDED;
494 +      }
495 +      
496 +  bool isUsed() { return ((flags & gfo_flag_LST_KEEP)!=0); }
497 +  void isUsed(bool v) {
498 +      if (v) flags |= gfo_flag_LST_KEEP;
499 +        else flags &= ~gfo_flag_LST_KEEP;
500 +      }
501 +  bool isTranscript() { return ((flags & gfo_flag_IS_TRANSCRIPT)!=0); }
502 +  void isTranscript(bool v) {
503 +      if (v) flags |= gfo_flag_IS_TRANSCRIPT;
504 +        else flags &= ~gfo_flag_IS_TRANSCRIPT;
505 +      }
506 +  bool promotedChildren() { return ((flags & gfo_flag_CHILDREN_PROMOTED)!=0); }
507 +  void promotedChildren(bool v) {
508 +    if (v) flags |= gfo_flag_CHILDREN_PROMOTED;
509 +      else flags &= ~gfo_flag_CHILDREN_PROMOTED;
510 +     }
511 +  void setLevel(byte v) {
512 +    if (v==0) flags &= ~gfo_flag_LEVEL_MSK;
513 +         else flags &= ~(((uint)v) << gfo_flagShift_LEVEL);
514 +    }
515 +  byte incLevel() {
516 +    uint v=((flags & gfo_flag_LEVEL_MSK) >> gfo_flagShift_LEVEL);
517 +    v++;
518 +    flags &= ~(v << gfo_flagShift_LEVEL);
519 +    return v;
520 +    }
521 +  byte getLevel() {
522 +    return ((byte)((flags & gfo_flag_LEVEL_MSK) >> gfo_flagShift_LEVEL));
523 +    }
524  
525 <  bool ismRNA() { return (ftype_id==gff_fid_mRNA); }
525 >  bool isValidTranscript() {
526 >    //return (ftype_id==gff_fid_mRNA && exons.Count()>0);
527 >    return (isTranscript() && exons.Count()>0);
528 >    }
529 >  
530  
531    int addExon(uint segstart, uint segend, double sc=0, char fr='.',
532               int qs=0, int qe=0, bool iscds=false, char exontype=0);
533  
534 <  int addExon(GffLine* gl, bool keepAttr=false, bool noExonAttr=true);
534 >  int addExon(GffReader* reader, GffLine* gl, bool keepAttr=false, bool noExonAttr=true);
535 >
536    void removeExon(int idx);
537 <  /*
378 <  uint  gstart; // global feature coordinates on genomic sequence
379 <  uint  gend;   // ALWAYS gstart <= gend
380 <  */
537 >  void removeExon(GffExon* p);
538    char  strand; //true if features are on the reverse complement strand
539    double gscore;
540    double uscore; //custom, user-computed score, if needed
541    int covlen; //total coverage of  reference genomic sequence (sum of maxcf segment lengths)
542 <
542 >  
543     //--------- optional data:
544    int qlen; //query length, start, end - if available
545    int qstart;
# Line 394 | Line 551
551     //if gfline->Parent!=NULL then this will also add the first sub-feature
552     // otherwise, only the main feature is created
553    void clearAttrs() {
554 <    if (attrs!=NULL) delete attrs;
554 >    if (attrs!=NULL) {
555 >      bool sharedattrs=(exons.Count()>0 && exons[0]->attrs==attrs);
556 >      delete attrs; attrs=NULL;
557 >      if (sharedattrs) exons[0]->attrs=NULL;
558 >      }
559      }
560 <  GffObj(char* anid=NULL):GSeg(0,0), exons(true,true,true) { //exons: sorted, free, unique
560 >  GffObj(char* anid=NULL):GSeg(0,0), exons(true,true,false), children(1,false) {
561 >                                   //exons: sorted, free, non-unique
562         gffID=NULL;
563         uptr=NULL;
564         ulink=NULL;
565 +       flags=0;
566         udata=0;
567 <       hasErrors=false;
567 >       parent=NULL;
568         ftype_id=-1;
569 <       subftype_id=-1;
569 >       exon_ftype_id=-1;
570         if (anid!=NULL) gffID=Gstrdup(anid);
571         gffnames_ref(names);
572         qlen=0;
# Line 417 | Line 580
580         CDphase=0;
581         gseq_id=-1;
582         track_id=-1;
420       /*
421       gstart=0;
422       gend=0;
423       */
583         xstart=0;
584         xend=0;
585         xstatus=0;
586 <       strand=0;
586 >       strand='.';
587         gscore=0;
588         uscore=0;
589         attrs=NULL;
590         covlen=0;
591 <       gname=NULL;
591 >       gene_name=NULL;
592 >       geneID=NULL;
593         }
594     ~GffObj() {
595         GFREE(gffID);
596 <       GFREE(gname);
597 <       if (attrs!=NULL) delete attrs;
596 >       GFREE(gene_name);
597 >       GFREE(geneID);
598 >       clearAttrs();
599         gffnames_unref(names);
600         }
440   /*
441    void addFeature(GffLine* gfline);
442    int getFeatureId(int fnid);
443    int getFeatureId(const char* fname);
444    GFeature* hasFeature(char* parentID);
445    void promote(GFeature* subf, GffLine* gfline); */
601     //--------------
602 <   GffObj* finalize(bool mergeCloseExons=false); //finalize parsing: must be called in order to merge adjacent/close proximity subfeatures
603 <   void parseAttrs(GffAttrs*& atrlist, char* info, bool noExonAttr=false);
602 >   GffObj* finalize(GffReader* gfr, bool mergeCloseExons=false,
603 >               bool keepAttrs=false, bool noExonAttr=true);
604 >               //complete parsing: must be called in order to merge adjacent/close proximity subfeatures
605 >   void parseAttrs(GffAttrs*& atrlist, char* info, bool isExon=false);
606     const char* getSubfName() { //returns the generic feature type of the entries in exons array
607 <     int sid=subftype_id;
607 >     int sid=exon_ftype_id;
608       if (sid==gff_fid_exon && isCDS) sid=gff_fid_CDS;
609       return names->feats.getName(sid);
610       }
611 +   bool monoFeature() {
612 +     return (exons.Count()==0 ||
613 +          (exons.Count()==1 && exon_ftype_id==ftype_id));
614 +     }
615 +
616 +   bool hasCDS() { return (CDstart>0); }
617 +
618     const char* getFeatureName() {
619       return names->feats.getName(ftype_id);
620       }
621 <   void addAttr(const char* attrname, char* attrvalue);
621 >   void setFeatureName(const char* feature);
622 >  
623 >   void addAttr(const char* attrname, const char* attrvalue);
624 >   int removeAttr(const char* attrname, const char* attrval=NULL);
625 >   int removeAttr(int aid, const char* attrval=NULL);
626 >   int removeExonAttr(GffExon& exon, const char* attrname, const char* attrval=NULL);
627 >   int removeExonAttr(GffExon& exon, int aid, const char* attrval=NULL);
628     const char* getAttrName(int i) {
629       if (attrs==NULL) return NULL;
630       return names->attrs.getName(attrs->Get(i)->attr_id);
631       }
632 <   char* getAttr(const char* atrname) {
633 <     if (attrs==NULL || names==NULL || atrname==NULL) return NULL;
634 <     return attrs->getAttr(names, atrname);
632 >   char* getAttr(const char* attrname, bool checkFirstExon=false) {
633 >     if (names==NULL || attrname==NULL) return NULL;
634 >     char* r=NULL;
635 >     if (attrs==NULL) {
636 >         if (!checkFirstExon) return NULL;
637 >         }
638 >       else r=attrs->getAttr(names, attrname);
639 >     if (r!=NULL) return r;
640 >     if (checkFirstExon && exons.Count()>0) {
641 >        r=exons[0]->getAttr(names, attrname);
642 >        }
643 >     return r;
644       }
645  
646 +   char* getExonAttr(GffExon* exon, const char* attrname) {
647 +      if (exon==NULL || attrname==NULL) return NULL;
648 +      return exon->getAttr(names, attrname);
649 +      }
650 +
651 +   char* getExonAttr(int exonidx, const char* attrname) {
652 +      if (exonidx<0 || exonidx>=exons.Count() || attrname==NULL) return NULL;
653 +      return exons[exonidx]->getAttr(names, attrname);
654 +      }
655 +
656     char* getAttrValue(int i) {
657       if (attrs==NULL) return NULL;
658       return attrs->Get(i)->attr_val;
# Line 471 | Line 660
660     const char* getGSeqName() {
661       return names->gseqs.getName(gseq_id);
662       }
663 +
664 +   const char* getRefName() {
665 +     return names->gseqs.getName(gseq_id);
666 +     }
667 +   void setRefName(const char* newname);
668 +  
669     const char* getTrackName() {
670       return names->tracks.getName(track_id);
671       }
672 <   /*
478 <   bool overlap(GffObj* mrna) {
479 <     //basic overlap: just segment ends
480 <     return overlap(*mrna);
481 <     }
482 <   bool overlap(GffObj& d) {
483 <      //ignores strand and gseq_id -- the user must do that in advance
484 <     // just rough locus overlap, exons may not overlap
485 <      return (gstart<d.gstart ? (d.gstart<=gend) : (gstart<=d.gend));
486 <      }
487 <
488 <    bool overlap(uint s, uint e) {
489 <      if (s>e) swap(s,e);
490 <      return (gstart<s ? (s<=gend) : (gstart<=e));
491 <      }
492 <    */
493 <    bool exonOverlap(uint s, uint e) {//check if ANY exon overlaps given segment
672 >   bool exonOverlap(uint s, uint e) {//check if ANY exon overlaps given segment
673        //ignores strand!
674        if (s>e) swap(s,e);
675        for (int i=0;i<exons.Count();i++) {
# Line 506 | Line 685
685              if (exons[i]->start>m.exons[j]->end) continue;
686              if (m.exons[j]->start>exons[i]->end) break;
687              //-- overlap if we are here:
509            // if (exons[i]->overlap(m.exons[j])) return true;
688              return true;
689              }
690           }
691        return false;
692        }
693 <
693 >    
694      int exonOverlapIdx(uint s, uint e, int* ovlen=NULL) {
695 <      //return the exons' index for the overlapping exon
695 >      //return the exons' index for the overlapping OR ADJACENT exon
696        //ovlen, if given, will return the overlap length
697        if (s>e) swap(s,e);
698 +      s--;e++; //to also catch adjacent exons
699        for (int i=0;i<exons.Count();i++) {
700              if (exons[i]->start>e) break;
701              if (s>exons[i]->end) continue;
702              //-- overlap if we are here:
703              if (ovlen!=NULL) {
704 +              s++;e--;
705                int ovlend= (exons[i]->end>e) ? e : exons[i]->end;
706                *ovlen= ovlend - ((s>exons[i]->start)? s : exons[i]->start)+1;
707                }
# Line 530 | Line 710
710        *ovlen=0;
711        return -1;
712        }
713 <
713 >    
714      int exonOverlapLen(GffObj& m) {
715        if (start>m.end || m.start>end) return 0;
716        int i=0;
# Line 641 | Line 821
821     bool operator>(GffObj& d){
822        if (gseq_id!=d.gseq_id) return (gseq_id>d.gseq_id);
823        if (start==d.start) {
824 <         if (end==d.end) return strcmp(gffID, d.gffID)>0;
825 <                      else return end>d.end;
824 >         if (getLevel()==d.getLevel()) {
825 >             if (end==d.end) return (strcmp(gffID, d.gffID)>0);
826 >                        else return (end>d.end);
827 >             } else return (getLevel()>d.getLevel());
828           } else return (start>d.start);
829        }
830     bool operator<(GffObj& d){
831       if (gseq_id!=d.gseq_id) return (gseq_id<d.gseq_id);
832       if (start==d.start) {
833 <        if (end==d.end) return strcmp(gffID, d.gffID)<0;
833 >         if (getLevel()==d.getLevel()) {
834 >            if (end==d.end) return strcmp(gffID, d.gffID)<0;
835                       else return end<d.end;
836 +            } else return (getLevel()<d.getLevel());
837          } else return (start<d.start);
838       }
839     char* getID() { return gffID; }
840 <   char* getGene() { return gname; }
841 <   //void calcScore();
842 <   /*int exonCount() { return exoncount; }
843 <   GffExon* getExonSegs() { return exons; }
844 <   int cdsCount() { return cdscount; }
845 <   GffExon* getCDSegs() { return cds; } */
846 <
847 <   /* bool validateMapping(int qrylen, bool pmap_parsing, int minpid,
848 <                                         int mincov, int maxintron);*/
849 <   /* int addSeg(char* feat, int nstart, int nend, int sc=0, char fr='.',
666 <                                          char* tid=NULL, char* tinfo=NULL);*/
840 >   char* getGeneID() { return geneID; }
841 >   char* getGeneName() { return gene_name; }
842 >   void setGeneName(const char* gname) {
843 >        GFREE(gene_name);
844 >        if (gname) gene_name=Gstrdup(gname);
845 >        }
846 >   void setGeneID(const char* gene_id) {
847 >        GFREE(geneID);
848 >        if (gene_id) geneID=Gstrdup(gene_id);
849 >        }
850     int addSeg(GffLine* gfline);
851     int addSeg(int fnid, GffLine* gfline);
669    // (int fnid, char* feat, int nstart, int nend, int sc=0,
670    //                           char fr='.', char* tid=NULL, char* tinfo=NULL);
852     void getCDSegs(GArray<GffCDSeg>& cds);
853 <   void printGxfLine(FILE* fout, char* tlabel, char* gseqname, bool iscds,
854 <                                uint segstart, uint segend, int exidx, char phase, bool gff3);
855 <   void printGxf(FILE* fout, GffPrintMode gffp=pgffExon, char* tlabel=NULL);
856 <   void printGtf(FILE* fout, char* tlabel=NULL) {
853 >
854 >   void updateExonPhase(); //for CDS-only features, updates GExon::phase
855 >
856 >   void printGxfLine(FILE* fout, const char* tlabel, const char* gseqname,
857 >          bool iscds, uint segstart, uint segend, int exidx, char phase, bool gff3);
858 >   void printGxf(FILE* fout, GffPrintMode gffp=pgffExon,
859 >             const char* tlabel=NULL, const char* gfparent=NULL);
860 >   void printGtf(FILE* fout, const char* tlabel=NULL) {
861        printGxf(fout, pgtfAny, tlabel);
862        }
863 <   void printGff(FILE* fout, char* tlabel=NULL) {
864 <      printGxf(fout, pgffAny, tlabel);
863 >   void printGff(FILE* fout, const char* tlabel=NULL,
864 >                                const char* gfparent=NULL) {
865 >      printGxf(fout, pgffAny, tlabel, gfparent);
866        }
867 <   void print_mRNAGff(FILE* fout, char* tlabel=NULL, bool showCDS=false) {
868 <      if (ismRNA())
869 <         printGxf(fout, showCDS ? pgffBoth : pgffExon, tlabel);
867 >   void printTranscriptGff(FILE* fout, char* tlabel=NULL,
868 >                            bool showCDS=false, const char* gfparent=NULL) {
869 >      if (isValidTranscript())
870 >         printGxf(fout, showCDS ? pgffBoth : pgffExon, tlabel, gfparent);
871        }
872     void printSummary(FILE* fout=NULL);
873     void getCDS_ends(uint& cds_start, uint& cds_end);
874     void mRNA_CDS_coords(uint& cds_start, uint& cds_end);
875     char* getSpliced(GFaSeqGet* faseq, bool CDSonly=false, int* rlen=NULL,
876             uint* cds_start=NULL, uint* cds_end=NULL, GList<GSeg>* seglst=NULL);
877 +    char* getUnspliced(GFaSeqGet* faseq, int* rlen, GList<GSeg>* seglst);
878     char* getSplicedTr(GFaSeqGet* faseq, bool CDSonly=true, int* rlen=NULL);
879     //bool validCDS(GFaSeqGet* faseq); //has In-Frame Stop Codon ?
880     bool empty() { return (start==0); }
881   };
882  
695 //int cmpGMapScore(const pointer a, const pointer b);
696
883   typedef bool GffRecFunc(GffObj* gobj, void* usrptr1, void* usrptr2);
884   //user callback after parsing a mapping object:
885   // Returns: "done with it" status:
# Line 711 | Line 897
897     //int fcount;//number of features on this gseq
898     uint mincoord;
899     uint maxcoord;
900 <   GList<GffObj> gflst;
901 <   GSeqStat(int id=-1, char* name=NULL):gflst(true,false,false) {
900 >   uint maxfeat_len; //maximum feature length on this genomic sequence
901 >   GffObj* maxfeat;
902 >   GSeqStat(int id=-1, char* name=NULL) {
903       gseqid=id;
904       gseqname=name;
905       mincoord=MAXUINT;
906       maxcoord=0;
907 +     maxfeat_len=0;
908 +     maxfeat=NULL;
909       }
910     bool operator>(GSeqStat& g) {
911      return (gseqid>g.gseqid);
# Line 733 | Line 922
922   int gfo_cmpByLoc(const pointer p1, const pointer p2);
923  
924   class GfList: public GList<GffObj> {
925 < //just adding the option to sort by genomic sequence and coordinate
925 >  //just adding the option to sort by genomic sequence and coordinate
926 >   bool mustSort;
927   public:
928     GfList(bool sortbyloc=false):GList<GffObj>(false,false,false) {
929 <    if (sortbyloc) this->setSorted((GCompareProc*)gfo_cmpByLoc);
930 <    }
929 >     //GffObjs in this list are NOT deleted when the list is cleared
930 >     //-- for deallocation of these objects, call freeAll() or freeUnused() as needed
931 >     mustSort=sortbyloc;
932 >     }
933 >   void sortedByLoc(bool v=true) {
934 >     bool prev=mustSort;
935 >     mustSort=v;
936 >     if (fCount>0 && mustSort && !prev) {
937 >       this->setSorted((GCompareProc*)gfo_cmpByLoc);
938 >       }
939 >     }
940 >   void finalize(GffReader* gfr, bool mergeCloseExons,
941 >                bool keepAttrs=false, bool noExonAttr=true) { //if set, enforce sort by locus
942 >     if (mustSort) { //force (re-)sorting
943 >        this->setSorted(false);
944 >        this->setSorted((GCompareProc*)gfo_cmpByLoc);
945 >        }
946 >     int delcount=0;
947 >     for (int i=0;i<Count();i++) {
948 >       //finish the parsing for each GffObj
949 >       fList[i]->finalize(gfr, mergeCloseExons, keepAttrs, noExonAttr);
950 >       }
951 >     if (delcount>0) this->Pack();
952 >     }
953 >   void freeAll() {
954 >     for (int i=0;i<fCount;i++) {
955 >       delete fList[i];
956 >       fList[i]=NULL;
957 >       }
958 >     Clear();
959 >     }
960 >   void freeUnused() {
961 >     for (int i=0;i<fCount;i++) {
962 >       if (fList[i]->isUsed()) continue;
963 >       //inform the children
964 >       for (int c=0;c<fList[i]->children.Count();c++) {
965 >          fList[i]->children[c]->parent=NULL;
966 >          }
967 >       delete fList[i];
968 >       fList[i]=NULL;
969 >       }
970 >     Clear();
971 >     }
972 >
973   };
974  
975 + class GfoHolder {
976 + public:
977 +   int idx; //position in GffReader::gflst array
978 +   GffObj* gffobj;
979 +   GfoHolder(GffObj* gfo=NULL, int i=0) {
980 +     idx=i;
981 +     gffobj=gfo;
982 +     }
983 + };
984 +
985 + class CNonExon { //utility class used in subfeature promotion
986 + public:
987 +   int idx;
988 +   GffObj* parent;
989 +   GffExon* exon;
990 +   GffLine* gffline;
991 +   CNonExon(int i, GffObj* p, GffExon* e, GffLine* gl) {
992 +     parent=p;
993 +     exon=e;
994 +     idx=i;
995 +     gffline=new GffLine(gl);
996 +     }
997 +  ~CNonExon() {
998 +     delete gffline;
999 +     }
1000 + };
1001 +
1002 +
1003   class GffReader {
1004    friend class GffObj;
1005    friend class GffLine;
# Line 747 | Line 1007
1007    off_t fpos;
1008    int buflen;
1009   protected:
1010 +  bool gff_warns; //warn about duplicate IDs, etc. even when they are on different chromosomes
1011    FILE* fh;
1012    char* fname;  //optional fasta file with the underlying genomic sequence to be attached to this reader
1013    GffNames* names; //just a pointer to the global static Gff names repository in GffObj
1014    GffLine* gffline;
1015 <  bool mrnaOnly; //read only mRNAs ? (exon/CDS features only)
1016 <  bool sortbyloc; //sort by location: genomic sequence and start coordinate
1017 <  GHash<GffObj> phash; //transcript_id (Parent~Contig) => GffObj pointer
1015 >  bool transcriptsOnly; //keep only transcripts w/ their exon/CDS features
1016 >  GHash<int> discarded_ids; //for transcriptsOnly mode, keep track
1017 >                            // of discarded parent IDs
1018 >  GHash<GfoHolder> phash; //transcript_id+contig (Parent~Contig) => [gflst index, GffObj]
1019 >  GHash<int> tids; //transcript_id uniqueness
1020    char* gfoBuildId(const char* id, const char* ctg);
1021    void gfoRemove(const char* id, const char* ctg);
1022 <  void gfoAdd(const char* id, const char* ctg, GffObj* gfo);
1023 <  GffObj* gfoFind(const char* id, const char* ctg);
1022 >  GfoHolder* gfoAdd(const char* id, const char* ctg, GffObj* gfo, int idx);
1023 >  GfoHolder* gfoFind(const char* id, const char* ctg);
1024 >  CNonExon* subfPoolCheck(GffLine* gffline, GHash<CNonExon>& pex, char*& subp_name);
1025 >  void subfPoolAdd(GHash<CNonExon>& pex, GfoHolder* newgfo);
1026 >  GfoHolder* promoteFeature(CNonExon* subp, char*& subp_name, GHash<CNonExon>& pex,
1027 >                                  bool keepAttr, bool noExonAttr);
1028   public:
1029 <  GfList gflst; //all read gflst
1029 >  GfList gflst; //accumulate GffObjs being read
1030 >  GfoHolder* newGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr,
1031 >                               GffObj* parent=NULL, GffExon* pexon=NULL);
1032 >  GfoHolder* replaceGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr, int replaceidx);
1033 >  GfoHolder* updateGffRec(GfoHolder* prevgfo, GffLine* gffline,
1034 >                                         bool keepAttr);
1035 >  GfoHolder* updateParent(GfoHolder* newgfh, GffObj* parent);
1036 >  bool addExonFeature(GfoHolder* prevgfo, GffLine* gffline, GHash<CNonExon>& pex, bool noExonAttr);
1037    GList<GSeqStat> gseqstats; //list of all genomic sequences seen by this reader, accumulates stats
1038 <  GffReader(FILE* f, bool justmrna=false, bool sort=false):phash(false),gflst(sort), gseqstats(true,true,true) {
1038 >  GffReader(FILE* f=NULL, bool t_only=false, bool sortbyloc=false):discarded_ids(true),
1039 >                       phash(true), tids(true), gflst(sortbyloc), gseqstats(true,true,true) {
1040 >      gff_warns=gff_show_warnings;
1041        names=NULL;
1042        gffline=NULL;
1043 <      mrnaOnly=justmrna;
768 <      sortbyloc=sort;
1043 >      transcriptsOnly=t_only;
1044        fpos=0;
1045        fname=NULL;
1046        fh=f;
1047        GMALLOC(linebuf, GFF_LINELEN);
1048        buflen=GFF_LINELEN-1;
1049        }
1050 <  GffReader(char* fn, bool justmrna=false, bool sort=false):phash(false),gflst(sort) {
1050 >  void init(FILE *f, bool t_only=false, bool sortbyloc=false) {
1051 >      fname=NULL;
1052 >      fh=f;
1053 >      if (fh!=NULL) rewind(fh);
1054 >      fpos=0;
1055 >      transcriptsOnly=t_only;
1056 >      gflst.sortedByLoc(sortbyloc);
1057 >      }
1058 >  GffReader(char* fn, bool t_only=false, bool sort=false):discarded_ids(true), phash(true),
1059 >                             tids(true),gflst(sort),gseqstats(true,true,true) {
1060 >      gff_warns=gff_show_warnings;
1061        names=NULL;
1062        fname=Gstrdup(fn);
1063 <      mrnaOnly=justmrna;
779 <      sortbyloc=sort;
1063 >      transcriptsOnly=t_only;
1064        fh=fopen(fname, "rb");
1065        fpos=0;
1066        gffline=NULL;
# Line 784 | Line 1068
1068        buflen=GFF_LINELEN-1;
1069        }
1070  
1071 <  virtual ~GffReader() {
1071 > ~GffReader() {
1072 >      delete gffline;
1073        gffline=NULL;
1074        fpos=0;
1075 <      delete gffline;
1075 >      gflst.freeUnused();
1076        gflst.Clear();
1077 +      discarded_ids.Clear();
1078        phash.Clear();
1079        gseqstats.Clear();
1080        GFREE(fname);
1081        GFREE(linebuf);
1082        }
1083  
1084 +  void showWarnings(bool v=true) {
1085 +      gff_warns=v;
1086 +      gff_show_warnings=v;
1087 +      }
1088 +      
1089    GffLine* nextGffLine();
1090  
1091 <  // parse -> block parsing functions -- do not use,
1092 <  // they always assume that subfeatures (exons) are grouped together by parent
802 <  GffObj* parse(bool keepAttr=false, bool noExonAttr=true);
803 <  void parseAll(GffRecFunc* gproc, bool keepAttr=false, bool noExonAttr=true, void* userptr1=NULL, void* userptr2=NULL);
804 <
805 <  // use this instead of parse: load all subfeatures, re-group them in memory:
806 <  void readAll(bool keepAttr=false, bool mergeCloseExons=false, bool noExonAttr=true); //just reads all gff records into gflst GList
1091 >  // load all subfeatures, re-group them:
1092 >  void readAll(bool keepAttr=false, bool mergeCloseExons=false, bool noExonAttr=true);
1093  
1094   }; // end of GffReader
1095  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines