1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
//#include "tablemaker.h"
#include "rlink.h"
#include <numeric>
extern GStr ballgown_dir;
int rc_cov_inc(int i) {
return ++i;
}
void BundleData::keepGuide(GffObj* t, GPVec<RC_TData>* rc_tdata,
GPVec<RC_Feature>* rc_edata, GPVec<RC_Feature>* rc_idata) {
if (rc_data==NULL) {
rc_init(t, rc_tdata, rc_edata, rc_idata);
}
keepguides.Add(t);
t->udata=(int)rc_data->addTranscript(*t);
}
struct COvlSorter {
bool operator() (pair<int, const RC_Feature*> i,
pair<int, const RC_Feature*> j) {
return (i.first>j.first); //sort in decreasing order of overlap length
}
} OvlSorter;
void rc_updateExonCounts(const RC_ExonOvl& exonovl, int nh) {
//this only gets read overlaps > 5bp and otherwise filtered in evalReadAln()
exonovl.feature->rcount++;
if (nh>1) {
exonovl.feature->mrcount += (1.0/nh);
exonovl.feature->movlcount += ((double)exonovl.ovlen/nh);
}
else { // nh<=1
exonovl.feature->mrcount++;
exonovl.feature->movlcount += exonovl.ovlen;
exonovl.feature->ucount++;
}
}
bool BundleData::evalReadAln(GReadAlnData& alndata, char& xstrand) {
//GSamRecord& brec, char& strand, int nh) {
if (rc_data==NULL) {
return false; //no ref transcripts available for this reads' region
}
GSamRecord& brec=*(alndata.brec);
int mate_pos=brec.mate_start();
int nh=alndata.nh;
if ((int)brec.end<rc_data->lmin || (int)brec.start>rc_data->rmax) {
return false; //hit outside coverage area
}
if (rc_data->g_exons.Count()==0 || rc_data->g_tdata.Count()==0)
return false; //nothing to do without transcripts
//check this read alignment against ref exons and introns
char strandbits=0;
bool result=false;
bool is_in_guide=true; // exons and junctions are in reference transcripts but they might be in different guides
for (int i=0;i<brec.exons.Count();i++) {
if (ballgown)
rc_data->updateCov(xstrand, nh, brec.exons[i].start, brec.exons[i].len());
GArray<RC_ExonOvl> exonOverlaps(true, true); //overlaps sorted by decreasing length
if (rc_data->findOvlExons(exonOverlaps, brec.exons[i].start,
brec.exons[i].end, xstrand, mate_pos)) {
result=true;
int max_ovl=exonOverlaps[0].ovlen;
if(is_in_guide && (uint)max_ovl<brec.exons[i].len()) is_in_guide=false;
//alndata.g_exonovls.Add(new GVec<RC_ExonOvl>(exonOverlaps));
for (int k=0;k<exonOverlaps.Count();++k) {
//if (exonOverlaps[k].ovlen < 5) break; //ignore very short overlaps
if (k && (exonOverlaps[k].mate_ovl < exonOverlaps[0].mate_ovl
|| exonOverlaps[k].ovlen+5<max_ovl) )
break; //ignore further overlaps after a mate matched or if they are shorter than max_overlap-5
if (exonOverlaps[k].feature->strand=='+') strandbits |= 0x01;
else if (exonOverlaps[k].feature->strand=='-') strandbits |= 0x02;
//TODO: perhaps we could use a better approach for non-overlapping ref exons
// spanned by this same read alignment
//counting this overlap for multiple exons if it's similarly large
//(in the shared region of overlapping exons)
rc_updateExonCounts(exonOverlaps[k], nh);
}
} //ref exon overlaps
if (i>0) { //intron processing
int j_l=brec.exons[i-1].end+1;
int j_r=brec.exons[i].start-1;
RC_Feature* ri=rc_data->findIntron(j_l, j_r, xstrand);
alndata.juncs.Add(new CJunction(j_l, j_r)); //don't set strand, etc. for now
if (ri) { //update guide intron counts
ri->rcount++;
ri->mrcount += (nh > 1) ? (1.0/nh) : 1;
if (nh==1) ri->ucount++;
alndata.juncs.Last()->guide_match=1;
}
else is_in_guide=false;
} //intron processing
}
if (xstrand=='.' && strandbits && strandbits<3) {
xstrand = (strandbits==1) ? '+' : '-';
}
if(!mergeMode && is_in_guide) alndata.in_guide=true;
return result;
}
FILE* rc_fwopen(const char* fname) {
if (strcmp(fname,"-")==0) return stdout;
GStr fpath(ballgown_dir); //always ends with '/'
//fpath += '.';
fpath += fname;
//fpath += "/";
//fpath += fname;
fpath += ".ctab";
FILE* fh=fopen(fpath.chars(), "w");
if (fh==NULL) {
fprintf(stderr, "Error: cannot create file %s\n",
fpath.chars());
exit(1);
}
return fh;
}
FILE* rc_frenopen(const char* fname) {
//if (strcmp(fname,"-")==0) return stdout;
GStr fpath(ballgown_dir);
//fpath += '.';
fpath += fname;
//fpath += "/";
//fpath += fname;
fpath += ".ctab";
GStr fren(fpath);
fren += ".tmp";
//rename fpath to fren and open fren for reading
if (rename(fpath.chars(), fren.chars())!=0) {
GError("Error: cannot rename %s to %s!\n", fpath.chars(), fren.chars());
}
FILE* fh=fopen(fren.chars(), "r");
if (fh==NULL) {
GError("Error: cannot open file %s\n", fren.chars());
}
return fh;
}
void rc_frendel(const char* fname) {
GStr fpath(ballgown_dir);
//fpath += '.';
fpath += fname;
fpath += ".ctab";
fpath += ".tmp";
if (remove(fpath.chars())!=0) {
GMessage("Warning: could not remove file %s!\n",fpath.chars());
}
}
void rc_write_f2t(FILE* fh, map<uint, set<uint> >& f2t) {
for (map<uint, set<uint> >::iterator m=f2t.begin(); m!=f2t.end(); ++m) {
uint f_id=(*m).first;
set<uint>& tset = (*m).second;
for (set<uint>::iterator it=tset.begin();it!=tset.end();++it) {
uint t_id = *it;
fprintf(fh, "%u\t%u\n", f_id, t_id);
}
}
fflush(fh);
}
void rc_update_exons(RC_BundleData& rc) {
//update stdev etc. for all exons in bundle
for (int f=0;f<rc.g_exons.Count(); ++f) {
RC_Feature& exon = *(rc.g_exons[f]);
//assert( exon.l >= rc.lmin );
int L=exon.l-rc.lmin;
int xlen=exon.r-exon.l+1;
if (exon.l < rc.lmin) {
//shouldn't be here, bundle read-counting boundaries should be based on exons!
if (exon.r<rc.lmin) continue;
xlen-=(rc.lmin-exon.l+1);
L=0;
}
if (rc.rmax<exon.r) {
if (exon.l>rc.rmax) continue; //should never happen
xlen-=(exon.r-rc.rmax+1);
}
int R=L+xlen;
vector<int>::iterator xcov_begin;
vector<int>::iterator xcov_end;
vector<float>::iterator xmcov_begin;
vector<float>::iterator xmcov_end;
if (exon.strand=='+' || exon.strand=='.') {
xcov_begin = rc.f_cov.begin()+L;
xcov_end = rc.f_cov.begin()+R;
xmcov_begin = rc.f_mcov.begin()+L;
xmcov_end = rc.f_mcov.begin()+R;
} else {
xcov_begin = rc.r_cov.begin()+L;
xcov_end = rc.r_cov.begin()+R;
xmcov_begin = rc.r_mcov.begin()+L;
xmcov_end = rc.r_mcov.begin()+R;
}
double avg = (double)accumulate(xcov_begin, xcov_end, 0) / xlen;
vector<double> diff(xlen);
transform(xcov_begin, xcov_end, diff.begin(),
bind2nd( minus<double>(), avg));
double sq_sum = inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
double stdev = sqrt(sq_sum / xlen);
double mavg = (double)accumulate(xmcov_begin, xmcov_end, 0) / xlen;
vector<double> mdiff(xlen);
transform(xmcov_begin, xmcov_end, mdiff.begin(),
bind2nd( minus<double>(), mavg));
sq_sum = inner_product(mdiff.begin(), mdiff.end(), mdiff.begin(), 0.0);
double mstdev = sqrt(sq_sum / xlen);
exon.avg=avg;
exon.stdev=stdev;
exon.mavg=mavg;
exon.mstdev=mstdev;
} //for each exon in bundle
}
void rc_write_RCfeature( GPVec<RC_TData>& rcdata, GPVec<RC_Feature>& features, FILE*& fdata, FILE*& f2t,
bool is_exon=false) {
for (int i=0;i<features.Count();++i) {
RC_Feature& f=*(features[i]);
const char* ref_name=rcdata[f.t_ids[0]-1]->ref_t->getGSeqName();
if (is_exon) {
fprintf(fdata, "%u\t%s\t%c\t%d\t%d\t%d\t%d\t%.2f\t%.4f\t%.4f\t%.4f\t%.4f\n",
f.id, ref_name, f.strand, f.l, f.r, f.rcount,
f.ucount, f.mrcount, f.avg, f.stdev, f.mavg, f.mstdev);
}
else { //introns
fprintf(fdata,"%u\t%s\t%c\t%d\t%d\t%d\t%d\t%.2f\n",f.id, ref_name,
f.strand, f.l, f.r, f.rcount, f.ucount, f.mrcount);
}
// f2t -------
for (int t=0;t<f.t_ids.Count();++t)
fprintf(f2t, "%u\t%u\n", f.id, f.t_ids[t]);
} //for each feature
fclose(fdata);
fclose(f2t);
}
/*void rc_write_counts(const char* refname, BundleData& bundle) {
RC_BundleData& rc = *bundle.rc_data;
//if (rc.exons.size()==0) return;
*
*/
void rc_writeRC(GPVec<RC_TData>& RC_data,
GPVec<RC_Feature>& RC_exons,
GPVec<RC_Feature>& RC_introns,
FILE* &f_tdata, FILE* &f_edata, FILE* &f_idata,
FILE* &f_e2t, FILE* &f_i2t) {
for (int t=0;t<RC_data.Count();++t) {
//File: t_data.ctab
//t_id tname chr strand start end num_exons gene_id gene_name cufflinks_cov cufflinks_fpkm
RC_TData& sd=*RC_data[t];
const char* refname = sd.ref_t->getGSeqName();
const char* genename= sd.ref_t->getGeneName();
const char* geneID= sd.ref_t->getGeneID();
if (genename==NULL) genename=".";
if (geneID==NULL) geneID=".";
fprintf(f_tdata, "%u\t%s\t%c\t%d\t%d\t%s\t%d\t%d\t%s\t%s\t%f\t%f\n",
sd.t_id, refname, sd.ref_t->strand, sd.l, sd.r, sd.ref_t->getID(),
sd.t_exons.Count(), sd.eff_len, geneID,
genename, sd.cov, sd.fpkm);
}//for each transcript
//fflush(f_tdata);
fclose(f_tdata);
//File: e_data.ctab
//e_id chr gstart gend rcount ucount mrcount
rc_write_RCfeature(RC_data, RC_exons, f_edata, f_e2t, true);
//File: i_data.ctab
//i_id chr gstart gend rcount ucount mrcount
rc_write_RCfeature(RC_data, RC_introns, f_idata, f_i2t);
}
void RC_TData::rc_addFeatures(uint& c_e_id, GList<RC_Feature>& exonSet, GPVec<RC_Feature>& exonTable,
uint& c_i_id, GList<RC_Feature>& intronSet, GPVec<RC_Feature>& intronTable) {
GASSERT(ref_t);
GffObj& m = *(ref_t);
int ecache_idx = exonSet.Count()-1;
int icache_idx = intronSet.Count()-1;
//int ecache_idx = e_idx_cache>=0 ? e_idx_cache : exonSet.Count()-1;
//int icache_idx = i_idx_cache>=0 ? i_idx_cache : intronSet.Count()-1;
for (int i = 0; i < m.exons.Count(); ++i) {
addFeature((int)m.exons[i]->start, (int)m.exons[i]->end, t_exons, c_e_id, exonSet, exonTable, ecache_idx);
//if (i==0) e_idx_cache=ecache_idx;
if (i>0) { //store intron
//if (i==1) i_idx_cache=icache_idx;
addFeature(m.exons[i-1]->end+1, m.exons[i]->start-1, t_introns, c_i_id,
intronSet, intronTable, icache_idx);
} //for each intron
} //for each exon
}
void RC_TData::addFeature(int fl, int fr, GPVec<RC_Feature>& fvec,
uint& f_id, GList<RC_Feature>& fset,
GPVec<RC_Feature>& fdata, int& cache_idx) {
//f_id is the largest f_id inserted so far in fset
bool add_new = true;
RC_Feature* newseg=new RC_Feature(fl, fr, ref_t->strand, 0, this->t_id);
//RC_Feature* newfeature=NULL;
int fit=cache_idx<0 ? fset.Count()-1 : cache_idx;
int fp_id=-1;
if (fset.Count()>0) {
if (*newseg < *(fset[fit])) {
bool eq=false;
while (*newseg < *(fset[fit]) || (eq = (*newseg==*(fset[fit])))) {
if (eq) {
add_new = false;
fp_id = fset[fit]->id; //fset[fit]->id;
break;
}
//newseg< fset[fit]
--fit;
if (fit<0) break; //newseg should be inserted at 0
} //while newseg<fset[fit]
if (add_new) ++fit;
// newseg < fset[fit+1]
//we'll insert newseg at position fit+1
}
else { //newseg >= *fset[fit]
bool eq=false;
while (*(fset[fit]) < *newseg || (eq = (*newseg==*(fset[fit])))) {
if (eq) {
add_new = false;
fp_id = fset[fit]->id;
break;
}
++fit;
if (fit==fset.Count()) {
//newseg should be appended to the list
break;
}
}
//if (fit<=fset.Count() && !add_new) {
//fset[fit-1] < newseg < fset[fit]
//we'll insert newseg at position fit
//}
}
} //check existing set
if (add_new) { //did not see this feature before
newseg->id = ++f_id;
if (fit<0) fit = fset.Add(newseg);
else fset.sortInsert(fit, newseg);
if (fit<0) {
GError("Error: feature %d-%d (%c) already in feature set!\n",
newseg->l, newseg->r, newseg->strand);
}
cache_idx=fit;
fp_id=fdata.Add(newseg)+1;
#ifdef DEBUG
if (fdata.Count()!=(int)f_id) {
GMessage("Error: fdata.Count=%d, f_id=%d\n", fdata.Count(), f_id);
}
#endif
GASSERT((uint)fdata.Count()==f_id);
}
else { //feature seen before, update its parent list
fdata[fp_id-1]->t_ids.Add(this->t_id);
delete newseg;
}
//fvec.push_back(newseg);
GASSERT(fdata[fp_id-1]->id==(uint)fp_id);
fvec.Add(fdata[fp_id-1]);
}
void Ballgown_setupFiles(FILE* &f_tdata, FILE* &f_edata, FILE* &f_idata,
FILE* &f_e2t, FILE* &f_i2t) {
if (f_tdata == NULL) {
//first call, create the files
f_tdata = rc_fwopen("t_data");
fprintf(f_tdata, "t_id\tchr\tstrand\tstart\tend\tt_name\tnum_exons\tlength\tgene_id\tgene_name\tcov\tFPKM\n");
f_edata = rc_fwopen("e_data");
fprintf(f_edata, "e_id\tchr\tstrand\tstart\tend\trcount\tucount\tmrcount\tcov\tcov_sd\tmcov\tmcov_sd\n");
f_idata = rc_fwopen("i_data");
fprintf(f_idata, "i_id\tchr\tstrand\tstart\tend\trcount\tucount\tmrcount\n");
f_e2t = rc_fwopen("e2t");
fprintf(f_e2t, "e_id\tt_id\n");
f_i2t = rc_fwopen("i2t");
fprintf(f_i2t, "i_id\tt_id\n");
}
}
|