ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/cuffcompare/gtf_tracking.h
Revision: 155
Committed: Wed Jan 25 21:06:46 2012 UTC (7 years, 6 months ago) by gpertea
File size: 45392 byte(s)
Log Message:
fix for a subtle XLocus building bug

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

Properties

Name Value
svn:executable *