void matrix_multiply_4(float result[4][4], float first[4][4], float second[4][4]) { float tmp1[4][4], tmp2[4][4]; // so you can apply the thing to the same goddamn matrix duplicate_matrix(tmp1, first); duplicate_matrix(tmp2, second); transpose_4(tmp2); for (int i = 0 ; i < 4 ; i++) { for (int j = 0 ; j < 4 ; j++) { result[i][j] = dot_product(tmp1[i], tmp2[j]); } } }
/******************************************************************************* * float matrix_determinant(matrix_t A) * * *******************************************************************************/ float matrix_determinant(matrix_t A){ int i,j,k; float ratio, det; if(!A.initialized){ printf("ERROR: matrix not initialized yet\n"); return -1; } if (A.rows != A.cols){ printf("Error: Matrix is not square\n"); return -1; } matrix_t temp = duplicate_matrix(A); for(i=0;i<A.rows;i++){ for(j=0;j<A.rows;j++){ if(j>i){ ratio = temp.data[j][i]/temp.data[i][i]; for(k=0;k<A.rows;k++){ temp.data[j][k] = temp.data[j][k] - ratio * temp.data[i][k]; } } } } det = 1; //storage for determinant for(i=0;i<A.rows;i++) det = det*temp.data[i][i]; destroy_matrix(&temp); return det; }
/*********************************************************************** * Allocate memory for a transfac motif ***********************************************************************/ TRANSFAC_MOTIF_T *new_transfac_motif( const char *accession, const char *id, const char *name, const char *description, const char *consensus, STRING_LIST_T *species_list, MATRIX_T *counts ) { TRANSFAC_MOTIF_T *motif = mm_malloc(sizeof(TRANSFAC_MOTIF_T)); motif->accession = NULL; motif->id = NULL; motif->name = NULL; motif->description = NULL; motif->species_list = NULL; motif->consensus = NULL; motif->counts = NULL; copy_string(&motif->accession, accession); copy_string(&motif->id, id); copy_string(&motif->name, name); copy_string(&motif->description, description); copy_string(&motif->consensus, consensus); if (species_list != NULL) { motif->species_list = copy_string_list(species_list); } if (counts != NULL) { motif->counts = duplicate_matrix(counts); } return motif; }
/******************************************************************************* * Return matrix of TRANSFAC counts. * Caller is responsible for freeing the MATRIX_T. ******************************************************************************/ MATRIX_T *get_transfac_counts(TRANSFAC_MOTIF_T *motif) { MATRIX_T *counts = NULL; if (motif->counts != NULL) { counts = duplicate_matrix(motif->counts); } return counts; }
/*********************************************************************** * allocate memory for a motif ***********************************************************************/ MOTIF_T* allocate_motif(char *id, MATRIX_T* freqs){ MOTIF_T* motif = mm_malloc(sizeof(MOTIF_T)); assert(id != NULL); assert(freqs != NULL); motif->length = 0; motif->alph_size = 0; motif->ambigs = 0; motif->evalue = 0.0; motif->num_sites = 0.0; motif->complexity = 0.0; motif->freqs = duplicate_matrix(freqs); motif->scores = NULL; // Set required fields. int length = strlen(id) + 1; strncpy(motif->id, id, min(length,MAX_MOTIF_ID_LENGTH)); motif->alph_size = get_num_cols(motif->freqs); motif->length = get_num_rows(motif->freqs); motif->url = NULL; motif->trim_left = 0; motif->trim_right = 0; return motif; }
void set_motif_freqs (MATRIX_T* freqs, MOTIF_T* motif){ // free existing matrix if (motif->freqs != NULL){ free_matrix(motif->freqs); } motif->freqs = duplicate_matrix(freqs); }
/*********************************************************************** * Allocate memory for a MEME motif ***********************************************************************/ MOTIF_T* allocate_motif( char *id, ALPH_T alph, MATRIX_T* scores, MATRIX_T* freqs ) { MOTIF_T* motif = mm_malloc(sizeof(MOTIF_T)); assert(id != NULL); if (freqs == NULL && scores == NULL) { die( "A matrix of scores, or frequencies, or both, " "must be provided when allocating a motif\n" ); } motif->length = get_num_rows(freqs); motif->alph = alph; motif->flags = 0; motif->evalue = 0.0; motif->log_evalue = -HUGE_VAL; motif->num_sites = 0.0; motif->complexity = 0.0; int length = strlen(id) + 1; strncpy(motif->id, id, MIN(length, MAX_MOTIF_ID_LENGTH)); if (scores != NULL) { motif->scores = duplicate_matrix(scores); } if (freqs != NULL) { motif->freqs = duplicate_matrix(freqs); } motif->url = NULL; motif->trim_left = 0; motif->trim_right = 0; return motif; }
/************************************************************************* * Copies the motif frequency matrix and converts it into a odds matrix *************************************************************************/ MATRIX_T* create_odds_matrix(MOTIF_T *motif, ARRAY_T* bg_freqs){ const int asize = alph_size(get_motif_alph(motif), ALPH_SIZE); int pos, aidx; MATRIX_T *odds; odds = duplicate_matrix(get_motif_freqs(motif)); const int num_pos = get_num_rows(odds); for (aidx = 0; aidx < asize; ++aidx) { double bg_likelihood = get_array_item(aidx, bg_freqs); for (pos = 0; pos < num_pos; ++pos) { odds->rows[pos]->items[aidx] /= bg_likelihood; } } return odds; }
/* * PURPOSE: run the commands which user entered * INPUTS: * cmd double pointer that holds all commands * mats the matrix list * num_mats the number of matrix in the list * RETURN: void * If no errors occurred during process then return nothing * else print error message **/ void run_commands (Commands_t* cmd, Matrix_t** mats, unsigned int num_mats) { //TODO ERROR CHECK INCOMING PARAMETERS if(!cmd){ printf("commands array is null\n"); return; } if(!(*mats)){ printf("matrix list is null\n"); return; } /*Parsing and calling of commands*/ if (strncmp(cmd->cmds[0],"display",strlen("display") + 1) == 0 && cmd->num_cmds == 2) { /*find the requested matrix*/ int idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); if (idx >= 0) { display_matrix (mats[idx]); } else { printf("Matrix (%s) doesn't exist\n", cmd->cmds[1]); return; } } else if (strncmp(cmd->cmds[0],"add",strlen("add") + 1) == 0 && cmd->num_cmds == 4) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]); if (mat1_idx >= 0 && mat2_idx >= 0) { Matrix_t* c = NULL; if( !create_matrix (&c,cmd->cmds[3], mats[mat1_idx]->rows, mats[mat1_idx]->cols)) { printf("Failure to create the result Matrix (%s)\n", cmd->cmds[3]); return; } if(add_matrix_to_array(mats,c, num_mats) == 999){ perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n"); return; } //TODO ERROR CHECK NEEDED if (! add_matrices(mats[mat1_idx], mats[mat2_idx],c) ) { printf("Failure to add %s with %s into %s\n", mats[mat1_idx]->name, mats[mat2_idx]->name,c->name); return; } } } else if (strncmp(cmd->cmds[0],"duplicate",strlen("duplicate") + 1) == 0 && cmd->num_cmds == 3 && strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); if (mat1_idx >= 0 ) { Matrix_t* dup_mat = NULL; if( !create_matrix (&dup_mat,cmd->cmds[2], mats[mat1_idx]->rows, mats[mat1_idx]->cols)) { return; } if(!duplicate_matrix (mats[mat1_idx], dup_mat)){ perror("PROGRAM FAILED TO DUPLICATE MATRIX\n"); return; } //TODO ERROR CHECK NEEDED if(add_matrix_to_array(mats,dup_mat,num_mats) == 999){ perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n"); return; } //TODO ERROR CHECK NEEDED printf ("Duplication of %s into %s finished\n", mats[mat1_idx]->name, cmd->cmds[2]); } else { printf("Duplication Failed\n"); return; } } else if (strncmp(cmd->cmds[0],"equal",strlen("equal") + 1) == 0 && cmd->num_cmds == 2) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]); if (mat1_idx >= 0 && mat2_idx >= 0) { if ( equal_matrices(mats[mat1_idx],mats[mat2_idx]) ) { printf("SAME DATA IN BOTH\n"); } else { printf("DIFFERENT DATA IN BOTH\n"); } } else { printf("Equal Failed\n"); return; } } else if (strncmp(cmd->cmds[0],"shift",strlen("shift") + 1) == 0 && cmd->num_cmds == 4) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); const int shift_value = atoi(cmd->cmds[3]); if (mat1_idx >= 0 ) { if(!bitwise_shift_matrix(mats[mat1_idx],cmd->cmds[2][0], shift_value)){ perror("PROGRAM FAILED TO SHIFT MATRIX\n"); return; } //TODO ERROR CHECK NEEDED printf("Matrix (%s) has been shifted by %d\n", mats[mat1_idx]->name, shift_value); } else { printf("Matrix shift failed\n"); return; } } else if (strncmp(cmd->cmds[0],"read",strlen("read") + 1) == 0 && cmd->num_cmds == 2) { Matrix_t* new_matrix = NULL; if(! read_matrix(cmd->cmds[1],&new_matrix)) { printf("Read Failed\n"); return; } if(add_matrix_to_array(mats,new_matrix, num_mats) == 999){ perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n"); return; } //TODO ERROR CHECK NEEDED printf("Matrix (%s) is read from the filesystem\n", cmd->cmds[1]); } else if (strncmp(cmd->cmds[0],"write",strlen("write") + 1) == 0 && cmd->num_cmds == 2) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); if(! write_matrix(mats[mat1_idx]->name,mats[mat1_idx])) { printf("Write Failed\n"); return; } else { printf("Matrix (%s) is wrote out to the filesystem\n", mats[mat1_idx]->name); } } else if (strncmp(cmd->cmds[0], "create", strlen("create") + 1) == 0 && strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN && cmd->num_cmds == 4) { Matrix_t* new_mat = NULL; const unsigned int rows = atoi(cmd->cmds[2]); const unsigned int cols = atoi(cmd->cmds[3]); if(!create_matrix(&new_mat,cmd->cmds[1],rows, cols)){ perror("PROGRAM FAILED TO ADD CREATE TO ARRAY\n"); return; } //TODO ERROR CHECK NEEDED if(add_matrix_to_array(mats,new_mat,num_mats) == 999){ perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n"); return; } // TODO ERROR CHECK NEEDED printf("Created Matrix (%s,%u,%u)\n", new_mat->name, new_mat->rows, new_mat->cols); } else if (strncmp(cmd->cmds[0], "random", strlen("random") + 1) == 0 && cmd->num_cmds == 4) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); const unsigned int start_range = atoi(cmd->cmds[2]); const unsigned int end_range = atoi(cmd->cmds[3]); if(!random_matrix(mats[mat1_idx],start_range, end_range)) { perror("PROGRAM FAILED TO RANDOMIZE MATRIX\n"); return; } //TODO ERROR CHECK NEEDED printf("Matrix (%s) is randomized between %u %u\n", mats[mat1_idx]->name, start_range, end_range); } else { printf("Not a command in this application\n"); } }
/** * Recursive function. While there is a significant position/residue pair, * will update pattern and return a count matrix for this pattern. */ MATRIX_T* add_to_pattern(char* pattern, ARRAYLST_T* phospho_seqs, ARRAYLST_T* bg_seqs, MOTIFX_STATUS_T** phospho_status, MOTIFX_STATUS_T** bg_status, int* num_active, int* num_bg_active, MATRIX_T* phospho_count, MATRIX_T* bg_count, double* motif_score, SUMMARY_T* summary, MOMO_OPTIONS_T* options) { int i; const char* alph_letters = summary->alph_letters; // Set binomial to bg count, normalize to frequencies, and then convert it into a binomial matrix MATRIX_T* binomial = duplicate_matrix(bg_count); // at this point it is a bg count matrix normalize_rows(0.0, binomial); // at this point it is a bg freq matrix convert_bg_freqs_to_binomial(phospho_count, binomial, *num_active); // Find minimum, and update if passes thresholds. int row_idx = -1; int col_idx = -1; double minimum = NAN; find_most_significant_within_matrix(binomial, phospho_count, &row_idx, &col_idx, &minimum, options); // clean up binomial after usage. free_matrix(binomial); // If one of the position/residue pairs pass, update pattern and statuses, then try to add another character to the pattern. if (row_idx >= 0 && col_idx >= 0) { pattern[row_idx] = alph_letters[col_idx]; *motif_score = *motif_score - (minimum / log(10)); // remove sequences an update matrices for phospho and bg remove_sequences_and_update_matrix(alph_letters[col_idx], row_idx, phospho_seqs, phospho_status, num_active, phospho_count, summary, options); remove_sequences_and_update_matrix(alph_letters[col_idx], row_idx, bg_seqs, bg_status, num_bg_active, bg_count, summary, options); return add_to_pattern(pattern, phospho_seqs, bg_seqs, phospho_status, bg_status, num_active, num_bg_active, phospho_count, bg_count, motif_score, summary, options); } return phospho_count; }
/******************************************************************************* * vector_t lin_system_solve(matrix_t A, vector_t b) * * Returns the vector x that solves Ax=b * Thank you to Henry Guennadi Levkin for open sourcing this routine. *******************************************************************************/ vector_t lin_system_solve(matrix_t A, vector_t b){ float fMaxElem, fAcc; int nDim,i,j,k,m; vector_t xout = create_empty_vector(); if(!A.initialized || !b.initialized){ printf("ERROR: matrix or vector not initialized yet\n"); return xout; } if(A.cols != b.len){ printf("ERROR: matrix dimensions do not match\n"); return xout; } nDim = A.cols; xout = create_vector(nDim); matrix_t Atemp = duplicate_matrix(A); // duplicate the given matrix vector_t btemp = duplicate_vector(b); // duplicate the given vector for(k=0; k<(nDim-1); k++){ // base row of matrix // search of line with max element fMaxElem = fabs( Atemp.data[k][k]); m = k; for(i=k+1; i<nDim; i++){ if(fMaxElem < fabs(Atemp.data[i][k])){ fMaxElem = Atemp.data[i][k]; m = i; } } // permutation of base line (index k) and max element line(index m) if(m != k){ for(i=k; i<nDim; i++){ fAcc = Atemp.data[k][i]; Atemp.data[k][i] = Atemp.data[m][i]; Atemp.data[m][i] = fAcc; } fAcc = btemp.data[k]; btemp.data[k] = btemp.data[m]; btemp.data[m] = fAcc; } if(Atemp.data[k][k] == 0.0) return xout; // needs improvement !!! // triangulation of matrix with coefficients for(j=(k+1); j<nDim; j++){ // current row of matrix fAcc = - Atemp.data[j][k] / Atemp.data[k][k]; for(i=k; i<nDim; i++){ Atemp.data[j][i] = Atemp.data[j][i] + fAcc*Atemp.data[k][i] ; } // free member recalculation btemp.data[j] = btemp.data[j] + fAcc*btemp.data[k]; } } for(k=(nDim-1); k>=0; k--){ xout.data[k] = btemp.data[k]; for(i=(k+1); i<nDim; i++){ xout.data[k] -= (Atemp.data[k][i]*xout.data[i]); } xout.data[k] = xout.data[k] / Atemp.data[k][k]; } destroy_matrix(&Atemp); destroy_vector(&btemp); return xout; }
/******************************************************************************* * int QR_decomposition(matrix_t A, matrix_t* Q, matrix_t* R) * * *******************************************************************************/ int QR_decomposition(matrix_t A, matrix_t* Q, matrix_t* R){ int i, j, k, s; int m = A.rows; int n = A.cols; vector_t xtemp; matrix_t Qt, Rt, Qi, F, temp; if(!A.initialized){ printf("ERROR: matrix not initialized yet\n"); return -1; } destroy_matrix(Q); destroy_matrix(R); Qt = create_matrix(m,m); for(i=0;i<m;i++){ // initialize Qt as I Qt.data[i][i] = 1; } Rt = duplicate_matrix(A); // duplicate A to Rt for(i=0;i<n;i++){ // iterate through columns of A xtemp = create_vector(m-i); // allocate length, decreases with i for(j=i;j<m;j++){ // take col of -R from diag down xtemp.data[j-i] = -Rt.data[j][i]; } if(Rt.data[i][i] > 0) s = -1; // check the sign else s = 1; xtemp.data[0] += s*vector_norm(xtemp); // add norm to 1st element Qi = create_square_matrix(m); // initialize Qi F = create_square_matrix(m-i); // initialize shrinking householder_matrix F = householder_matrix(xtemp); // fill in Househodor for(j=0;j<i;j++){ Qi.data[j][j] = 1; // fill in partial I matrix } for(j=i;j<m;j++){ // fill in remainder (householder_matrix) for(k=i;k<m;k++){ Qi.data[j][k] = F.data[j-i][k-i]; } } // multiply new Qi to old Qtemp temp = duplicate_matrix(Qt); destroy_matrix(&Qt); Qt = multiply_matrices(Qi,temp); destroy_matrix(&temp); // same with Rtemp temp = duplicate_matrix(Rt); destroy_matrix(&Rt); Rt = multiply_matrices(Qi,temp); destroy_matrix(&temp); // free other allocation used in this step destroy_matrix(&Qi); destroy_matrix(&F); destroy_vector(&xtemp); } transpose_matrix(&Qt); *Q = Qt; *R = Rt; return 0; }
/* * PURPOSE: Runs various commands for the operations to be performed on the matrices * INPUTS: command array, matrix array, size of matrix array * RETURN: Nothing **/ void run_commands (Commands_t* cmd, Matrix_t** mats, unsigned int num_mats) { if(cmd == NULL || mats == NULL || num_mats <= 0) return; /*Parsing and calling of commands*/ if (strncmp(cmd->cmds[0],"display",strlen("display") + 1) == 0 && cmd->num_cmds == 2) { /*find the requested matrix*/ int idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); if (idx >= 0) { display_matrix (mats[idx]); } else { printf("Matrix (%s) doesn't exist\n", cmd->cmds[1]); return; } } else if (strncmp(cmd->cmds[0],"add",strlen("add") + 1) == 0 && cmd->num_cmds == 4) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]); if (mat1_idx >= 0 && mat2_idx >= 0) { Matrix_t* c = NULL; if( !create_matrix (&c,cmd->cmds[3], mats[mat1_idx]->rows, mats[mat1_idx]->cols)) { printf("Failure to create the result Matrix (%s)\n", cmd->cmds[3]); return; } if(add_matrix_to_array(mats,c, num_mats) == -1){ printf("Failed to add matrix to the array"); return; } if (! add_matrices(mats[mat1_idx], mats[mat2_idx],c) ) { printf("Failure to add %s with %s into %s\n", mats[mat1_idx]->name, mats[mat2_idx]->name,c->name); return; } } } else if (strncmp(cmd->cmds[0],"duplicate",strlen("duplicate") + 1) == 0 && cmd->num_cmds == 3 && strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); if (mat1_idx >= 0 ) { Matrix_t* dup_mat = NULL; if( !create_matrix (&dup_mat,cmd->cmds[2], mats[mat1_idx]->rows, mats[mat1_idx]->cols)) { return; } if(duplicate_matrix (mats[mat1_idx], dup_mat) == false){ printf("Duplication Failed\n"); return; } if(add_matrix_to_array(mats,dup_mat,num_mats) == -1){ printf("Failed to add matrix to array\n"); return; } printf ("Duplication of %s into %s finished\n", mats[mat1_idx]->name, cmd->cmds[2]); } else { printf("Duplication Failed\n"); return; } } else if (strncmp(cmd->cmds[0],"equal",strlen("equal") + 1) == 0 && cmd->num_cmds == 3) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]); if (mat1_idx >= 0 && mat2_idx >= 0) { if ( equal_matrices(mats[mat1_idx],mats[mat2_idx]) ) { printf("SAME DATA IN BOTH\n"); } else { printf("DIFFERENT DATA IN BOTH\n"); } } else { printf("Equal Failed\n"); return; } } else if (strncmp(cmd->cmds[0],"shift",strlen("shift") + 1) == 0 && cmd->num_cmds == 4) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); const int shift_value = atoi(cmd->cmds[3]); if (mat1_idx <= 0 ) { printf("Matrix shift failed\n"); return; } if(bitwise_shift_matrix(mats[mat1_idx],cmd->cmds[2][0], shift_value) == false){ printf("Matrix shift failed\n"); return; } else printf("Matrix (%s) has been shifted by %d\n", mats[mat1_idx]->name, shift_value); } else if (strncmp(cmd->cmds[0],"read",strlen("read") + 1) == 0 && cmd->num_cmds == 2) { Matrix_t* new_matrix = NULL; if(! read_matrix(cmd->cmds[1],&new_matrix)) { printf("Read Failed\n"); return; } if(add_matrix_to_array(mats,new_matrix, num_mats) == -1){ printf("Failed to add matrix to the array"); ; } printf("Matrix (%s) is read from the filesystem\n", cmd->cmds[1]); } else if (strncmp(cmd->cmds[0],"write",strlen("write") + 1) == 0 && cmd->num_cmds == 2) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); if(! write_matrix(mats[mat1_idx]->name,mats[mat1_idx])) { printf("Write Failed\n"); return; } else { printf("Matrix (%s) is wrote out to the filesystem\n", mats[mat1_idx]->name); } } else if (strncmp(cmd->cmds[0], "create", strlen("create") + 1) == 0 && strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN && cmd->num_cmds == 4) { Matrix_t* new_mat = NULL; const unsigned int rows = atoi(cmd->cmds[2]); const unsigned int cols = atoi(cmd->cmds[3]); if(create_matrix(&new_mat,cmd->cmds[1],rows, cols) == false){ printf("Failed to create the matrix"); return; } if(add_matrix_to_array(mats,new_mat,num_mats) == -1){ printf("Failed to add matrix to the array"); return; } printf("Created Matrix (%s,%u,%u)\n", new_mat->name, new_mat->rows, new_mat->cols); } else if (strncmp(cmd->cmds[0], "random", strlen("random") + 1) == 0 && cmd->num_cmds == 4) { int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]); const unsigned int start_range = atoi(cmd->cmds[2]); const unsigned int end_range = atoi(cmd->cmds[3]); if(random_matrix(mats[mat1_idx],start_range, end_range) == false){ printf("Failed to randomize\n"); return;//TODO ERROR CHECK NEEDED } printf("Matrix (%s) is randomized between %u %u\n", mats[mat1_idx]->name, start_range, end_range); } else { printf("Not a command in this application\n"); } }