ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/cuffcompare/gtf_tracking.h
Revision: 54
Committed: Thu Sep 8 05:46:16 2011 UTC (8 years, 11 months ago) by gpertea
File size: 45496 byte(s)
Log Message:
debug RPS20P29 ENSG00000241828 not collapsing

Line User Rev File contents
1 gpertea 20 #ifndef GTF_TRACKING_H
2     #define GTF_TRACKING_H
3     /*
4     * gtf_tracking.h
5     * cufflinks
6     *
7     * Created by Cole Trapnell on 9/5/09.
8     * Copyright 2009 Geo Pertea. All rights reserved.
9     *
10     */
11    
12     #include "gff.h"
13     #include "GFaSeqGet.h"
14     #include "GFastaIndex.h"
15     #include "GStr.h"
16    
17    
18     #define MAX_QFILES 500
19    
20     extern bool gtf_tracking_verbose;
21    
22     extern bool gtf_tracking_largeScale;
23     //many input files, no accuracy stats are generated, no *.tmap
24     // and exon attributes are discarded
25    
26     int cmpByPtr(const pointer p1, const pointer p2);
27    
28     bool t_contains(GffObj& a, GffObj& b);
29     //returns true only IF b has fewer exons than a AND a "contains" b
30    
31     char* getGSeqName(int gseq_id);
32    
33     //genomic fasta sequence handling
34     class GFastaHandler {
35     public:
36     char* fastaPath;
37     GFastaIndex* faIdx;
38     char* getFastaFile(int gseq_id) {
39     if (fastaPath==NULL) return NULL;
40     GStr s(fastaPath);
41     s.trimR('/');
42     s.appendfmt("/%s",getGSeqName(gseq_id));
43     GStr sbase(s);
44     if (!fileExists(s.chars())) s.append(".fa");
45     if (!fileExists(s.chars())) s.append("sta");
46     if (fileExists(s.chars())) return Gstrdup(s.chars());
47     else {
48     GMessage("Warning: cannot find genomic sequence file %s{.fa,.fasta}\n",sbase.chars());
49     return NULL;
50     }
51     }
52    
53     GFastaHandler(const char* fpath=NULL) {
54     fastaPath=NULL;
55     faIdx=NULL;
56     if (fpath!=NULL && fpath[0]!=0) init(fpath);
57     }
58    
59     void init(const char* fpath) {
60     if (fpath==NULL || fpath[0]==0) return;
61     if (!fileExists(fpath))
62     GError("Error: file/directory %s does not exist!\n",fpath);
63     fastaPath=Gstrdup(fpath);
64     if (fastaPath!=NULL) {
65     if (fileExists(fastaPath)>1) { //exists and it's not a directory
66     GStr fainame(fastaPath);
67     //the .fai name might have been given directly
68     if (fainame.rindex(".fai")==fainame.length()-4) {
69     //.fai index file given directly
70     fastaPath[fainame.length()-4]=0;
71     if (!fileExists(fastaPath))
72     GError("Error: cannot find fasta file for index %s !\n", fastaPath);
73     }
74     else fainame.append(".fai");
75     //fainame.append(".fai");
76     faIdx=new GFastaIndex(fastaPath,fainame.chars());
77     GStr fainamecwd(fainame);
78     int ip=-1;
79     if ((ip=fainamecwd.rindex('/'))>=0)
80     fainamecwd.cut(0,ip+1);
81     if (!faIdx->hasIndex()) { //could not load index
82     //try current directory
83     if (fainame!=fainamecwd) {
84     if (fileExists(fainamecwd.chars())>1) {
85     faIdx->loadIndex(fainamecwd.chars());
86     }
87     }
88     } //tried to load index
89     if (!faIdx->hasIndex()) {
90     GMessage("No fasta index found for %s. Rebuilding, please wait..\n",fastaPath);
91     faIdx->buildIndex();
92     if (faIdx->getCount()==0) GError("Error: no fasta records found!\n");
93     GMessage("Fasta index rebuilt.\n");
94     FILE* fcreate=fopen(fainame.chars(), "w");
95     if (fcreate==NULL) {
96     GMessage("Warning: cannot create fasta index %s! (permissions?)\n", fainame.chars());
97     if (fainame!=fainamecwd) fcreate=fopen(fainamecwd.chars(), "w");
98     if (fcreate==NULL)
99     GError("Error: cannot create fasta index %s!\n", fainamecwd.chars());
100     }
101     if (faIdx->storeIndex(fcreate)<faIdx->getCount())
102     GMessage("Warning: error writing the index file!\n");
103     } //index created and attempted to store it
104     } //multi-fasta
105     } //genomic sequence given
106     }
107     GFaSeqGet* fetch(int gseq_id, bool checkFasta=false) {
108     if (fastaPath==NULL) return NULL;
109     //genomic sequence given
110     GFaSeqGet* faseq=NULL;
111     if (faIdx!=NULL) { //fastaPath was the multi-fasta file name
112     char* gseqname=getGSeqName(gseq_id);
113     GFastaRec* farec=faIdx->getRecord(gseqname);
114     if (farec!=NULL) {
115     faseq=new GFaSeqGet(fastaPath,farec->seqlen, farec->fpos,
116     farec->line_len, farec->line_blen);
117     faseq->loadall(); //just cache the whole sequence, it's faster
118     }
119     else {
120     GMessage("Warning: couldn't find fasta record for '%s'!\n",gseqname);
121     return NULL;
122     }
123     }
124     else //if (fileExists(fastaPath)==1)
125     {
126     char* sfile=getFastaFile(gseq_id);
127     if (sfile!=NULL) {
128     //if (gtf_tracking_verbose)
129     // GMessage("Processing sequence from fasta file '%s'\n",sfile);
130     faseq=new GFaSeqGet(sfile,checkFasta);
131     faseq->loadall();
132     GFREE(sfile);
133     }
134     } //one fasta file per contig
135     return faseq;
136     }
137    
138     ~GFastaHandler() {
139     GFREE(fastaPath);
140     delete faIdx;
141     }
142     };
143    
144    
145    
146     bool betterRef(GffObj* a, GffObj* b); //for better CovLink reference ranking
147    
148     class GLocus;
149    
150     class COvLink {
151     public:
152     static int coderank(char c) {
153     switch (c) {
154     case '=': return 0; //ichain match
155     case 'c': return 2; //containment (ichain fragment)
156     case 'j': return 4; // overlap with at least a junction match
157     case 'e': return 6; // single exon transfrag overlapping an intron of reference (possible pre-mRNA)
158     case 'o': return 8; // generic exon overlap
159     case 's': return 16; //"shadow" - an intron overlaps with a ref intron on the opposite strand
160     case 'x': return 18; // exon overlap on opposite strand (usually wrong strand mapping)
161     case 'i': return 20; // intra-intron
162     case 'p': return 90; //polymerase run
163     case 'r': return 92; //repeats
164     case 'u': return 94; //intergenic
165     case 0 : return 100;
166     default: return 96;
167     }
168     }
169     char code;
170     int rank;
171     GffObj* mrna;
172     int ovlen;
173     COvLink(char c=0,GffObj* m=NULL, int ovl=0) {
174     code=c;
175     mrna=m;
176     ovlen=ovl;
177     rank=coderank(c);
178     }
179     bool operator<(COvLink& b) {
180     if (rank==b.rank)
181     return (ovlen==b.ovlen)? betterRef(mrna, b.mrna) : (ovlen>b.ovlen);
182     else return rank<b.rank;
183     }
184     bool operator>(COvLink& b) {
185     if (rank==b.rank)
186     return (ovlen==b.ovlen)? betterRef(b.mrna, mrna) : (ovlen<b.ovlen);
187     else return rank>b.rank;
188     }
189     bool operator==(COvLink& b) {
190     return (rank==b.rank && mrna==b.mrna);
191     }
192     };
193    
194     class GISeg: public GSeg {
195     public:
196     GffObj* t; //pointer to the largest transcript with a segment this exact exon coordinates
197     GISeg(uint s=0,uint e=0, GffObj* ot=NULL):GSeg(s,e) { t=ot; }
198     };
199    
200     class GIArray:public GArray<GISeg> {
201     public:
202     GIArray(bool uniq=true):GArray<GISeg>(true,uniq) { }
203     int IAdd(GISeg* item) {
204     if (item==NULL) return -1;
205     int result=-1;
206     if (Found(*item, result)) {
207     if (fUnique) {
208     //cannot add a duplicate, return index of existing item
209     if (item->t!=NULL && fArray[result].t!=NULL &&
210     item->t->covlen>fArray[result].t->covlen)
211     fArray[result].t=item->t;
212     return result;
213     }
214     }
215     //Found sets result to the position where the item should be
216     idxInsert(result, *item);
217     return result;
218     }
219    
220     };
221    
222     class CEqList: public GList<GffObj> {
223     public:
224     GffObj* head;
225     CEqList():GList<GffObj>((GCompareProc*)cmpByPtr, (GFreeProc*)NULL, true) {
226     head=NULL;
227     }
228     };
229    
230     class CTData { //transcript associated data
231     public:
232     GffObj* mrna; //owner transcript
233     GLocus* locus;
234     GList<COvLink> ovls; //overlaps with other transcripts (ref vs query)
235     //-- just for ichain match tracking:
236     GffObj* eqref; //ref transcript having an ichain match
237     int qset; //qry set index (qfidx), -1 means reference dataset
238     //GffObj* eqnext; //next GffObj in the linked list of matching transfrags
239     CEqList* eqlist; //keep track of matching transfrags
240     //int eqdata; // flags for EQ list (is it a list head?)
241     // Cufflinks specific data:
242     double FPKM;
243     double conf_hi;
244     double conf_lo;
245     double cov;
246     char classcode; //the best/final classcode
247     CTData(GffObj* m=NULL, GLocus* l=NULL):ovls(true,true,true) {
248     mrna=m;
249     if (mrna!=NULL) mrna->uptr=this;
250     locus=l;
251     classcode=0;
252     eqref=NULL;
253     //eqnext=NULL;
254     eqlist=NULL;
255     //eqdata=0;
256     qset=-2;
257     FPKM=0;
258     conf_lo=0;
259     conf_hi=0;
260     cov=0;
261     }
262    
263     ~CTData() {
264     ovls.Clear();
265     //if ((eqdata & EQHEAD_TAG)!=0) delete eqlist;
266     if (isEqHead()) delete eqlist;
267     }
268    
269     //inline bool eqHead() { return ((eqdata & EQHEAD_TAG)!=0); }
270     bool isEqHead() {
271     if (eqlist==NULL) return false;
272     return (eqlist->head==this->mrna);
273     }
274    
275     void joinEqList(GffObj* m) { //add list from m
276     //list head is set to the transfrag with the lower qset#
277     CTData* md=(CTData*)(m->uptr);
278     //ASSERT(md);
279     if (eqlist==NULL) {
280     if (md->eqlist!=NULL) {
281     eqlist=md->eqlist;
282     eqlist->Add(this->mrna);
283     CTData* md_head_d=(CTData*)(md->eqlist->head->uptr);
284     if (this->qset < md_head_d->qset)
285     eqlist->head=this->mrna;
286     }
287     else { //m was not in an EQ list
288     //eqlist=new GList<GffObj>((GCompareProc*)cmpByPtr, (GFreeProc*)NULL, true);
289     eqlist=new CEqList();
290     eqlist->Add(this->mrna);
291     eqlist->Add(m);
292     md->eqlist=eqlist;
293     if (qset<md->qset) eqlist->head=this->mrna;
294     else eqlist->head=m;
295     }
296     }//no eqlist before
297     else { //merge two eqlists
298     if (eqlist==md->eqlist) //already in the same eqlist, nothing to do
299     return;
300     if (md->eqlist!=NULL) { //copy elements of m's eqlist
301     //copy the smaller list into the larger one
302     CEqList* srclst, *destlst;
303     if (md->eqlist->Count()<eqlist->Count()) {
304     srclst=md->eqlist;
305     destlst=eqlist;
306     }
307     else {
308     srclst=eqlist;
309     destlst=md->eqlist;
310     }
311     for (int i=0;i<srclst->Count();i++) {
312     destlst->Add(srclst->Get(i));
313     CTData* od=(CTData*)((*srclst)[i]->uptr);
314     od->eqlist=destlst;
315     //od->eqdata=od->qset+1;
316     }
317     this->eqlist=destlst;
318     CTData* s_head_d=(CTData*)(srclst->head->uptr);
319     CTData* d_head_d=(CTData*)(destlst->head->uptr);
320     if (s_head_d->qset < d_head_d->qset )
321     this->eqlist->head=srclst->head;
322     delete srclst;
323     }
324     else { //md->eqlist==NULL
325     eqlist->Add(m);
326     md->eqlist=eqlist;
327     CTData* head_d=(CTData*)(eqlist->head->uptr);
328     if (md->qset<head_d->qset)
329     eqlist->head=m;
330     }
331     }
332     }
333    
334     void addOvl(char code,GffObj* target=NULL, int ovlen=0) {
335     ovls.AddIfNew(new COvLink(code, target, ovlen));
336     }
337     char getBestCode() {
338     return (ovls.Count()>0) ? ovls[0]->code : 0 ;
339     }
340     bool operator>(CTData& b) { return (mrna > b.mrna); }
341     bool operator<(CTData& b) { return (mrna < b.mrna); }
342     bool operator==(CTData& b) { return (mrna==b.mrna); }
343     };
344    
345     class GSuperLocus;
346     class GTrackLocus;
347     class GXLocus;
348    
349     //Data structure holding a query locus data (overlapping mRNAs on the same strand)
350     // and also the accuracy data of all mRNAs of a query locus
351     // (against all reference loci overlapping the same region)
352     class GLocus:public GSeg {
353     public:
354     int gseq_id; //id of underlying genomic sequence
355     int qfidx; // for locus tracking
356     GTrackLocus* t_ptr; //for locus tracking cluster
357     GffObj* mrna_maxcov; //transcript with maximum coverage (for main "ref" transcript)
358     GffObj* mrna_maxscore; //transcript with maximum gscore (for major isoform)
359     GList<GffObj> mrnas; //list of transcripts (isoforms) for this locus
360     GArray<GSeg> uexons; //list of unique exons (covered segments) in this region
361     GArray<GSeg> mexons; //list of merged exons in this region
362     GIArray introns;
363     GList<GLocus> cmpovl; //temp list of overlapping qry/ref loci to compare to (while forming superloci)
364    
365     //only for reference loci --> keep track of all superloci found for each qry dataset
366     // which contain this reference locus
367     GList<GSuperLocus>* superlst;
368     GXLocus* xlocus; //superlocus formed by exon overlaps across all qry datasets
369     // -- if genomic sequence was given:
370     int spl_major; // number of GT-AG splice site consensi
371     int spl_rare; // number of GC-AG, AT-AC and other rare splice site consensi
372     int spl_wrong; //number of "wrong" (unrecognized) splice site consensi
373     int ichains; //number of multi-exon mrnas
374     int ichainTP;
375     int ichainATP;
376     int mrnaTP;
377     int mrnaATP;
378     int v; //user flag/data
379     GLocus(GffObj* mrna=NULL, int qidx=-1):mrnas(true,false,false),uexons(true,true),mexons(true,true),
380     introns(), cmpovl(true,false,true) {
381     //this will NOT free mrnas!
382     ichains=0;
383     gseq_id=-1;
384     qfidx=qidx;
385     t_ptr=NULL;
386     creset();
387     xlocus=NULL;
388     mrna_maxcov=NULL;
389     mrna_maxscore=NULL;
390     superlst=new GList<GSuperLocus>(true,false,false);
391     if (mrna!=NULL) {
392     start=mrna->exons.First()->start;
393     end=mrna->exons.Last()->end;;
394     gseq_id=mrna->gseq_id;
395     GISeg seg;
396     for (int i=0;i<mrna->exons.Count();i++) {
397     seg.start=mrna->exons[i]->start;
398     seg.end=mrna->exons[i]->end;
399     uexons.Add(seg);
400     mexons.Add(seg);
401     if (i>0) {
402     seg.start=mrna->exons[i-1]->end+1;
403     seg.end=mrna->exons[i]->start-1;
404     seg.t=mrna;
405     introns.Add(seg);
406     }
407     }
408     mrnas.Add(mrna);
409     if (mrna->exons.Count()>1) ichains++;
410     ((CTData*)(mrna->uptr))->locus=this;
411     mrna_maxscore=mrna;
412     mrna_maxcov=mrna;
413     }
414     }
415     ~GLocus() {
416     delete superlst;
417     }
418     void creset() {
419     spl_major=0;spl_rare=0;spl_wrong=0;
420     v=0; //visited/other data
421     ichainTP=0;
422     ichainATP=0;
423     mrnaTP=0;
424     mrnaATP=0;
425     cmpovl.Clear();
426     }
427    
428     void addMerge(GLocus& locus, GffObj* lnkmrna) {
429     //add all the elements of the other locus (merging)
430     //-- merge mexons
431     GArray<int> ovlexons(true,true); //list of locus.mexons indexes overlapping existing mexons
432     int i=0; //index of first mexons with a merge
433     int j=0; //index current mrna exon
434     while (i<mexons.Count() && j<locus.mexons.Count()) {
435     uint istart=mexons[i].start;
436     uint iend=mexons[i].end;
437     uint jstart=locus.mexons[j].start;
438     uint jend=locus.mexons[j].end;
439     if (iend<jstart) { i++; continue; }
440     if (jend<istart) { j++; continue; }
441     //if (mexons[i].overlap(jstart, jend)) {
442     //exon overlap was found :
443     ovlexons.Add(j);
444     //extend mexons[i] as needed
445     if (jstart<istart) mexons[i].start=jstart;
446     if (jend>iend) { //mexons[i] end extend
447     mexons[i].end=jend;
448     //now this could overlap the next mexon(s), so we have to merge them all
449     while (i<mexons.Count()-1 && mexons[i].end>mexons[i+1].start) {
450     uint nextend=mexons[i+1].end;
451     mexons.Delete(i+1);
452     if (nextend>mexons[i].end) {
453     mexons[i].end=nextend;
454     break; //no need to check next mexons
455     }
456     } //while next mexons merge
457     } // mexons[i] end extend
458     // } //exon overlap
459     j++; //check the next locus.mexon
460     }
461     //-- add the rest of the non-overlapping mexons:
462     GSeg seg;
463     for (int i=0;i<locus.mexons.Count();i++) {
464     seg.start=locus.mexons[i].start;
465     seg.end=locus.mexons[i].end;
466     if (!ovlexons.Exists(i)) mexons.Add(seg);
467     }
468     // -- merge uexons
469     //add to uexons:
470     for (int i=0;i<locus.uexons.Count();i++) {
471     uexons.Add(locus.uexons[i]);
472     }
473     for (int i=0;i<locus.introns.Count();i++) {
474     introns.IAdd(&(locus.introns[i]));
475     }
476    
477     // -- add locus.mrnas
478     for (int i=0;i<locus.mrnas.Count();i++) {
479     ((CTData*)(locus.mrnas[i]->uptr))->locus=this;
480     if (locus.mrnas[i]!=lnkmrna) {
481     mrnas.Add(locus.mrnas[i]);
482     if (locus.mrnas[i]->exons.Count()>1) ichains++;
483     }
484     }
485     // -- adjust start/end as needed
486     if (start>locus.start) start=locus.start;
487     if (end<locus.end) end=locus.end;
488     if (mrna_maxcov->covlen<locus.mrna_maxcov->covlen)
489     mrna_maxcov=locus.mrna_maxcov;
490     if (mrna_maxscore->gscore<locus.mrna_maxscore->gscore)
491     mrna_maxscore=locus.mrna_maxscore;
492     }
493    
494    
495     bool exonOverlap(GLocus& loc) {
496     //check if any mexons overlap!
497     int i=0;
498     int j=0;
499     while (i<mexons.Count() && j<loc.mexons.Count()) {
500     uint istart=mexons[i].start;
501     uint iend=mexons[i].end;
502     uint jstart=loc.mexons[j].start;
503     uint jend=loc.mexons[j].end;
504     if (iend<jstart) { i++; continue; }
505     if (jend<istart) { j++; continue; }
506     //exon overlap found if we're here:
507     return true;
508     }
509     return false;
510     }
511    
512     bool add_mRNA(GffObj* mrna) {
513     if (mrnas.Count()>0 && mrna->gseq_id!=gseq_id) return false; //mrna must be on the same genomic seq
514     //check for exon overlap with existing mexons
515     //also update uexons and mexons accordingly, if mrna is added
516     uint mrna_start=mrna->exons.First()->start;
517     uint mrna_end=mrna->exons.Last()->end;
518     if (mrna_start>end || start>mrna_end) return false;
519     bool hasovl=false;
520     int i=0; //index of first mexons with a merge
521     int j=0; //index current mrna exon
522     GArray<int> ovlexons(true,true); //list of mrna exon indexes overlapping mexons
523     while (i<mexons.Count() && j<mrna->exons.Count()) {
524     uint istart=mexons[i].start;
525     uint iend=mexons[i].end;
526     uint jstart=mrna->exons[j]->start;
527     uint jend=mrna->exons[j]->end;
528     if (iend<jstart) { i++; continue; }
529     if (jend<istart) { j++; continue; }
530     //exon overlap found if we're here:
531     ovlexons.Add(j);
532     hasovl=true;
533     //extend mexons[i] as needed
534     if (jstart<istart) mexons[i].start=jstart;
535     if (jend>iend) { //mexon stretch up
536     mexons[i].end=jend;
537     //now this could overlap the next mexon(s), so we have to merge them all
538     while (i<mexons.Count()-1 && mexons[i].end>mexons[i+1].start) {
539     uint nextend=mexons[i+1].end;
540     mexons.Delete(i+1);
541     if (nextend>mexons[i].end) {
542     mexons[i].end=nextend;
543     break; //no need to check next mexons
544     }
545     } //while next mexons merge
546     } //possible mexons merge
547    
548     j++; //check the next mrna exon
549     }//all vs all exon check loop
550     if (hasovl) {
551     GSeg seg;
552     //add the rest of the non-overlapping exons,
553     // and also to uexons etc.
554     for (int i=0;i<mrna->exons.Count();i++) {
555     seg.start=mrna->exons[i]->start;
556     seg.end=mrna->exons[i]->end;
557     if (!ovlexons.Exists(i)) mexons.Add(seg);
558     uexons.Add(seg);
559     GISeg iseg;
560     if (i>0) {
561     iseg.start=mrna->exons[i-1]->end+1;
562     iseg.end=mrna->exons[i]->start-1;
563     iseg.t=mrna;
564     introns.Add(iseg);
565     }
566     }
567    
568     mrnas_add(mrna);
569     // add to mrnas
570     ((CTData*)mrna->uptr)->locus=this;
571     gseq_id=mrna->gseq_id;
572     if (mrna->exons.Count()>1) ichains++;
573     }
574     return hasovl;
575     }
576    
577     //simpler,basic adding of a mrna
578     void mrnas_add(GffObj* mrna) {
579     mrnas.Add(mrna);
580     // adjust start/end
581     if (start>mrna->start) start=mrna->start;
582     if (end<mrna->end) end=mrna->end;
583     if (mrna_maxcov->covlen<mrna->covlen) mrna_maxcov=mrna;
584     if (mrna_maxscore->gscore<mrna->gscore) mrna_maxscore=mrna;
585     }
586     };
587    
588     class GSuperLocus;
589     class GTrackLocus;
590    
591     class GSuperLocus : public GSeg {
592     public:
593     int qfidx; //index of query dataset/file for which this superlocus was created
594     GList<GLocus> qloci;
595     GList<GLocus> rloci;
596     GList<GffObj> qmrnas; //list of transcripts (isoforms) for this locus
597     GArray<GSeg> qmexons; //list of merged exons in this region
598     GArray<GSeg> quexons; //list of unique exons (covered segments) in this region
599     GIArray qintrons; //list of unique exons (covered segments) in this region
600     //same lists for reference:
601     GList<GffObj> rmrnas; //list of transcripts (isoforms) for this locus
602     GArray<GSeg> rmexons; //list of merged exons in this region
603     GArray<GSeg> ruexons; //list of unique exons (covered segments) in this region
604     GArray<GISeg> rintrons; //list of unique exons (covered segments) in this region
605     // store problematic introns for printing:
606     GIArray i_missed; //missed reference introns (not overlapped by any qry intron)
607     GIArray i_notp; //wrong ref introns (one or both ends not matching any qry intron)
608     //
609     GIArray i_qwrong; //totally wrong qry introns (not overlapped by any ref intron)
610     GIArray i_qnotp; //imperfect qry introns (may overlap but has no "perfect" match)
611    
612    
613     int qbases_all;
614     int rbases_all; //in fact, it's all ref bases overlapping any query loci
615     int in_rmrnas; //count of ALL ref mrnas and loci given for this region
616     int in_rloci; //not just those overlapping qry data
617     // this will keep track of total qry loci, mrnas and exons in an area
618     int total_superloci;
619     int total_qloci;
620     int total_qloci_alt; //total qloci with multiple transcripts
621    
622     int total_qmrnas;
623     int total_qichains; //multi exon mrnas
624     int total_qexons; //unique exons
625     int total_qmexons;
626     int total_qintrons; //unique introns
627     // these ref totals are in fact only limited to data from
628     // loci overlapping any of qry loci
629     int total_rmexons;
630     int total_richains; //multi exon mrnas
631     int total_rloci;
632     int total_rmrnas;
633     int total_rexons;
634     int total_rintrons; //unique introns
635    
636     //--- accuracy data after compared to ref loci:
637     int locusTP; // 1 if mrnaTP>0
638     int locusATP; // 1 if ichainATP + mrnaATP > 0
639     int locusFP;
640     int locusAFP;
641     int locusAFN;
642     int locusFN;
643     //---transcript level accuracy -- all exon coordinates should match (most stringent)
644     int mrnaTP; // number of qry mRNAs with perfect match with ref transcripts
645     int mrnaFP; // number of qry mRNAs with no perfect match with a ref transcript
646     int mrnaFN; // number of ref mRNAs in this region having no perfect match with a qry transcript
647     int mrnaATP;
648     int mrnaAFN;
649     int mrnaAFP;
650     //---intron level accuracy (comparing the ordered set of splice sites):
651     int ichainTP; // number of qry intron chains covering a reference intron chain
652     // (covering meaning that the ordered set of reference splice sites
653     // is the same with a ordered subset of the query splice sites)
654     int ichainFP; // number of qry intron chains not covering a reference intron chain
655     int ichainFN; // number of ref intron chains in this region not being covered by a reference intron chain
656     // same as above, but approximate -- allowing a 10bp distance error for splice sites
657     int ichainATP;
658     int ichainAFP;
659     int ichainAFN;
660     //---projected features ---
661     //---exon level accuracy:
662     int exonTP; //number of perfectly overlapping exons (true positives)
663     int exonFP; //number of exons of query with no perfect match with a reference exon
664     int exonFN; //number of exons of reference with no perfect match with a query exon
665     // same as the above but with acceptable approximation (10bp error window):
666     int exonATP;
667     int exonAFP;
668     int exonAFN;
669    
670     int intronTP; //number of perfectly overlapping introns (true positives)
671     int intronFP; //number of introns of query with no perfect match with a reference intron
672     int intronFN; //number of introns of reference with no perfect match with a query intron
673     // same as the above but with acceptable approximation (10bp error window):
674     int intronATP;
675     int intronAFP;
676     int intronAFN;
677    
678     //-- EGASP added these too:
679     int m_exons; //number of exons totally missed (not overlapped *at all* by any query exon)
680     int w_exons; //numer of totally wrong exons (query exons not overlapping *at all* any reference exon)
681     int m_introns; //number of introns totally missed (not overlapped *at all* by any query intron)
682     int w_introns; //numer of totally wrong introns (query introns not overlapping *at all* any reference intron)
683     int m_loci; //missed loci
684     int w_loci; //novel/wrong loci
685     //---base level accuracy
686     int baseTP; //number of overlapping bases
687     int baseFP; //number of qry bases not overlapping reference
688     int baseFN; //number of ref bases not overlapping qry
689     // sorted,free,unique sorted,unique
690     GSuperLocus(uint lstart=0,uint lend=0):qloci(true,false,false),rloci(true,false,false),
691     qmrnas(true,false,false), qmexons(true,false), quexons(true,false), qintrons(false),
692     rmrnas(true,false,false), rmexons(true,false), ruexons(true,false), rintrons(false),
693     i_missed(false),i_notp(false), i_qwrong(false), i_qnotp(false){
694     qfidx=-1;
695     start=lstart;
696     end=lend;
697     qbases_all=0;
698     rbases_all=0;
699     baseTP=0;baseFP=0;baseFN=0;
700     locusTP=0;locusATP=0;
701     locusFP=0;locusAFP=0;locusAFN=0;
702     locusFN=0;
703     in_rmrnas=0;
704     in_rloci=0;
705     w_loci=0;
706     m_loci=0;
707     total_superloci=0;
708     mrnaTP=0;mrnaFP=0;mrnaFN=0;
709     mrnaATP=0;mrnaAFP=0;mrnaAFN=0;
710     ichainTP=0;ichainFP=0;ichainFN=0;
711     ichainATP=0;ichainAFP=0;ichainAFN=0;
712     exonTP=0;exonFP=0;exonFN=0;
713     exonATP=0;exonAFP=0;exonAFN=0;
714     intronTP=0;intronFP=0;intronFN=0;
715     intronATP=0;intronAFP=0;intronAFN=0;
716     total_rmexons=0;
717     total_qmexons=0;
718     total_qexons=0;total_qloci=0;total_qmrnas=0;
719     total_qloci_alt=0;
720     total_qintrons=0;total_qichains=0;
721     total_rexons=0;total_rloci=0;total_rmrnas=0;
722     total_rintrons=0;total_richains=0;
723     w_exons=0;
724     m_exons=0;
725     w_introns=0;
726     m_introns=0;
727     }
728     void addQlocus(GLocus& loc) {
729     if (start==0 || start>loc.start) start=loc.start;
730     if (end<loc.end) end=loc.end;
731     qloci.Add(&loc);
732     total_qloci++;
733     if (loc.ichains>0 && loc.mrnas.Count()>1)
734     total_qloci_alt++;
735     qmrnas.Add(loc.mrnas);
736     total_qmrnas+=loc.mrnas.Count();
737     total_qichains+=loc.ichains;
738     qmexons.Add(loc.mexons);
739     total_qmexons+=loc.mexons.Count();
740     quexons.Add(loc.uexons);
741     total_qexons+=loc.uexons.Count();
742     qintrons.Add(loc.introns);
743     total_qintrons+=loc.introns.Count();
744     }
745     void addRlocus(GLocus& loc) {
746     if (start==0 || start>loc.start) start=loc.start;
747     if (end<loc.end) end=loc.end;
748     rloci.Add(&loc);
749     total_rloci++;
750     rmrnas.Add(loc.mrnas);
751     total_rmrnas+=loc.mrnas.Count();
752     total_richains+=loc.ichains;
753     rmexons.Add(loc.mexons);
754     total_rmexons+=loc.mexons.Count();
755     ruexons.Add(loc.uexons);
756     total_rexons+=loc.uexons.Count();
757     rintrons.Add(loc.introns);
758     total_rintrons+=loc.introns.Count();
759     }
760    
761     void calcF() {
762     // base level
763     baseFP=qbases_all-baseTP;
764     baseFN=rbases_all-baseTP;
765     //exon level:
766     exonAFP=total_qexons-exonATP;
767     exonFP=total_qexons-exonTP;
768     exonAFN=total_rexons-exonATP;
769     exonFN=total_rexons-exonTP;
770     //intron stats
771     intronAFP=total_qintrons-intronATP;
772     intronFP=total_qintrons-intronTP;
773     intronAFN=total_rintrons-intronATP;
774     intronFN=total_rintrons-intronTP;
775    
776     // ichain and transcript levels:
777     ichainAFP=total_qichains-ichainATP;
778     ichainFP=total_qichains-ichainTP;
779     ichainAFN=total_richains-ichainATP;
780     ichainFN=total_richains-ichainTP;
781     mrnaFP=total_qmrnas-mrnaTP;
782     mrnaFN=total_rmrnas-mrnaTP;
783     mrnaAFP=total_qmrnas-mrnaATP;
784     mrnaAFN=total_rmrnas-mrnaATP;
785     // locus/gene level:
786     locusAFP=total_qloci-locusATP;
787     locusFP=total_qloci-locusTP;
788     locusAFN=total_rloci-locusATP;
789     locusFN=total_rloci-locusTP;
790     }
791    
792     void addStats(GSuperLocus& s) {
793     in_rmrnas+=s.in_rmrnas;
794     in_rloci+=s.in_rloci;
795     baseTP+=s.baseTP;
796     exonTP+=s.exonTP;
797     exonATP+=s.exonATP;
798     intronTP+=s.intronTP;
799     intronATP+=s.intronATP;
800     ichainTP+=s.ichainTP;
801     ichainATP+=s.ichainATP;
802     mrnaTP+=s.mrnaTP;
803     mrnaATP+=s.mrnaATP;
804     locusTP+=s.locusTP;
805     locusATP+=s.locusATP;
806     m_exons+=s.m_exons;
807     w_exons+=s.w_exons;
808     m_introns+=s.m_introns;
809     w_introns+=s.w_introns;
810     if (s.total_superloci==0 && s.qloci.Count()>0) s.total_superloci=1;
811     total_superloci+=s.total_superloci;
812     qbases_all+=s.qbases_all;
813     rbases_all+=s.rbases_all;
814     m_loci+=s.m_loci;
815     w_loci+=s.w_loci;
816     total_qexons+=s.total_qexons;
817     total_qintrons+=s.total_qintrons;
818     total_qmexons+=s.total_qmexons;
819     total_rexons+=s.total_rexons;
820     total_rintrons+=s.total_rintrons;
821     total_rmexons+=s.total_rmexons;
822     total_qmrnas+=s.total_qmrnas;
823     total_qichains+=s.total_qichains;
824     total_rmrnas+=s.total_rmrnas;
825     total_richains+=s.total_richains;
826     total_qloci+=s.total_qloci;
827     total_qloci_alt+=s.total_qloci_alt;
828     total_rloci+=s.total_rloci;
829     }
830     };
831    
832     class GSeqData {
833     int gseq_id;
834     public:
835     const char* gseq_name;
836     GList<GffObj> refs_f; //forward strand mRNAs
837     GList<GffObj> refs_r; //reverse strand mRNAs
838     GList<GffObj> mrnas_f; //forward strand mRNAs
839     GList<GffObj> mrnas_r; //reverse strand mRNAs
840     GList<GLocus> loci_f; //forward strand loci
841     GList<GLocus> loci_r; //reverse strand loci
842     //--> the fields below are not used by reference data --
843     GList<GSuperLocus> gstats_f; //stats for forward strand superloci
844     GList<GSuperLocus> gstats_r; //stats for reverse strand superloci
845     GList<GLocus> nloci_f; //"novel" loci on forward strand
846     GList<GLocus> nloci_r; //"novel" loci on reverse strand
847     GList<GffObj> umrnas; //unknown orientation mrnas
848     GList<GLocus> nloci_u; //"novel" loci with no orientation found
849    
850     GList<CTData> tdata; //transcript data (uptr holder for all mrnas here)
851    
852     int get_gseqid() { return gseq_id; }
853    
854     //--<
855     GSeqData(int gid=-1):mrnas_f(true,true,false),mrnas_r(true,true,false),
856     loci_f(true,true,true),loci_r(true,true,true),
857     gstats_f(true,true,false),gstats_r(true,true,false),
858     nloci_f(true,false,true), nloci_r(true,false,true),
859     umrnas(true,true,false), nloci_u(true,true,true), tdata(false,true,false) {
860     gseq_id=gid;
861     if (gseq_id>=0)
862     gseq_name=GffObj::names->gseqs.getName(gseq_id);
863     }
864     bool operator==(GSeqData& d){
865     return (gseq_id==d.gseq_id);
866     }
867     bool operator>(GSeqData& d){
868     return (gseq_id>d.gseq_id);
869     }
870     bool operator<(GSeqData& d){
871     return (gseq_id<d.gseq_id);
872     }
873     };
874    
875    
876     // a group of qry loci and a transcript cluster for a single qry dataset
877     class GQCluster : public GList<GffObj> {
878     public:
879     GffObj* mrna_maxcov; //transcript with maximum coverage (for largest transcript)
880     GffObj* mrna_maxscore; //transcript with maximum gscore ( = major isoform for Cufflinks)
881     uint start;
882     uint end;
883     GList<GLocus> qloci;
884     //GCluster cl; //just a more compact way of keeping all transcripts in these loci
885     GQCluster(GList<GLocus>* loci=NULL):GList<GffObj>(true,false,false),
886     qloci(true,false,false) {
887     mrna_maxcov=NULL;
888     mrna_maxscore=NULL;
889     start=0;
890     end=0;
891     if (loci!=NULL) {
892     qloci.Add(*loci);
893     for (int i=0;i<loci->Count();i++) {
894     addLocus(loci->Get(i),false);
895     }
896     }
897     }
898     void addLocus(GLocus* loc, bool toLoci=true) {
899     //check so we don't add locus duplicates
900     if (toLoci) {
901     for (int i=0;i<qloci.Count();i++) {
902     if (loc==qloci[i]) return;
903     }
904     qloci.Add(loc);
905     }
906     for (int m=0;m<loc->mrnas.Count();m++) {
907     GffObj* mrna=loc->mrnas[m];
908     Add(mrna);
909     if (start==0 || start>mrna->start) start=mrna->start;
910     if (end<mrna->end) end=mrna->end;
911     if (mrna_maxcov==NULL || mrna_maxcov->covlen<mrna->covlen) mrna_maxcov=mrna;
912     if (mrna_maxscore==NULL || mrna_maxscore->gscore<mrna->gscore) mrna_maxscore=mrna;
913     }
914     }
915     };
916    
917     //track a set of clustered qloci across multiple qry datasets
918     // the qloci in qcls[] overlap but not necessarily at exon level
919     // (so there can be multiple genes here in fact)
920     class GTrackLocus:public GSeg {
921     public:
922     char strand;
923     bool hasQloci;
924     //GLocus* rloc; //corresponding reference locus, if available
925     GList<GLocus> rloci; //ref loci found overlapping this region
926     GQCluster* qcls[MAX_QFILES]; //all qloci for this superlocus, grouped by dataset
927     GTrackLocus(GLocus* qloc=NULL, int q=-1):GSeg(0,0),rloci(true,false,true) {
928     strand='.';
929     for (int i=0;i<MAX_QFILES;i++) qcls[i]=NULL;
930     if (qloc!=NULL) addQLocus(qloc,q);
931     }
932    
933     void addRLocus(GLocus* rl) {
934     if (rl==NULL) return;
935     if (rl->qfidx>=0)
936     GError("Error: GTrackLocus::addRLocus called with a query locus (set# %d)\n",
937     rl->qfidx+1);
938     if (strand=='.') strand=rl->mrna_maxcov->strand;
939     if (start==0 || start>rl->start) start=rl->start;
940     if (end==0 || end<rl->end) end=rl->end;
941     rl->t_ptr=this;
942     rloci.Add(rl);
943     }
944    
945     void addQLocus(GLocus* loc, int q=-1) { //adding qry locus
946     if (loc==NULL) return;
947     if (strand=='.' && loc->mrna_maxcov->strand!='.')
948     strand=loc->mrna_maxcov->strand;
949     if (loc->qfidx<0 && q<0)
950     GError("Error at GTrackLocus::addQLocus(): locus.qfidx not set and index not given!\n");
951     if (q>=0) loc->qfidx=q;
952     else q=loc->qfidx;
953     if (start==0 || start>loc->start) start=loc->start;
954     if (end==0 || end<loc->end) end=loc->end;
955     if (qcls[q]==NULL) qcls[q]=new GQCluster();
956     hasQloci=true;
957     loc->t_ptr = this;
958     qcls[q]->addLocus(loc);
959     }
960    
961     bool add_Locus(GLocus* loc) {
962     if (start==0 || overlap(*loc)) { //simple range overlap, not exon overlap
963     if (loc->qfidx<0) addRLocus(loc);
964     else addQLocus(loc);
965     return true;
966     }
967     return false;
968     }
969    
970    
971     void addQCl(int q, GQCluster* qcl, GLocus* lnkloc) {
972     for (int i=0;i<qcl->qloci.Count();i++) {
973     GLocus* loc=qcl->qloci[i];
974     if (loc==lnkloc || loc->t_ptr==this) continue;
975     hasQloci=true;
976     loc->t_ptr=this;
977     qcls[q]->addLocus(loc);
978     }
979     }
980    
981     void addMerge(GTrackLocus* loctrack, int qcount, GLocus* lnkloc) {
982     if (loctrack==NULL) return;
983     //merge qloci
984     for (int q=0; q < qcount; q++) {
985     if (qcls[q]==NULL) {
986     if (loctrack->qcls[q]!=NULL) {
987     qcls[q]=loctrack->qcls[q];
988     loctrack->qcls[q]=NULL; //just move pointer here
989     //set all t_ptr pointers for moved loci
990     for (int ql = 0; ql < qcls[q]->qloci.Count(); ql++) {
991     qcls[q]->qloci[ql]->t_ptr=this;
992     }
993     hasQloci=true;
994     }
995     }
996     else //existing qloci at q
997     if (loctrack->qcls[q]!=NULL) { //merge elements
998     addQCl(q, loctrack->qcls[q], lnkloc);
999     }
1000     }//for each qset
1001     //merge rloci, if any
1002     if (loctrack->rloci.Count()>0) {
1003     for (int l=0;l<loctrack->rloci.Count();l++) {
1004     if (loctrack->rloci[l]!=lnkloc && loctrack->rloci[l]->t_ptr!=this) {
1005     rloci.Add(loctrack->rloci[l]);
1006     loctrack->rloci[l]->t_ptr=this;
1007     }
1008     }
1009     }
1010     if (loctrack->start<start) start=loctrack->start;
1011     if (loctrack->end>end) end=loctrack->end;
1012     if (strand=='.' && loctrack->strand!='.') strand=loctrack->strand;
1013     }
1014    
1015     /*
1016     void add_QLoci(GList<GLocus>* loci, int q, GLocus& r) {
1017     // only add loci overlapping given refloc
1018     //rloc=&r;
1019     if (loci==NULL) return;
1020     for (int i=0;i<loci->Count();i++) {
1021     GLocus* loc=loci->Get(i);
1022     // if (!loc->exonOverlap(r)) continue; //do we really needed exon overlap?
1023     if (!loc->overlap(r)) continue;
1024     if (start==0 || start>loc->start) start=loc->start;
1025     if (end==0 || end<loc->end) end=loc->end;
1026     loc->t_ptr=this;
1027     loc->qfidx=q;
1028     if (qcls[q]==NULL) qcls[q]=new GQCluster();
1029     qcls[q]->addLocus(loc);
1030     }
1031     strand=r.mrnas[0]->strand;
1032     }
1033     */
1034     ~GTrackLocus() {
1035     for (int q=0;q<MAX_QFILES;q++)
1036     if (qcls[q]!=NULL) { delete qcls[q]; qcls[q]=NULL; }
1037     }
1038    
1039     GQCluster* operator[](int q) {
1040     if (q<0 || q>=MAX_QFILES)
1041     GError("Error: qfidx index out of bounds (%d) for GTrackLocus!\n",q);
1042     return qcls[q];
1043     }
1044     };
1045    
1046     class GXConsensus:public GSeg {
1047     public:
1048     static int count;
1049     int id; //XConsensus ID
1050     int tss_id; //group id for those xconsensi with shared first exon
1051     int p_id; //group id for those xconsensi with "similar" protein
1052     GffObj* tcons; //longest transcript to represent the combined "consensus" structure
1053     GffObj* ref; //overlapping reference transcript
1054     char refcode; // the code for ref relationship (like in the tracking file)
1055     char* aa;
1056     int aalen;
1057     GXConsensus* contained; //if contained into another GXConsensus
1058     //list of ichain-matching query (cufflinks) transcripts that contributed to this consensus
1059     GList<GffObj> qchain;
1060     GXConsensus(GffObj* c, CEqList* qlst, GffObj* r=NULL, char rcode=0)
1061     :qchain(false,false,false) {
1062     ref=r;
1063     refcode=rcode;
1064     tcons=c;
1065     if (qlst!=NULL) qchain.Add(*((GList<GffObj>*)qlst));
1066     else qchain.Add(c);
1067     count++;
1068     tss_id=0;
1069     p_id=0;
1070     aalen=0;
1071     id=count;
1072     aa=NULL;
1073     start=tcons->start;
1074     end=tcons->end;
1075     contained=NULL;
1076     }
1077     ~GXConsensus() {
1078     if (aa!=NULL) GFREE(aa);
1079     }
1080     };
1081    
1082     class GXLocus:public GSeg {
1083     public:
1084     int id;
1085     int num_mtcons; //number of multi-exon "consensus" transcripts in this locus
1086     char strand;
1087     GList<GLocus> rloci; //list of ref loci overlapping any of the mexons
1088     GList<GLocus> qloci; //loci from all qry datasets that have overlapping exons with this region
1089     GArray<GSeg> mexons; //list of merged exonic regions for this locus
1090     GList<GXConsensus> tcons;
1091     GXLocus(GLocus* lfirst=NULL):GSeg(0,0),
1092     rloci((GCompareProc*)cmpByPtr, (GFreeProc*)NULL, true),
1093     qloci((GCompareProc*)cmpByPtr, (GFreeProc*)NULL, true),
1094     mexons(true,true), tcons(true,true,false) {
1095     strand='.';
1096     num_mtcons=0;
1097     if (lfirst!=NULL) {
1098     add_Locus(lfirst);
1099     }
1100     id=0;
1101     }
1102    
1103     bool add_Locus(GLocus* loc) {
1104     if (mexons.Count()>0 && (end<loc->start || start > loc->end))
1105     return false; //no chance for overlapping exons
1106     if (mexons.Count()==0) {
1107     mexons.Add(loc->mexons);
1108     start=loc->start;
1109     end=loc->end;
1110     if (loc->qfidx<0) rloci.Add(loc);
1111     else qloci.Add(loc);
1112     strand=loc->mrna_maxcov->strand;
1113     loc->xlocus=this;
1114     return true;
1115     }
1116     int f=0;
1117     if (loc->qfidx<0) {
1118     if (rloci.Found(loc,f)) return false;
1119     }
1120     else if (qloci.Found(loc,f)) return false;
1121    
1122     // -- merge mexons
1123     GArray<int> ovlexons(true,true); //list of locus.mexons indexes overlapping existing mexons
1124     int i=0; //index of first mexons with a merge
1125     int j=0; //index current mrna exon
1126     while (i<mexons.Count() && j<loc->mexons.Count()) {
1127     uint istart=mexons[i].start;
1128     uint iend=mexons[i].end;
1129     uint jstart=loc->mexons[j].start;
1130     uint jend=loc->mexons[j].end;
1131     if (iend<jstart) { i++; continue; }
1132     if (jend<istart) { j++; continue; }
1133     //if (mexons[i].overlap(jstart, jend)) {
1134     //exon overlap was found :
1135     ovlexons.Add(j);
1136     //extend mexons[i] as needed
1137     if (jstart<istart) mexons[i].start=jstart;
1138     if (jend>iend) { //mexons[i] end extend
1139     mexons[i].end=jend;
1140     //now this could overlap the next mexon(s), so we have to merge them all
1141     while (i<mexons.Count()-1 && mexons[i].end>mexons[i+1].start) {
1142     uint nextend=mexons[i+1].end;
1143     mexons.Delete(i+1);
1144     if (nextend>mexons[i].end) {
1145     mexons[i].end=nextend;
1146     break; //no need to check next mexons
1147     }
1148     } //while next mexons merge
1149     } // mexons[i] end extend
1150     // } //exon overlap
1151     j++; //check the next locus.mexon
1152     }//while mexons
1153     if (ovlexons.Count()==0) return false;
1154     if (strand=='.' && loc->mrna_maxcov->strand!='.')
1155     strand=loc->mrna_maxcov->strand;
1156     //have exon overlap:
1157     //-- add the rest of the non-overlapping mexons:
1158     GSeg seg;
1159     for (int i=0;i<loc->mexons.Count();i++) {
1160     seg.start=loc->mexons[i].start;
1161     seg.end=loc->mexons[i].end;
1162     if (!ovlexons.Exists(i)) mexons.Add(seg);
1163     }
1164     // -- adjust start/end as needed
1165     if (start>loc->start) start=loc->start;
1166     if (end<loc->end) end=loc->end;
1167     loc->xlocus=this;
1168     if (loc->qfidx<0) rloci.Add(loc);
1169     else qloci.Add(loc);
1170     return true;
1171     }
1172    
1173     void addMerge(GXLocus& oxloc) {
1174     GArray<int> ovlexons(true,true); //list of oxloc.mexons indexes overlapping existing mexons
1175     int i=0; //index of first mexons with a merge
1176     int j=0; //index current mrna exon
1177     while (i<mexons.Count() && j<oxloc.mexons.Count()) {
1178     uint istart=mexons[i].start;
1179     uint iend=mexons[i].end;
1180     uint jstart=oxloc.mexons[j].start;
1181     uint jend=oxloc.mexons[j].end;
1182     if (iend<jstart) { i++; continue; }
1183     if (jend<istart) { j++; continue; }
1184     //if (mexons[i].overlap(jstart, jend)) {
1185     //exon overlap was found :
1186     ovlexons.Add(j);
1187     //extend mexons[i] as needed
1188     if (jstart<istart) mexons[i].start=jstart;
1189     if (jend>iend) { //mexons[i] end extend
1190     mexons[i].end=jend;
1191     //now this could overlap the next mexon(s), so we have to merge them all
1192     while (i<mexons.Count()-1 && mexons[i].end>mexons[i+1].start) {
1193     uint nextend=mexons[i+1].end;
1194     mexons.Delete(i+1);
1195     if (nextend>mexons[i].end) {
1196     mexons[i].end=nextend;
1197     break; //no need to check next mexons
1198     }
1199     } //while next mexons merge
1200     } // mexons[i] end extend
1201     // } //exon overlap
1202     j++; //check the next oxloc.mexon
1203     }
1204     if (ovlexons.Count()==0) {
1205     GError("Error: attempt to merge GXLoci with non-overlapping exons!\n");
1206     }
1207     //-- add the rest of the non-overlapping mexons:
1208     GSeg seg;
1209     for (int i=0;i<oxloc.mexons.Count();i++) {
1210     seg.start=oxloc.mexons[i].start;
1211     seg.end=oxloc.mexons[i].end;
1212     if (!ovlexons.Exists(i)) mexons.Add(seg);
1213     }
1214     if (start>oxloc.start) start=oxloc.start;
1215     if (end<oxloc.end) end=oxloc.end;
1216     if (strand=='.') strand=oxloc.strand;
1217     //-- steal all qloci and rloci
1218     for (int i=0;i<oxloc.qloci.Count();i++) {
1219     if (oxloc.qloci[i]->xlocus==this) continue;
1220     qloci.Add(oxloc.qloci[i]);
1221     oxloc.qloci[i]->xlocus=this;
1222     }
1223     for (int i=0;i<oxloc.rloci.Count();i++) {
1224     if (oxloc.rloci[i]->xlocus==this) continue;
1225     rloci.Add(oxloc.rloci[i]);
1226     oxloc.rloci[i]->xlocus=this;
1227     }
1228     } //::addMerge()
1229    
1230    
1231     void checkContainment() {
1232     //checking containment
1233     for (int j=0;j<tcons.Count()-1;j++) {
1234     GXConsensus* t=tcons[j];
1235     for (int i=j+1;i<tcons.Count();i++) {
1236     if (tcons[i]->contained!=NULL && t->tcons->exons.Count()>1) continue; //will check the container later anyway
1237     int c_status=checkXConsContain(t->tcons, tcons[i]->tcons);
1238     if (c_status==0) continue; //no containment relationship between t and tcons[i]
1239     if (c_status>0) { //t is a container for tcons[i]
1240     tcons[i]->contained=t;
1241     }
1242     else { //contained into exising XCons
1243     t->contained=tcons[i];
1244     break;
1245     }
1246     }
1247     }
1248     }
1249    
1250     int checkXConsContain(GffObj* a, GffObj* b) {
1251     // returns 1 if a is the container of b
1252     // -1 if a is contained in b
1253     // 0 if no
1254     if (a->end<b->start || b->end<a->start) return 0;
1255     if (a->exons.Count()==b->exons.Count()) {
1256     if (a->exons.Count()>1) return 0; //same number of exons - no containment possible
1257     //because equivalence was already tested
1258     else { //single exon containment testing
1259     //this is fuzzy and messy (end result may vary depending on the testing order)
1260     int ovlen=a->exons[0]->overlapLen(b->exons[0]);
1261     int minlen=GMIN(a->covlen, b->covlen);
1262     if (ovlen>=minlen*0.8) { //if at least 80% of the shorter one is covered, it is contained
1263     return ((a->covlen>b->covlen) ? 1 : -1);
1264     }
1265     else return 0;
1266     //if (a->start<=b->start+10 && a->end+10>=b->end) return 1;
1267     // else { if (b->start<=a->start+10 && b->end+10>=a->end) return -1;
1268     // else return 0;
1269     //}
1270     }
1271     }
1272     //different number of exons:
1273     if (a->exons.Count()>b->exons.Count()) return t_contains(*a, *b) ? 1:0;
1274     else return t_contains(*b, *a) ? -1 : 0;
1275     }
1276    
1277     void addXCons(GXConsensus* t) {
1278     tcons.Add(t);
1279     }
1280    
1281     }; //GXLocus
1282    
1283    
1284    
1285     int parse_mRNAs(GfList& mrnas,
1286     GList<GSeqData>& glstdata,
1287     bool is_ref_set=true,
1288     bool check_for_dups=false,
1289     int qfidx=-1, bool only_multiexon=false);
1290    
1291     //reading a mRNAs from a gff file and grouping them into loci
1292     void read_mRNAs(FILE* f, GList<GSeqData>& seqdata, GList<GSeqData>* ref_data=NULL,
1293     bool check_for_dups=false, int qfidx=-1, const char* fname=NULL,
1294     bool only_multiexon=false);
1295    
1296     void read_transcripts(FILE* f, GList<GSeqData>& seqdata, bool keepAttrs=true);
1297     void sort_GSeqs_byName(GList<GSeqData>& seqdata);
1298    
1299    
1300 gpertea 54 bool tMatch(GffObj& a, GffObj& b, int& ovlen, bool fuzzunspl=false, bool contain_only=false);
1301 gpertea 20
1302     //use qsearch to "position" a given coordinate x within a list of transcripts sorted
1303     //by their start (lowest) coordinate; the returned int is the list index of the
1304     //closest GffObj starting just *ABOVE* coordinate x
1305     //Convention: returns -1 if there is no such GffObj (i.e. last GffObj start <= x)
1306     int qsearch_mrnas(uint x, GList<GffObj>& mrnas);
1307     int qsearch_loci(uint x, GList<GLocus>& segs); // same as above, but for GSeg lists
1308    
1309     GSeqData* getRefData(int gid, GList<GSeqData>& ref_data); //returns reference GSeqData for a specific genomic sequence
1310    
1311     #endif

Properties

Name Value
svn:executable *