#include "CipresNativeC.h" #ifdef __cplusplus # include # include # include # include extern "C" { #else # include # include # include # include #endif static void *SetMatrixRowPtrs(void *a_arg, void *p_arg, unsigned nrows, unsigned ncols, size_t elSize) { char *p, **a; unsigned i; size_t rowBytes; a = (char **)a_arg; p = (char *)p_arg; rowBytes = ncols*elSize; for (i = 0; i < nrows; i++) { a[i] = p; p += rowBytes; } return a_arg; } //@ move to a CIPR_Matrix file int CIPR_AllocMatrix(void *pA, size_t elSize, unsigned nrows, unsigned ncols) { char **a; void **pblock; assert(pA != NULL); assert(elSize != 0); assert(nrows != 0); /* Allocate array of row pointers */ a = (char **)calloc(nrows, sizeof(void *)); if (a == NULL) return 1; pblock = (void **)pA; *pblock = a; if (ncols != 0) { /* Allocate block for data and set pointers to each matrix row */ a[0] = (char *)calloc(nrows*ncols, elSize); if (a[0] == NULL) return 1; (void)SetMatrixRowPtrs(a, a[0], nrows, ncols, elSize); } return 0; } const char * datatypeEnumToString(const CIPR_Datatypes d) { switch(d) { case CIPR_DNA_Datatype: return "DNA"; case CIPR_RNA_Datatype: return "RNA"; case CIPR_AA_Datatype: return "AA"; case CIPR_Codon_Datatype: return "Codon"; case CIPR_Generic_Datatype: return "Generic"; default: assert(0); return NULL; } } #if defined(OLD_CIPRES_MATRIX) void showMatrix(const CIPR_Matrix matrix, int m, int n) { int i, j; char *dna = "ACGT"; printf("Data matrix\n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) printf("%c", dna[(int)matrix[i][j]]); printf("\n"); } } #else void showMatrix(const CIPR_Matrix matrix) { unsigned i, j, k; int index; const int nSymbols = strlen(matrix.symbolsList); printf("Data matrix\n"); printf(" type = %s\n", datatypeEnumToString(matrix.datatype)); printf(" symbols = %s\n", matrix.symbolsList); printf(" nStates = %d\n", matrix.nStates); printf(" nChar = %d\n", matrix.nChar); printf(" nTax = %d\n", matrix.nTax); printf(" %d different state sets were observed:\n", matrix.nObservedStateSets); for (i = 0; i < matrix.nObservedStateSets; ++i) { const int stateListInd = matrix.stateListPos[i]; const unsigned nAmbigElements = matrix.stateList[stateListInd]; printf("\tstateListPos[%d] = %d\tstateList[%d] = %d\n", i, stateListInd, stateListInd, nAmbigElements); for (j = 1 ; j <= nAmbigElements; ++j) printf("\t\t\t\t\tstateList[%d] = %d\n", stateListInd + j, matrix.stateList[stateListInd + j]); } printf("Matrix:\n"); for (i = 0; i < matrix.nTax; ++i) { printf("%d\t", i); for (j = 0; j < matrix.nChar; ++j) printf("%2d", (int)matrix.matrix[i][j]); printf("\n"); } printf("Decoded Matrix:\n"); for (i = 0; i < matrix.nTax; ++i) { printf("%d\t", i); for (j = 0; j < matrix.nChar; ++j) { index = matrix.matrix[i][j]; if (index >= nSymbols || matrix.symbolsList[index] == ' ') { const int stateListInd = matrix.stateListPos[j]; const int nSymbols = (int)matrix.stateList[stateListInd]; assert(nSymbols >= 0); printf("{"); for (k = 0; k < (unsigned)nSymbols; ++k) printf("%c", matrix.symbolsList[matrix.stateList[stateListInd + 1 + k]]); printf("}"); } else printf("%c", matrix.symbolsList[index]); } printf("\n"); } } #endif #ifdef __cplusplus } /*extern C */ #endif