示例#1
0
  void Print(const char* aName, LogLevel aLevel, const char* aFmt, va_list aArgs)
  {
    const size_t kBuffSize = 1024;
    char buff[kBuffSize];

    char* buffToWrite = buff;

    // For backwards compat we need to use the NSPR format string versions
    // of sprintf and friends and then hand off to printf.
    va_list argsCopy;
    va_copy(argsCopy, aArgs);
    size_t charsWritten = PR_vsnprintf(buff, kBuffSize, aFmt, argsCopy);
    va_end(argsCopy);

    if (charsWritten == kBuffSize - 1) {
      // We may have maxed out, allocate a buffer instead.
      buffToWrite = PR_vsmprintf(aFmt, aArgs);
      charsWritten = strlen(buffToWrite);
    }

    // Determine if a newline needs to be appended to the message.
    const char* newline = "";
    if (charsWritten == 0 || buffToWrite[charsWritten - 1] != '\n') {
      newline = "\n";
    }

    FILE* out = mOutFile ? mOutFile : stderr;

    // This differs from the NSPR format in that we do not output the
    // opaque system specific thread pointer (ie pthread_t) cast
    // to a long. The address of the current PR_Thread continues to be
    // prefixed.
    //
    // Additionally we prefix the output with the abbreviated log level
    // and the module name.
    if (!mAddTimestamp) {
      fprintf_stderr(out,
                     "[%p]: %s/%s %s%s",
                     PR_GetCurrentThread(), ToLogStr(aLevel),
                     aName, buffToWrite, newline);
    } else {
      PRExplodedTime now;
      PR_ExplodeTime(PR_Now(), PR_GMTParameters, &now);
      fprintf_stderr(
          out,
          "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%p]: %s/%s %s%s",
          now.tm_year, now.tm_month + 1, now.tm_mday,
          now.tm_hour, now.tm_min, now.tm_sec, now.tm_usec,
          PR_GetCurrentThread(), ToLogStr(aLevel),
          aName, buffToWrite, newline);
    }

    if (mIsSync) {
      fflush(out);
    }

    if (buffToWrite != buff) {
      PR_smprintf_free(buffToWrite);
    }
  }
示例#2
0
int Update_init_manually(em_phyclust_struct *empcs, Q_matrix_array *QA, em_control *EMC, em_fp *EMFP){
	int n_X, k, ret_stop = 0;

	for(n_X = 0; n_X < empcs->N_X; n_X++){
		for(k = 0; k < empcs->K; k++){
			empcs->Z_normalized[n_X][k] = 0.0;
		}
		empcs->Z_normalized[n_X][empcs->class_id[empcs->map_X_to_X_org[n_X]]] = 1.0;
	}

	reset_Q_matrix_array(QA);
	if(EMC->se_type == SE_YES){
		reset_SE_P_matrix(empcs->SE_P);
	}
	assign_Mu_by_class(empcs->N_X_org, empcs->K, empcs->L, empcs->ncode, empcs->gap_index,
				empcs->class_id, empcs->X_org, empcs->Mu);
	ret_stop = init_m_step(empcs, QA, EMC, EMFP);
	if(ret_stop > 0){
		#if PRINT_ERROR > 0
			fprintf_stderr("PE: Initialization error.\n");
		#endif
		return(ret_stop);
	}
	if(!is_finite(EMFP->LogL_observed(empcs, QA))){
		#if PRINT_ERROR > 0
			fprintf_stderr("PE: manual initialization leads to non-finite observed log likelihood\n");
		#endif
		return(1);
	}

	return(ret_stop);
} /* End of Update_init_manually(). */
示例#3
0
// xxx copied from ../gtk/main.c
void
cmdarg_err(const char *fmt, ...)
{
    va_list ap;

    fprintf_stderr("wireshark: ");
    va_start(ap, fmt);
    vfprintf_stderr(fmt, ap);
    va_end(ap);
    fprintf_stderr("\n");
}
示例#4
0
void
DeprecatedContentHostBase::Dump(FILE* aFile,
                      const char* aPrefix,
                      bool aDumpHtml)
{
  if (!aDumpHtml) {
    return;
  }
  if (!aFile) {
    aFile = stderr;
  }
  fprintf_stderr(aFile, "<ul>");
  if (mDeprecatedTextureHost) {
    fprintf_stderr(aFile, "%s", aPrefix);
    fprintf_stderr(aFile, "<li> <a href=");
    DumpDeprecatedTextureHost(aFile, mDeprecatedTextureHost);
    fprintf_stderr(aFile, "> Front buffer </a></li> ");
  }
  if (mDeprecatedTextureHostOnWhite) {
    fprintf_stderr(aFile, "%s", aPrefix);
    fprintf_stderr(aFile, "<li> <a href=");
    DumpDeprecatedTextureHost(aFile, mDeprecatedTextureHostOnWhite);
    fprintf_stderr(aFile, "> Front buffer on white </a> </li> ");
  }
  fprintf_stderr(aFile, "</ul>");
}
示例#5
0
/* Collection of all initialization procedures. */
void init_em_step(phyclust_struct *pcs, Q_matrix_array *QA, em_control *EMC, em_fp *EMFP){
	double lower_bound_org, lower_bound;

	if(pcs->K * EMC->min_n_class >= pcs->N_X){
		fprintf_stderr("PE: K is too huge.\n");
		exit(1);
	}
	lower_bound_org = (double) EMC->min_n_class / (double) pcs->N_X_org;
	lower_bound = 1.0 / (double) pcs->N_X;
	EMC->Eta_lower_bound = (lower_bound_org > lower_bound) ? lower_bound_org : lower_bound;
	if(pcs->K > 1){
		EMC->Eta_upper_bound = 1.0 - EMC->Eta_lower_bound;
	} else{
		EMC->Eta_upper_bound = 1.0;
	}

	/* Check exceptions. */
	if(pcs->label->label_method != NONE){
		EMC->init_method = randomMu;
	}
	if(EMC->init_method == randomNJ && pcs->K == 1){
		EMC->exhaust_iter = 1;
		EMC->init_procedure = exhaustEM;
	}
	update_em_control(EMC);
	update_Q_matrix_array(QA, pcs);

	#if (EMDEBUG & 1) == 1
		printf("init proc: %s, method: %s, sub model: %s, dist: %s.\n",
		INIT_PROCEDURE[EMC->init_procedure], INIT_METHOD[EMC->init_method],
		SUBSTITUTION_MODEL[EMC->substitution_model], EDISTANCE_MODEL[EMC->edist_model]);
	#endif
	switch(EMC->init_procedure){
		case exhaustEM:
			exhaust_EM(pcs, QA, EMC, EMFP);
			break;
		case emEM:
			em_EM(pcs, QA, EMC, EMFP);
			break;
		case RndEM:
			Rnd_EM(pcs, QA, EMC, EMFP);
			break;
		case RndpEM:
			Rnd_EM(pcs, QA, EMC, EMFP);
			break;
		default:
			fprintf_stderr("PE: The initial procedure is not found.\n");
			exit(1);
	}
} /* End of init_em_step(). */
示例#6
0
void
LayerManager::DumpSelf(FILE* aFile, const char* aPrefix)
{
  nsAutoCString str;
  PrintInfo(str, aPrefix);
  fprintf_stderr(FILEOrDefault(aFile), "%s\n", str.get());
}
示例#7
0
void
ImageHost::Dump(FILE* aFile,
                const char* aPrefix,
                bool aDumpHtml)
{
  if (!aFile) {
    aFile = stderr;
  }
  if (mFrontBuffer) {
    fprintf_stderr(aFile, "%s", aPrefix);
    fprintf_stderr(aFile, aDumpHtml ? "<ul><li>TextureHost: "
                             : "TextureHost: ");
    DumpTextureHost(aFile, mFrontBuffer);
    fprintf_stderr(aFile, aDumpHtml ? " </li></ul> " : " ");
  }
}
示例#8
0
void
DeprecatedImageHostSingle::Dump(FILE* aFile,
                                const char* aPrefix,
                                bool aDumpHtml)
{
  if (!aFile) {
    aFile = stderr;
  }
  if (mDeprecatedTextureHost) {
    fprintf_stderr(aFile, "%s", aPrefix);
    fprintf_stderr(aFile, aDumpHtml ? "<ul><li>DeprecatedTextureHost: "
                             : "DeprecatedTextureHost: ");
    DumpDeprecatedTextureHost(aFile, mDeprecatedTextureHost);
    fprintf_stderr(aFile, aDumpHtml ? " </li></ul> " : " ");
  }
}
示例#9
0
/*
 * Remove an endpoint from an endpoint group.
 *
 * Only succeeds if they are both active and the endpoint is actually
 * on the endpoint group.  Used by both endpoint_join_epgroup and
 * epgroup_deallocate subroutines; needs to be specific about the
 * epgroup for the epgroup deallocate subroutines.
 *
 * Takes two argument; the endpoint and endpoint group.
 * Returns success or error code.
 *
 * Only side effect is to remove the endpoint from the endpoint group */
