Exemple #1
0
tdt_result_t* tdt_result_new(char *chromosome, int chromosome_len, unsigned long int position, char *reference, int reference_len, 
                             char *alternate, int alternate_len, double t1, double t2, double chi_square) {
    tdt_result_t *result = (tdt_result_t*) malloc (sizeof(tdt_result_t));
    
    result->chromosome = strndup(chromosome, chromosome_len);
    result->position = position;
    result->reference = strndup(reference, reference_len);
    result->alternate = strndup(alternate, alternate_len);
    result->t1 = t1;
    result->t2 = t2;
    result->odds_ratio = (t2 == 0.0) ? NAN : ((double) t1/t2);
    result->chi_square = chi_square;
    result->p_value = 1 - gsl_cdf_chisq_P(chi_square, 1);
    
    return result;
}
assoc_basic_result_t* assoc_basic_result_new(char *chromosome, int chromosome_len, unsigned long int position, 
                                             char *reference, int reference_len, char *alternate, int alternate_len, 
                                             int affected1, int affected2, int unaffected1, int unaffected2, double chi_square) {
    assoc_basic_result_t *result = (assoc_basic_result_t*) malloc (sizeof(assoc_basic_result_t));
    
    result->chromosome = strndup(chromosome, chromosome_len);
    result->position = position;
    result->reference = strndup(reference, reference_len);
    result->alternate = strndup(alternate, alternate_len);
    result->affected1 = affected1;
    result->affected2 = affected2;
    result->unaffected1 = unaffected1;
    result->unaffected2 = unaffected2;
    result->odds_ratio = (affected2 == 0 || unaffected1 == 0) ? NAN : 
                         ((double) affected1 / affected2) * ((double) unaffected2 / unaffected1);
    result->chi_square = chi_square;
    result->p_value = 1 - gsl_cdf_chisq_P(chi_square, 1);
    
    return result;
}
string GraphState::to_s ( unsigned int frequency ) {
    float p, sig;
    if (fm::chisq->active) {
        if (!fm::regression) {
            p = fm::chisq->p;
            sig = fm::chisq->sig;
        }
        else {
            p = fm::ks->p;
            sig = fm::ks->sig;
        }
    }

    if (!fm::chisq->active || p >= sig) {

        string oss;

        if (fm::gsp_out) {
            to_s(oss);
            return oss;
        }

        else {
            if (fm::do_yaml) oss.append ("- [ ");

            // output smarts
            if (fm::do_yaml) oss.append ("\"");
            int i;
            for ( i = nodes.size()-1; i >= 0; i-- ) {   // edges
                if (nodes[i].edges.size()==1) break;
            }
            DfsOut(i, oss, i);
            if (fm::do_yaml) oss.append ("\", ");
            else oss.append("\t");

            // output chisq
            if (fm::chisq->active) {
                if (fm::do_yaml) {
                    if (!fm::pvalues || fm::regression) {
                        char x[20];
                        sprintf(x,"%.4f", p);
                        (oss.append(x)).append(", ");
                    }
                    else {
                        char x[20];
                        sprintf(x,"%.4f", gsl_cdf_chisq_P(p, 1));
                        (oss.append(x)).append(", ");
                    }
                }

            }

            // output freq
            if (fm::chisq->active) {
                if (!fm::regression && frequency != (fm::chisq->fa+fm::chisq->fi)) {
                    cerr << "Notice: Wrong counts for frequency " << frequency << " [!=" << fm::chisq->fa << "(fa)+" << fm::chisq->fi << "(fi)]." << endl;
                }
            }
            else {
                char x[20];
                sprintf(x,"%i", frequency);
                oss.append(x);
            }

            // output occurrences
            if (fm::chisq->active) {

                set<Tid> fa_set, fi_set;
                if (!fm::regression) {
                    fa_set = fm::chisq->fa_set;
                    fi_set = fm::chisq->fi_set;
                }
                else {
                    fa_set = fm::ks->fa_set;
                    fi_set = fm::ks->fi_set;
                }

                oss.append ("[");

                set<Tid>::iterator iter;
                char x[20];

                if (fm::do_yaml) {
                    set<Tid>::iterator begin = fa_set.begin();
                    set<Tid>::iterator end = fa_set.end();
                    set<Tid>::iterator last = end;
                    if (fa_set.size()) last = --(fa_set.end());

                    for (iter = begin; iter != end; iter++) {
                        if (iter != begin) oss.append (",");
                        oss.append (" ");
                        sprintf(x,"%i", (*iter));
                        oss.append (x);
                        if ((last != end) && (iter == last)) oss.append (" ");
                    }
                    if (!fm::regression) oss.append ("], [");

                    begin = fi_set.begin();
                    end = fi_set.end();
                    last = end;
                    if (fi_set.size()) last = --(fi_set.end());

                    for (iter = begin; iter != end; iter++) {
                        if (iter != begin) oss.append (",");
                        oss.append (" ");
                        sprintf(x,"%i", (*iter));
                        oss.append (x);
                        if ((last != end) && (iter == last)) oss.append (" ");
                    }
                }

                if (!fm::do_yaml) {
                    set<Tid> ids;
                    ids.insert(fa_set.begin(), fa_set.end());
                    ids.insert(fi_set.begin(), fi_set.end());
                    for (iter = ids.begin(); iter != ids.end(); iter++) {
                        sprintf(x,"%i", (*iter));
                        (oss.append (" ")).append(x);
                    }
                }
                if (!fm::do_yaml) oss.append (" ]");
                else oss.append ("]");
            }

            if (fm::do_yaml) oss.append (" ]");

            fm::console_out ? oss.append ("\n") : oss.append ("");


            return oss;
        }

    }
    else return "";
}
void GraphState::print ( unsigned int frequency ) {
    float p, sig;
    if (fm::chisq->active) {
        if (!fm::regression) {
            p = fm::chisq->p;
            sig = fm::chisq->sig;
        }
        else {
            p = fm::ks->p;
            sig = fm::ks->sig;
        }
    }
    if (!fm::chisq->active || p >= sig) {
        if (fm::gsp_out) {
            print(stdout);
        }
        else {
            if (fm::do_yaml) {
                putchar('-');
                putchar(' ');
                putchar('[');
                putchar(' ');
                putchar('"');
            }
            // output smarts
            int i;
            for ( i = nodes.size()-1; i >= 0; i-- ) {   // edges
                if (nodes[i].edges.size()==1) break;
            }
            DfsOut(i, i);
            if (fm::do_yaml) {
                putchar('"');
                putchar(',');
                putchar(' ');
            }
            // output chisq
            if (fm::chisq->active) {
                if (fm::do_yaml) {
                    if (!fm::pvalues || fm::regression) printf("%.4f, ", p);
                    else printf("%.4f, ", gsl_cdf_chisq_P(p,1));
                }
                else putchar('\t');
            }
            // output freq
            if (fm::chisq->active) {
                if (!fm::regression && frequency != (fm::chisq->fa+fm::chisq->fi)) {
                    cerr << "Error: wrong counts! " << frequency << "!=" << fm::chisq->fa + fm::chisq->fi << "(" << fm::chisq->fa << "+" << fm::chisq->fi << ")" << endl;
                }
            }
            else {
                if (fm::do_yaml) {
                    printf("%i", frequency);
                }
                else {
                    printf("%i\t", frequency);
                };
            }
            // output occurrences
            if (fm::chisq->active) {

                set<Tid> fa_set, fi_set;
                if (!fm::regression) {
                    fa_set = fm::chisq->fa_set;
                    fi_set = fm::chisq->fi_set;
                }
                else {
                    fa_set = fm::ks->fa_set;
                    fi_set = fm::ks->fi_set;
                }

                putchar ('[');
                set<Tid>::iterator iter;
                if (fm::do_yaml) {
                    for (iter = fa_set.begin(); iter != fa_set.end(); iter++) {
                        if (iter != fa_set.begin()) putchar (',');
                        putchar (' ');
                        printf("%i", (*iter));
                    }
                    if (!fm::regression) {
                        putchar (']');
                        putchar (',');
                        putchar (' ');
                        putchar ('[');
                    }
                }
                if (fm::do_yaml) {
                    for (iter = fi_set.begin(); iter != fi_set.end(); iter++) {
                        if (iter != fi_set.begin()) putchar (',');
                        printf(" %i", (*iter));
                    }
                }
                if (!fm::do_yaml) {
                    set<Tid> ids;
                    ids.insert(fa_set.begin(), fa_set.end());
                    ids.insert(fi_set.begin(), fi_set.end());
                    for (iter = ids.begin(); iter != ids.end(); iter++) {
                        putchar(' ');
                        printf("%i", (*iter));
                    }
                }
                putchar(' ');
                putchar(']');
            }
            if (fm::do_yaml) {
                putchar(' ');
                putchar(']');
            }
            if(fm::console_out) putchar('\n');
        }
    }
}
void postprocessing()
{
long int i,num;
int j,k,l,s,isNew;
double eva,p_value;
int* loci;
loci=new int[iEpiModel];
vector<double> record;
record.resize(iEpiModel+2);
cout<<"Post-processing"<<endl;
cout<<"Dealing with top ranking SNP sets"<<endl;
num=comb_num(iLociModel,iEpiModel);
cout<<"Number of interatcions evaluated: "<<num*iTopModel<<endl;
int** combination;
combination=new int*[num];
for(i=0;i<num;i++)
combination[i]=new int[iEpiModel];
combination=throughout(iLociModel,iEpiModel,combination);
for(i=iTopModel-1;i>=0;i--)
{
for(j=0;j<num;j++)
{
for(k=0;k<iEpiModel;k++)
{
loci[k]=loci_TopModel[i][combination[j][k]];
}
eva=chi_square(loci,iEpiModel);
p_value=1-gsl_cdf_chisq_P(eva,pow(3,iEpiModel)-1);
if(p_value<pvalue)
{
isNew=1;
for(l=0;l<interactions.size();l++)
{
if(fabs(eva-interactions[l][iEpiModel])<0.00000001)
{
isNew=0;
break;
}
}
if(isNew)
{
for(s=0;s<iEpiModel;s++)
{
record[s]=(double)loci[s];
}
record[iEpiModel]=eva;
record[iEpiModel+1]=p_value;
interactions.push_back(record);
}
}
}
}
for(i=0;i<num;i++)
delete []combination[i];
delete []combination;
cout<<"Dealing with top ranking loci"<<endl;
num=comb_num(iTopLoci,iEpiModel);
//cout<<"hello"<<endl;
int** combination1;
combination1=new int*[num];
for(i=0;i<num;i++)
combination1[i]=new int[iEpiModel];
combination1=throughout(iTopLoci,iEpiModel,combination1);
cout<<"Number of interactions evaluated: "<<num<<endl;
for(i=0;i<num;i++)
{
for(j=0;j<iEpiModel;j++)
{
loci[j]=loci_TopLoci[combination1[i][j]];
//cout<<loci[j]<<" ";
}
//cout<<endl;
eva=chi_square(loci,iEpiModel); 
p_value=1-gsl_cdf_chisq_P(eva,pow(3,iEpiModel)-1); 
if(p_value<pvalue)
{
isNew=1;
for(l=0;l<interactions.size();l++)
{
if(fabs(eva-interactions[l][iEpiModel])<0.00000001)
{
isNew=0;
break;
}
}
if(isNew)
{
for(s=0;s<iEpiModel;s++)
{
record[s]=(double)loci[s];  
}
record[iEpiModel]=eva; 
record[iEpiModel+1]=p_value;  
interactions.push_back(record);
}
}
}
for(i=0;i<num;i++)
delete []combination1[i];
delete []combination1;
}
double pp_model::calculate_waiting_times_log_predictive_df( double increment, bool lower_tail, bool two_sided, bool increment_parameters ){
  double sum_log_pvals = 0;// sum_log_pvals2 = 0;
  unsigned long long int how_many = 0;
  unsigned long long int i2 = m_data_cont ? m_data_cont->find_data_index(m_current_t+increment,0,m_current_data_index) : (unsigned long long int)(m_current_t+increment);
  double current_t = m_current_t;
  if(i2>m_current_data_index){
    while(i2>m_current_data_index){
      double t = m_pp_time_scale ? m_pp_time_scale->cumulative_function( current_t, (*m_data_cont)[0][m_current_data_index] ) : (*m_data_cont)[0][m_current_data_index] - current_t;
      m_log_predictive_df = calculate_log_posterior_predictive_pdf(t,0);//upper tail
      if(t<=0){
	cerr << "Rounding errors in event times"<< endl;
	exit(1);
      }
      if(lower_tail||two_sided){
	m_log_predictive_df2 = log(1-exp(m_log_predictive_df));//lower tail
	if(lower_tail)
	  m_log_predictive_df = m_log_predictive_df2;
      }
      /*      if(two_sided){//two-sided p-values version two - combine at the start
	if(m_log_predictive_df > m_log_predictive_df2)
	  m_log_predictive_df = m_log_predictive_df2;
	  m_log_predictive_df += LOG_TWO;
      }*/
      sum_log_pvals += m_log_predictive_df;
      //      if(two_sided)//two-sided p-values version one - combine at the end
      //	sum_log_pvals2 += m_log_predictive_df2;
      m_r++;
      m_t += t;
      m_alpha_star++;
      m_beta_star += t;
      how_many++;
      current_t = (*m_data_cont)[0][m_current_data_index];
      m_current_data_index++;
    }
  }
  double t = (m_pp_time_scale&&m_data_cont) ? m_pp_time_scale->cumulative_function( current_t, m_current_t+increment ) : m_current_t+increment - current_t;
  if(t>0){
    m_log_predictive_df = calculate_log_posterior_predictive_pdf(t,0) - LOG_TWO;//upper tail
    if(lower_tail||two_sided){
      m_log_predictive_df2 = log(1-exp(m_log_predictive_df));//lower tail
      if(lower_tail)
	m_log_predictive_df = m_log_predictive_df2;
    }
    /*    if(two_sided){
      if(m_log_predictive_df > m_log_predictive_df2)
	m_log_predictive_df = m_log_predictive_df2;
      m_log_predictive_df += LOG_TWO;
      }*/
    sum_log_pvals += m_log_predictive_df;
    //    if(two_sided)
    //      sum_log_pvals2 += m_log_predictive_df2;
    m_t += t;
    m_beta_star += t;
    how_many++;
  }
  //  if(two_sided && sum_log_pvals2 < sum_log_pvals)
  //    sum_log_pvals = sum_log_pvals2;
  m_currently_observable = how_many > 0;
  if(m_currently_observable){
    m_log_predictive_df = how_many==1 ? sum_log_pvals : log(gsl_cdf_chisq_Q(-2*sum_log_pvals, 2*how_many));
    //    if(two_sided)
    //      m_log_predictive_df += LOG_TWO;
    if(two_sided){
    if(m_log_predictive_df>=0)//rounding erros
      m_log_predictive_df2 = log(gsl_cdf_chisq_P(-2*sum_log_pvals, 2*how_many));
    else
      m_log_predictive_df2 = log(1-exp(m_log_predictive_df));//lower tail
      if(m_log_predictive_df > m_log_predictive_df2)
	m_log_predictive_df = m_log_predictive_df2;
      m_log_predictive_df += LOG_TWO;
    }
  }
  else
    m_log_predictive_df = -LOG_TWO;
  m_pvalue_pair = make_pair(m_log_predictive_df,m_log_predictive_df);
  m_pvalue_pair_on_log_scale = true;
  return m_log_predictive_df;
}