ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/gff.cpp
Revision: 310
Committed: Fri Mar 22 20:06:27 2013 UTC (6 years, 6 months ago) by gpertea
File size: 70550 byte(s)
Log Message:
sync with igm repo

Line File contents
1 #include "gff.h"
2
3 //GffNames* GffReader::names=NULL;
4 GffNames* GffObj::names=NULL;
5 //global set of feature names, attribute names etc.
6 // -- common for all GffObjs in current application!
7
8 const uint GFF_MAX_LOCUS = 7000000; //longest known gene in human is ~2.2M, UCSC claims a gene for mouse of ~ 3.1 M
9 const uint GFF_MAX_EXON = 30000; //longest known exon in human is ~11K
10 const uint GFF_MAX_INTRON= 6000000; //Ensembl shows a >5MB human intron
11 bool gff_show_warnings = false; //global setting, set by GffReader->showWarnings()
12 const int gff_fid_mRNA=0;
13 const int gff_fid_transcript=1;
14 const int gff_fid_exon=2;
15 const int gff_fid_CDS=3; //never really used in GffObj ftype_id or subftype_id
16 const uint gfo_flag_HAS_ERRORS = 0x00000001;
17 const uint gfo_flag_CHILDREN_PROMOTED= 0x00000002;
18 const uint gfo_flag_IS_GENE = 0x00000004;
19 const uint gfo_flag_IS_TRANSCRIPT = 0x00000008;
20 const uint gfo_flag_HAS_GFF_ID = 0x00000010; //found GFF3 feature line with its own ID
21 const uint gfo_flag_BY_EXON = 0x00000020; //created by subfeature (exon) directly
22 const uint gfo_flag_DISCARDED = 0x00000100;
23 const uint gfo_flag_LST_KEEP = 0x00000200;
24 const uint gfo_flag_LEVEL_MSK = 0x00FF0000;
25 const byte gfo_flagShift_LEVEL = 16;
26
27 void gffnames_ref(GffNames* &n) {
28 if (n==NULL) n=new GffNames();
29 n->numrefs++;
30 }
31
32 void gffnames_unref(GffNames* &n) {
33 if (n==NULL) GError("Error: attempt to remove reference to null GffNames object!\n");
34 n->numrefs--;
35 if (n->numrefs==0) { delete n; n=NULL; }
36 }
37
38 int gfo_cmpByLoc(const pointer p1, const pointer p2) {
39
40 GffObj& g1=*((GffObj*)p1);
41 GffObj& g2=*((GffObj*)p2);
42 if (g1.gseq_id==g2.gseq_id) {
43 if (g1.start!=g2.start)
44 return (int)(g1.start-g2.start);
45 else if (g1.getLevel()!=g2.getLevel())
46 return (int)(g1.getLevel()-g2.getLevel());
47 else
48 if (g1.end!=g2.end)
49 return (int)(g1.end-g2.end);
50 else return strcmp(g1.getID(), g2.getID());
51 }
52 else return (int)(g1.gseq_id-g2.gseq_id);
53 }
54
55 char* GffLine::extractAttr(const char* attr, bool caseStrict, bool enforce_GTF2) {
56 //parse a key attribute and remove it from the info string
57 //(only works for attributes that have values following them after ' ' or '=')
58 static const char GTF2_ERR[]="Error parsing attribute %s ('\"' required) at GTF line:\n%s\n";
59 int attrlen=strlen(attr);
60 char cend=attr[attrlen-1];
61 //char* pos = (caseStrict) ? strstr(info, attr) : strifind(info, attr);
62 //must make sure attr is not found in quoted text
63 char* pos=info;
64 char prevch=0;
65 bool in_str=false;
66 bool notfound=true;
67 int (*strcmpfn)(const char*, const char*, int) = caseStrict ? Gstrcmp : Gstricmp;
68 while (notfound && *pos) {
69 char ch=*pos;
70 if (ch=='"') {
71 in_str=!in_str;
72 pos++;
73 prevch=ch;
74 continue;
75 }
76 if (!in_str && (prevch==0 || prevch==' ' || prevch == ';')
77 && strcmpfn(attr, pos, attrlen)==0) {
78 //attr match found
79 //check for word boundary on right
80 char* epos=pos+attrlen;
81 if (cend=='=' || cend==' ' || *epos==0 || *epos==' ') {
82 notfound=false;
83 break;
84 }
85 //not a perfect match, move on
86 pos=epos;
87 prevch=*(pos-1);
88 continue;
89 }
90 //not a match or in_str
91 prevch=ch;
92 pos++;
93 }
94 if (notfound) return NULL;
95 char* vp=pos+attrlen;
96 while (*vp==' ') vp++;
97 if (*vp==';' || *vp==0)
98 GError("Error parsing value of GFF attribute \"%s\", line:\n%s\n", attr, dupline);
99 bool dq_enclosed=false; //value string enclosed by double quotes
100 if (*vp=='"') {
101 dq_enclosed=true;
102 vp++;
103 }
104 if (enforce_GTF2 && !dq_enclosed)
105 GError(GTF2_ERR,attr, dupline);
106 char* vend=vp;
107 if (dq_enclosed) {
108 while (*vend!='"' && *vend!=';' && *vend!=0) vend++;
109 }
110 else {
111 while (*vend!=';' && *vend!=0) vend++;
112 }
113 if (enforce_GTF2 && *vend!='"')
114 GError(GTF2_ERR, attr, dupline);
115 char *r=Gstrdup(vp, vend-1);
116 //-- now remove this attribute from the info string
117 while (*vend!=0 && (*vend=='"' || *vend==';' || *vend==' ')) vend++;
118 if (*vend==0) vend--;
119 for (char *src=vend, *dest=pos;;src++,dest++) {
120 *dest=*src;
121 if (*src==0) break;
122 }
123 return r;
124 }
125
126 static char fnamelc[128];
127
128 GffLine::GffLine(GffReader* reader, const char* l) {
129 llen=strlen(l);
130 GMALLOC(line,llen+1);
131 memcpy(line, l, llen+1);
132 GMALLOC(dupline, llen+1);
133 memcpy(dupline, l, llen+1);
134 skip=true;
135 gseqname=NULL;
136 track=NULL;
137 ftype=NULL;
138 info=NULL;
139 _parents=NULL;
140 _parents_len=0;
141 num_parents=0;
142 parents=NULL;
143 is_gff3=false;
144 is_cds=false;
145 is_transcript=false;
146 is_exon=false;
147 is_gene=false;
148 exontype=0;
149 gene_id=NULL;
150 gene_name=NULL;
151 qstart=0;
152 qend=0;
153 qlen=0;
154 ID=NULL;
155 char* t[9];
156 int i=0;
157 int tidx=1;
158 t[0]=line;
159
160 while (line[i]!=0) {
161 if (line[i]=='\t') {
162 line[i]=0;
163 t[tidx]=line+i+1;
164 tidx++;
165 if (tidx>8) break;
166 }
167 i++;
168 }
169
170 if (tidx<8) { // ignore non-GFF lines
171 // GMessage("Warning: error parsing GFF/GTF line:\n%s\n", l);
172 return;
173 }
174 gseqname=t[0];
175 track=t[1];
176 ftype=t[2];
177 info=t[8];
178 char* p=t[3];
179 if (!parseUInt(p,fstart)) {
180 //chromosome_band entries in Flybase
181 GMessage("Warning: invalid start coordinate at line:\n%s\n",l);
182 return;
183 }
184 p=t[4];
185 if (!parseUInt(p,fend)) {
186 GMessage("Warning: invalid end coordinate at line:\n%s\n",l);
187 return;
188 }
189 if (fend<fstart) Gswap(fend,fstart); //make sure fstart>=fend, always
190 p=t[5];
191 if (p[0]=='.' && p[1]==0) {
192 score=0;
193 }
194 else {
195 if (!parseDouble(p,score))
196 GError("Error parsing feature score from GFF line:\n%s\n",l);
197 }
198 strand=*t[6];
199 if (strand!='+' && strand!='-' && strand!='.')
200 GError("Error parsing strand (%c) from GFF line:\n%s\n",strand,l);
201 phase=*t[7]; // must be '.', '0', '1' or '2'
202 ID=NULL;
203 // exon/CDS/mrna filter
204 strncpy(fnamelc, ftype, 127);
205 fnamelc[127]=0;
206 strlower(fnamelc); //convert to lower case
207 bool is_t_data=false;
208 if (strstr(fnamelc, "utr")!=NULL) {
209 exontype=exgffUTR;
210 is_exon=true;
211 is_t_data=true;
212 }
213 else if (endsWith(fnamelc, "exon")) {
214 exontype=exgffExon;
215 is_exon=true;
216 is_t_data=true;
217 }
218 else if (strstr(fnamelc, "stop") &&
219 (strstr(fnamelc, "codon") || strstr(fnamelc, "cds"))){
220 exontype=exgffStop;
221 is_cds=true; //though some place it outside the last CDS segment
222 is_t_data=true;
223 }
224 else if (strstr(fnamelc, "start") &&
225 ((strstr(fnamelc, "codon")!=NULL) || strstr(fnamelc, "cds")!=NULL)){
226 exontype=exgffStart;
227 is_cds=true;
228 is_t_data=true;
229 }
230 else if (strcmp(fnamelc, "cds")==0) {
231 exontype=exgffCDS;
232 is_cds=true;
233 is_t_data=true;
234 }
235 else if (endsWith(fnamelc, "gene") || startsWith(fnamelc, "gene")) {
236 is_gene=true;
237 is_t_data=true; //because its name will be attached to parented transcripts
238 }
239 else if (endsWith(fnamelc,"rna") || endsWith(fnamelc,"transcript")) {
240 is_transcript=true;
241 is_t_data=true;
242 }
243
244 if (reader->transcriptsOnly && !is_t_data) {
245 char* id=extractAttr("ID=");
246 if (id==NULL) id=extractAttr("transcript_id");
247 //GMessage("Discarding non-transcript line:\n%s\n",l);
248 if (id!=NULL) {
249 reader->discarded_ids.Add(id, new int(1));
250 GFREE(id);
251 }
252 return; //skip this line, unwanted feature name
253 }
254 ID=extractAttr("ID=",true);
255 char* Parent=extractAttr("Parent=",true);
256 is_gff3=(ID!=NULL || Parent!=NULL);
257 if (is_gff3) {
258 //parse as GFF3
259 if (ID!=NULL) {
260 //has ID attr so it's likely to be a parent feature
261 //look for explicit gene name
262 gene_name=extractAttr("gene_name=");
263 if (gene_name==NULL) {
264 gene_name=extractAttr("geneName=");
265 if (gene_name==NULL) {
266 gene_name=extractAttr("gene_sym=");
267 if (gene_name==NULL) {
268 gene_name=extractAttr("gene=");
269 }
270 }
271 }
272 gene_id=extractAttr("geneID=");
273 if (gene_id==NULL) {
274 gene_id=extractAttr("gene_id=");
275 }
276 if (is_gene) {
277 //special case: keep the Name and ID attributes of the gene feature
278 if (gene_name==NULL)
279 gene_name=extractAttr("Name=");
280 if (gene_id==NULL) //the ID is also gene_id in this case
281 gene_id=Gstrdup(ID);
282 //skip=false;
283 //return;
284 GFREE(Parent); //TMI, we really don't care about gene Parents?
285 } //gene feature
286 }// has GFF3 ID
287 if (Parent!=NULL) {
288 //keep Parent attr
289 //parse multiple parents
290 num_parents=1;
291 p=Parent;
292 int last_delim_pos=-1;
293 while (*p!=';' && *p!=0) {
294 if (*p==',' && *(p+1)!=0 && *(p+1)!=';') {
295 num_parents++;
296 last_delim_pos=(p-Parent);
297 }
298 p++;
299 }
300 _parents_len=p-Parent+1;
301 _parents=Parent;
302 GMALLOC(parents, num_parents*sizeof(char*));
303 parents[0]=_parents;
304 int i=1;
305 if (last_delim_pos>0) {
306 for (p=_parents+1;p<=_parents+last_delim_pos;p++) {
307 if (*p==',') {
308 char* ep=p-1;
309 while (*ep==' ' && ep>_parents) ep--;
310 *(ep+1)=0; //end the string there
311 parents[i]=p+1;
312 i++;
313 }
314 }
315 }
316 } //has Parent field
317 } //GFF3
318 else { // GTF-like expected
319 Parent=extractAttr("transcript_id",true);
320 if (Parent!=NULL) { //GTF2 format detected
321 if (is_transcript) {
322 // atypical GTF with a parent transcript line declared
323 ID=Parent;
324 Parent=NULL;
325 }
326 gene_id=extractAttr("gene_id"); // for GTF this is the only attribute accepted as geneID
327 if (gene_id==NULL)
328 gene_id=extractAttr("geneid");
329 gene_name=extractAttr("gene_name");
330 if (gene_name==NULL) {
331
332 gene_name=extractAttr("gene_sym");
333 if (gene_name==NULL) {
334 gene_name=extractAttr("gene");
335 if (gene_name==NULL)
336 gene_name=extractAttr("genesymbol");
337 }
338 }
339 //prepare for parseAttr by adding '=' character instead of spaces for all attributes
340 //after the attribute name
341 p=info;
342 bool noed=true; //not edited after the last delim
343 bool nsp=false; //non-space found after last delim
344 while (*p!=0) {
345 if (*p==' ') {
346 if (nsp && noed) {
347 *p='=';
348 noed=false;
349 p++;
350 continue;
351 }
352 }
353 else nsp=true; //non-space
354 if (*p==';') { noed=true; nsp=false; }
355 p++;
356 }
357 } //GTF2 detected (no parent line)
358 else {// Parent is NULL, check for jigsaw format or other pre-GTF2 format
359 //char* fexon=strstr(fnamelc, "exon");
360 //if (fexon!=NULL) {
361 if (exontype==exgffExon) {
362 if (startsWith(track,"jigsaw")) {
363 is_cds=true;
364 strcpy(track,"jigsaw");
365 p=strchr(info,';');
366 if (p==NULL) { Parent=Gstrdup(info); info=NULL; }
367 else { Parent=Gstrdup(info,p-1);
368 info=p+1;
369 }
370 }
371 } //exon feature?
372 if (Parent==NULL && exontype>=exgffCDS &&
373 (i=strcspn(info,"; \t\n\r"))<=(int)(strlen(info)+1)) {
374 //one word ID ? really desperate attempt to parse it here
375 Parent=Gstrdup(info,info+i-1);
376 info=NULL; //discard anything else on the line
377 }
378 }
379 if (Parent!=NULL) { //GTF transcript_id for exon/CDS feature
380 _parents=Parent;
381 GMALLOC(parents,sizeof(char*));
382 num_parents=1;
383 parents[0]=_parents;
384 }
385 } //GTF-like
386
387 //parse other potentially useful features
388 if (is_gff3) {
389 if ((p=strstr(info,"Target="))!=NULL) { //has Target attr
390 p+=7;
391 while (*p!=';' && *p!=0 && *p!=' ') p++;
392 if (*p!=' ') {
393 GError("Error parsing target coordinates from GFF line:\n%s\n",l);
394 }
395 if (!parseUInt(p,qstart))
396 GError("Error parsing target start coordinate from GFF line:\n%s\n",l);
397 if (*p!=' ') {
398 GError("Error parsing next target coordinate from GFF line:\n%s\n",l);
399 }
400 p++;
401 if (!parseUInt(p,qend))
402 GError("Error parsing target end coordinate from GFF line:\n%s\n",l);
403 }
404 if ((p=strifind(info,"Qreg="))!=NULL) { //has Qreg attr
405 p+=5;
406 if (!parseUInt(p,qstart))
407 GError("Error parsing target start coordinate from GFF line:\n%s\n",l);
408 if (*p!='-') {
409 GError("Error parsing next target coordinate from GFF line:\n%s\n",l);
410 }
411 p++;
412 if (!parseUInt(p,qend))
413 GError("Error parsing target end coordinate from GFF line:\n%s\n",l);
414 if (*p=='|' || *p==':') {
415 p++;
416 if (!parseUInt(p,qlen))
417 GError("Error parsing target length from GFF Qreg|: \n%s\n",l);
418 }
419 }//has Qreg attr
420 if (qlen==0 && (p=strifind(info,"Qlen="))!=NULL) {
421 p+=5;
422 if (!parseUInt(p,qlen))
423 GError("Error parsing target length from GFF Qlen:\n%s\n",l);
424 }
425 }//parsing some useful attributes in GFF3 records
426 if (ID==NULL && parents==NULL) {
427 if (reader->gff_warns)
428 GMessage("Warning: could not parse ID or Parent from GFF line:\n%s\n",dupline);
429 return; //skip
430 }
431 skip=false;
432 }
433
434
435 void GffObj::addCDS(uint cd_start, uint cd_end, char phase) {
436 if (cd_start>=this->start) {
437 this->CDstart=cd_start;
438 if (strand=='+') this->CDphase=phase;
439 }
440 else this->CDstart=this->start;
441 if (cd_end<=this->end) {
442 this->CDend=cd_end;
443 if (strand=='-') this->CDphase=phase;
444 }
445 else this->CDend=this->end;
446 isTranscript(true);
447 exon_ftype_id=gff_fid_exon;
448 if (monoFeature()) {
449 if (exons.Count()==0) addExon(this->start, this->end,0,'.',0,0,false,exgffExon);
450 else exons[0]->exontype=exgffExon;
451 }
452 }
453
454 int GffObj::addExon(GffReader* reader, GffLine* gl, bool keepAttr, bool noExonAttr) {
455 //this will make sure we have the right subftype_id!
456 //int subf_id=-1;
457 if (!isTranscript() && gl->is_cds) {
458 isTranscript(true);
459 exon_ftype_id=gff_fid_exon;
460 if (exons.Count()==1) exons[0]->exontype=exgffExon;
461 }
462 if (isTranscript()) {
463 if (exon_ftype_id<0) {//exon_ftype_id=gff_fid_exon;
464 if (gl->exontype>0) exon_ftype_id=gff_fid_exon;
465 else exon_ftype_id=names->feats.addName(gl->ftype);
466 }
467 //any recognized mRNA segment gets the generic "exon" type (also applies to CDS)
468 if (gl->exontype==0 && !gl->is_transcript) {
469 //extraneous mRNA feature, discard
470 if (reader->gff_warns)
471 GMessage("Warning: discarding unrecognized transcript subfeature %s of %s\n",
472 gl->ftype, gffID);
473 return -1;
474 }
475 }
476 else { //non-mRNA parent feature, check this subf type
477 int subf_id=names->feats.addName(gl->ftype);
478 if (exon_ftype_id<0 || exons.Count()==0) //never assigned a subfeature type before (e.g. first exon being added)
479 exon_ftype_id=subf_id;
480 else {
481 if (exon_ftype_id!=subf_id) {
482 //
483 if (exon_ftype_id==ftype_id && exons.Count()==1 && exons[0]->start==start && exons[0]->end==end) {
484 //the existing exon was just a dummy one created by default, discard it
485 exons.Clear();
486 covlen=0;
487 exon_ftype_id=subf_id; //allow the new subfeature to completely takeover
488 }
489 else { //multiple subfeatures, prefer those with
490 if (reader->gff_warns)
491 GMessage("GFF Warning: multiple subfeatures (%s and %s) found for %s, discarding ",
492 names->feats.getName(subf_id), names->feats.getName(exon_ftype_id),gffID);
493 if (gl->exontype!=0) { //new feature is an exon, discard previously parsed subfeatures
494 if (reader->gff_warns) GMessage("%s.\n", names->feats.getName(exon_ftype_id));
495 exon_ftype_id=subf_id;
496 exons.Clear();
497 covlen=0;
498 }
499 else { //discard new feature
500 if (reader->gff_warns) GMessage("%s.\n", names->feats.getName(subf_id));
501 return -1; //skip this 2nd subfeature type for this parent!
502 }
503 }
504 } //incoming subfeature is of different type
505 } //new subfeature type
506 } //non-mRNA parent
507 int eidx=addExon(gl->fstart, gl->fend, gl->score, gl->phase,
508 gl->qstart,gl->qend, gl->is_cds, gl->exontype);
509 if (eidx<0) return eidx; //this should never happen
510 if (keepAttr) {
511 if (noExonAttr) {
512 if (attrs==NULL) //place the parsed attributes directly at transcript level
513 parseAttrs(attrs, gl->info);
514 }
515 else { //need all exon-level attributes
516 parseAttrs(exons[eidx]->attrs, gl->info, true);
517 }
518 }
519 return eidx;
520 }
521
522
523 int GffObj::addExon(uint segstart, uint segend, double sc, char fr, int qs, int qe, bool iscds, char exontype) {
524 if (exons.Count()==0) {
525 if (iscds) isCDS=true; //for now, assume CDS only if first "exon" given is a CDS
526 if (exon_ftype_id<0) {
527 exon_ftype_id = isTranscript() ? gff_fid_exon : ftype_id;
528 }
529 }
530 //special treatment of start/stop codon features, they might be broken/split between exons
531 //and in that case some providers will still give the wrong end coordinate as start+2 (e.g. UCSC)
532 //so we should not trust the end coordinate for such features
533 if (exontype==exgffStart || exontype==exgffStop) {
534 if (strand=='-') segstart=segend;
535 else segend=segstart;
536 if (exontype==exgffStart) {
537 if (CDstart==0 || segstart<CDstart) CDstart=segstart;
538 }
539 else {
540 if (segstart>CDend) CDend=segstart;
541 }
542 }
543 else if (iscds) { //update CDS anchors:
544 if (CDstart==0 || segstart<CDstart) {
545 CDstart=segstart;
546 if (exontype==exgffCDS && strand=='+') CDphase=fr;
547 }
548 if (segend>CDend) {
549 if (exontype==exgffCDS && strand=='-') CDphase=fr;
550 CDend=segend;
551 }
552 }
553 else { // not a CDS/start/stop
554 isCDS=false;
555 }
556 if (qs || qe) {
557 if (qs>qe) Gswap(qs,qe);
558 if (qs==0) qs=1;
559 }
560 int ovlen=0;
561 if (exontype>0) { //check for overlaps between exon-type segments
562 int oi=exonOverlapIdx(segstart, segend, &ovlen);
563 if (oi>=0) { //overlap existing segment
564 if (ovlen==0) {
565 //adjacent segments will be merged
566 //e.g. CDS to (UTR|exon)
567 if ((exons[oi]->exontype>=exgffUTR && exontype==exgffCDS) ||
568 (exons[oi]->exontype==exgffCDS && exontype>=exgffUTR)) {
569 expandExon(oi, segstart, segend, exgffCDSUTR, sc, fr, qs, qe);
570 return oi;
571 }
572 //CDS adjacent to stop_codon: UCSC does (did?) this
573 if ((exons[oi]->exontype==exgffStop && exontype==exgffCDS) ||
574 (exons[oi]->exontype==exgffCDS && exontype==exgffStop)) {
575 expandExon(oi, segstart, segend, exgffCDS, sc, fr, qs, qe);
576 return oi;
577 }
578 }
579 //only allow this for CDS within exon, stop_codon within (CDS|UTR|exon),
580 // start_codon within (CDS|exon)
581 if (exons[oi]->exontype>exontype &&
582 exons[oi]->start<=segstart && exons[oi]->end>=segend &&
583 !(exons[oi]->exontype==exgffUTR && exontype==exgffCDS)) {
584 //larger segment given first, now the smaller included one is redundant
585 return oi; //only used to store attributes from current GffLine
586 }
587 if (exontype>exons[oi]->exontype &&
588 segstart<=exons[oi]->start && segend>=exons[oi]->end &&
589 !(exontype==exgffUTR && exons[oi]->exontype==exgffCDS)) {
590 //smaller segment given first, so we have to enlarge it
591 expandExon(oi, segstart, segend, exontype, sc, fr, qs, qe);
592 //this should also check for overlapping next exon (oi+1) ?
593 return oi;
594 }
595 //there is also the special case of "ribosomal slippage exception" (programmed frameshift)
596 //where two CDS segments may actually overlap for 1 or 2 bases, but there should be only one encompassing exon
597 //if (ovlen>2 || exons[oi]->exontype!=exgffCDS || exontype!=exgffCDS) {
598 // had to relax this because of some weird UCSC annotations with exons partially overlapping the CDS segments
599 /*
600 if (ovlen>2 && exons[oi]->exontype!=exgffUTR && exontype!=exgffUTR) {
601 if (gff_show_warnings)
602 GMessage("GFF Warning: discarding overlapping feature segment (%d-%d) (vs %d-%d (%s)) for GFF ID %s on %s\n",
603 segstart, segend, exons[oi]->start, exons[oi]->end, getSubfName(), gffID, getGSeqName());
604 hasErrors(true);
605 return -1; //segment NOT added
606 }
607 */
608
609 if ((ovlen>2 || ovlen==0) || exons[oi]->exontype!=exgffCDS || exontype!=exgffCDS) {
610 if (gff_show_warnings)
611 GMessage("GFF Warning: merging overlapping/adjacent feature segment (%d-%d) into (%d-%d) (%s) for GFF ID %s on %s\n",
612 segstart, segend, exons[oi]->start, exons[oi]->end, getSubfName(), gffID, getGSeqName());
613 expandExon(oi, segstart, segend, exontype, sc, fr, qs, qe);
614 return oi;
615 }
616 // else add the segment if the overlap is small and between two CDS segments
617 //TODO: we might want to add an attribute here with the slippage coordinate and size?
618 covlen-=ovlen;
619 }//overlap or adjacent to existing segment
620 } //check for overlap
621 // --- no overlap, or accepted micro-overlap (ribosomal slippage)
622 // create & add the new segment
623 /*
624 if (start>0 && exontype==exgffCDS && exons.Count()==0) {
625 //adding a CDS directly as the first subfeature of a declared parent
626 segstart=start;
627 segend=end;
628 }
629 */
630 GffExon* enew=new GffExon(segstart, segend, sc, fr, qs, qe, exontype);
631 int eidx=exons.Add(enew);
632 if (eidx<0) {
633 //this would actually be acceptable if the object is a "Gene" and "exons" are in fact isoforms
634 if (gff_show_warnings)
635 GMessage("GFF Warning: failed adding segment %d-%d for %s (discarded)!\n",
636 segstart, segend, gffID);
637 delete enew;
638 hasErrors(true);
639 return -1;
640 }
641 covlen+=(int)(exons[eidx]->end-exons[eidx]->start)+1;
642 //adjust parent feature coordinates to contain this exon
643 if (start==0 || start>exons.First()->start) {
644 start=exons.First()->start;
645 }
646 if (end<exons.Last()->end) end=exons.Last()->end;
647
648 return eidx;
649 }
650
651 void GffObj::expandExon(int oi, uint segstart, uint segend, char exontype, double sc, char fr, int qs, int qe) {
652 //oi is the index of the *first* overlapping segment found that must be enlarged
653 covlen-=exons[oi]->len();
654 if (segstart<exons[oi]->start)
655 exons[oi]->start=segstart;
656 if (qs && qs<exons[oi]->qstart) exons[oi]->qstart=qs;
657 if (segend>exons[oi]->end)
658 exons[oi]->end=segend;
659 if (qe && qe>exons[oi]->qend) exons[oi]->qend=qe;
660 //warning: score cannot be properly adjusted! e.g. if it's a p-value it's just going to get worse
661 if (sc!=0) exons[oi]->score=sc;
662 covlen+=exons[oi]->len();
663 //if (exons[oi]->exontype< exontype) -- always true
664 exons[oi]->exontype = exontype;
665 if (exontype==exgffCDS) exons[oi]->phase=fr;
666 //we must check if any more exons are also overlapping this
667 int ni=oi+1; //next exon index after oi
668 while (ni<exons.Count() && segend>=exons[ni]->start) { // next segment overlaps new enlarged segment
669 //only allow this if next segment is fully included, and a subordinate
670 if (exons[ni]->exontype<exontype && exons[ni]->end<=segend) {
671 /* I guess we have to relax this due to stupid UCSC hg18 files having a start_codon sticking out
672 chr1 hg18_knownGene start_codon 69806911 69806913 0.000000 + .
673 chr1 hg18_knownGene CDS 69806911 69806912 0.000000 + 0
674 chr1 hg18_knownGene exon 69805456 69806912 0.000000 + .
675 */
676 if (exons[ni]->qstart<exons[oi]->qstart) exons[oi]->qstart=exons[ni]->qstart;
677 if (exons[ni]->qend>exons[oi]->qend) exons[oi]->qend=exons[ni]->qend;
678 exons.Delete(ni);
679 }
680 else {
681 if (gff_show_warnings) GMessage("GFF Warning: overlapping existing exon(%d-%d) while expanding to %d-%d for GFF ID %s\n",
682 exons[ni]->start, exons[ni]->end, segstart, segend, gffID);
683 //hasErrors(true);
684 break;
685 }
686 }
687 // -- make sure any other related boundaries are updated:
688 start=exons.First()->start;
689 end=exons.Last()->end;
690 if (uptr!=NULL) { //collect stats about the underlying genomic sequence
691 GSeqStat* gsd=(GSeqStat*)uptr;
692 if (start<gsd->mincoord) gsd->mincoord=start;
693 if (end>gsd->maxcoord) gsd->maxcoord=end;
694 if (this->len()>gsd->maxfeat_len) {
695 gsd->maxfeat_len=this->len();
696 gsd->maxfeat=this;
697 }
698 }
699 }
700
701 void GffObj::removeExon(int idx) {
702 /*
703 if (idx==0 && segs[0].start==gstart)
704 gstart=segs[1].start;
705 if (idx==segcount && segs[segcount].end==gend)
706 gend=segs[segcount-1].end;
707 */
708 if (idx<0 || idx>=exons.Count()) return;
709 int segstart=exons[idx]->start;
710 int segend=exons[idx]->end;
711 exons.Delete(idx);
712 covlen -= (int)(segend-segstart)+1;
713 start=exons.First()->start;
714 end=exons.Last()->end;
715 if (isCDS) { CDstart=start; CDend=end; }
716 }
717
718 void GffObj::removeExon(GffExon* p) {
719 for (int idx=0;idx<exons.Count();idx++) {
720 if (exons[idx]==p) {
721 int segstart=exons[idx]->start;
722 int segend=exons[idx]->end;
723 exons.Delete(idx);
724 covlen -= (int)(segend-segstart)+1;
725
726 if (exons.Count() > 0) {
727 start=exons.First()->start;
728 end=exons.Last()->end;
729 if (isCDS) { CDstart=start; CDend=end; }
730 }
731 return;
732 }
733 }
734 }
735
736
737
738 GffObj::GffObj(GffReader *gfrd, GffLine* gffline, bool keepAttr, bool noExonAttr):
739 GSeg(0,0), exons(true,true,false), children(1,false) {
740 xstart=0;
741 xend=0;
742 xstatus=0;
743 partial=false;
744 isCDS=false;
745 uptr=NULL;
746 ulink=NULL;
747 parent=NULL;
748 udata=0;
749 flags=0;
750 CDstart=0;
751 CDend=0;
752 CDphase=0;
753 geneID=NULL;
754 gene_name=NULL;
755 attrs=NULL;
756 gffID=NULL;
757 track_id=-1;
758 gseq_id=-1;
759 ftype_id=-1;
760 exon_ftype_id=-1;
761 strand='.';
762 if (gfrd==NULL)
763 GError("Cannot use this GffObj constructor with a NULL GffReader!\n");
764 gffnames_ref(names);
765 if (gfrd->names==NULL) gfrd->names=names;
766 //qlen=0;qstart=0;qend=0;
767 gscore=0;
768 uscore=0;
769 covlen=0;
770 qcov=0;
771 start=gffline->fstart;
772 end=gffline->fend;
773 gseq_id=names->gseqs.addName(gffline->gseqname);
774 track_id=names->tracks.addName(gffline->track);
775 strand=gffline->strand;
776 qlen=gffline->qlen;
777 qstart=gffline->qstart;
778 qend=gffline->qend;
779 //setup flags from gffline
780 isCDS=gffline->is_cds; //for now
781 isGene(gffline->is_gene);
782 isTranscript(gffline->is_transcript || gffline->exontype!=0);
783 //fromGff3(gffline->is_gff3);
784
785 if (gffline->parents!=NULL && !gffline->is_transcript) {
786 //GTF style -- create a GffObj directly by subfeature
787 //(also possible orphan GFF3 exon line, or an exon given before its parent (chado))
788 if (gffline->exontype!=0) { //recognized exon-like feature
789 ftype_id=gff_fid_transcript; //so this is some sort of transcript
790 exon_ftype_id=gff_fid_exon; //subfeatures MUST be exons
791 }
792 else {//unrecognized subfeatures
793 //make this GffObj of the same feature type
794 ftype_id=names->feats.addName(gffline->ftype);
795 }
796 if (gffline->ID==NULL) { //typical GTF2 without "transcript" line
797 gffID=Gstrdup(gffline->parents[0]);
798 this->createdByExon(true);
799 //this is likely the first exon/segment of the feature
800 addExon(gfrd, gffline, keepAttr, noExonAttr);
801 }
802 else { //a parented feature with an ID: orphan or premature GFF3 subfeature line
803 if (gffline->is_gff3 && gffline->exontype!=0) {
804 //premature exon given before its parent transcript
805 //create the transcript entry here
806 gffID=Gstrdup(gffline->parents[0]);
807 this->createdByExon(true);
808 //this is the first exon/segment of the transcript
809 addExon(gfrd, gffline, keepAttr, noExonAttr);
810 }
811 else { //unrecognized non-exon feature ? use the ID instead
812 this->hasGffID(true);
813 gffID=Gstrdup(gffline->ID);
814 if (keepAttr) this->parseAttrs(attrs, gffline->info);
815 }
816 }
817 } //non-transcript parented subfeature given directly
818 else {
819 //non-parented feature OR a recognizable transcript
820 //create a parent feature in its own right
821 gscore=gffline->score;
822 if (gffline->ID==NULL || gffline->ID[0]==0)
823 GError("Error: no ID found for GFF record start\n");
824 this->hasGffID(true);
825 gffID=Gstrdup(gffline->ID); //there must be an ID here
826 //if (gffline->is_transcript) ftype_id=gff_fid_mRNA;
827 //else
828 ftype_id=names->feats.addName(gffline->ftype);
829 if (gffline->is_transcript)
830 exon_ftype_id=gff_fid_exon;
831 if (keepAttr) this->parseAttrs(attrs, gffline->info);
832 }//no parent
833
834 if (gffline->gene_name!=NULL) {
835 gene_name=Gstrdup(gffline->gene_name);
836 }
837 if (gffline->gene_id) {
838 geneID=Gstrdup(gffline->gene_id);
839 }
840 else if (gffline->is_transcript && gffline->parents) {
841 geneID=Gstrdup(gffline->parents[0]);
842 }
843
844 //GSeqStat* gsd=gfrd->gseqstats.AddIfNew(new GSeqStat(gseq_id,names->gseqs.lastNameUsed()),true);
845 GSeqStat* gsd=gfrd->gseqstats.AddIfNew(new GSeqStat(gseq_id,gffline->gseqname), true);
846 uptr=gsd;
847 /*
848 if (start<gsd->mincoord) gsd->mincoord=start;
849 if (end>gsd->maxcoord) gsd->maxcoord=end;
850 if (this->len()>gsd->maxfeat_len) {
851 gsd->maxfeat_len=this->len();
852 gsd->maxfeat=this;
853 }
854 */
855 }
856
857 GffLine* GffReader::nextGffLine() {
858 if (gffline!=NULL) return gffline; //caller should free gffline after processing
859 while (gffline==NULL) {
860 int llen=0;
861 buflen=GFF_LINELEN-1;
862 char* l=fgetline(linebuf, buflen, fh, &fpos, &llen);
863 if (l==NULL) {
864 return NULL; //end of file
865 }
866 int ns=0; //first nonspace position
867 while (l[ns]!=0 && isspace(l[ns])) ns++;
868 if (l[ns]=='#' || llen<10) continue;
869 gffline=new GffLine(this, l);
870 if (gffline->skip) {
871 delete gffline;
872 gffline=NULL;
873 continue;
874 }
875 if (gffline->ID==NULL && gffline->parents==NULL) { //it must have an ID
876 //this might not be needed, already checked in the GffLine constructor
877 if (gff_warns)
878 GMessage("Warning: malformed GFF line, no parent or record Id (kipping\n");
879 delete gffline;
880 gffline=NULL;
881 //continue;
882 }
883 }
884 return gffline;
885 }
886
887
888 char* GffReader::gfoBuildId(const char* id, const char* ctg) {
889 //caller must free the returned pointer
890 char* buf=NULL;
891 int idlen=strlen(id);
892 GMALLOC(buf, idlen+strlen(ctg)+2);
893 strcpy(buf, id);
894 buf[idlen]='~';
895 strcpy(buf+idlen+1, ctg);
896 return buf;
897 }
898 /*
899 void GffReader::gfoRemove(const char* id, const char* ctg) {
900 char* buf=gfoBuildId(id,ctg);
901 phash.Remove(buf);
902 GFREE(buf);
903 }
904 */
905 GfoHolder* GffReader::gfoAdd(GffObj* gfo, int idx) {
906 //Warning: if gflst gets altered, idx becomes obsolete
907 GVec<GfoHolder>* glst=phash.Find(gfo->gffID);
908 if (glst==NULL)
909 glst=new GVec<GfoHolder>(1);
910 GfoHolder gh(gfo,idx);
911 int i=glst->Add(gh);
912 phash.Add(gfo->gffID, glst);
913 return &(glst->Get(i));
914 }
915
916 GfoHolder* GffReader::gfoAdd(GVec<GfoHolder>& glst, GffObj* gfo, int idx) {
917 GfoHolder gh(gfo,idx);
918 int i=glst.Add(gh);
919 return &(glst[i]);
920 }
921
922 GfoHolder* GffReader::gfoFind(const char* id, const char* ctg,
923 GVec<GfoHolder>** glst, char strand, uint start, uint end) {
924 GVec<GfoHolder>* gl=phash.Find(id);
925 GfoHolder* gh=NULL;
926 if (gl) {
927 for (int i=0;i<gl->Count();i++) {
928 GfoHolder& gfo = gl->Get(i);
929 if (ctg!=NULL && strcmp(ctg, gfo.gffobj->getGSeqName())!=0)
930 continue;
931 if (strand && gfo.gffobj->strand!='.' && strand != gfo.gffobj->strand)
932 continue;
933 if (start>0) {
934 if (abs((int)start-(int)gfo.gffobj->start)> (int)GFF_MAX_LOCUS)
935 continue;
936 if (end>0 && (gfo.gffobj->start>end || gfo.gffobj->end<start))
937 continue;
938 }
939 //must be the same transcript, according to given comparison criteria
940 gh=&gfo;
941 break;
942 }
943 }
944 if (glst) *glst=gl;
945 return gh;
946 }
947
948 GfoHolder* GffReader::replaceGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr, int replaceidx) {
949 GffObj* newgfo=new GffObj(this, gffline, keepAttr, noExonAttr);
950 GfoHolder* r=NULL;
951 if (replaceidx>=0) {
952 gflst.Put(replaceidx,newgfo);
953 r=gfoAdd(newgfo, replaceidx);
954 }
955 else {
956 int gfoidx=gflst.Add(newgfo);
957 r=gfoAdd(newgfo, gfoidx);
958 }
959 /*
960 if (gff_warns) {
961 int* pcount=tids.Find(newgfo->gffID);
962 if (pcount!=NULL) {
963 if (gff_warns) GMessage("Warning: duplicate GFF ID: %s\n", newgfo->gffID);
964 (*pcount)++;
965 }
966 else {
967 tids.Add(newgfo->gffID,new int(1));
968 }
969 }
970 */
971 return r;
972 }
973
974 GfoHolder* GffReader::updateParent(GfoHolder* newgfh, GffObj* parent) {
975 //assert(parent);
976 //assert(newgfo);
977 parent->children.Add(newgfh->gffobj);
978 if (newgfh->gffobj->parent==NULL) newgfh->gffobj->parent=parent;
979 newgfh->gffobj->setLevel(parent->getLevel()+1);
980 if (parent->isGene()) {
981 if (parent->gene_name!=NULL && newgfh->gffobj->gene_name==NULL)
982 newgfh->gffobj->gene_name=Gstrdup(parent->gene_name);
983 if (parent->geneID!=NULL && newgfh->gffobj->geneID==NULL)
984 newgfh->gffobj->geneID=Gstrdup(parent->geneID);
985 }
986
987 return newgfh;
988 }
989
990 GfoHolder* GffReader::newGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr,
991 GffObj* parent, GffExon* pexon, GVec<GfoHolder>* glst) {
992 GffObj* newgfo=new GffObj(this, gffline, keepAttr, noExonAttr);
993 GfoHolder* r=NULL;
994 int gfoidx=gflst.Add(newgfo);
995 r=(glst) ? gfoAdd(*glst, newgfo, gfoidx) : gfoAdd(newgfo, gfoidx);
996 if (parent!=NULL) {
997 updateParent(r, parent);
998 if (pexon!=NULL) parent->removeExon(pexon);
999 }
1000 /*
1001 if (gff_warns) {
1002 int* pcount=tids.Find(newgfo->gffID);
1003 if (pcount!=NULL) {
1004 if (gff_warns) GMessage("Warning: duplicate GFF ID: %s\n", newgfo->gffID);
1005 (*pcount)++;
1006 }
1007 else {
1008 tids.Add(newgfo->gffID,new int(1));
1009 }
1010 }
1011 */
1012 return r;
1013 }
1014
1015 GfoHolder* GffReader::updateGffRec(GfoHolder* prevgfo, GffLine* gffline,
1016 bool keepAttr) {
1017 if (prevgfo==NULL) return NULL;
1018 //prevgfo->gffobj->createdByExon(false);
1019 prevgfo->gffobj->ftype_id=prevgfo->gffobj->names->feats.addName(gffline->ftype);
1020 prevgfo->gffobj->start=gffline->fstart;
1021 prevgfo->gffobj->end=gffline->fend;
1022 prevgfo->gffobj->isGene(gffline->is_gene);
1023 prevgfo->gffobj->isTranscript(gffline->is_transcript || gffline->exontype!=0);
1024 prevgfo->gffobj->hasGffID(gffline->ID!=NULL);
1025 if (keepAttr) {
1026 if (prevgfo->gffobj->attrs!=NULL) prevgfo->gffobj->attrs->Clear();
1027 prevgfo->gffobj->parseAttrs(prevgfo->gffobj->attrs, gffline->info);
1028 }
1029 return prevgfo;
1030 }
1031
1032
1033 bool GffReader::addExonFeature(GfoHolder* prevgfo, GffLine* gffline, GHash<CNonExon>& pex, bool noExonAttr) {
1034 bool r=true;
1035 if (gffline->strand!=prevgfo->gffobj->strand) {
1036 if (prevgfo->gffobj->strand=='.') {
1037 prevgfo->gffobj->strand=gffline->strand;
1038 }
1039 else {
1040 GMessage("GFF Error at %s (%c): exon %d-%d (%c) found on different strand; discarded.\n",
1041 prevgfo->gffobj->gffID, prevgfo->gffobj->strand,
1042 gffline->fstart, gffline->fend, gffline->strand, prevgfo->gffobj->getGSeqName());
1043 //r=false;
1044 return true;
1045 }
1046 }
1047 int gdist=(gffline->fstart>prevgfo->gffobj->end) ? gffline->fstart-prevgfo->gffobj->end :
1048 ((gffline->fend<prevgfo->gffobj->start)? prevgfo->gffobj->start-gffline->fend :
1049 0 );
1050 if (gdist>(int)GFF_MAX_LOCUS) { //too far apart, most likely this is a duplicate ID
1051 GMessage("Error: duplicate GFF ID '%s' (or exons too far apart)!\n",prevgfo->gffobj->gffID);
1052 //validation_errors = true;
1053 r=false;
1054 if (!gff_warns) exit(1);
1055 }
1056 int eidx=prevgfo->gffobj->addExon(this, gffline, !noExonAttr, noExonAttr);
1057 if (eidx>=0 && gffline->ID!=NULL && gffline->exontype==0)
1058 subfPoolAdd(pex, prevgfo);
1059 return r;
1060 }
1061
1062 CNonExon* GffReader::subfPoolCheck(GffLine* gffline, GHash<CNonExon>& pex, char*& subp_name) {
1063 CNonExon* subp=NULL;
1064 subp_name=NULL;
1065 for (int i=0;i<gffline->num_parents;i++) {
1066 if (transcriptsOnly && discarded_ids.Find(gffline->parents[i])!=NULL)
1067 continue;
1068 subp_name=gfoBuildId(gffline->parents[i], gffline->gseqname); //e.g. mRNA name
1069 subp=pex.Find(subp_name);
1070 if (subp!=NULL)
1071 return subp;
1072 GFREE(subp_name);
1073 }
1074 return NULL;
1075 }
1076
1077 void GffReader::subfPoolAdd(GHash<CNonExon>& pex, GfoHolder* newgfo) {
1078 //this might become a parent feature later
1079 if (newgfo->gffobj->exons.Count()>0) {
1080 char* xbuf=gfoBuildId(gffline->ID, gffline->gseqname);
1081 pex.Add(xbuf, new CNonExon(newgfo->idx, newgfo->gffobj,
1082 newgfo->gffobj->exons[0], gffline));
1083 GFREE(xbuf);
1084 }
1085 }
1086
1087 GfoHolder* GffReader::promoteFeature(CNonExon* subp, char*& subp_name, GHash<CNonExon>& pex,
1088 bool keepAttr, bool noExonAttr) {
1089 GffObj* prevp=subp->parent; //grandparent of gffline (e.g. gene)
1090 if (prevp!=gflst[subp->idx])
1091 GError("Error promoting subfeature %s, gflst index mismatch?!\n", subp->gffline->ID);
1092 subp->gffline->discardParent();
1093 GfoHolder* gfoh=newGffRec(subp->gffline, keepAttr, noExonAttr, prevp, subp->exon);
1094 pex.Remove(subp_name); //no longer a potential parent, moved it to phash already
1095 prevp->promotedChildren(true);
1096 return gfoh; //returns the holder of newly promoted feature
1097 }
1098
1099 //have to parse the whole file because exons can be scattered all over
1100 //trans-splicing and fusions are only accepted in proper GFF3 format, with a single parent feature ID entry
1101 void GffReader::readAll(bool keepAttr, bool mergeCloseExons, bool noExonAttr) {
1102 bool validation_errors = false;
1103 //loc_debug=false;
1104 GHash<CNonExon> pex; //keep track of any "exon"-like features that have an ID
1105 //and thus could become promoted to parent features
1106 while (nextGffLine()!=NULL) {
1107 GfoHolder* prevseen=NULL;
1108 GVec<GfoHolder>* prevgflst=NULL;
1109 if (gffline->ID && gffline->exontype==0) {
1110 //>> for a parent-like IDed feature (mRNA, gene, etc.)
1111 //look for same ID on the same chromosome/strand/locus
1112 prevseen=gfoFind(gffline->ID, gffline->gseqname, &prevgflst, gffline->strand, gffline->fstart);
1113 if (prevseen!=NULL) {
1114 //same ID/chromosome combo encountered before
1115 if (prevseen->gffobj->createdByExon() &&
1116 prevseen->gffobj->start>=gffline->fstart &&
1117 prevseen->gffobj->end<=gffline->fend) {
1118 //an exon of this ID was given before
1119 //this line has the main attributes for this ID
1120 updateGffRec(prevseen, gffline, keepAttr);
1121 }
1122 else {
1123 //- duplicate ID -- this must be a discontiguous feature
1124 // e.g. a trans-spliced transcript
1125 if (prevseen->gffobj->overlap(gffline->fstart, gffline->fend)) {
1126 //overlapping with same ID not allowed
1127 GMessage("Error: duplicate GFF ID '%s' encountered!\n",gffline->ID);
1128 //validation_errors = true;
1129 if (gff_warns) {
1130 delete gffline;
1131 gffline=NULL;
1132 continue;
1133 }
1134 else exit(1);
1135 }
1136 //create a new entry with the same ID
1137 prevseen=newGffRec(gffline, keepAttr, noExonAttr,
1138 prevseen->gffobj->parent, NULL, prevgflst);
1139 } //duplicate ID on the same chromosome
1140 } //prevseeen != NULL
1141 } //parent-like ID feature
1142 if (gffline->parents==NULL) {//start GFF3-like record with no parent (mRNA, gene)
1143 if (!prevseen) newGffRec(gffline, keepAttr, noExonAttr, NULL, NULL, prevgflst);
1144 }
1145 else { //--- it's a child feature (exon/CDS but could still be a mRNA with gene(s) as parent)
1146 //updates all the declared parents with this child
1147 bool found_parent=false;
1148 GfoHolder* newgfo=prevseen;
1149 GVec<GfoHolder>* newgflst=NULL;
1150 for (int i=0;i<gffline->num_parents;i++) {
1151 if (transcriptsOnly && discarded_ids.Find(gffline->parents[i])!=NULL)
1152 continue; //skipping discarded parent feature
1153 GfoHolder* parentgfo=NULL;
1154 if (gffline->is_transcript || gffline->exontype==0) {//possibly a transcript
1155 parentgfo=gfoFind(gffline->parents[i], gffline->gseqname,
1156 &newgflst, gffline->strand, gffline->fstart, gffline->fend);
1157 }
1158 else {
1159 //for exon-like entities we only need a parent to be in locus distance,
1160 //on the same strand
1161 parentgfo=gfoFind(gffline->parents[i], gffline->gseqname,
1162 &newgflst, gffline->strand, gffline->fstart);
1163 }
1164 if (parentgfo!=NULL) { //parent GffObj parsed earlier
1165 found_parent=true;
1166 if (parentgfo->gffobj->isGene() && gffline->is_transcript
1167 && gffline->exontype==0) {
1168 //not an exon, but a transcript parented by a gene
1169 if (newgfo) {
1170 updateParent(newgfo, parentgfo->gffobj);
1171 }
1172 else {
1173 newgfo=newGffRec(gffline, keepAttr, noExonAttr, parentgfo->gffobj);
1174 }
1175 }
1176 else { //potential exon subfeature
1177 if (!addExonFeature(parentgfo, gffline, pex, noExonAttr))
1178 validation_errors=true;
1179 }
1180 } //overlapping parent feature found
1181 } //for each parsed parent Id
1182 if (!found_parent) { //new GTF-like record starting here with a subfeature directly
1183 //or it could be some chado GFF3 barf with exons coming BEFORE their parent :(
1184 //check if this feature isn't parented by a previously stored "exon" subfeature
1185 char* subp_name=NULL;
1186 CNonExon* subp=subfPoolCheck(gffline, pex, subp_name);
1187 if (subp!=NULL) { //found a subfeature that is the parent of this gffline
1188 //promote that subfeature to a full GffObj
1189 GfoHolder* gfoh=promoteFeature(subp, subp_name, pex, keepAttr, noExonAttr);
1190 //add current gffline as an exon of the newly promoted subfeature
1191 if (!addExonFeature(gfoh, gffline, pex, noExonAttr))
1192 validation_errors=true;
1193 }
1194 else { //no parent seen before,
1195 //loc_debug=true;
1196 GfoHolder* ngfo=prevseen;
1197 if (ngfo==NULL) {
1198 //if it's an exon type, create directly the parent with this exon
1199 //but if it's recognized as a transcript, the object itself is created
1200 ngfo=newGffRec(gffline, keepAttr, noExonAttr, NULL, NULL, newgflst);
1201 }
1202 if (!ngfo->gffobj->isTranscript() &&
1203 gffline->ID!=NULL && gffline->exontype==0)
1204 subfPoolAdd(pex, ngfo);
1205 //even those with errors will be added here!
1206 }
1207 GFREE(subp_name);
1208 } //no previous parent found
1209 } //parented feature
1210 //--
1211 delete gffline;
1212 gffline=NULL;
1213 }//while gff lines
1214 if (gflst.Count()>0) {
1215 gflst.finalize(this, mergeCloseExons, keepAttr, noExonAttr); //force sorting by locus if so constructed
1216 gseqStats.setCount(gseqstats.Last()->gseqid+1);
1217 for (int gi=0;gi<gseqstats.Count();gi++) {
1218 gseqStats.Put(gseqstats[gi]->gseqid, gseqstats[gi]); //copy the pointer only
1219 }
1220 }
1221 // all gff records are now loaded in GList gflst
1222 // so we can free the hash
1223 phash.Clear();
1224 //tids.Clear();
1225 if (validation_errors) {
1226 exit(1);
1227 }
1228 }
1229
1230 void GfList::finalize(GffReader* gfr, bool mergeCloseExons,
1231 bool keepAttrs, bool noExonAttr) { //if set, enforce sort by locus
1232 if (mustSort) { //force (re-)sorting
1233 this->setSorted(false);
1234 this->setSorted((GCompareProc*)gfo_cmpByLoc);
1235 }
1236 GList<GffObj> discarded(false,true,false);
1237 for (int i=0;i<Count();i++) {
1238 //finish the parsing for each GffObj
1239 fList[i]->finalize(gfr, mergeCloseExons, keepAttrs, noExonAttr);
1240 if (fList[i]->isDiscarded()) {
1241 discarded.Add(fList[i]);
1242 this->Forget(i);
1243 }
1244 }
1245 if (discarded.Count()>0) this->Pack();
1246 }
1247
1248 GffObj* GffObj::finalize(GffReader* gfr, bool mergeCloseExons, bool keepAttrs, bool noExonAttr) {
1249 //merge
1250 //always merge adjacent or overlapping segments
1251 //but if mergeCloseExons then merge even when distance is up to 5 bases
1252 udata=0;
1253 uptr=NULL;
1254 if (gfr->transcriptsOnly && !(isTranscript() || (isGene() && children.Count()==0))) {
1255 isDiscarded(true);
1256 }
1257 if (ftype_id==gff_fid_transcript && CDstart>0) {
1258 ftype_id=gff_fid_mRNA;
1259 //exon_ftype_id=gff_fid_exon;
1260 }
1261 if (exons.Count()>0 && (isTranscript() || exon_ftype_id==gff_fid_exon)) {
1262 if (mergeCloseExons) {
1263 int mindist=mergeCloseExons ? 5:1;
1264 for (int i=0;i<exons.Count()-1;i++) {
1265 int ni=i+1;
1266 uint mend=exons[i]->end;
1267 while (ni<exons.Count()) {
1268 int dist=(int)(exons[ni]->start-mend);
1269 if (dist>mindist) break; //no merging with next segment
1270 if (gfr!=NULL && gfr->gff_warns && dist!=0 && (exons[ni]->exontype!=exgffUTR && exons[i]->exontype!=exgffUTR)) {
1271 GMessage("GFF warning: merging adjacent/overlapping segments of %s on %s (%d-%d, %d-%d)\n",
1272 gffID, getGSeqName(), exons[i]->start, exons[i]->end,exons[ni]->start, exons[ni]->end);
1273 }
1274 mend=exons[ni]->end;
1275 covlen-=exons[i]->len();
1276 exons[i]->end=mend;
1277 covlen+=exons[i]->len();
1278 covlen-=exons[ni]->len();
1279 if (exons[ni]->attrs!=NULL && (exons[i]->attrs==NULL ||
1280 exons[i]->attrs->Count()<exons[ni]->attrs->Count())) {
1281 //use the other exon attributes, if more
1282 delete(exons[i]->attrs);
1283 exons[i]->attrs=exons[ni]->attrs;
1284 exons[ni]->attrs=NULL;
1285 }
1286 exons.Delete(ni);
1287 } //check for merge with next exon
1288 } //for each exon
1289 } //merge close exons
1290 //shrink transcript to the exons' span
1291 this->start=exons.First()->start;
1292 this->end=exons.Last()->end;
1293 //also update the stats for the reference sequence
1294 if (uptr!=NULL) { //collect stats about the underlying genomic sequence
1295 GSeqStat* gsd=(GSeqStat*)uptr;
1296 if (start<gsd->mincoord) gsd->mincoord=start;
1297 if (end>gsd->maxcoord) gsd->maxcoord=end;
1298 if (this->len()>gsd->maxfeat_len) {
1299 gsd->maxfeat_len=this->len();
1300 gsd->maxfeat=this;
1301 }
1302 }
1303 this->uptr=NULL;
1304 this->udata=0;
1305 }
1306 //attribute reduction for GTF records
1307 if (keepAttrs && !noExonAttr && !hasGffID()
1308 && exons.Count()>0 && exons[0]->attrs!=NULL) {
1309 bool attrs_discarded=false;
1310 for (int a=0;a<exons[0]->attrs->Count();a++) {
1311 int attr_name_id=exons[0]->attrs->Get(a)->attr_id;
1312 char* attr_name=names->attrs.getName(attr_name_id);
1313 char* attr_val =exons[0]->attrs->Get(a)->attr_val;
1314 bool sameExonAttr=true;
1315 for (int i=1;i<exons.Count();i++) {
1316 char* ov=exons[i]->getAttr(attr_name_id);
1317 if (ov==NULL || (strcmp(ov,attr_val)!=0)) {
1318 sameExonAttr=false;
1319 break;
1320 }
1321 }
1322 if (sameExonAttr) {
1323 //delete this attribute from exons level
1324 attrs_discarded=true;
1325 this->addAttr(attr_name, attr_val);
1326 for (int i=1;i<exons.Count();i++) {
1327 removeExonAttr(*(exons[i]), attr_name_id);
1328 }
1329 exons[0]->attrs->freeItem(a);
1330 }
1331 }
1332 if (attrs_discarded) exons[0]->attrs->Pack();
1333 }
1334 return this;
1335 }
1336
1337 void GffObj::parseAttrs(GffAttrs*& atrlist, char* info, bool isExon) {
1338 if (names==NULL)
1339 GError(ERR_NULL_GFNAMES, "parseAttrs()");
1340 if (atrlist==NULL)
1341 atrlist=new GffAttrs();
1342 char* endinfo=info+strlen(info);
1343 char* start=info;
1344 char* pch=start;
1345 while (start<endinfo) {
1346 //skip spaces
1347 while (*start==' ' && start<endinfo) start++;
1348 pch=strchr(start, ';');
1349 if (pch==NULL) pch=endinfo;
1350 else {
1351 *pch='\0';
1352 pch++;
1353 }
1354 char* ech=strchr(start,'=');
1355 if (ech!=NULL) { // attr=value format found
1356 *ech='\0';
1357 //if (noExonAttr && (strcmp(start, "exon_number")==0 || strcmp(start, "exon")==0)) { start=pch; continue; }
1358 if (strcmp(start, "exon_number")==0 || strcmp(start, "exon")==0 ||
1359 strcmp(start, "exon_id")==0)
1360 { start=pch; continue; }
1361 ech++;
1362 while (*ech==' ' && ech<endinfo) ech++;//skip extra spaces after the '='
1363 //atrlist->Add(new GffAttr(names->attrs.addName(start),ech));
1364 //make sure we don't add the same attribute more than once
1365 if (isExon && (strcmp(start, "protein_id")==0)) {
1366 //Ensembl special case
1367 this->addAttr(start, ech);
1368 start=pch;
1369 continue;
1370 }
1371 atrlist->add_or_update(names, start, ech);
1372 }
1373 /*
1374 else { //not an attr=value format
1375 atrlist->Add(new GffAttr(names->attrs.addName(start),"1"));
1376 }
1377 */
1378 start=pch;
1379 }
1380 if (atrlist->Count()==0) { delete atrlist; atrlist=NULL; }
1381 }
1382
1383 void GffObj::addAttr(const char* attrname, const char* attrvalue) {
1384 if (this->attrs==NULL)
1385 this->attrs=new GffAttrs();
1386 //this->attrs->Add(new GffAttr(names->attrs.addName(attrname),attrvalue));
1387 this->attrs->add_or_update(names, attrname, attrvalue);
1388 }
1389
1390
1391 void GffObj::setFeatureName(const char* feature) {
1392 //change the feature name/type for a transcript
1393 int fid=names->feats.addName(feature);
1394 if (monoFeature() && exons.Count()>0)
1395 this->exon_ftype_id=fid;
1396 this->ftype_id=fid;
1397 }
1398
1399 void GffObj::setRefName(const char* newname) {
1400 //change the feature name/type for a transcript
1401 int rid=names->gseqs.addName(newname);
1402 this->gseq_id=rid;
1403 }
1404
1405
1406
1407 int GffObj::removeAttr(const char* attrname, const char* attrval) {
1408 if (this->attrs==NULL || attrname==NULL || attrname[0]==0) return 0;
1409 int aid=this->names->attrs.getId(attrname);
1410 if (aid<0) return 0;
1411 int delcount=0; //could be more than one ?
1412 for (int i=0;i<this->attrs->Count();i++) {
1413 if (aid==this->attrs->Get(i)->attr_id) {
1414 if (attrval==NULL ||
1415 strcmp(attrval, this->attrs->Get(i)->attr_val)==0) {
1416 delcount++;
1417 this->attrs->freeItem(i);
1418 }
1419 }
1420 }
1421 if (delcount>0) this->attrs->Pack();
1422 return delcount;
1423 }
1424
1425 int GffObj::removeAttr(int aid, const char* attrval) {
1426 if (this->attrs==NULL || aid<0) return 0;
1427 int delcount=0; //could be more than one ?
1428 for (int i=0;i<this->attrs->Count();i++) {
1429 if (aid==this->attrs->Get(i)->attr_id) {
1430 if (attrval==NULL ||
1431 strcmp(attrval, this->attrs->Get(i)->attr_val)==0) {
1432 delcount++;
1433 this->attrs->freeItem(i);
1434 }
1435 }
1436 }
1437 if (delcount>0) this->attrs->Pack();
1438 return delcount;
1439 }
1440
1441
1442 int GffObj::removeExonAttr(GffExon& exon, const char* attrname, const char* attrval) {
1443 if (exon.attrs==NULL || attrname==NULL || attrname[0]==0) return 0;
1444 int aid=this->names->attrs.getId(attrname);
1445 if (aid<0) return 0;
1446 int delcount=0; //could be more than one
1447 for (int i=0;i<exon.attrs->Count();i++) {
1448 if (aid==exon.attrs->Get(i)->attr_id) {
1449 if (attrval==NULL ||
1450 strcmp(attrval, exon.attrs->Get(i)->attr_val)==0) {
1451 delcount++;
1452 exon.attrs->freeItem(i);
1453 }
1454 }
1455 }
1456 if (delcount>0) exon.attrs->Pack();
1457 return delcount;
1458 }
1459
1460 int GffObj::removeExonAttr(GffExon& exon, int aid, const char* attrval) {
1461 if (exon.attrs==NULL || aid<0) return 0;
1462 int delcount=0; //could be more than one
1463 for (int i=0;i<exon.attrs->Count();i++) {
1464 if (aid==exon.attrs->Get(i)->attr_id) {
1465 if (attrval==NULL ||
1466 strcmp(attrval, exon.attrs->Get(i)->attr_val)==0) {
1467 delcount++;
1468 exon.attrs->freeItem(i);
1469 }
1470 }
1471 }
1472 if (delcount>0) exon.attrs->Pack();
1473 return delcount;
1474 }
1475
1476
1477 void GffObj::getCDS_ends(uint& cds_start, uint& cds_end) {
1478 cds_start=0;
1479 cds_end=0;
1480 if (CDstart==0 || CDend==0) return; //no CDS info
1481 int cdsadj=0;
1482 if (CDphase=='1' || CDphase=='2') {
1483 cdsadj=CDphase-'0';
1484 }
1485 cds_start=CDstart;
1486 cds_end=CDend;
1487 if (strand=='-') cds_end-=cdsadj;
1488 else cds_start+=cdsadj;
1489 }
1490
1491 void GffObj::mRNA_CDS_coords(uint& cds_mstart, uint& cds_mend) {
1492 //sets cds_start and cds_end to the CDS start,end coordinates on the spliced mRNA transcript
1493 cds_mstart=0;
1494 cds_mend=0;
1495 if (CDstart==0 || CDend==0) return; //no CDS info
1496 //restore normal coordinates, just in case
1497 unxcoord();
1498 int cdsadj=0;
1499 if (CDphase=='1' || CDphase=='2') {
1500 cdsadj=CDphase-'0';
1501 }
1502 /*
1503 uint seqstart=CDstart;
1504 uint seqend=CDend;
1505 */
1506 uint seqstart=exons.First()->start;
1507 uint seqend=exons.Last()->end;
1508 int s=0; //resulting nucleotide counter
1509 if (strand=='-') {
1510 for (int x=exons.Count()-1;x>=0;x--) {
1511 uint sgstart=exons[x]->start;
1512 uint sgend=exons[x]->end;
1513 if (seqend<sgstart || seqstart>sgend) continue;
1514 if (seqstart>=sgstart && seqstart<=sgend)
1515 sgstart=seqstart; //seqstart within this segment
1516 if (seqend>=sgstart && seqend<=sgend)
1517 sgend=seqend; //seqend within this segment
1518 s+=(int)(sgend-sgstart)+1;
1519 if (CDstart>=sgstart && CDstart<=sgend) {
1520 //CDstart in this segment
1521 //and we are getting the whole transcript
1522 cds_mend=s-(int)(CDstart-sgstart);
1523 }
1524 if (CDend>=sgstart && CDend<=sgend) {
1525 //CDstart in this segment
1526 //and we are getting the whole transcript
1527 cds_mstart=s-(int)(CDend-cdsadj-sgstart);
1528 }
1529 } //for each exon
1530 } // - strand
1531 else { // + strand
1532 for (int x=0;x<exons.Count();x++) {
1533 uint sgstart=exons[x]->start;
1534 uint sgend=exons[x]->end;
1535 if (seqend<sgstart || seqstart>sgend) continue;
1536 if (seqstart>=sgstart && seqstart<=sgend)
1537 sgstart=seqstart; //seqstart within this segment
1538 if (seqend>=sgstart && seqend<=sgend)
1539 sgend=seqend; //seqend within this segment
1540 s+=(int)(sgend-sgstart)+1;
1541 /* for (uint i=sgstart;i<=sgend;i++) {
1542 spliced[s]=gsubseq[i-gstart];
1543 s++;
1544 }//for each nt
1545 */
1546 if (CDstart>=sgstart && CDstart<=sgend) {
1547 //CDstart in this segment
1548 cds_mstart=s-(int)(sgend-CDstart-cdsadj);
1549 }
1550 if (CDend>=sgstart && CDend<=sgend) {
1551 //CDend in this segment
1552 cds_mend=s-(int)(sgend-CDend);
1553 }
1554 } //for each exon
1555 } // + strand
1556 //spliced[s]=0;
1557 //if (rlen!=NULL) *rlen=s;
1558 //return spliced;
1559 }
1560
1561 char* GffObj::getUnspliced(GFaSeqGet* faseq, int* rlen, GList<GSeg>* seglst)
1562 {
1563 if (faseq==NULL) { GMessage("Warning: getUnspliced(NULL,.. ) called!\n");
1564 return NULL;
1565 }
1566 //restore normal coordinates:
1567 unxcoord();
1568 if (exons.Count()==0) return NULL;
1569 int fspan=end-start+1;
1570 const char* gsubseq=faseq->subseq(start, fspan);
1571 if (gsubseq==NULL) {
1572 GError("Error getting subseq for %s (%d..%d)!\n", gffID, start, end);
1573 }
1574 char* unspliced=NULL;
1575
1576 int seqstart=exons.First()->start;
1577 int seqend=exons.Last()->end;
1578
1579 int unsplicedlen = 0;
1580
1581 unsplicedlen += seqend - seqstart + 1;
1582
1583 GMALLOC(unspliced, unsplicedlen+1); //allocate more here
1584 //uint seqstart, seqend;
1585
1586 int s = 0; //resulting nucleotide counter
1587 if (strand=='-')
1588 {
1589 if (seglst!=NULL)
1590 seglst->Add(new GSeg(s+1,s+1+seqend-seqstart));
1591 for (int i=seqend;i>=seqstart;i--)
1592 {
1593 unspliced[s] = ntComplement(gsubseq[i-start]);
1594 s++;
1595 }//for each nt
1596 } // - strand
1597 else
1598 { // + strand
1599 if (seglst!=NULL)
1600 seglst->Add(new GSeg(s+1,s+1+seqend-seqstart));
1601 for (int i=seqstart;i<=seqend;i++)
1602 {
1603 unspliced[s]=gsubseq[i-start];
1604 s++;
1605 }//for each nt
1606 } // + strand
1607 //assert(s <= unsplicedlen);
1608 unspliced[s]=0;
1609 if (rlen!=NULL) *rlen=s;
1610 return unspliced;
1611 }
1612
1613 char* GffObj::getSpliced(GFaSeqGet* faseq, bool CDSonly, int* rlen, uint* cds_start, uint* cds_end,
1614 GList<GSeg>* seglst) {
1615 if (CDSonly && CDstart==0) return NULL;
1616 if (faseq==NULL) { GMessage("Warning: getSpliced(NULL,.. ) called!\n");
1617 return NULL;
1618 }
1619 //restore normal coordinates:
1620 unxcoord();
1621 if (exons.Count()==0) return NULL;
1622 int fspan=end-start+1;
1623 const char* gsubseq=faseq->subseq(start, fspan);
1624 if (gsubseq==NULL) {
1625 GError("Error getting subseq for %s (%d..%d)!\n", gffID, start, end);
1626 }
1627 if (fspan<(int)(end-start+1)) { //special case: stop coordinate was extended past the gseq length, must adjust
1628 int endadj=end-start+1-fspan;
1629 uint prevend=end;
1630 end-=endadj;
1631 if (CDend>end) CDend=end;
1632 if (exons.Last()->end>end) {
1633 exons.Last()->end=end; //this could get us into trouble if exon start is also > end
1634 if (exons.Last()->start>exons.Last()->end) {
1635 GError("GffObj::getSpliced() error: improper genomic coordinate %d on %s for %s\n",
1636 prevend,getGSeqName(), getID());
1637 }
1638 covlen-=endadj;
1639 }
1640 }
1641 char* spliced=NULL;
1642 GMALLOC(spliced, covlen+1); //allocate more here
1643 uint seqstart, seqend;
1644 int cdsadj=0;
1645 if (CDphase=='1' || CDphase=='2') {
1646 cdsadj=CDphase-'0';
1647 }
1648 if (CDSonly) {
1649 seqstart=CDstart;
1650 seqend=CDend;
1651 if (strand=='-') seqend-=cdsadj;
1652 else seqstart+=cdsadj;
1653 }
1654 else {
1655 seqstart=exons.First()->start;
1656 seqend=exons.Last()->end;
1657 }
1658 int s=0; //resulting nucleotide counter
1659 if (strand=='-') {
1660 for (int x=exons.Count()-1;x>=0;x--) {
1661 uint sgstart=exons[x]->start;
1662 uint sgend=exons[x]->end;
1663 if (seqend<sgstart || seqstart>sgend) continue;
1664 if (seqstart>=sgstart && seqstart<=sgend)
1665 sgstart=seqstart; //seqstart within this segment
1666 if (seqend>=sgstart && seqend<=sgend)
1667 sgend=seqend; //seqend within this segment
1668 if (seglst!=NULL)
1669 seglst->Add(new GSeg(s+1,s+1+sgend-sgstart));
1670 for (uint i=sgend;i>=sgstart;i--) {
1671 spliced[s] = ntComplement(gsubseq[i-start]);
1672 s++;
1673 }//for each nt
1674
1675 if (!CDSonly && cds_start!=NULL && CDstart>0) {
1676 if (CDstart>=sgstart && CDstart<=sgend) {
1677 //CDstart in this segment
1678 //and we are getting the whole transcript
1679 *cds_end=s-(CDstart-sgstart);
1680 }
1681 if (CDend>=sgstart && CDend<=sgend) {
1682 //CDstart in this segment
1683 //and we are getting the whole transcript
1684 *cds_start=s-(CDend-cdsadj-sgstart);
1685 }
1686 }//update local CDS coordinates
1687 } //for each exon
1688 } // - strand
1689 else { // + strand
1690 for (int x=0;x<exons.Count();x++) {
1691 uint sgstart=exons[x]->start;
1692 uint sgend=exons[x]->end;
1693 if (seqend<sgstart || seqstart>sgend) continue;
1694 if (seqstart>=sgstart && seqstart<=sgend)
1695 sgstart=seqstart; //seqstart within this segment
1696 if (seqend>=sgstart && seqend<=sgend)
1697 sgend=seqend; //seqend within this segment
1698 if (seglst!=NULL)
1699 seglst->Add(new GSeg(s+1,s+1+sgend-sgstart));
1700 for (uint i=sgstart;i<=sgend;i++) {
1701 spliced[s]=gsubseq[i-start];
1702 s++;
1703 }//for each nt
1704 if (!CDSonly && cds_start!=NULL && CDstart>0) {
1705 if (CDstart>=sgstart && CDstart<=sgend) {
1706 //CDstart in this segment
1707 //and we are getting the whole transcript
1708 *cds_start=s-(sgend-CDstart-cdsadj);
1709 }
1710 if (CDend>=sgstart && CDend<=sgend) {
1711 //CDstart in this segment
1712 //and we are getting the whole transcript
1713 *cds_end=s-(sgend-CDend);
1714 }
1715 }//update local CDS coordinates
1716 } //for each exon
1717 } // + strand
1718 spliced[s]=0;
1719 if (rlen!=NULL) *rlen=s;
1720 return spliced;
1721 }
1722
1723 char* GffObj::getSplicedTr(GFaSeqGet* faseq, bool CDSonly, int* rlen) {
1724 if (CDSonly && CDstart==0) return NULL;
1725 //restore normal coordinates:
1726 unxcoord();
1727 if (exons.Count()==0) return NULL;
1728 int fspan=end-start+1;
1729 const char* gsubseq=faseq->subseq(start, fspan);
1730 if (gsubseq==NULL) {
1731 GError("Error getting subseq for %s (%d..%d)!\n", gffID, start, end);
1732 }
1733
1734 char* translation=NULL;
1735 GMALLOC(translation, (int)(covlen/3)+1);
1736 uint seqstart, seqend;
1737 int cdsadj=0;
1738 if (CDphase=='1' || CDphase=='2') {
1739 cdsadj=CDphase-'0';
1740 }
1741 if (CDSonly) {
1742 seqstart=CDstart;
1743 seqend=CDend;
1744 if (strand=='-') seqend-=cdsadj;
1745 else seqstart+=cdsadj;
1746 }
1747 else {
1748 seqstart=exons.First()->start;
1749 seqend=exons.Last()->end;
1750 }
1751 Codon codon;
1752 int nt=0; //codon nucleotide counter (0..2)
1753 int aa=0; //aminoacid count
1754 if (strand=='-') {
1755 for (int x=exons.Count()-1;x>=0;x--) {
1756 uint sgstart=exons[x]->start;
1757 uint sgend=exons[x]->end;
1758 if (seqend<sgstart || seqstart>sgend) continue;
1759 if (seqstart>=sgstart && seqstart<=sgend)
1760 sgstart=seqstart; //seqstart within this segment
1761 if (seqend>=sgstart && seqend<=sgend) {
1762 sgend=seqend; //seqend within this segment
1763 }
1764 for (uint i=sgend;i>=sgstart;i--) {
1765 codon.nuc[nt]=ntComplement(gsubseq[i-start]);
1766 nt++;
1767 if (nt==3) {
1768 nt=0;
1769 translation[aa]=codon.translate();
1770 aa++;
1771 }
1772 }//for each nt
1773 } //for each exon
1774 } // - strand
1775 else { // + strand
1776 for (int x=0;x<exons.Count();x++) {
1777 uint sgstart=exons[x]->start;
1778 uint sgend=exons[x]->end;
1779 if (seqend<sgstart || seqstart>sgend) continue;
1780 if (seqstart>=sgstart && seqstart<=sgend)
1781 sgstart=seqstart; //seqstart within this segment
1782 if (seqend>=sgstart && seqend<=sgend)
1783 sgend=seqend; //seqend within this segment
1784 for (uint i=sgstart;i<=sgend;i++) {
1785 codon.nuc[nt]=gsubseq[i-start];
1786 nt++;
1787 if (nt==3) {
1788 nt=0;
1789 translation[aa]=codon.translate();
1790 aa++;
1791 }
1792 }//for each nt
1793 } //for each exon
1794 } // + strand
1795 translation[aa]=0;
1796 if (rlen!=NULL) *rlen=aa;
1797 return translation;
1798 }
1799
1800 void GffObj::printSummary(FILE* fout) {
1801 if (fout==NULL) fout=stdout;
1802 fprintf(fout, "%s\t%c\t%d\t%d\t%4.2f\t%4.1f\n", gffID,
1803 strand, start, end, gscore, (float)qcov/10.0);
1804 }
1805
1806 void GffObj::printGxfLine(FILE* fout, const char* tlabel, const char* gseqname, bool iscds,
1807 uint segstart, uint segend, int exidx, char phase, bool gff3) {
1808 static char scorestr[14];
1809 strcpy(scorestr,".");
1810 GffAttrs* xattrs=NULL;
1811 if (exidx>=0) {
1812 if (exons[exidx]->score) sprintf(scorestr,"%.2f", exons[exidx]->score);
1813 xattrs=exons[exidx]->attrs;
1814 }
1815 if (phase==0 || !iscds) phase='.';
1816 const char* ftype=iscds ? "CDS" : getSubfName();
1817 if (gff3) {
1818 fprintf(fout,
1819 "%s\t%s\t%s\t%d\t%d\t%s\t%c\t%c\tParent=%s",
1820 gseqname, tlabel, ftype, segstart, segend, scorestr, strand,
1821 phase, gffID);
1822 if (xattrs!=NULL) {
1823 for (int i=0;i<xattrs->Count();i++)
1824 fprintf(fout, ";%s=%s",names->attrs.getName(xattrs->Get(i)->attr_id),
1825 xattrs->Get(i)->attr_val);
1826 }
1827 fprintf(fout, "\n");
1828 } //GFF
1829 else {//for GTF -- we print only transcripts
1830 //if (isValidTranscript())
1831 fprintf(fout, "%s\t%s\t%s\t%d\t%d\t%s\t%c\t%c\ttranscript_id \"%s\";",
1832 gseqname, tlabel, ftype, segstart, segend, scorestr, strand, phase, gffID);
1833 //char* geneid=(geneID!=NULL)? geneID : gffID;
1834 if (geneID)
1835 fprintf(fout," gene_id \"%s\";",geneID);
1836 if (gene_name!=NULL) {
1837 //fprintf(fout, " gene_name ");
1838 //if (gene_name[0]=='"') fprintf (fout, "%s;",gene_name);
1839 // else fprintf(fout, "\"%s\";",gene_name);
1840 fprintf(fout," gene_name \"%s\";",gene_name);
1841 }
1842 if (xattrs!=NULL) {
1843 for (int i=0;i<xattrs->Count();i++) {
1844 if (xattrs->Get(i)->attr_val==NULL) continue;
1845 const char* attrname=names->attrs.getName(xattrs->Get(i)->attr_id);
1846 fprintf(fout, " %s ",attrname);
1847 if (xattrs->Get(i)->attr_val[0]=='"')
1848 fprintf(fout, "%s;",xattrs->Get(i)->attr_val);
1849 else fprintf(fout, "\"%s\";",xattrs->Get(i)->attr_val);
1850 }
1851 }
1852 //for GTF, also append the GffObj attributes to each exon line
1853 if ((xattrs=this->attrs)!=NULL) {
1854 for (int i=0;i<xattrs->Count();i++) {
1855 if (xattrs->Get(i)->attr_val==NULL) continue;
1856 const char* attrname=names->attrs.getName(xattrs->Get(i)->attr_id);
1857 fprintf(fout, " %s ",attrname);
1858 if (xattrs->Get(i)->attr_val[0]=='"')
1859 fprintf(fout, "%s;",xattrs->Get(i)->attr_val);
1860 else fprintf(fout, "\"%s\";",xattrs->Get(i)->attr_val);
1861 }
1862 }
1863 fprintf(fout, "\n");
1864 }//GTF
1865 }
1866
1867 void GffObj::printGxf(FILE* fout, GffPrintMode gffp,
1868 const char* tlabel, const char* gfparent) {
1869 static char tmpstr[255];
1870 if (tlabel==NULL) {
1871 tlabel=track_id>=0 ? names->tracks.Get(track_id)->name :
1872 (char*)"gffobj" ;
1873 }
1874 unxcoord();
1875 //if (exons.Count()==0) return;
1876 const char* gseqname=names->gseqs.Get(gseq_id)->name;
1877 bool gff3 = (gffp>=pgffAny);
1878 bool showCDS = (gffp==pgtfAny || gffp==pgtfCDS || gffp==pgffCDS || gffp==pgffAny || gffp==pgffBoth);
1879 bool showExon = (gffp<=pgtfExon || gffp==pgffAny || gffp==pgffExon || gffp==pgffBoth);
1880 if (gff3) {
1881 //print GFF3 mRNA line:
1882 if (gscore>0.0) sprintf(tmpstr,"%.2f", gscore);
1883 else strcpy(tmpstr,".");
1884 uint pstart, pend;
1885 if (gffp==pgffCDS) {
1886 pstart=CDstart;
1887 pend=CDend;
1888 }
1889 else { pstart=start;pend=end; }
1890 //const char* ftype=isTranscript() ? "mRNA" : getFeatureName();
1891 const char* ftype=getFeatureName();
1892 fprintf(fout,
1893 "%s\t%s\t%s\t%d\t%d\t%s\t%c\t.\tID=%s",
1894 gseqname, tlabel, ftype, pstart, pend, tmpstr, strand, gffID);
1895 if (CDstart>0 && !showCDS && !isCDS) fprintf(fout,";CDS=%d-%d",CDstart,CDend);
1896 if (gfparent!=NULL) {
1897 //parent override
1898 fprintf(fout, ";Parent=%s",gfparent);
1899 }
1900 else {
1901 if (parent!=NULL && !parent->isDiscarded())
1902 fprintf(fout, ";Parent=%s",parent->getID());
1903 }
1904 if (geneID!=NULL)
1905 fprintf(fout, ";geneID=%s",geneID);
1906 if (gene_name!=NULL)
1907 fprintf(fout, ";gene_name=%s",gene_name);
1908 if (attrs!=NULL) {
1909 for (int i=0;i<attrs->Count();i++) {
1910 const char* attrname=names->attrs.getName(attrs->Get(i)->attr_id);
1911 fprintf(fout,";%s=%s", attrname,
1912 attrs->Get(i)->attr_val);
1913 }
1914 }
1915 fprintf(fout,"\n");
1916 }// gff3 mRNA line
1917 if (showExon) {
1918 //print exons
1919 if (isCDS && exons.Count()>0 &&
1920 ((strand=='-' && exons.Last()->phase<'0') || (strand=='+' && exons.Last()->phase<'0')))
1921 updateExonPhase();
1922
1923 for (int i=0;i<exons.Count();i++) {
1924 printGxfLine(fout, tlabel, gseqname, isCDS, exons[i]->start, exons[i]->end, i, exons[i]->phase, gff3);
1925 }
1926 }//printing exons
1927 if (showCDS && !isCDS && CDstart>0) {
1928 GArray<GffCDSeg> cds(true,true);
1929 getCDSegs(cds);
1930 for (int i=0;i<cds.Count();i++) {
1931 printGxfLine(fout, tlabel, gseqname, true, cds[i].start, cds[i].end, -1, cds[i].phase, gff3);
1932 }
1933 } //showCDS
1934 }
1935
1936 void GffObj::updateExonPhase() {
1937 if (!isCDS) return;
1938 int cdsacc=0;
1939 if (CDphase=='1' || CDphase=='2') {
1940 cdsacc+= 3-(CDphase-'0');
1941 }
1942 if (strand=='-') { //reverse strand
1943 for (int i=exons.Count()-1;i>=0;i--) {
1944 exons[i]->phase='0'+ (3-cdsacc%3)%3;
1945 cdsacc+=exons[i]->end-exons[i]->start+1;
1946 }
1947 }
1948 else { //forward strand
1949 for (int i=0;i<exons.Count();i++) {
1950 exons[i]->phase='0'+ (3-cdsacc%3)%3;
1951 cdsacc+=exons[i]->end-exons[i]->start+1;
1952 }
1953 }
1954 }
1955
1956
1957 void GffObj::getCDSegs(GArray<GffCDSeg>& cds) {
1958 GffCDSeg cdseg;
1959 int cdsacc=0;
1960 if (CDphase=='1' || CDphase=='2') {
1961 cdsacc+= 3-(CDphase-'0');
1962 }
1963 if (strand=='-') {
1964 for (int x=exons.Count()-1;x>=0;x--) {
1965 uint sgstart=exons[x]->start;
1966 uint sgend=exons[x]->end;
1967 if (CDend<sgstart || CDstart>sgend) continue;
1968 if (CDstart>=sgstart && CDstart<=sgend)
1969 sgstart=CDstart; //cdstart within this segment
1970 if (CDend>=sgstart && CDend<=sgend)
1971 sgend=CDend; //cdend within this segment
1972 cdseg.start=sgstart;
1973 cdseg.end=sgend;
1974 cdseg.exonidx=x;
1975 //cdseg.phase='0'+(cdsacc>0 ? (3-cdsacc%3)%3 : 0);
1976 cdseg.phase='0'+ (3-cdsacc%3)%3;
1977 cdsacc+=sgend-sgstart+1;
1978 cds.Add(cdseg);
1979 } //for each exon
1980 } // - strand
1981 else { // + strand
1982 for (int x=0;x<exons.Count();x++) {
1983 uint sgstart=exons[x]->start;
1984 uint sgend=exons[x]->end;
1985 if (CDend<sgstart || CDstart>sgend) continue;
1986 if (CDstart>=sgstart && CDstart<=sgend)
1987 sgstart=CDstart; //seqstart within this segment
1988 if (CDend>=sgstart && CDend<=sgend)
1989 sgend=CDend; //seqend within this segment
1990 cdseg.start=sgstart;
1991 cdseg.end=sgend;
1992 cdseg.exonidx=x;
1993 //cdseg.phase='0'+(cdsacc>0 ? (3-cdsacc%3)%3 : 0);
1994 cdseg.phase='0' + (3-cdsacc%3)%3 ;
1995 cdsacc+=sgend-sgstart+1;
1996 cds.Add(cdseg);
1997 } //for each exon
1998 } // + strand
1999 }