ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/gff.h
Revision: 225
Committed: Mon Mar 26 12:07:13 2012 UTC (7 years, 5 months ago) by gpertea
File size: 35201 byte(s)
Log Message:
Line File contents
1 #ifndef GFF_H
2 #define GFF_H
3
4 #include "GBase.h"
5 #include "gdna.h"
6 #include "codons.h"
7 #include "GFaSeqGet.h"
8 #include "GList.hh"
9 #include "GHash.hh"
10
11 /*
12 const byte exMskMajSpliceL = 0x01;
13 const byte exMskMajSpliceR = 0x02;
14 const byte exMskMinSpliceL = 0x04;
15 const byte exMskMinSpliceR = 0x08;
16 const byte exMskTag = 0x80;
17 */
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_HAS_GFF_ID; //found a GFF3 formatted main feature with its own ID
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, //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* 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
75 uint qend;
76 uint qlen; //query len, if given
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_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 // -- 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** 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=false, 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 _parents=NULL;
141 _parents_len=0;
142 parents=NULL;
143 num_parents=0;
144 ID=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_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(_parents);
162 GFREE(parents);
163 GFREE(ID);
164 GFREE(gene_name);
165 GFREE(gene_id);
166 }
167 };
168
169 class GffAttr {
170 public:
171 int attr_id;
172 char* attr_val;
173 GffAttr(int an_id, const char* av=NULL) {
174 attr_id=an_id;
175 attr_val=NULL;
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 }
208 bool operator>(GffAttr& d){
209 return (this>&d);
210 }
211 bool operator<(GffAttr& d){
212 return (this<&d);
213 }
214
215 };
216
217 class GffNameList;
218 class GffNames;
219
220 class GffNameInfo {
221 friend class GffNameList;
222 protected:
223 int idx;
224 public:
225 char* name;
226 GffNameInfo() { name=NULL; idx=-1; }
227 GffNameInfo(const char* n) {
228 name=Gstrdup(n);
229 }
230
231 ~GffNameInfo() {
232 GFREE(name);
233 }
234
235 bool operator==(GffNameInfo& d){
236 return (strcmp(this->name, d.name)==0);
237 }
238 bool operator<(GffNameInfo& d){
239 return (strcmp(this->name, d.name)<0);
240 }
241 };
242
243 class GffNameList:public GList<GffNameInfo> {
244 friend class GffNameInfo;
245 friend class GffNames;
246 protected:
247 GHash<GffNameInfo> byName;//hash with shared keys
248 int idlast; //fList index of last added/reused name
249 void addStatic(const char* tname) {// fast add
250 GffNameInfo* f=new GffNameInfo(tname);
251 idlast=this->Add(f);
252 f->idx=idlast;
253 byName.shkAdd(f->name,f);
254 }
255 public:
256 GffNameList():GList<GffNameInfo>(false,true,true), byName(false) {
257 idlast=-1;
258 }
259 char* lastNameUsed() { return idlast<0 ? NULL : Get(idlast)->name; }
260 int lastNameId() { return idlast; }
261 char* getName(int nid) { //retrieve name by its ID
262 if (nid<0 || nid>=fCount)
263 GError("GffNameList Error: invalid index (%d)\n",nid);
264 return fList[nid]->name;
265 }
266
267 int addName(const char* tname) {//returns or create an id for the given name
268 //check idlast first, chances are it's the same feature name checked
269 if (idlast>=0 && strcmp(fList[idlast]->name,tname)==0)
270 return idlast;
271 GffNameInfo* f=byName.Find(tname);
272 int fidx=-1;
273 if (f!=NULL) fidx=f->idx;
274 else {//add new entry
275 f=new GffNameInfo(tname);
276 fidx=this->Add(f);
277 f->idx=fidx;
278 byName.shkAdd(f->name,f);
279 }
280 idlast=fidx;
281 return fidx;
282 }
283
284 int addNewName(const char* tname) {
285 GffNameInfo* f=new GffNameInfo(tname);
286 int fidx=this->Add(f);
287 f->idx=fidx;
288 byName.shkAdd(f->name,f);
289 return fidx;
290 }
291
292 int getId(const char* tname) { //only returns a name id# if found
293 GffNameInfo* f=byName.Find(tname);
294 if (f==NULL) return -1;
295 return f->idx;
296 }
297 int removeName() {
298 GError("Error: removing names from GffNameList not allowed!\n");
299 return -1;
300 }
301 };
302
303 class GffNames {
304 public:
305 int numrefs;
306 GffNameList tracks;
307 GffNameList gseqs;
308 GffNameList attrs;
309 GffNameList feats; //feature names: 'mRNA', 'exon', 'CDS' etc.
310 GffNames():tracks(),gseqs(),attrs(), feats() {
311 numrefs=0;
312 //the order below is critical!
313 //has to match: gff_fid_mRNA, gff_fid_exon, gff_fid_CDS
314 feats.addStatic("mRNA");//index 0=gff_fid_mRNA
315 feats.addStatic("transcript");//index 1=gff_fid_transcript
316 feats.addStatic("exon");//index 1=gff_fid_exon
317 feats.addStatic("CDS"); //index 2=gff_fid_CDS
318 }
319 };
320
321 void gffnames_ref(GffNames* &n);
322 void gffnames_unref(GffNames* &n);
323
324 enum GffPrintMode {
325 pgtfAny, //print record as read
326 pgtfExon,
327 pgtfCDS,
328 pgffAny, //print record as read
329 pgffExon,
330 pgffCDS,
331 pgffBoth,
332 };
333
334
335 class GffAttrs:public GList<GffAttr> {
336 public:
337 GffAttrs():GList<GffAttr>(false,true,false) { }
338 void add_or_update(GffNames* names, const char* attrname, const char* val) {
339 int aid=names->attrs.getId(attrname);
340 if (aid>=0) {
341 //attribute found in the dictionary
342 for (int i=0;i<Count();i++) {
343 //do we have it?
344 if (aid==Get(i)->attr_id) {
345 //update the value
346 Get(i)->setValue(val);
347 return;
348 }
349 }
350 }
351 else {
352 aid=names->attrs.addNewName(attrname);
353 }
354 this->Add(new GffAttr(aid, val));
355 }
356
357 char* getAttr(GffNames* names, const char* attrname) {
358 int aid=names->attrs.getId(attrname);
359 if (aid>=0)
360 for (int i=0;i<Count();i++)
361 if (aid==Get(i)->attr_id) return Get(i)->attr_val;
362 return NULL;
363 }
364 char* getAttr(int aid) {
365 if (aid>=0)
366 for (int i=0;i<Count();i++)
367 if (aid==Get(i)->attr_id) return Get(i)->attr_val;
368 return NULL;
369 }
370 };
371
372
373 class GffExon : public GSeg {
374 public:
375 void* uptr; //for later extensions
376 GffAttrs* attrs; //other attributes kept for this exon
377 double score; // gff score column
378 char phase; //GFF phase column - for CDS segments only
379 // '.' = undefined (UTR), '0','1','2' for CDS exons
380 char exontype; // 1="exon" 2="cds" 3="utr" 4="stop_codon"
381 int qstart; // for mRNA/protein exon mappings: coordinates on query
382 int qend;
383 GffExon(int s=0, int e=0, double sc=0, char fr=0, int qs=0, int qe=0, char et=0) {
384 uptr=NULL;
385 attrs=NULL;
386 if (s<e) {
387 start=s;
388 end=e;
389 }
390 else {
391 start=e;
392 end=s;
393 }
394 if (qs<qe) {
395 qstart=qs;
396 qend=qe;
397 } else {
398 qstart=qe;
399 qend=qs;
400 }
401 score=sc;
402 phase=fr;
403 exontype=et;
404 } //constructor
405
406 char* getAttr(GffNames* names, const char* atrname) {
407 if (attrs==NULL || names==NULL || atrname==NULL) return NULL;
408 return attrs->getAttr(names, atrname);
409 }
410
411 char* getAttr(int aid) {
412 if (attrs==NULL) return NULL;
413 return attrs->getAttr(aid);
414 }
415
416 ~GffExon() { //destructor
417 if (attrs!=NULL) delete attrs;
418 }
419 };
420
421
422 class GffCDSeg:public GSeg {
423 public:
424 char phase;
425 int exonidx;
426 };
427 //one GFF mRNA object -- e.g. a mRNA with its exons and/or CDS segments
428 class GffObj:public GSeg {
429 //utility segment-merging function for addExon()
430 void expandExon(int xovl, uint segstart, uint segend,
431 char exontype, double sc, char fr, int qs, int qe);
432 protected:
433 //coordinate transformation data:
434 uint xstart; //absolute genomic coordinates of reference region
435 uint xend;
436 char xstatus; //coordinate transform status:
437 //0 : (start,end) coordinates are absolute
438 //'+' : (start,end) coords are relative to xstart..xend region
439 //'-' : (start,end) are relative to the reverse complement of xstart..xend region
440 //--
441 char* gffID; // ID name for mRNA (parent) feature
442 char* gene_name; //value of gene_name attribute (GTF) if present or Name attribute of the parent gene feature (GFF3)
443 char* geneID; //value of gene_id attribute (GTF) if present or ID attribute of a parent gene feature (GFF3)
444 unsigned int flags;
445 //-- friends:
446 friend class GffReader;
447 friend class GffExon;
448 public:
449 static GffNames* names; // dictionary storage that holds the various attribute names etc.
450 int track_id; // index of track name in names->tracks
451 int gseq_id; // index of genomic sequence name in names->gseqs
452 int ftype_id; // index of this record's feature name in names->feats, or the special gff_fid_mRNA value
453 int exon_ftype_id; //index of child subfeature name in names->feats (that subfeature stored in "exons")
454 //if ftype_id==gff_fid_mRNA then this value is ignored
455 GList<GffExon> exons; //for non-mRNA entries, these can be any subfeature of type subftype_id
456 GPVec<GffObj> children;
457 GffObj* parent;
458 int udata; //user data, flags etc.
459 void* uptr; //user pointer (to a parent object, cluster, locus etc.)
460 GffObj* ulink; //link to another GffObj (user controlled field)
461 // mRNA specific fields:
462 bool isCDS; //just a CDS, no UTRs
463 bool partial; //partial CDS
464 uint CDstart; //CDS start coord
465 uint CDend; //CDS end coord
466 char CDphase; //initial phase for CDS start
467 bool hasErrors() { return ((flags & gfo_flag_HAS_ERRORS)!=0); }
468 void hasErrors(bool v) {
469 if (v) flags |= gfo_flag_HAS_ERRORS;
470 else flags &= ~gfo_flag_HAS_ERRORS;
471 }
472 bool hasGffID() { return ((flags & gfo_flag_HAS_GFF_ID)!=0); }
473 void hasGffID(bool v) {
474 if (v) flags |= gfo_flag_HAS_GFF_ID;
475 else flags &= ~gfo_flag_HAS_GFF_ID;
476 }
477 bool createdByExon() { return ((flags & gfo_flag_BY_EXON)!=0); }
478 void createdByExon(bool v) {
479 if (v) flags |= gfo_flag_BY_EXON;
480 else flags &= ~gfo_flag_BY_EXON;
481 }
482 bool isGene() { return ((flags & gfo_flag_IS_GENE)!=0); }
483 void isGene(bool v) {
484 if (v) flags |= gfo_flag_IS_GENE;
485 else flags &= ~gfo_flag_IS_GENE;
486 }
487 bool isDiscarded() { return ((flags & gfo_flag_DISCARDED)!=0); }
488 void isDiscarded(bool v) {
489 if (v) flags |= gfo_flag_DISCARDED;
490 else flags &= ~gfo_flag_DISCARDED;
491 }
492
493 bool isUsed() { return ((flags & gfo_flag_LST_KEEP)!=0); }
494 void isUsed(bool v) {
495 if (v) flags |= gfo_flag_LST_KEEP;
496 else flags &= ~gfo_flag_LST_KEEP;
497 }
498 bool isTranscript() { return ((flags & gfo_flag_IS_TRANSCRIPT)!=0); }
499 void isTranscript(bool v) {
500 if (v) flags |= gfo_flag_IS_TRANSCRIPT;
501 else flags &= ~gfo_flag_IS_TRANSCRIPT;
502 }
503 bool promotedChildren() { return ((flags & gfo_flag_CHILDREN_PROMOTED)!=0); }
504 void promotedChildren(bool v) {
505 if (v) flags |= gfo_flag_CHILDREN_PROMOTED;
506 else flags &= ~gfo_flag_CHILDREN_PROMOTED;
507 }
508 void setLevel(byte v) {
509 if (v==0) flags &= ~gfo_flag_LEVEL_MSK;
510 else flags &= ~(((uint)v) << gfo_flagShift_LEVEL);
511 }
512 byte incLevel() {
513 uint v=((flags & gfo_flag_LEVEL_MSK) >> gfo_flagShift_LEVEL);
514 v++;
515 flags &= ~(v << gfo_flagShift_LEVEL);
516 return v;
517 }
518 byte getLevel() {
519 return ((byte)((flags & gfo_flag_LEVEL_MSK) >> gfo_flagShift_LEVEL));
520 }
521
522 bool isValidTranscript() {
523 //return (ftype_id==gff_fid_mRNA && exons.Count()>0);
524 return (isTranscript() && exons.Count()>0);
525 }
526
527
528 int addExon(uint segstart, uint segend, double sc=0, char fr='.',
529 int qs=0, int qe=0, bool iscds=false, char exontype=0);
530
531 int addExon(GffReader* reader, GffLine* gl, bool keepAttr=false, bool noExonAttr=true);
532
533 void removeExon(int idx);
534 void removeExon(GffExon* p);
535 char strand; //true if features are on the reverse complement strand
536 double gscore;
537 double uscore; //custom, user-computed score, if needed
538 int covlen; //total coverage of reference genomic sequence (sum of maxcf segment lengths)
539
540 //--------- optional data:
541 int qlen; //query length, start, end - if available
542 int qstart;
543 int qend;
544 int qcov; //query coverage - percent
545 GffAttrs* attrs; //other gff3 attributes found for the main mRNA feature
546 //constructor by gff line parsing:
547 GffObj(GffReader* gfrd, GffLine* gffline, bool keepAttrs=false, bool noExonAttr=true);
548 //if gfline->Parent!=NULL then this will also add the first sub-feature
549 // otherwise, only the main feature is created
550 void clearAttrs() {
551 if (attrs!=NULL) {
552 bool sharedattrs=(exons.Count()>0 && exons[0]->attrs==attrs);
553 delete attrs; attrs=NULL;
554 if (sharedattrs) exons[0]->attrs=NULL;
555 }
556 }
557 GffObj(char* anid=NULL):GSeg(0,0), exons(true,true,false), children(1,false) {
558 //exons: sorted, free, non-unique
559 gffID=NULL;
560 uptr=NULL;
561 ulink=NULL;
562 flags=0;
563 udata=0;
564 parent=NULL;
565 ftype_id=-1;
566 exon_ftype_id=-1;
567 if (anid!=NULL) gffID=Gstrdup(anid);
568 gffnames_ref(names);
569 qlen=0;
570 qstart=0;
571 qend=0;
572 qcov=0;
573 partial=true;
574 isCDS=false;
575 CDstart=0; // hasCDS <=> CDstart>0
576 CDend=0;
577 CDphase=0;
578 gseq_id=-1;
579 track_id=-1;
580 xstart=0;
581 xend=0;
582 xstatus=0;
583 strand='.';
584 gscore=0;
585 uscore=0;
586 attrs=NULL;
587 covlen=0;
588 gene_name=NULL;
589 geneID=NULL;
590 }
591 ~GffObj() {
592 GFREE(gffID);
593 GFREE(gene_name);
594 GFREE(geneID);
595 clearAttrs();
596 gffnames_unref(names);
597 }
598 //--------------
599 GffObj* finalize(GffReader* gfr, bool mergeCloseExons=false,
600 bool keepAttrs=false, bool noExonAttr=true);
601 //complete parsing: must be called in order to merge adjacent/close proximity subfeatures
602 void parseAttrs(GffAttrs*& atrlist, char* info, bool isExon=false);
603 const char* getSubfName() { //returns the generic feature type of the entries in exons array
604 int sid=exon_ftype_id;
605 if (sid==gff_fid_exon && isCDS) sid=gff_fid_CDS;
606 return names->feats.getName(sid);
607 }
608 void addCDS(uint cd_start, uint cd_end, char phase=0);
609
610 bool monoFeature() {
611 return (exons.Count()==0 ||
612 (exons.Count()==1 && //exon_ftype_id==ftype_id &&
613 exons[0]->end==this->end && exons[0]->start==this->start));
614 }
615
616 bool hasCDS() { return (CDstart>0); }
617
618 const char* getFeatureName() {
619 return names->feats.getName(ftype_id);
620 }
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* 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;
659 }
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 bool exonOverlap(uint s, uint e) {//check if ANY exon overlaps given segment
673 //ignores strand!
674 if (s>e) Gswap(s,e);
675 for (int i=0;i<exons.Count();i++) {
676 if (exons[i]->overlap(s,e)) return true;
677 }
678 return false;
679 }
680 bool exonOverlap(GffObj& m) {//check if ANY exon overlaps given segment
681 //if (gseq_id!=m.gseq_id) return false;
682 // ignores strand and gseq_id, must check in advance
683 for (int i=0;i<exons.Count();i++) {
684 for (int j=0;j<m.exons.Count();j++) {
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:
688 return true;
689 }
690 }
691 return false;
692 }
693
694 int exonOverlapIdx(uint s, uint e, int* ovlen=NULL) {
695 //return the exons' index for the overlapping OR ADJACENT exon
696 //ovlen, if given, will return the overlap length
697 if (s>e) Gswap(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 }
708 return i;
709 } //for each exon
710 *ovlen=0;
711 return -1;
712 }
713
714 int exonOverlapLen(GffObj& m) {
715 if (start>m.end || m.start>end) return 0;
716 int i=0;
717 int j=0;
718 int ovlen=0;
719 while (i<exons.Count() && j<m.exons.Count()) {
720 uint istart=exons[i]->start;
721 uint iend=exons[i]->end;
722 uint jstart=m.exons[j]->start;
723 uint jend=m.exons[j]->end;
724 if (istart>jend) { j++; continue; }
725 if (jstart>iend) { i++; continue; }
726 //exon overlap
727 uint ovstart=GMAX(istart,jstart);
728 if (iend<jend) {
729 ovlen+=iend-ovstart+1;
730 i++;
731 }
732 else {
733 ovlen+=jend-ovstart+1;
734 j++;
735 }
736 }//while comparing exons
737 return ovlen;
738 }
739
740 bool exonOverlap(GffObj* m) {
741 return exonOverlap(*m);
742 }
743 //---------- coordinate transformation
744 void xcoord(uint grstart, uint grend, char xstrand='+') {
745 //relative coordinate transform, and reverse-complement transform if xstrand is '-'
746 //does nothing if xstatus is the same already
747 if (xstatus) {
748 if (xstatus==xstrand && grstart==xstart && grend==xend) return;
749 unxcoord();//restore original coordinates
750 }
751 xstatus=xstrand;
752 xstart=grstart;
753 xend=grend;
754 if (CDstart>0) xcoordseg(CDstart, CDend);
755 for (int i=0;i<exons.Count();i++) {
756 xcoordseg(exons[i]->start, exons[i]->end);
757 }
758 if (xstatus=='-') {
759 exons.Reverse();
760 int flen=end-start;
761 start=xend-end+1;
762 end=start+flen;
763 }
764 else {
765 start=start-xstart+1;
766 end=end-xstart+1;
767 }
768 }
769
770 //transform an arbitrary segment based on current xstatus/xstart-xend
771 void xcoordseg(uint& segstart, uint &segend) {
772 if (xstatus==0) return;
773 if (xstatus=='-') {
774 int flen=segend-segstart;
775 segstart=xend-segend+1;
776 segend=segstart+flen;
777 return;
778 }
779 else {
780 segstart=segstart-xstart+1;
781 segend=segend-xstart+1;
782 }
783 }
784
785 void unxcoord() { //revert back to absolute genomic/gff coordinates if xstatus==true
786 if (xstatus==0) return; //nothing to do, no transformation appplied
787 if (CDstart>0) unxcoordseg(CDstart, CDend);
788 //restore all GffExon intervals too
789 for (int i=0;i<exons.Count();i++) {
790 unxcoordseg(exons[i]->start, exons[i]->end);
791 }
792 if (xstatus=='-') {
793 exons.Reverse();
794 int flen=end-start;
795 start=xend-end+1;
796 end=start+flen;
797 }
798 else {
799 start=start+xstart-1;
800 end=end+xstart-1;
801 }
802 xstatus=0;
803 }
804 void unxcoordseg(uint& astart, uint &aend) {
805 //restore an arbitrary interval -- does NOT change the transform state!
806 if (xstatus==0) return;
807 if (xstatus=='-') {
808 int flen=aend-astart;
809 astart=xend-aend+1;
810 aend=astart+flen;
811 }
812 else {
813 astart=astart+xstart-1;
814 aend=aend+xstart-1;
815 }
816 }
817 //---------------------
818 bool operator==(GffObj& d){
819 return (gseq_id==d.gseq_id && start==d.start && end==d.end && strcmp(gffID, d.gffID)==0);
820 }
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 (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 (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* 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);
852 void getCDSegs(GArray<GffCDSeg>& cds);
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, const char* tlabel=NULL,
864 const char* gfparent=NULL) {
865 printGxf(fout, pgffAny, tlabel, gfparent);
866 }
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
883 typedef bool GffRecFunc(GffObj* gobj, void* usrptr1, void* usrptr2);
884 //user callback after parsing a mapping object:
885 // Returns: "done with it" status:
886 // TRUE if gobj is no longer needed so it's FREEd upon return
887 // FALSE if the user needs the gobj pointer and is responsible for
888 // collecting and freeing all GffObj objects
889
890
891 //GSeqStat: collect basic stats about a common underlying genomic sequence
892 // for multiple GffObj
893 class GSeqStat {
894 public:
895 int gseqid; //gseq id in the global static pool of gseqs
896 char* gseqname; //just a pointer to the name of gseq
897 //int fcount;//number of features on this gseq
898 uint mincoord;
899 uint maxcoord;
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);
912 }
913 bool operator<(GSeqStat& g) {
914 return (gseqid<g.gseqid);
915 }
916 bool operator==(GSeqStat& g) {
917 return (gseqid==g.gseqid);
918 }
919 };
920
921
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
926 bool mustSort;
927 public:
928 GfList(bool sortbyloc=false):GList<GffObj>(false,false,false) {
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 struct GfoHolder {
976 int idx; //position in GffReader::gflst array
977 GffObj* gffobj;
978 GfoHolder(GffObj* gfo=NULL, int i=0) {
979 idx=i;
980 gffobj=gfo;
981 }
982 };
983
984 class CNonExon { //utility class used in subfeature promotion
985 public:
986 int idx;
987 GffObj* parent;
988 GffExon* exon;
989 GffLine* gffline;
990 CNonExon(int i, GffObj* p, GffExon* e, GffLine* gl) {
991 parent=p;
992 exon=e;
993 idx=i;
994 gffline=new GffLine(gl);
995 }
996 ~CNonExon() {
997 delete gffline;
998 }
999 };
1000
1001
1002 class GffReader {
1003 friend class GffObj;
1004 friend class GffLine;
1005 char* linebuf;
1006 off_t fpos;
1007 int buflen;
1008 protected:
1009 bool gff_warns; //warn about duplicate IDs, etc. even when they are on different chromosomes
1010 FILE* fh;
1011 char* fname; //optional fasta file with the underlying genomic sequence to be attached to this reader
1012 GffNames* names; //just a pointer to the global static Gff names repository in GffObj
1013 GffLine* gffline;
1014 bool transcriptsOnly; //keep only transcripts w/ their exon/CDS features
1015 GHash<int> discarded_ids; //for transcriptsOnly mode, keep track
1016 // of discarded parent IDs
1017 GHash< GVec<GfoHolder> > phash; //transcript_id+contig (Parent~Contig) => [gflst index, GffObj]
1018 //GHash<int> tids; //just for transcript_id uniqueness
1019 char* gfoBuildId(const char* id, const char* ctg);
1020 //void gfoRemove(const char* id, const char* ctg);
1021 GfoHolder* gfoAdd(GffObj* gfo, int idx);
1022 GfoHolder* gfoAdd(GVec<GfoHolder>& glst, GffObj* gfo, int idx);
1023 // const char* id, const char* ctg, char strand, GVec<GfoHolder>** glst, uint start, uint end
1024 GfoHolder* gfoFind(const char* id, const char* ctg=NULL, GVec<GfoHolder>** glst=NULL,
1025 char strand=0, uint start=0, uint end=0);
1026 CNonExon* subfPoolCheck(GffLine* gffline, GHash<CNonExon>& pex, char*& subp_name);
1027 void subfPoolAdd(GHash<CNonExon>& pex, GfoHolder* newgfo);
1028 GfoHolder* promoteFeature(CNonExon* subp, char*& subp_name, GHash<CNonExon>& pex,
1029 bool keepAttr, bool noExonAttr);
1030 public:
1031 GfList gflst; //accumulate GffObjs being read
1032 GfoHolder* newGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr,
1033 GffObj* parent=NULL, GffExon* pexon=NULL, GVec<GfoHolder>* glst=NULL);
1034 GfoHolder* replaceGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr, int replaceidx);
1035 GfoHolder* updateGffRec(GfoHolder* prevgfo, GffLine* gffline,
1036 bool keepAttr);
1037 GfoHolder* updateParent(GfoHolder* newgfh, GffObj* parent);
1038 bool addExonFeature(GfoHolder* prevgfo, GffLine* gffline, GHash<CNonExon>& pex, bool noExonAttr);
1039 GList<GSeqStat> gseqstats; //list of all genomic sequences seen by this reader, accumulates stats
1040 GffReader(FILE* f=NULL, bool t_only=false, bool sortbyloc=false):discarded_ids(true),
1041 phash(true), gflst(sortbyloc), gseqstats(true,true,true) {
1042 gff_warns=gff_show_warnings;
1043 names=NULL;
1044 gffline=NULL;
1045 transcriptsOnly=t_only;
1046 fpos=0;
1047 fname=NULL;
1048 fh=f;
1049 GMALLOC(linebuf, GFF_LINELEN);
1050 buflen=GFF_LINELEN-1;
1051 }
1052 void init(FILE *f, bool t_only=false, bool sortbyloc=false) {
1053 fname=NULL;
1054 fh=f;
1055 if (fh!=NULL) rewind(fh);
1056 fpos=0;
1057 transcriptsOnly=t_only;
1058 gflst.sortedByLoc(sortbyloc);
1059 }
1060 GffReader(char* fn, bool t_only=false, bool sort=false):discarded_ids(true), phash(true),
1061 gflst(sort),gseqstats(true,true,true) {
1062 gff_warns=gff_show_warnings;
1063 names=NULL;
1064 fname=Gstrdup(fn);
1065 transcriptsOnly=t_only;
1066 fh=fopen(fname, "rb");
1067 fpos=0;
1068 gffline=NULL;
1069 GMALLOC(linebuf, GFF_LINELEN);
1070 buflen=GFF_LINELEN-1;
1071 }
1072
1073 ~GffReader() {
1074 delete gffline;
1075 gffline=NULL;
1076 fpos=0;
1077 gflst.freeUnused();
1078 gflst.Clear();
1079 discarded_ids.Clear();
1080 phash.Clear();
1081 gseqstats.Clear();
1082 GFREE(fname);
1083 GFREE(linebuf);
1084 }
1085
1086 void showWarnings(bool v=true) {
1087 gff_warns=v;
1088 gff_show_warnings=v;
1089 }
1090
1091 GffLine* nextGffLine();
1092
1093 // load all subfeatures, re-group them:
1094 void readAll(bool keepAttr=false, bool mergeCloseExons=false, bool noExonAttr=true);
1095
1096 }; // end of GffReader
1097
1098 #endif