FLIPC_return_t flipc_epgroup_remove_endpoint(flipc_epgroup_t epgroup,
					     flipc_endpoint_t endpoint)
{

    /* Take endpoint group's simple lock.  */
    flipc_simple_lock_acquire(&epgroup->pail_lock);

    if (!epgroup->sail_enabled) {
	/* Well, we'll fail.  How badly?  */
	if (FLIPC_EPGROUP_PTR(endpoint->sail_epgroup)
	    == epgroup) {
	    /* Badly.  This isn't allowed, for an endpoint group to be
	       disabled and have an endpoint on it.  */
	    flipc_simple_lock_release(&epgroup->pail_lock);
	    fprintf_stderr("Data structure corruption on comm buffer.\n");
	    exit(-1);
	}

	/* Not too too badly.  Cleanup and report.  */
	flipc_simple_lock_release(&epgroup->pail_lock);
	return FLIPC_EPGROUP_INACTIVE;
    }

    /* Is the endpoint on the endpoint group?  */
    if (FLIPC_EPGROUP_PTR(endpoint->sail_epgroup)
	== epgroup) {
	/* Yep.  Take it off.  First edit the chain.  */
	volatile flipc_cb_ptr *last_cbptr;
	flipc_endpoint_t chain_endpoint;

	for (last_cbptr = &epgroup->pail_first_endpoint,
	     chain_endpoint = FLIPC_ENDPOINT_PTR(*last_cbptr);
	     chain_endpoint != endpoint;
	     last_cbptr = &chain_endpoint->pail_next_eg_endpoint,
	     chain_endpoint = FLIPC_ENDPOINT_PTR(*last_cbptr))
	    ;

	*last_cbptr = endpoint->pail_next_eg_endpoint;

	/* Now, modify the internal pointer.  This is a synchronization step
	   with the rest of the AIL, as we give up ownership of the
	   endpoint group related data structures in the endpoint at this
	   point.  */
	SYNCVAR_WRITE(endpoint->sail_epgroup = FLIPC_CBPTR_NULL);

	/* Increment epgroup's version counter.  */
	epgroup->pail_version++;

	/* All done; clean up and return.  */
	flipc_simple_lock_release(&epgroup->pail_lock);

	return FLIPC_SUCCESS;
    } else {
	/* Nope; can't complete the mission.  */
	flipc_simple_lock_release(&epgroup->pail_lock);

	return FLIPC_ENDPOINT_NOT_ON_EPGROUP;
    }
}
示例#10
0
void WriteSnapshotToDumpFile_internal(T* aObj, DataSourceSurface* aSurf)
{
  nsRefPtr<gfxImageSurface> deprecatedSurf =
    new gfxImageSurface(aSurf->GetData(),
                        ThebesIntSize(aSurf->GetSize()),
                        aSurf->Stride(),
                        SurfaceFormatToImageFormat(aSurf->GetFormat()));
  nsCString string(aObj->Name());
  string.Append('-');
  string.AppendInt((uint64_t)aObj);
  if (gfxUtils::sDumpPaintFile) {
    fprintf_stderr(gfxUtils::sDumpPaintFile, "array[\"%s\"]=\"", string.BeginReading());
  }
  deprecatedSurf->DumpAsDataURL(gfxUtils::sDumpPaintFile);
  if (gfxUtils::sDumpPaintFile) {
    fprintf_stderr(gfxUtils::sDumpPaintFile, "\";");
  }
}
示例#11
0
/* Print an ASCII-formatted list of interfaces. */
void
capture_opts_print_interfaces(GList *if_list)
{
    int         i;
    GList       *if_entry;
    if_info_t   *if_info;

    i = 1;  /* Interface id number */
    for (if_entry = g_list_first(if_list); if_entry != NULL;
         if_entry = g_list_next(if_entry)) {
        if_info = (if_info_t *)if_entry->data;
        fprintf_stderr("%d. %s", i++, if_info->name);

        /* Print the description if it exists */
        if (if_info->description != NULL)
            fprintf_stderr(" (%s)", if_info->description);
        fprintf_stderr("\n");
    }
}
示例#12
0
void WriteSnapshotLinkToDumpFile(T* aObj, FILE* aFile)
{
  if (!aObj) {
    return;
  }
  nsCString string(aObj->Name());
  string.Append('-');
  string.AppendInt((uint64_t)aObj);
  fprintf_stderr(aFile, "href=\"javascript:ViewImage('%s')\"", string.BeginReading());
}
示例#13
0
void
nsPlaceholderFrame::List(FILE* out, const char* aPrefix, uint32_t aFlags) const
{
  nsCString str;
  ListGeneric(str, aPrefix, aFlags);

  if (mOutOfFlowFrame) {
    str += " outOfFlowFrame=";
    nsFrame::ListTag(str, mOutOfFlowFrame);
  }
  fprintf_stderr(out, "%s\n", str.get());
}
示例#14
0
void
TiledContentHost::Dump(FILE* aFile,
                       const char* aPrefix,
                       bool aDumpHtml)
{
  if (!aFile) {
    aFile = stderr;
  }

  TiledLayerBufferComposite::Iterator it = mVideoMemoryTiledBuffer.TilesBegin();
  TiledLayerBufferComposite::Iterator stop = mVideoMemoryTiledBuffer.TilesEnd();
  if (aDumpHtml) {
    fprintf_stderr(aFile, "<ul>");
  }
  for (;it != stop; ++it) {
    fprintf_stderr(aFile, "%s", aPrefix);
    fprintf_stderr(aFile, aDumpHtml ? "<li> <a href=" : "Tile ");
    DumpDeprecatedTextureHost(aFile, it->mDeprecatedTextureHost);
    fprintf_stderr(aFile, aDumpHtml ? " >Tile</a></li>" : " ");
  }
    if (aDumpHtml) {
    fprintf_stderr(aFile, "</ul>");
  }
}
示例#15
0
void
mach_error(const char *str, mach_error_t err)
{
	char *err_str;
	char buf[1024];
	boolean_t diag;

	err_str = mach_error_string_int(err, &diag);

	if (diag) {
		_mach_snprintf(buf, sizeof(buf), "%s %s (%x)", mach_error_type(err), err_str, err);
		err_str = buf;
	}

	fprintf_stderr("%s %s\n", str, err_str);
}
示例#16
0
void respond(struct response *resp)
{
	char line[MAXSTR * 2];

	sprintf_resp(line, NULL, resp);

	if (output != stdout) {
		fputs(line, output);
		fflush(output);
	}

	if (resp->r_status >= STATUS_ERRNO)
		fprintf_stderr("%s", line);
	else if (!quiet)
		fprintf(stdout, "%s", line);
}
示例#17
0
void
Layer::Dump(FILE* aFile, const char* aPrefix, bool aDumpHtml)
{
  if (aDumpHtml) {
    fprintf_stderr(aFile, "<li><a id=\"%p\" ", this);
#ifdef MOZ_DUMP_PAINTING
    if (GetType() == TYPE_CONTAINER || GetType() == TYPE_THEBES) {
      WriteSnapshotLinkToDumpFile(this, aFile);
    }
#endif
    fprintf_stderr(aFile, ">");
  }
  DumpSelf(aFile, aPrefix);

#ifdef MOZ_DUMP_PAINTING
  if (gfxUtils::sDumpPainting && AsLayerComposite() && AsLayerComposite()->GetCompositableHost()) {
    AsLayerComposite()->GetCompositableHost()->Dump(aFile, aPrefix, aDumpHtml);
  }
#endif

  if (aDumpHtml) {
    fprintf_stderr(aFile, "</a>");
  }

  if (Layer* mask = GetMaskLayer()) {
    fprintf_stderr(aFile, "%s  Mask layer:\n", aPrefix);
    nsAutoCString pfx(aPrefix);
    pfx += "    ";
    mask->Dump(aFile, pfx.get(), aDumpHtml);
  }

  if (Layer* kid = GetFirstChild()) {
    nsAutoCString pfx(aPrefix);
    pfx += "  ";
    if (aDumpHtml) {
      fprintf_stderr(aFile, "<ul>");
    }
    kid->Dump(aFile, pfx.get(), aDumpHtml);
    if (aDumpHtml) {
      fprintf_stderr(aFile, "</ul>");
    }
  }

  if (aDumpHtml) {
    fprintf_stderr(aFile, "</li>");
  }
  if (Layer* next = GetNextSibling())
    next->Dump(aFile, aPrefix, aDumpHtml);
}
示例#18
0
void
LayerManager::Dump(FILE* aFile, const char* aPrefix, bool aDumpHtml)
{
  FILE* file = FILEOrDefault(aFile);

#ifdef MOZ_DUMP_PAINTING
  if (aDumpHtml) {
    fprintf_stderr(file, "<ul><li><a ");
    WriteSnapshotLinkToDumpFile(this, file);
    fprintf_stderr(file, ">");
  }
#endif
  DumpSelf(file, aPrefix);
#ifdef MOZ_DUMP_PAINTING
  if (aDumpHtml) {
    fprintf_stderr(file, "</a>");
  }
#endif

  nsAutoCString pfx(aPrefix);
  pfx += "  ";
  if (!GetRoot()) {
    fprintf_stderr(file, "%s(null)", pfx.get());
    if (aDumpHtml) {
      fprintf_stderr(file, "</li></ul>");
    }
    return;
  }

  if (aDumpHtml) {
    fprintf_stderr(file, "<ul>");
  }
  GetRoot()->Dump(file, pfx.get(), aDumpHtml);
  if (aDumpHtml) {
    fprintf_stderr(file, "</ul></li></ul>");
  }
  fprintf_stderr(file, "\n");
}
示例#19
0
/* For models using empirical pi's in Q. */
void update_Q_matrix_array(Q_matrix_array *QA, phyclust_struct *pcs){
	int s, n_X_org, l, k, flag = 0;
	double pi[QA->ncode], total = 0.0, sum = 0.0;

	for(s = 0; s < QA->ncode; s++){
		pi[s] = 0;
	}
	for(n_X_org = 0; n_X_org < pcs->N_X_org; n_X_org++){
		for(l = 0; l < pcs->L; l++){
			if(pcs->X_org[n_X_org][l] == pcs->gap_index){	/* For gaps. */
				continue;
			}
			pi[pcs->X_org[n_X_org][l]]++;
			total++;
		}
	}
	for(s = 0; s < (QA->ncode - 1); s++){
		pi[s] = pi[s] / total;
		sum += pi[s];
	}
	pi[s] = 1.0 - sum;
	for(s = 0; s < QA->ncode; s++){
		flag |= ((pi[s] <= QA->lower_bound) || (pi[s] >= QA->upper_bound));
	}
	if(flag){
		fprintf_stderr("PE: Empirical pi's:");
		for(s = 0; s < QA->ncode; s++){
			printf(" %e", pi[s]);
		}
		printf("\n");
		exit(1);
	} else{
		for(k = 0; k < QA->K; k++){
			for(s = 0; s < QA->ncode; s++){
				QA->Q[k]->pi[s] = pi[s];
			}
		}
		QA->Update_log_Pt(QA);
	}
} /* End of update_Q_matrix_array(). */
示例#20
0
void
capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
                                   gboolean monitor_mode)
{
    GList *lt_entry;
    data_link_info_t *data_link_info;

    if (caps->can_set_rfmon)
        fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
                       name, monitor_mode ? "" : "not ");
    else
        fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
    for (lt_entry = caps->data_link_types; lt_entry != NULL;
         lt_entry = g_list_next(lt_entry)) {
        data_link_info = (data_link_info_t *)lt_entry->data;
        fprintf_stderr("  %s", data_link_info->name);
        if (data_link_info->description != NULL)
            fprintf_stderr(" (%s)", data_link_info->description);
        else
            fprintf_stderr(" (not supported)");
        fprintf_stderr("\n");
    }
}
示例#21
0
/* Pick clusters by PAM.
 * There is no randomness for this method, so the following settings may be suggested.
 * EMC->init_procedure = exhaustEM;
 * EMC->exhaust_iter = 1; */
int Update_init_pam(em_phyclust_struct *empcs, Q_matrix_array *QA, em_control *EMC, em_fp *EMFP){
	int ret_stop = 0;
	int n_X_org, n_X, k, l, N_X_org = empcs->N_X_org, N_X = empcs->N_X, K = empcs->K, L = empcs->L;
	int center_id[K], class_id[N_X_org];
	int consensus_Mu[L];
	double init_logL_observed;
	edist_struct *eds;
	
	find_consensus_Mu(empcs->N_X_org, L, empcs->ncode, empcs->gap_index, empcs->X_org, consensus_Mu);
	eds = initialize_edist_struct_LT_pam(EMC->edist_model, N_X_org, L, empcs->X_org);
	assign_class_by_pam(N_X_org, K, eds->EDM, center_id, class_id);

	/* Pick mu from X. */
	for(k = 0; k < K; k++){
		for(l = 0; l < L; l++){
			empcs->Mu[k][l] = empcs->X_org[center_id[k]][l];
		}
		empcs->n_class[k] = 0;
	}

	/* Assign X to the nearest mu by distance, and recreate Z_normalized. */
	for(n_X = 0; n_X < N_X; n_X++){
		for(k = 0; k < K; k++){
			empcs->Z_normalized[n_X][k] = 0.0;
		}
		empcs->Z_normalized[n_X][class_id[empcs->map_X_to_X_org[n_X]]] = 1.0;
	}
	for(n_X_org = 0; n_X_org < N_X_org; n_X_org++){
		empcs->n_class[class_id[n_X_org]]++;
	}

	/* Replace gaps by the concensus. */
	for(k = 0; k < K; k++){
		for(l = 0; l < L; l++){
			if(empcs->Mu[k][l] == empcs->gap_index || empcs->Mu[k][l] == MISSING_ALLELE){
				empcs->Mu[k][l] = consensus_Mu[l];
			}
		}
	}

	if(check_all_min_n_class(K, empcs->n_class, EMC->min_n_class)){
		ret_stop = init_m_step(empcs, QA, EMC, EMFP);
		if(ret_stop > 0){
			#if PRINT_ERROR > 0
				fprintf_stderr("PE: Initialization error. (%s)\n", INIT_METHOD[EMC->init_method]);
			#endif
			free_edist_struct(eds);
			return(ret_stop);
		}
		init_logL_observed = EMFP->LogL_observed(empcs, QA);
		if(!is_finite(init_logL_observed)){
			#if PRINT_ERROR > 0
				fprintf_stderr("PE: Initial logL_observed is not finit. (%s)\n",
						INIT_METHOD[EMC->init_method]);
			#endif
			free_edist_struct(eds);
			return(1);
		}
	} else{
		#if PRINT_ERROR > 0
			fprintf_stderr("PE: Initialization is not valid for min_n_class = %d. (%s)\n",
					EMC->min_n_class, INIT_METHOD[EMC->init_method]);
		#endif
		free_edist_struct(eds);
		return(1);
	}

	free_edist_struct(eds);
	return(ret_stop);
} /* End of Update_init_pam(). */
示例#22
0
void em_EM(phyclust_struct *pcs, Q_matrix_array *org_QA, em_control *org_EMC, em_fp *EMFP){
	int init_flag = 0, init_iter = 0;
	int converge_iter = 0, converge_inner_iter = 0, converge_cm_iter = 0;
	int short_iter = org_EMC->short_iter;
	double short_eps = org_EMC->short_eps;
	Q_matrix_array *new_QA;
	em_control *new_EMC;
	em_phyclust_struct *org_empcs, *new_empcs;

	new_QA = duplicate_Q_matrix_array(org_QA);
	new_EMC = duplicate_em_control(org_EMC);
	org_empcs = initialize_em_phyclust_struct(pcs);
	new_empcs = initialize_em_phyclust_struct(pcs);

	org_empcs->logL_observed = -Inf;
	while(new_EMC->short_iter > 0){
		init_flag = 1;
		init_iter = 0;
		while(init_flag > 0 && init_iter < org_EMC->max_init_iter){
			init_flag = EMFP->Update_init(new_empcs, new_QA, new_EMC, EMFP);
			init_iter++;
		}
		if(init_flag > 0){
			new_EMC->short_iter--;
			continue;
		}

		EMFP->Short_em_step(new_empcs, new_QA, new_EMC, EMFP);
		converge_iter += new_EMC->converge_iter;
		converge_inner_iter += new_EMC->converge_inner_iter;
		converge_cm_iter += new_EMC->converge_cm_iter;

		if(new_empcs->logL_observed > org_empcs->logL_observed){
			EMFP->Copy_empcs(new_empcs, org_empcs);
			org_QA->Copy_Q_matrix_array(new_QA, org_QA);
			copy_EMC(new_EMC, org_EMC);
		}
		new_EMC->short_iter -= new_EMC->converge_iter;
	}

	if(org_empcs->logL_observed == -Inf){
		free_Q_matrix_array(new_QA);
		free_em_control(new_EMC);
		free_em_phyclust_struct(org_empcs);
		free_em_phyclust_struct(new_empcs);
		fprintf_stderr("PE: Initialization error. (%s, %s)\n", INIT_PROCEDURE[org_EMC->init_procedure],
				INIT_METHOD[org_EMC->init_method]);
		exit(1);
	}

	org_EMC->short_iter = short_iter;
	org_EMC->short_eps = short_eps;
	EMFP->Em_step(org_empcs, org_QA, org_EMC, EMFP);
	org_EMC->converge_iter += converge_iter;
	org_EMC->converge_inner_iter += converge_inner_iter;
	org_EMC->converge_cm_iter += converge_cm_iter;

	/* Copy results from empcs to pcs. */
	EMFP->Copy_empcs_to_pcs(org_empcs, pcs);

	free_Q_matrix_array(new_QA);
	free_em_control(new_EMC);
	free_em_phyclust_struct(org_empcs);
	free_em_phyclust_struct(new_empcs);
} /* End of em_EM(). */
示例#23
0
/* Pick clusters by randomly cutting the longest K internal branches of neighbor-joining tree. */
int Update_init_random_nj_unique(em_phyclust_struct *empcs, Q_matrix_array *QA, em_control *EMC, em_fp *EMFP){
	int init_iter = 0, ret_stop = 0;
	int n_X_org, n_X, k;
	int N_X_org = empcs->N_X_org, N_X = empcs->N_X, K = empcs->K, L = empcs->L;
	int random_branch_id[N_X - 3], class_id[N_X];
	double init_logL_observed;
	edist_struct *eds;
	nj_struct *njs;
	
	eds = initialize_edist_struct_UT(EMC->edist_model, N_X, L, empcs->X);
	njs = initialize_nj_struct(N_X);
	njs->D = eds->EDM[0];
	phyclust_ape_nj(njs);
	if(! check_njs(njs)){
		#if PRINT_ERROR > 0
			fprintf_stderr("PE: NJ may be not valid!\n");
		#endif
		print_njs(njs->n_edge, njs);
		free_edist_struct(eds);
		free_nj_struct(njs);
		return(1);
	}
	#if INITDEBUG > 0
		print_njs(njs->n_edge, njs);
	#endif

	while(init_iter < EMC->max_init_iter){
		init_iter++;
		reset_Q_matrix_array(QA);
		if(EMC->se_type == SE_YES){
			reset_SE_P_matrix(empcs->SE_P);
		}

		/* Randomly pick mu from X. */
		random_branch(njs, random_branch_id);
		ret_stop = assign_class_by_njs_branch(K, njs, random_branch_id, class_id);
		if(ret_stop != 0){
			free_edist_struct(eds);
			free_nj_struct(njs);
			continue;
		}

		/* Assign Mu and recreate Z_normalized. */
		for(n_X_org = 0; n_X_org < N_X_org; n_X_org++){
			empcs->class_id[n_X_org] = class_id[empcs->map_X_org_to_X[n_X_org]];
		}
		assign_Mu_by_class(empcs->N_X_org, empcs->K, empcs->L, empcs->ncode, empcs->gap_index,
					empcs->class_id, empcs->X_org, empcs->Mu);
		for(k = 0; k < K; k++){
			empcs->n_class[k] = 0;
		}
		for(n_X = 0; n_X < N_X; n_X++){
			for(k = 0; k < K; k++){
				empcs->Z_normalized[n_X][k] = 0.0;
			}
			empcs->Z_normalized[n_X][class_id[empcs->map_X_org_to_X[empcs->map_X_to_X_org[n_X]]]]
				= 1.0;
		}
		for(n_X = 0; n_X < N_X; n_X++){
			empcs->n_class[class_id[n_X]] += empcs->replication_X[n_X];
		}

		if(check_all_min_n_class(K, empcs->n_class, EMC->min_n_class)){
			ret_stop = init_m_step(empcs, QA, EMC, EMFP);
			if(ret_stop > 0){
				continue;
			}
			init_logL_observed = EMFP->LogL_observed(empcs, QA);
			if(is_finite(init_logL_observed)){
				break;
			}
		}
	}

	if(init_iter >= EMC->max_init_iter){
		ret_stop = init_m_step(empcs, QA, EMC, EMFP);
		if(ret_stop > 0){
			#if PRINT_ERROR > 0
				fprintf_stderr("PE: Initialization error. (%s)\n", INIT_METHOD[EMC->init_method]);
			#endif
			free_edist_struct(eds);
			free_nj_struct(njs);
			return(ret_stop);	
		}
		init_logL_observed = EMFP->LogL_observed(empcs, QA);
		if(!is_finite(init_logL_observed)){
			#if PRINT_ERROR > 0
				fprintf_stderr("PE: Initial logL_observed is not finit. (%s)\n",
						INIT_METHOD[EMC->init_method]);
			#endif
			free_edist_struct(eds);
			free_nj_struct(njs);
			return(1);
		}
	}

	free_edist_struct(eds);
	free_nj_struct(njs);
	return(ret_stop);
} /* End of Update_init_random_nj_unique(). */
示例#24
0
/* Initialization procedure. */
void exhaust_EM(phyclust_struct *pcs, Q_matrix_array *org_QA, em_control *org_EMC, em_fp *EMFP){
	int init_flag = 0, init_iter = 0;
	int converge_iter = 0, converge_inner_iter = 0, converge_cm_iter = 0;
	int iter = 1, exhaust_iter = org_EMC->exhaust_iter;
	Q_matrix_array *new_QA;
	em_control *new_EMC;
	em_phyclust_struct *org_empcs, *new_empcs;

	new_QA = duplicate_Q_matrix_array(org_QA);
	new_EMC = duplicate_em_control(org_EMC);
	org_empcs = initialize_em_phyclust_struct(pcs);
	new_empcs = initialize_em_phyclust_struct(pcs);

	#if verbosity_exhaust_EM > 0
		printf("Iteration %d/%d", iter, org_EMC->exhaust_iter);
	#endif
	#if verbosity_exhaust_EM > 1
		printf(":\n\t");
	#elif verbosity_exhaust_EM > 0
		printf("\n");
	#endif
	init_flag = EMFP->Update_init(new_empcs, new_QA, new_EMC, EMFP);

	if(exhaust_iter == 1 && init_flag > 0){
	 	free_Q_matrix_array(new_QA);
		free_em_control(new_EMC);
		free_em_phyclust_struct(org_empcs);
		free_em_phyclust_struct(new_empcs);
		fprintf_stderr("PE: Initialization error. (%s, %s)\n", INIT_PROCEDURE[org_EMC->init_procedure],
				INIT_METHOD[org_EMC->init_method]);
		exit(1);
	}

	EMFP->Em_step(new_empcs, new_QA, new_EMC, EMFP);
	EMFP->Copy_empcs(new_empcs, org_empcs);
	org_QA->Copy_Q_matrix_array(new_QA, org_QA);

	copy_EMC(new_EMC, org_EMC);
	converge_iter += new_EMC->converge_iter;
	converge_inner_iter += new_EMC->converge_inner_iter;
	converge_cm_iter += new_EMC->converge_cm_iter;

	for(iter = 1; iter < exhaust_iter; iter++){
		#if verbosity_exhaust_EM > 0
			printf("\nIteration %d/%d", iter+1, org_EMC->exhaust_iter);
		#endif
		#if verbosity_exhaust_EM > 1
			printf(":\n\t");
		#elif verbosity_exhaust_EM > 0
			printf("\n");
		#endif
		init_flag = 1;
		init_iter = 0;
		while(init_flag > 0 && init_iter < org_EMC->max_init_iter){
			init_flag = EMFP->Update_init(new_empcs, new_QA, new_EMC, EMFP);
			init_iter++;
		}
		if(init_flag > 0){
			iter++;
			continue;
		}

		EMFP->Em_step(new_empcs, new_QA, new_EMC, EMFP);
		converge_iter += new_EMC->converge_iter;
		converge_inner_iter += new_EMC->converge_inner_iter;
		converge_cm_iter += new_EMC->converge_cm_iter;

		if(new_empcs->logL_observed > org_empcs->logL_observed &&
		   new_EMC->converge_flag < 2){
			EMFP->Copy_empcs(new_empcs, org_empcs);
			org_QA->Copy_Q_matrix_array(new_QA, org_QA);
			copy_EMC(new_EMC, org_EMC);
		}
	}

	if(org_empcs->logL_observed == -Inf){
		free_Q_matrix_array(new_QA);
		free_em_control(new_EMC);
		free_em_phyclust_struct(org_empcs);
		free_em_phyclust_struct(new_empcs);
		fprintf_stderr("PE: Initialization error. (%s, %s)\n", INIT_PROCEDURE[org_EMC->init_procedure],
				INIT_METHOD[org_EMC->init_method]);
		exit(1);
	}

	org_EMC->converge_iter = converge_iter;
	org_EMC->converge_inner_iter = converge_inner_iter;
	org_EMC->converge_cm_iter = converge_cm_iter;
	#if verbosity_exhaust_EM > 0
		printf("\n");
	#endif

	/* Copy results from empcs to pcs. */
	EMFP->Copy_empcs_to_pcs(org_empcs, pcs);

	free_Q_matrix_array(new_QA);
	free_em_control(new_EMC);
	free_em_phyclust_struct(org_empcs);
	free_em_phyclust_struct(new_empcs);
} /* End of exhaust_EM(). */
示例#25
0
int Update_init_random_Mu_unique_label(em_phyclust_struct *empcs, Q_matrix_array *QA, em_control *EMC, em_fp *EMFP){
	int init_iter = 0, ret_stop = 0;
	int n_X, k, l, N_X = empcs->N_X, K = empcs->K, L = empcs->L,
		N_X_unlabeled = empcs->N_X_unlabeled, K_labeled = empcs->K_labeled, K_unlabeled = K - K_labeled,
		tmp_n, tmp_k;
	int center_id[K], tmp_center_id[K], tmp_id;
	int consensus_Mu[L];
	double tmp, tmp_min, init_logL_observed = 0.0;
	edist_struct *eds;

	find_consensus_Mu(empcs->N_X_org, L, empcs->ncode, empcs->gap_index, empcs->X_org, consensus_Mu);
	eds = initialize_edist_struct_UT(EMC->edist_model, N_X, L, empcs->X);

	while(init_iter < EMC->max_init_iter){
		init_iter++;
		reset_Q_matrix_array(QA);
		// reset_SE_P_matrix(empcs->SE_P);

		/* Randomly pick centers from X. */
		for(k = 0; k < K_labeled; k++){
			tmp_n = 0;
			for(n_X = 0; n_X < empcs->N_X_labeled; n_X++){
				if(empcs->label_semi[n_X] == k){
					tmp_n++;
				}
			}
			srswor(tmp_n, 1, tmp_center_id);

			tmp_n = -1;
			for(n_X = 0; n_X < empcs->N_X_labeled; n_X++){
				if(empcs->label_semi[n_X] == k){
					tmp_n++;
					if(tmp_n == tmp_center_id[0]){
						break;
					}
				}
			}
			tmp_n = n_X;

			for(n_X = 0; n_X < N_X; n_X++){
				if(empcs->X[n_X] == empcs->X_labeled[tmp_n]){
					center_id[k] = n_X;
					break;
				}
			}
		}

		if(K_unlabeled > 0){
			srswor(N_X_unlabeled, K_unlabeled, tmp_center_id);

			for(k = 0; k < K_unlabeled; k++){
				tmp_n = tmp_center_id[k];
				tmp_k = k + K_labeled;
				for(n_X = 0; n_X < N_X; n_X++){
					if(empcs->X[n_X] == empcs->X_unlabeled[tmp_n]){
						center_id[tmp_k] = n_X;
						break;
					}
				}
			}
		}

		/* Assign Mu by centers. */
		for(k = 0; k < K; k++){
			for(l = 0; l < L; l++){
				empcs->Mu[k][l] = empcs->X[center_id[k]][l];
			}
			empcs->n_class[k] = 0;
		}

		/* Assign X to the nearest mu by distance, and recreate Z_normalized. */
		for(n_X = 0; n_X < N_X; n_X++){
			tmp_min = eds->get_pair_edist(eds, n_X, center_id[0]);
			tmp_id = 0;
			for(k = 1; k < K; k++){
				tmp = eds->get_pair_edist(eds, n_X, center_id[k]);
				if(tmp < tmp_min){
					tmp_min = tmp;
					tmp_id = k;
				}
			}
	
			for(k = 0; k < K; k++){
				empcs->Z_normalized[n_X][k] = 0.0;
			}
			empcs->Z_normalized[n_X][tmp_id] = 1.0;
			empcs->n_class[tmp_id] += empcs->replication_X[n_X];
		}

		/* Replace gaps by the concensus. */
		for(k = 0; k < K; k++){
			for(l = 0; l < L; l++){
				if(empcs->Mu[k][l] == empcs->gap_index || empcs->Mu[k][l] == MISSING_ALLELE){
					empcs->Mu[k][l] = consensus_Mu[l];
				}
			}
		}

		if(check_all_min_n_class(K, empcs->n_class, EMC->min_n_class)){
			ret_stop = init_m_step(empcs, QA, EMC, EMFP);
			if(ret_stop > 0){
				continue;
			}
			init_logL_observed = EMFP->LogL_observed(empcs, QA);
			if(is_finite(init_logL_observed)){
				break;
			}
		}
	}

	if(init_iter >= EMC->max_init_iter){
		ret_stop = init_m_step(empcs, QA, EMC, EMFP);
		if(ret_stop > 0){
			#if PRINT_ERROR > 0
				fprintf_stderr("PE: Initialization error. (%s)\n", INIT_METHOD[EMC->init_method]);
			#endif
			free_edist_struct(eds);
			return(ret_stop);
		}
		init_logL_observed = EMFP->LogL_observed(empcs, QA);
		if(!is_finite(init_logL_observed)){
			#if PRINT_ERROR > 0
				fprintf_stderr("PE: Initial logL_observed is not finit. (%s)\n",
						INIT_METHOD[EMC->init_method]);
			#endif
			free_edist_struct(eds);
			return(1);
		}
	}

	free_edist_struct(eds);
	return(ret_stop);
} /* End of Update_init_random_Mu_unique(). */
示例#26
0
int main(int argc, char **argv)
{
	int opt;
	int len, rc;
	struct sigaction sigact;
	char *rest;
	int oflags = 0;
	int no_tag;

	memset(&sigact, 0, sizeof(sigact));
	sigact.sa_handler = sighandler;

	if (sigaction(SIGALRM, &sigact, NULL) == -1)
		return 1;

	if (sigaction(SIGPIPE, &sigact, NULL) == -1)
		return 1;

	input = stdin;
	output = stdout;
	/* now parsing options with getopt */
	while ((opt = getopt(argc, argv, options)) != EOF) {
		switch (opt) {
		case 'c':
			rc = chdir(optarg);
			if (rc == -1) {
				fprintf(stderr,
					"Can not change dir to %s errno = %d \"%s\"\n",
					optarg, errno, strerror(errno));
				exit(1);
			}
			break;

		case 'q':
			quiet = true;
			break;

		case 'd':
			duperrors = true;
			break;

		case 's':
			if (oflags > 7)
				show_usage(1, "Can not combine -x and -s\n");

			oflags |= 1;
			script = true;
			strncpy(server, optarg, MAXSTR);
			break;

		case 'x':
			if ((oflags & 7) != 0)
				show_usage(1,
					   "Can not combine -x and -s/-p/-n\n");

			oflags |= 8;
			script = true;
			input = fopen(optarg, "r");

			if (input == NULL)
				fatal("Could not open %s\n", optarg);
			break;

		case 'n':
			if (oflags > 7)
				show_usage(1, "Can not combine -x and -n\n");

			oflags |= 2;
			strncpy(name, optarg, MAXSTR);
			break;

		case 'p':
			if (oflags > 7)
				show_usage(1, "Can not combine -x and -p\n");

			oflags |= 4;
			strncpy(portstr, optarg, MAXSTR);
			port = atoi(optarg);
			break;

		case '?':
		case 'h':
		default:
			/* display the help */
			show_usage(0, "Help\n");
		}
	}

	if (oflags > 0 && oflags < 7)
		show_usage(1, "Must specify -s, -p, and -n together\n");

	if (oflags == 7)
		openserver();

	while (1) {
		len = readln(input, line, MAXSTR * 2);
		if (len < 0) {
			if (script)
				fatal("End of file on input\n");
			else
				break;
		} else {
			struct response resp;

			lno++;
			memset(&resp, 0, sizeof(resp));

			rest = SkipWhite(line, REQUIRES_MORE, "Invalid line");

			/* Skip totally blank line */
			if (rest == NULL)
				continue;

			if (script && !quiet)
				fprintf(stdout, "%s\n", rest);

			/* If line doesn't start with a tag, that's ok */
			no_tag = (!isdigit(*rest) && (*rest != '$'));

			/* Parse request into response structure */
			rest = parse_request(rest, &resp, no_tag);

			if (rest == NULL) {
				resp.r_status = STATUS_ERRNO;
				resp.r_errno = errno;
			} else {
				/* Make sure default status is ok */
				resp.r_status = STATUS_OK;

				if (*rest != '\0' && *rest != '#')
					fprintf_stderr(
					     "Command line not consumed, rest=\"%s\"\n",
					     rest);

				switch (resp.r_cmd) {
				case CMD_OPEN:
					do_open(&resp);
					break;
				case CMD_CLOSE:
					do_close(&resp);
					break;
				case CMD_LOCKW:
					do_lock(&resp);
					break;
				case CMD_LOCK:
					do_lock(&resp);
					break;
				case CMD_UNLOCK:
					do_unlock(&resp);
					break;
				case CMD_TEST:
					do_test(&resp);
					break;
				case CMD_LIST:
					do_list(&resp);
					break;
				case CMD_HOP:
					do_hop(&resp);
					break;
				case CMD_UNHOP:
					do_unhop(&resp);
					break;
				case CMD_SEEK:
					do_seek(&resp);
					break;
				case CMD_READ:
					do_read(&resp);
					break;
				case CMD_WRITE:
					do_write(&resp);
					break;
				case CMD_ALARM:
					do_alarm(&resp);
					break;

				case CMD_HELLO:
				case CMD_COMMENT:
				case CMD_QUIT:
					resp.r_status = STATUS_OK;
					break;

				case NUM_COMMANDS:
					fprintf_stderr("Invalid command %s\n",
						       line);
					continue;
				}
			}

			respond(&resp);

			if (resp.r_cmd == CMD_QUIT)
				exit(0);
		}
	}

	return 0;
}
示例#27
0
  void Print(const char* aName, LogLevel aLevel, const char* aFmt, va_list aArgs)
  {
    const size_t kBuffSize = 1024;
    char buff[kBuffSize];

    char* buffToWrite = buff;

    // For backwards compat we need to use the NSPR format string versions
    // of sprintf and friends and then hand off to printf.
    va_list argsCopy;
    va_copy(argsCopy, aArgs);
    size_t charsWritten = PR_vsnprintf(buff, kBuffSize, aFmt, argsCopy);
    va_end(argsCopy);

    if (charsWritten == kBuffSize - 1) {
      // We may have maxed out, allocate a buffer instead.
      buffToWrite = PR_vsmprintf(aFmt, aArgs);
      charsWritten = strlen(buffToWrite);
    }

    // Determine if a newline needs to be appended to the message.
    const char* newline = "";
    if (charsWritten == 0 || buffToWrite[charsWritten - 1] != '\n') {
      newline = "\n";
    }

    FILE* out = stderr;

    // In case we use rotate, this ensures the FILE is kept alive during
    // its use.  Increased before we load mOutFile.
    ++mPrintEntryCount;

    detail::LogFile* outFile = mOutFile;
    if (outFile) {
      out = outFile->File();
    }

    // This differs from the NSPR format in that we do not output the
    // opaque system specific thread pointer (ie pthread_t) cast
    // to a long. The address of the current PR_Thread continues to be
    // prefixed.
    //
    // Additionally we prefix the output with the abbreviated log level
    // and the module name.
    PRThread *currentThread = PR_GetCurrentThread();
    const char *currentThreadName = (mMainThread == currentThread)
      ? "Main Thread"
      : PR_GetThreadName(currentThread);

    char noNameThread[40];
    if (!currentThreadName) {
      SprintfLiteral(noNameThread, "Unnamed thread %p", currentThread);
      currentThreadName = noNameThread;
    }

    if (!mAddTimestamp) {
      fprintf_stderr(out,
                     "[%s]: %s/%s %s%s",
                     currentThreadName, ToLogStr(aLevel),
                     aName, buffToWrite, newline);
    } else {
      PRExplodedTime now;
      PR_ExplodeTime(PR_Now(), PR_GMTParameters, &now);
      fprintf_stderr(
          out,
          "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%s]: %s/%s %s%s",
          now.tm_year, now.tm_month + 1, now.tm_mday,
          now.tm_hour, now.tm_min, now.tm_sec, now.tm_usec,
          currentThreadName, ToLogStr(aLevel),
          aName, buffToWrite, newline);
    }

    if (mIsSync) {
      fflush(out);
    }

    if (buffToWrite != buff) {
      PR_smprintf_free(buffToWrite);
    }

    if (mRotate > 0 && outFile) {
      int32_t fileSize = ftell(out);
      if (fileSize > mRotate) {
        uint32_t fileNum = outFile->Num();

        uint32_t nextFileNum = fileNum + 1;
        if (nextFileNum >= kRotateFilesNumber) {
          nextFileNum = 0;
        }

        // And here is the trick.  The current out-file remembers its order
        // number.  When no other thread shifted the global file number yet,
        // we are the thread to open the next file.
        if (mOutFileNum.compareExchange(fileNum, nextFileNum)) {
          // We can work with mToReleaseFile because we are sure the
          // mPrintEntryCount can't drop to zero now - the condition
          // to actually delete what's stored in that member.
          // And also, no other thread can enter this piece of code
          // because mOutFile is still holding the current file with
          // the non-shifted number.  The compareExchange() above is
          // a no-op for other threads.
          outFile->mNextToRelease = mToReleaseFile;
          mToReleaseFile = outFile;

          mOutFile = OpenFile(false, nextFileNum);
        }
      }
    }

    if (--mPrintEntryCount == 0 && mToReleaseFile) {
      // We were the last Print() entered, if there is a file to release
      // do it now.  exchange() is atomic and makes sure we release the file
      // only once on one thread.
      detail::LogFile* release = mToReleaseFile.exchange(nullptr);
      delete release;
    }
  }