ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/gff.cpp
(Generate patch)
# Line 7 | Line 7
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;
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;
# Line 52 | Line 52
52               else return (int)(g1.gseq_id-g2.gseq_id);
53   }
54  
55 < char* GffLine::extractAttr(const char* pre, bool caseStrict, bool enforce_GTF2) {
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 lpre=strlen(pre);
60 < char cend=pre[lpre-1];
61 < char* pos = (caseStrict) ? strstr(info, pre) : strifind(info, pre);
62 < if (pos==NULL) return NULL;
63 < char* findstart=info;
64 < //require word boundary on the left:
65 < while (pos!=NULL && pos!=info && *(pos-1)!=';' && *(pos-1)!=' ') {
66 <    findstart=pos+lpre;
67 <    pos = (caseStrict) ? strstr(findstart, pre) : strifind(findstart, pre);
68 <    }
69 < if (pos==NULL) return NULL;
70 < if (cend!=' ' && cend!='=') {
71 <    //require word boundary on the right:
72 <    while (pos!=NULL && *(pos+lpre)!=' ' && *(pos+lpre)!='=') {
73 <       findstart=pos+lpre;
74 <       pos = (caseStrict) ? strstr(findstart, pre) : strifind(findstart, pre);
75 <       }
76 <    }
77 < if (pos==NULL) return NULL;
78 < char* vp=pos+lpre;
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", pre, dupline);
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,pre, dupline);
105 >      GError(GTF2_ERR,attr, dupline);
106   char* vend=vp;
107   if (dq_enclosed) {
108      while (*vend!='"' && *vend!=';' && *vend!=0) vend++;
# Line 94 | Line 111
111      while (*vend!=';' && *vend!=0) vend++;
112      }
113   if (enforce_GTF2 && *vend!='"')
114 <     GError(GTF2_ERR, pre, dupline);
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++;
# Line 159 | Line 176
176   ftype=t[2];
177   info=t[8];
178   char* p=t[3];
179 < if (!parseUInt(p,fstart))
180 <   GError("Error parsing start coordinate from GFF line:\n%s\n",l);
179 > if (!parseUInt(p,fstart)) {
180 >   //FIXME: 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 <   GError("Error parsing end coordinate from GFF line:\n%s\n",l);
187 < if (fend<fstart) swap(fend,fstart); //make sure fstart>=fend, always
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;
# Line 188 | Line 210
210     is_exon=true;
211     is_t_data=true;
212     }
213 <  else if (strstr(fnamelc, "exon")!=NULL) {
213 >  else if (endsWith(fnamelc, "exon")) {
214     exontype=exgffExon;
215     is_exon=true;
216     is_t_data=true;
# Line 229 | Line 251
251            }
252          return; //skip this line, unwanted feature name
253          }
254 < ID=extractAttr("ID=");
255 < char* Parent=extractAttr("Parent=");
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=",false);
262 >       gene_name=extractAttr("gene_name=");
263         if (gene_name==NULL) {
264 <           gene_name=extractAttr("geneName=",false);
264 >           gene_name=extractAttr("geneName=");
265             if (gene_name==NULL) {
266 <               gene_name=extractAttr("gene_sym=",false);
266 >               gene_name=extractAttr("gene_sym=");
267                 if (gene_name==NULL) {
268 <                   gene_name=extractAttr("gene=",false);
268 >                   gene_name=extractAttr("gene=");
269                     }
270                 }
271             }
272 <       gene_id=extractAttr("geneID=",false);
272 >       gene_id=extractAttr("geneID=");
273         if (gene_id==NULL) {
274 <          gene_id=extractAttr("gene_id=",false);
274 >          gene_id=extractAttr("gene_id=");
275            }
276         if (is_gene) {
277           //special case: keep the Name and ID attributes of the gene feature
# Line 294 | Line 316
316           } //has Parent field
317     } //GFF3
318    else { // GTF-like expected
319 <   Parent=extractAttr("transcript_id");
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
# Line 302 | Line 324
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)
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
# Line 403 | Line 431
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 && monoFeature()) {
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;
# Line 426 | Line 474
474            }
475       }
476    else { //non-mRNA parent feature, check this subf type
477 <    subf_id=names->feats.addName(gl->ftype);
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 {
# Line 506 | Line 554
554       isCDS=false;
555       }
556    if (qs || qe) {
557 <    if (qs>qe) swap(qs,qe);
557 >    if (qs>qe) Gswap(qs,qe);
558      if (qs==0) qs=1;
559          }
560    int ovlen=0;
# Line 572 | Line 620
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) {
# Line 584 | Line 639
639       return -1;            
640       }
641     covlen+=(int)(exons[eidx]->end-exons[eidx]->start)+1;
642 <   start=exons.First()->start;
643 <   end=exons.Last()->end;
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     if (uptr!=NULL) { //collect stats about the underlying genomic sequence
649         GSeqStat* gsd=(GSeqStat*)uptr;
650         if (start<gsd->mincoord) gsd->mincoord=start;
# Line 823 | Line 882
882   return gffline;
883   }
884  
885 +
886   char* GffReader::gfoBuildId(const char* id, const char* ctg) {
887   //caller must free the returned pointer
888   char* buf=NULL;
# Line 833 | Line 893
893   strcpy(buf+idlen+1, ctg);
894   return buf;
895   }
896 <
896 > /*
897   void GffReader::gfoRemove(const char* id, const char* ctg) {
898   char* buf=gfoBuildId(id,ctg);
899   phash.Remove(buf);
900   GFREE(buf);
901   }
902 <
902 > */
903   //Warning: if gflst gets altered, idx becomes obsolete
904 < GfoHolder* GffReader::gfoAdd(const char* id, const char* ctg, GffObj* gfo, int idx) {
905 < char* buf=gfoBuildId(id,ctg);
906 < GfoHolder* r=new GfoHolder(gfo,idx);
907 < phash.Add(buf, r);
908 < GFREE(buf);
909 < return r;
910 < }
911 <
912 < GfoHolder* GffReader::gfoFind(const char* id, const char* ctg) {
913 < char* buf=gfoBuildId(id,ctg);
914 < GfoHolder* r=phash.Find(buf);
915 < GFREE(buf);
916 < return r;
904 > GfoHolder* GffReader::gfoAdd(GffObj* gfo, int idx) {
905 > GVec<GfoHolder>* glst=new GVec<GfoHolder>(1);
906 > GfoHolder gh(gfo,idx);
907 > int i=glst->Add(gh);
908 > phash.Add(gfo->gffID, glst);
909 > return &(glst->Get(i));
910 > }
911 >
912 > GfoHolder* GffReader::gfoAdd(GVec<GfoHolder>& glst, GffObj* gfo, int idx) {
913 > GfoHolder gh(gfo,idx);
914 > int i=glst.Add(gh);
915 > return &(glst[i]);
916 > }
917 >
918 > GfoHolder* GffReader::gfoFind(const char* id, const char* ctg, char strand, uint start, GVec<GfoHolder>** glst) {
919 > GVec<GfoHolder>* gl=phash.Find(id);
920 > GfoHolder* gh=NULL;
921 > if (gl) {
922 >   for (int i=0;i<gl->Count();i++) {
923 >      GfoHolder& gfo = gl->Get(i);
924 >      if (ctg!=NULL && strcmp(ctg,gfo.gffobj->getGSeqName())!=0)
925 >           continue;
926 >      if (strand && strand != gfo.gffobj->strand)
927 >           continue;
928 >      if (start>0 && abs((int)start-(int)gfo.gffobj->start)>GFF_MAX_LOCUS)
929 >           continue;
930 >      //must be the same transcript, according to given comparison criteria
931 >      gh=&gfo;
932 >      break;
933 >      }
934 >   }
935 > if (glst) *glst=gl;
936 > return gh;
937   }
938  
939   GfoHolder* GffReader::replaceGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr, int replaceidx) {
# Line 861 | Line 941
941    GfoHolder* r=NULL;
942    if (replaceidx>=0) {
943       gflst.Put(replaceidx,newgfo);
944 <     r=gfoAdd(newgfo->gffID, gffline->gseqname, newgfo, replaceidx);
944 >     r=gfoAdd(newgfo, replaceidx);
945       }
946     else {
947       int gfoidx=gflst.Add(newgfo);
948 <     r=gfoAdd(newgfo->gffID, gffline->gseqname, newgfo, gfoidx);
948 >     r=gfoAdd(newgfo, gfoidx);
949       }
950    if (gff_warns) {
951      int* pcount=tids.Find(newgfo->gffID);
952      if (pcount!=NULL) {
953 <       if (gff_warns) GMessage("Warning: duplicate GFF ID: %s\n", newgfo->gffID);
953 >      if (gff_warns) GMessage("Warning: duplicate GFF ID: %s\n", newgfo->gffID);
954         (*pcount)++;
955         }
956       else {
# Line 897 | Line 977
977   }
978  
979   GfoHolder* GffReader::newGffRec(GffLine* gffline, bool keepAttr, bool noExonAttr,
980 <                          GffObj* parent, GffExon* pexon) {
980 >                          GffObj* parent, GffExon* pexon, GVec<GfoHolder>* glst) {
981    GffObj* newgfo=new GffObj(this, gffline, keepAttr, noExonAttr);
982    GfoHolder* r=NULL;
983    int gfoidx=gflst.Add(newgfo);
984 <  r=gfoAdd(newgfo->gffID, gffline->gseqname, newgfo, gfoidx);
984 >  r=(glst) ? gfoAdd(*glst, newgfo, gfoidx) :gfoAdd(newgfo, gfoidx);
985    if (parent!=NULL) {
986      updateParent(r, parent);
987      if (pexon!=NULL) parent->removeExon(pexon);
# Line 940 | Line 1020
1020   bool GffReader::addExonFeature(GfoHolder* prevgfo, GffLine* gffline, GHash<CNonExon>& pex, bool noExonAttr) {
1021    bool r=true;
1022    if (gffline->strand!=prevgfo->gffobj->strand) {
1023 <     GMessage("GFF Error: duplicate GFF ID '%s' (exons found on different strands of %s)\n",
1024 <        prevgfo->gffobj->gffID, prevgfo->gffobj->getGSeqName());
1025 <      r=false;
1026 <     }
1023 >  //TODO: add support for trans-splicing and even inter-chromosomal fusions
1024 >     if (prevgfo->gffobj->strand=='.') {
1025 >            prevgfo->gffobj->strand=gffline->strand;
1026 >        }
1027 >     else {
1028 >       GMessage("GFF Error at %s (%c): exon %d-%d (%c) found on different strand; discarded.\n",
1029 >       prevgfo->gffobj->gffID, prevgfo->gffobj->strand,
1030 >       gffline->fstart, gffline->fend, gffline->strand, prevgfo->gffobj->getGSeqName());
1031 >       //r=false;
1032 >       return true; //FIXME: split trans-spliced mRNAs by strand
1033 >       }
1034 >   }
1035    int gdist=(gffline->fstart>prevgfo->gffobj->end) ? gffline->fstart-prevgfo->gffobj->end :
1036                        ((gffline->fend<prevgfo->gffobj->start)? prevgfo->gffobj->start-gffline->fend :
1037                           0 );
# Line 1005 | Line 1093
1093    while (nextGffLine()!=NULL) {
1094         //seen this gff ID before?
1095       GfoHolder* prevseen=NULL;
1096 <     if (gffline->ID) //GFF3
1096 >     if (gffline->ID) //GFF3 parent-like feature (mRNA, gene, etc.)
1097           prevseen=gfoFind(gffline->ID, gffline->gseqname);
1098       if (prevseen!=NULL) {
1099              if (prevseen->gffobj->createdByExon()) {
1100 +                //just in case the exon was found before (shouldn't happen)
1101                  updateGffRec(prevseen, gffline, keepAttr);
1102                  }
1103               else {
# Line 1023 | Line 1112
1112      if (gffline->parents==NULL) {//start GFF3-like record with no parent (mRNA, gene)
1113         if (!prevseen) newGffRec(gffline, keepAttr, noExonAttr);
1114         }
1115 <    else { //--- it's a parented feature (could still be a mRNA)
1115 >    else { //--- it's a parented feature (exon/CDS, but might still be a mRNA)
1116         bool found_parent=false;
1117         GfoHolder* newgfo=prevseen;
1118 +       GVec<GfoHolder>* newgflst=NULL;
1119         for (int i=0;i<gffline->num_parents;i++) {
1120              if (transcriptsOnly && discarded_ids.Find(gffline->parents[i])!=NULL)
1121                  continue; //skipping discarded parent feature
1122 <            GfoHolder* parentgfo=gfoFind(gffline->parents[i], gffline->gseqname);
1122 >            GfoHolder* parentgfo=gfoFind(gffline->parents[i], gffline->gseqname, gffline->strand, gffline->fstart, &newgflst);
1123              if (parentgfo!=NULL) { //parent GffObj parsed earlier
1124                     found_parent=true;
1125                     if (parentgfo->gffobj->isGene() && gffline->is_transcript
# Line 1062 | Line 1152
1152                 }
1153                else { //no parent seen before, create one directly with this exon
1154                 //loc_debug=true;
1155 <               GfoHolder* newgfo=prevseen ? prevseen : newGffRec(gffline, keepAttr, noExonAttr);
1155 >               GfoHolder* newgfo=prevseen ? prevseen : newGffRec(gffline, keepAttr, noExonAttr, NULL, NULL, newgflst);
1156                 if (gffline->ID!=NULL && gffline->exontype==0)
1157                       subfPoolAdd(pex, newgfo);
1158                 //even those with errors will be added here!

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines