/** * * @brief Creates a UPGMA guide tree. This is a frontend function to * the ported Muscle UPGMA code (). * * @param[out] tree * created upgma tree. will be allocated here. use FreeMuscleTree() * to free * @param[in] labels * pointer to nseq sequence names * @param[in] distmat * distance matrix * @param[in] ftree * optional: if non-NULL, tree will be written to this files * * @see FreeMuscleTree() * @see MuscleUpgma2() * */ void GuideTreeUpgma(tree_t **tree, char **labels, symmatrix_t *distmat, char *ftree) { linkage_t linkage = LINKAGE_AVG; FILE *fp = NULL; if (NULL != ftree) { if (NULL == (fp=fopen(ftree, "w"))) { Log(&rLog, LOG_ERROR, "Couldn't open tree-file '%s' for writing. Skipping", ftree); } /* fp NULL is handled later */ } (*tree) = (tree_t *) CKMALLOC(1 * sizeof(tree_t)); MuscleUpgma2((*tree), distmat, linkage, labels); if (rLog.iLogLevelEnabled <= LOG_DEBUG) { Log(&rLog, LOG_DEBUG, "tree logging..."); LogTree((*tree), LogGetFP(&rLog, LOG_DEBUG)); } if (NULL != fp) { MuscleTreeToFile(fp, (*tree)); Log(&rLog, LOG_INFO, "Guide tree written to %s", ftree); fclose(fp); } }
/** * * @brief * * @param[out] tree * created upgma tree. will be allocated here. use FreeMuscleTree() * to free * @param[in] mseq * @param[in] ftree * * @return non-zero on error * */ int GuideTreeFromFile(tree_t **tree, mseq_t *mseq, char *ftree) { int iNodeCount; int iNodeIndex; (*tree) = (tree_t *) CKMALLOC(1 * sizeof(tree_t)); if (MuscleTreeFromFile((*tree), ftree)!=0) { Log(&rLog, LOG_ERROR, "%s", "MuscleTreeFromFile failed"); return -1; } /* Make sure tree is rooted */ if (!IsRooted((*tree))) { Log(&rLog, LOG_ERROR, "User tree must be rooted"); return -1; } if ((int)GetLeafCount((*tree)) != mseq->nseqs) { Log(&rLog, LOG_ERROR, "User tree does not match input sequences"); return -1; } /* compare tree labels and sequence names and set leaf-ids */ iNodeCount = GetNodeCount((*tree)); for (iNodeIndex = 0; iNodeIndex < iNodeCount; ++iNodeIndex) { char *LeafName; int iSeqIndex; if (!IsLeaf(iNodeIndex, (*tree))) continue; LeafName = GetLeafName(iNodeIndex, (*tree)); if ((iSeqIndex=FindSeqName(LeafName, mseq))==-1) { Log(&rLog, LOG_ERROR, "Label '%s' in tree could not be found in sequence names", LeafName); return -1; } SetLeafId((*tree), iNodeIndex, iSeqIndex); } if (rLog.iLogLevelEnabled <= LOG_DEBUG) { Log(&rLog, LOG_DEBUG, "tree logging..."); LogTree((*tree), LogGetFP(&rLog, LOG_DEBUG)); } return 0; }
// Copy the board config (piece locations and captured pieces for each player) // from "ptree" to "board". static void FillBoard(const char* label, JNIEnv* env, tree_t* ptree, /* source */ jobject board /* dest */) { LogTree(label, ptree); jclass boardClass = (*env)->GetObjectClass(env, board); jfieldID fid = (*env)->GetFieldID(env, boardClass, "mSquares", "[I"); jintArray jarray = (jintArray)((*env)->GetObjectField(env, board, fid)); jint tmp[nsquare]; for (int i = 0; i < nsquare; ++i) { tmp[i] = BOARD[i]; } (*env)->SetIntArrayRegion(env, jarray, 0, nsquare, tmp); fid = (*env)->GetFieldID(env, boardClass, "mCapturedBlack", "I"); (*env)->SetIntField(env, board, fid, ptree->posi.hand_black); fid = (*env)->GetFieldID(env, boardClass, "mCapturedWhite", "I"); (*env)->SetIntField(env, board, fid, ptree->posi.hand_white); }