/** * <EN> * Free an MFCC calculation instance. * </EN> * <JA> * MFCC計算インスタンスを開放する * </JA> * * @param mfcc [i/o] MFCC calculation instance * * @callgraph * @callergraph */ void j_mfcccalc_free(MFCCCalc *mfcc) { if (mfcc->rest_param) free_param(mfcc->rest_param); if (mfcc->param) free_param(mfcc->param); if (mfcc->wrk) WMP_free(mfcc->wrk); if (mfcc->tmpmfcc) free(mfcc->tmpmfcc); if (mfcc->db) WMP_deltabuf_free(mfcc->db); if (mfcc->ab) WMP_deltabuf_free(mfcc->ab); if (mfcc->cmn.wrk) CMN_realtime_free(mfcc->cmn.wrk); if (mfcc->frontend.ssbuf) free(mfcc->frontend.ssbuf); if (mfcc->frontend.mfccwrk_ss) WMP_free(mfcc->frontend.mfccwrk_ss); free(mfcc); }
/* Loads a boolean parameter */ int load_builtin_param_bool(const char * name, void * engine_val, short int flags, int init_val, const char * alt_name) { param_t * param; value_t iv, ub, lb; iv.int_val = init_val; ub.int_val = TRUE; lb.int_val = FALSE; param = create_param(name, P_TYPE_BOOL, flags, engine_val, NULL, iv, ub, lb); if (param == NULL) { return OUTOFMEM_ERROR; } if (insert_builtin_param(param) < 0) { free_param(param); return ERROR; } if (alt_name != NULL) { insert_param_alt_name(param, alt_name); } return SUCCESS; }
void url_coder::reset() { std::vector<URL_NV*>::iterator it = params_.begin(); for (; it != params_.end(); ++it) free_param(*it); params_.clear(); buf_->clear(); }
int main(){ const int N=5, L=10; int i,j; double alpha=0.5, tau=2.0; /* create a list of sequences */ struct list_dnaseq * test = create_list_dnaseq(N, L); for(i=0;i<N;i++){ for(j=0;j<L;j++){ if(i*j % 5 ==0) test->list[i]->seq[j] = 'a'; else if(i*j % 3 ==0)test->list[i]->seq[j] = 't'; else if(i*j % 2 ==0)test->list[i]->seq[j] = 'g'; else test->list[i]->seq[j] = 'c'; } } for(i=5;i<L;i++) test->list[0]->seq[i] = '-'; for(i=0;i<5;i++) test->list[N-1]->seq[i] = '-'; print_list_dnaseq(test); /* COMPUTE DISTANCES */ struct dna_dist *distinfo = compute_dna_distances(test); print_dna_dist(distinfo); /* COMPUTE LIKELIHOOD */ struct param *par = create_param(); double out=0, nu1 = 0.01, nu2=0.02, t_vec[5]={0.0, 10.0, 12.0, 50.0, 100.0}, T; int count=0; par->weightNaGen = 0.001; /* near zero if no data */ for(i=0;i<N-1;i++){ for(j=i+1;j<N;j++){ T = t_vec[i]>t_vec[j] ? t_vec[i] - t_vec[j] : t_vec[j]-t_vec[i]; printf("\npair %d <-> %d:",i+1,j+1); printf("\nnb transi %d:", get_transi(distinfo,i,j)); printf(" (esperance: %.1f)", T*nu1*get_nbcommon(distinfo,i,j)); printf("\nnb transv %d:", get_transv(distinfo,i,j)); printf(" (esperance: %.1f)", T*nu2*get_nbcommon(distinfo,i,j)); out = compute_genlike(i, j, t_vec[i], t_vec[j], nu1, nu2, alpha, tau, distinfo, par); printf("\npseudo-likelihood %d <-> %d: %.5f\n",i+1,j+1, out); } } printf("\n"); /* free and return */ free_list_dnaseq(test); free_dna_dist(distinfo); free_param(par); return 0; }
/* Loads a double parameter into the builtin database */ int load_builtin_param_double(const char * name, void * engine_val, void * matrix, short int flags, double init_val, double upper_bound, double lower_bound, const char * alt_name) { param_t * param = NULL; value_t iv, ub, lb; iv.double_val = init_val; ub.double_val = upper_bound; lb.double_val = lower_bound; /* Create new parameter of type double */ if (PARAM_DEBUG == 2) { printf("load_builtin_param_double: (name \"%s\") (alt_name = \"%s\") ", name, alt_name); fflush(stdout); } if ((param = create_param(name, P_TYPE_DOUBLE, flags, engine_val, matrix, iv, ub, lb)) == NULL) { return OUTOFMEM_ERROR; } if (PARAM_DEBUG == 2) { printf("created..."); fflush(stdout); } /* Insert the paremeter into the database */ if (insert_builtin_param(param) < 0) { free_param(param); return ERROR; } if (PARAM_DEBUG == 2) { printf("inserted..."); fflush(stdout); } /* If this parameter has an alternate name, insert it into the database as link */ if (alt_name != NULL) { insert_param_alt_name(param, alt_name); if (PARAM_DEBUG == 2) { printf("alt_name inserted..."); fflush(stdout); } } if (PARAM_DEBUG == 2) printf("finished\n"); /* Finished, return success */ return SUCCESS; }
/* Find a parameter given its name, will create one if not found */ param_t * find_param(char * name, preset_t * preset, int flags) { param_t * param = NULL; /* Null argument checks */ if (name == NULL) return NULL; if (preset == NULL) return NULL; /* First look in the builtin database */ param = (param_t *)splay_find(name, builtin_param_tree); /* If the search failed, check the user database */ if (param == NULL) { param = (param_t*)splay_find(name, preset->user_param_tree); } /* If it doesn't exist in the user (or builtin) database and create_flag is set, then make it and insert into the database */ if ((param == NULL) && (flags & P_CREATE)) { /* Check if string is valid */ if (!is_valid_param_string(name)) { if (PARAM_DEBUG) printf("find_param: invalid parameter name:\"%s\"\n", name); return NULL; } /* Now, create the user defined parameter given the passed name */ if ((param = create_user_param(name)) == NULL) { if (PARAM_DEBUG) printf("find_param: failed to create a new user parameter!\n"); return NULL; } /* Finally, insert the new parameter into this preset's proper splaytree */ if (splay_insert(param, param->name, preset->user_param_tree) < 0) { if (PARAM_DEBUG) printf("PARAM \"%s\" already exists in user parameter tree!\n", param->name); free_param(param); return NULL; } } /* Return the found (or created) parameter. Note that if P_CREATE is not set, this could be null */ return param; }
/* * Figure out what kind of data we've got and do something about it. */ void got_packet(struct tcp_stream *ts, void **data) { int todiscard=0; struct http_param *param=NULL; switch (ts->nids_state) { case NIDS_JUST_EST: ts->server.collect = 1; ts->client.collect = 1; param=calloc(sizeof(struct http_param), 1); assert(param); param->host=strdup(my_ntoa(ts->addr.saddr)); *data=param; break; case NIDS_DATA: if(ts->server.count_new!=0) { todiscard=process(ts, SERVER_STREAM, ts->server, data); } if(ts->client.count_new!=0) { todiscard=process(ts, CLIENT_STREAM, ts->client, data); } break; case NIDS_CLOSE: case NIDS_RESET: case NIDS_TIMED_OUT: param=*data; if(param) { /* Log automatically frees all the internal state */ log_request(param); free_param(param); free(param); } break; } nids_discard(ts, todiscard); }
/* Search for parameter 'name' in 'database', if create_flag is true, then generate the parameter and insert it into 'database' */ param_t * find_param_db(char * name, splaytree_t * database, int create_flag) { param_t * param = NULL; /* Null argument checks */ if (name == NULL) return NULL; if (database == NULL) return NULL; /* First look in the builtin database */ param = (param_t *)splay_find(name, database); if (((param = (param_t *)splay_find(name, database)) == NULL) && (create_flag == TRUE)) { /* Check if string is valid */ if (!is_valid_param_string(name)) return NULL; /* Now, create the user defined parameter given the passed name */ if ((param = create_user_param(name)) == NULL) return NULL; /* Finally, insert the new parameter into this preset's proper splaytree */ if (splay_insert(param, param->name, database) < 0) { free_param(param); return NULL; } } /* Return the found (or created) parameter. Note that this could be null */ return param; }
int main(int argc, char **argv) { vcedit_state *state; vorbis_comment *vc; param_t *param; int i; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); /* initialize the cmdline interface */ param = new_param(); parse_options(argc, argv, param); /* take care of opening the requested files */ /* relevent file pointers are returned in the param struct */ open_files(param); /* which mode are we in? */ if (param->mode == MODE_LIST) { state = vcedit_new_state(); if(vcedit_open(state, param->in) < 0) { fprintf(stderr, _("Failed to open file as vorbis: %s\n"), vcedit_error(state)); close_files(param); free_param(param); vcedit_clear(state); return 1; } /* extract and display the comments */ vc = vcedit_comments(state); print_comments(param->com, vc, param->raw); /* done */ vcedit_clear(state); close_files(param); free_param(param); return 0; } if (param->mode == MODE_WRITE || param->mode == MODE_APPEND) { state = vcedit_new_state(); if(vcedit_open(state, param->in) < 0) { fprintf(stderr, _("Failed to open file as vorbis: %s\n"), vcedit_error(state)); close_files(param); free_param(param); vcedit_clear(state); return 1; } /* grab and clear the exisiting comments */ vc = vcedit_comments(state); if(param->mode != MODE_APPEND) { vorbis_comment_clear(vc); vorbis_comment_init(vc); } for(i=0; i < param->commentcount; i++) { if(add_comment(param->comments[i], vc, param->raw) < 0) fprintf(stderr, _("Bad comment: \"%s\"\n"), param->comments[i]); } /* build the replacement structure */ if(param->commentcount==0) { /* FIXME should use a resizeable buffer! */ char *buf = (char *)malloc(sizeof(char)*1024); while (fgets(buf, 1024, param->com)) if (add_comment(buf, vc, param->raw) < 0) { fprintf(stderr, _("bad comment: \"%s\"\n"), buf); } free(buf); } /* write out the modified stream */ if(vcedit_write(state, param->out) < 0) { fprintf(stderr, _("Failed to write comments to output file: %s\n"), vcedit_error(state)); close_files(param); free_param(param); vcedit_clear(state); return 1; } /* done */ vcedit_clear(state); close_files(param); free_param(param); return 0; } /* should never reach this point */ fprintf(stderr, _("no action specified\n")); free_param(param); return 1; }
int write_ogg_tag(const string fname, struct fennec_audiotag *wtag) { vcedit_state *state; vorbis_comment *vc; param_t *param; FILE *tfile; FILE *of; string outname; struct fennec_audiotag_item *ct; if(!fname || !wtag)return -3; setlocale(LC_ALL, ""); /* initialize the cmdline interface */ param = new_param(); tfile = _wfsopen(fname, uni("r+b"), _SH_DENYRW); if(!tfile) { MessageBox(0, uni("Access denied, please stop playback and try again (you don't need to close this window)."), uni("Tag Editing"), MB_ICONINFORMATION); return -1; } state = vcedit_new_state(); if(vcedit_open(state, tfile) < 0) { fclose(tfile); free_param(param); vcedit_clear(state); return -2; } vc = vcedit_comments(state); ct = &wtag->tag_title; if(ct->tsize)local_addcomment(vc, "TITLE", ct->tdata); else local_addcomment(vc, "TITLE", uni("") ); ct = &wtag->tag_album; if(ct->tsize)local_addcomment(vc, "ALBUM", ct->tdata); else local_addcomment(vc, "ALBUM", uni("") ); ct = &wtag->tag_artist; if(ct->tsize)local_addcomment(vc, "ARTIST", ct->tdata); else local_addcomment(vc, "ARTIST", uni("") ); ct = &wtag->tag_origartist; if(ct->tsize)local_addcomment(vc, "ORIGINALARTIST", ct->tdata); else local_addcomment(vc, "ORIGINALARTIST", uni("") ); ct = &wtag->tag_composer; if(ct->tsize)local_addcomment(vc, "COMPOSER", ct->tdata); else local_addcomment(vc, "COMPOSER", uni("") ); ct = &wtag->tag_lyricist; if(ct->tsize)local_addcomment(vc, "LYRICIST", ct->tdata); else local_addcomment(vc, "LYRICIST", uni("") ); ct = &wtag->tag_band; if(ct->tsize)local_addcomment(vc, "BANDNAME", ct->tdata); else local_addcomment(vc, "BANDNAME", uni("") ); ct = &wtag->tag_copyright; if(ct->tsize)local_addcomment(vc, "COPYRIGHT", ct->tdata); else local_addcomment(vc, "COPYRIGHT", uni("") ); ct = &wtag->tag_publish; if(ct->tsize)local_addcomment(vc, "PUBLISHER", ct->tdata); else local_addcomment(vc, "PUBLISHER", uni("") ); ct = &wtag->tag_encodedby; if(ct->tsize)local_addcomment(vc, "ENCODEDBY", ct->tdata); else local_addcomment(vc, "ENCODEDBY", uni("") ); ct = &wtag->tag_genre; if(ct->tsize)local_addcomment(vc, "GENRE", ct->tdata); else local_addcomment(vc, "GENRE", uni("") ); ct = &wtag->tag_year; if(ct->tsize)local_addcomment(vc, "YEAR", ct->tdata); else local_addcomment(vc, "YEAR", uni("") ); ct = &wtag->tag_url; if(ct->tsize)local_addcomment(vc, "URL", ct->tdata); else local_addcomment(vc, "URL", uni("") ); ct = &wtag->tag_offiartisturl; if(ct->tsize)local_addcomment(vc, "OFFICIALARTISTURL", ct->tdata); else local_addcomment(vc, "OFFICIALARTISTURL", uni("") ); ct = &wtag->tag_comments; if(ct->tsize)local_addcomment(vc, "COMMENT", ct->tdata); else local_addcomment(vc, "COMMENT", uni("") ); ct = &wtag->tag_lyric; if(ct->tsize)local_addcomment(vc, "LYRIC", ct->tdata); else local_addcomment(vc, "LYRIC", uni("") ); ct = &wtag->tag_bpm; if(ct->tsize)local_addcomment(vc, "BPM", ct->tdata); else local_addcomment(vc, "BPM", uni("") ); ct = &wtag->tag_tracknum; if(ct->tsize)local_addcomment(vc, "TRACKNUMBER", ct->tdata); else local_addcomment(vc, "TRACKNUMBER", uni("") ); outname = (string) malloc(str_size(fname) + (5 * sizeof(letter))); str_cpy(outname, fname); str_cat(outname, uni(".tmp")); of = _wfopen(outname, uni("wb")); if(vcedit_write(state, of) < 0) { fclose(of); fclose(tfile); free_param(param); vcedit_clear(state); free(outname); return 1; } fclose(of); /* done */ vcedit_clear(state); fclose(tfile); free_param(param); _wremove(fname); sys_sleep(0); _wrename(outname, fname); free(outname); return 0; }
struct alignment* detect_and_read_sequences(struct alignment* aln,struct parameters* param) { int feature = 0; char **input = 0; unsigned short int* input_type = 0; unsigned short int* input_numseq = 0; int num_input = 0; int i = 0; int j = 0; int c = 0; int a,b; int free_read = 1; unsigned int numseq = get_kalign_context()->numseq; while(free_read == 1 || param->infile[i]){ num_input++; i++; free_read = 0; } numseq = 0; input = malloc(sizeof(char*) * num_input); input_type = malloc(sizeof(unsigned short int) * num_input); input_numseq = malloc(sizeof(unsigned short int) * num_input); for (i = 0; i < num_input;i++){ input[i] = 0; input_type[i] = 0; input_numseq[i] = 0; } free_read = 0; if(param->quiet){ c = 1; }else{ c = 0; } for (i = c; i < num_input;i++){ if(!param->infile[i]){ k_printf("reading from STDIN: "); }else{ k_printf("reading from %s: ",param->infile[i]); } input[i] = get_input_into_string(input[i],param->infile[i]); if(input[i]){ free_read++; if (byg_start("<macsim>",input[i]) != -1){ input_numseq[i] = count_sequences_macsim(input[i]); feature = 1; input_type[i] = 1; }else if (byg_start("<uniprot",input[i]) != -1){ input_numseq[i] = count_sequences_uniprot(input[i]); input_type[i] = 2; }else if(byg_start("This SWISS-PROT",input[i]) != -1){ input_numseq[i] = count_sequences_swissprot(input[i]); input_type[i] = 3; }else if (byg_start("This Swiss-Prot",input[i]) != -1){ input_numseq[i] = count_sequences_swissprot(input[i]); input_type[i] = 3; }else if (byg_start("CLUSTAL W",input[i]) != -1){ input_numseq[i] = count_sequences_clustalw(input[i]); input_type[i] = 4; }else if (byg_start("PileUp",input[i]) != -1){ input_numseq[i] = count_sequences_clustalw(input[i]); input_type[i] = 4; }else if (byg_start("MSF:",input[i]) != -1){ input_numseq[i] = count_sequences_clustalw(input[i]); input_type[i] = 4; }else if (byg_start("STOCKHOLM",input[i]) != -1){ input_numseq[i] = count_sequences_stockholm(input[i]); input_type[i] = 5; }else{ input_numseq[i] = count_sequences_fasta(input[i]); input_type[i] = 0; } k_printf("found %d sequences\n",input_numseq[i]); if(input_numseq[i] < 1){ free(input[i]); input[i] = 0; }else{ numseq += input_numseq[i]; } }else{ k_printf("found no sequences.\n"); if(!param->outfile && i){ param->outfile = param->infile[i]; k_printf("-> output file, in "); //try to set format.... if(!param->format){ if (byg_start("msf",param->outfile) != -1){ param->format = "msf"; }else if (byg_start("clustal",param->outfile) != -1){ param->format = "clustal"; }else if (byg_start("aln",param->outfile) != -1){ param->format = "clustal"; }else if (byg_start("macsim",param->outfile) != -1){ param->format = "macsim"; }else{ param->format = "fasta"; } if(param->reformat){ k_printf("unaligned fasta format\n"); }else if(param->format){ k_printf("%s format\n",param->format); }else{ k_printf("fasta format\n"); } } } k_printf("\n"); } } if(numseq < 2){ k_printf("%s\n", usage); if(!numseq){ k_printf("\nWARNING: No sequences found.\n\n"); }else{ k_printf("\nWARNING: Only one sequence found.\n\n"); } for (i = 0; i < num_input;i++){ free(input[i]); } free(input_numseq); free(input_type); free(input); free_param(param); exit(0); } if(byg_start(param->alignment_type,"profPROFprofilePROFILE") != -1){ if( free_read < 2){ k_printf("\nWARNING: You are trying to perform a profile - profile alignment but ony one input file was detected.\n\n"); param->alignment_type = "default"; } } if (param->feature_type && !feature){ for (i = 0; i < num_input;i++){ free(input[i]); } free(input_numseq); free(input_type); free(input); free_param(param); throwKalignException(k_printf("\nWARNING: You are trying to perform a feature alignment but the input format(s) do not contain feature information.\n")); } get_kalign_context()->numprofiles = (numseq << 1) - 1; aln = aln_alloc(aln); //numseq = 0; if(byg_start(param->alignment_type,"profPROFprofilePROFILE") != -1){ j = 0; for (i = 0; i < num_input;i++){ if(input[i]){ switch(input_type[i]){ case 0: aln = read_alignment(aln,input[i]); break; case 1: aln = read_alignment_macsim_xml(aln,input[i]); break; case 2: aln = read_alignment_uniprot_xml(aln,input[i]); break; case 3: aln = read_alignment_from_swissprot(aln, input[i]); break; case 4: aln = read_alignment_clustal(aln,input[i]); break; case 5: aln = read_alignment_stockholm(aln,input[i]); break; default: aln = read_alignment(aln,input[i]); break; } input[i] = 0; //create partial profile.... aln->nsip[numseq+j] = input_numseq[i]; aln->sip[numseq+j] = malloc(sizeof(int)*aln->nsip[numseq+j]); //k_printf("%d %d\n",numseq+j,aln->sl[numseq+j]); j++; } } num_input = j; c = 0; for (i = 0;i < num_input;i++){ // for ( j = 0; j < aln->nsip[numseq+i];j++){ aln->sip[numseq+i][j] = c; c++; // k_printf("%d ",aln->sip[numseq+i][j]); } aln->sl[numseq+i] = aln->sl[aln->sip[numseq+i][0]]; // k_printf("PROFILE:%d contains: %d long:%d\n",i+numseq,aln->nsip[numseq+i],aln->sl[numseq+i]); // k_printf("\n"); } //sanity check -are all input for (i = 0;i < num_input;i++){ for ( j = 0; j < aln->nsip[numseq+i]-1;j++){ a = aln->sip[numseq+i][j]; a = aln->sl[a]; for (c = j+1; j < aln->nsip[numseq+i];j++){ b = aln->sip[numseq+i][c]; b = aln->sl[b]; if(a != b){ for (i = 0; i < num_input;i++){ free(input[i]); } free(input_numseq); free(input_type); free(input); free_aln(aln); free_param(param); throwKalignException(k_printf("Unaligned sequences in input %s.\n",param->infile[i])); } } } } //exit(0); /*for (i = 0; i < numseq;i++){ k_printf("len%d:%d\n",i,aln->sl[i]); for ( j =0 ; j < aln->sl[i];j++){ //if(aln->s[i][j]> 23 || aln->s[i][j] < 0){ // aln->s[i][j] = -1; //} k_printf("%d ",aln->s[i][j]); } // k_printf("\n"); } exit(0);*/ }else{ for (i = 0; i < num_input;i++){ if(input[i]){ switch(input_type[i]){ case 0: aln = read_sequences(aln,input[i]); break; case 1: aln = read_sequences_macsim_xml(aln,input[i]); break; case 2: aln = read_sequences_uniprot_xml(aln,input[i]); break; case 3: aln = read_sequences_from_swissprot(aln, input[i]); break; case 4: aln = read_sequences_clustal(aln,input[i]); break; case 5: aln = read_sequences_stockholm(aln,input[i]); break; default: aln = read_sequences(aln,input[i]); break; } /*if (byg_start("<macsim>",input[i]) != -1){ aln = read_sequences_macsim_xml(aln,input[i]); }else if (byg_start("<uniprot",input[i]) != -1){ aln = read_sequences_uniprot_xml(aln,input[i]); }else if(byg_start("This SWISS-PROT entry is copyright.",input[i]) != -1){ aln = read_sequences_from_swissprot(aln, input[i]); }else if (byg_start("This Swiss-Prot entry is copyright.",input[i]) != -1){ aln = read_sequences_from_swissprot(aln, input[i]); }else if (byg_start("CLUSTAL W",input[i]) != -1){ aln = read_sequences_clustal(aln,input[i]); }else if (byg_start("PileUp",input[i]) != -1){ aln = read_sequences_clustal(aln,input[i]); }else if (byg_start("MSF:",input[i]) != -1){ aln = read_sequences_clustal(aln,input[i]); }else if (byg_start("STOCKHOLM",input[i]) != -1){ aln = read_sequences_stockholm(aln,input[i]); }else{ aln = read_sequences(aln,input[i]); }*/ input[i] = 0; } } } if(numseq < 2){ free_param(param); throwKalignException(k_printf("\nNo sequences could be read.\n")); } if(!param->format && param->outfile){ if (byg_start("msf",param->outfile) != -1){ param->format = "msf"; }else if (byg_start("clustal",param->outfile) != -1){ param->format = "clustal"; }else if (byg_start("aln",param->outfile) != -1){ param->format = "clustal"; }else if (byg_start("macsim",param->outfile) != -1){ param->format = "macsim"; } k_printf("Output file: %s, in %s format.\n",param->outfile,param->format); } free(input); free(input_type); free(input_numseq); return aln; }
/* Function to be called from R */ void R_epidemics(int *seqLength, double *mutRate, int *npop, int *nHostPerPop, double *beta, int *nStart, int *t1, int *t2, int *Nsample, int *Tsample, int *duration, int *nbnb, int *listnb, double *pdisp){ int i, nstep, counter_sample = 0, tabidx; /* Initialize random number generator */ int j; time_t t; t = time(NULL); // time in seconds, used to change the seed of the random generator gsl_rng * rng; const gsl_rng_type *typ; gsl_rng_env_setup(); typ=gsl_rng_default; rng=gsl_rng_alloc(typ); gsl_rng_set(rng,t); // changes the seed of the random generator /* transfer simulation parameters */ struct param * par; par = (struct param *) malloc(sizeof(struct param)); par->L = *seqLength; par->mu = *mutRate; par->muL = par->mu * par->L; par->rng = rng; par->npop = *npop; par->popsizes = nHostPerPop; par->beta = *beta; par->nstart = *nStart; par->t1 = *t1; par->t2 = *t2; par->t_sample = Tsample; par->n_sample = *Nsample; par->duration = *duration; par->cn_nb_nb = nbnb; par->cn_list_nb = listnb; par->cn_weights = pdisp; /* check/print parameters */ check_param(par); print_param(par); /* dispersal matrix */ struct network *cn = create_network(par); /* print_network(cn, TRUE); */ /* group sizes */ struct ts_groupsizes * grpsizes = create_ts_groupsizes(par); /* initiate population */ struct metapopulation * metapop; metapop = create_metapopulation(par); /* get sampling schemes (timestep+effectives) */ translate_dates(par); struct table_int *tabdates = get_table_int(par->t_sample, par->n_sample); printf("\n\nsampling at timesteps:"); print_table_int(tabdates); /* create sample */ struct sample ** samplist = (struct sample **) malloc(tabdates->n * sizeof(struct sample *)); struct sample *samp; /* MAKE METAPOPULATION EVOLVE */ nstep = 0; while(get_total_nsus(metapop)>0 && (get_total_ninf(metapop)+get_total_nexp(metapop))>0 && nstep<par->duration){ nstep++; /* age metapopulation */ age_metapopulation(metapop, par); /* process infections */ for(j=0;j<get_npop(metapop);j++){ process_infections(get_populations(metapop)[j], metapop, cn, par); } /* draw samples */ if((tabidx = int_in_vec(nstep, tabdates->items, tabdates->n)) > -1){ /* TRUE if step must be sampled */ samplist[counter_sample++] = draw_sample(metapop, tabdates->times[tabidx], par); } fill_ts_groupsizes(grpsizes, metapop, nstep); } /* we stopped after 'nstep' steps */ if(nstep < par->duration){ printf("\nEpidemics ended at time %d, before last sampling time (%d).\n", nstep, par->duration); } else { /* printf("\n\n-- FINAL METAPOPULATION --"); */ /* print_metapopulation(metapop, FALSE); */ /* merge samples */ samp = merge_samples(samplist, tabdates->n, par); /* write sample to file */ printf("\n\nWriting sample to file 'out-sample.txt'\n"); write_sample(samp); /* free memory */ free_sample(samp); } /* write group sizes to file */ printf("\n\nPrinting group sizes to file 'out-popsize.txt'\n"); write_ts_groupsizes(grpsizes); /* free memory */ free_metapopulation(metapop); free_param(par); for(i=0;i<counter_sample;i++) free_sample(samplist[i]); free(samplist); free_table_int(tabdates); free_network(cn); free_ts_groupsizes(grpsizes); }
/* all-in-one function testing epidemics growth, summary statistics, etc. */ void test_epidemics(int seqLength, double mutRate, int npop, int *nHostPerPop, double beta, int nStart, int t1, int t2, int Nsample, int *Tsample, int duration, int *nbnb, int *listnb, double *pdisp){ int i, j, nstep=0, tabidx, counter_sample = 0; /* Initialize random number generator */ time_t t; t = time(NULL); // time in seconds, used to change the seed of the random generator gsl_rng * rng; const gsl_rng_type *typ; gsl_rng_env_setup(); typ=gsl_rng_default; rng=gsl_rng_alloc(typ); gsl_rng_set(rng,t); // changes the seed of the random generator /* transfer simulation parameters */ struct param * par; par = (struct param *) malloc(sizeof(struct param)); par->L = seqLength; par->mu = mutRate; par->muL = par->mu * par->L; par->rng = rng; par->npop = npop; par->npop = npop; par->popsizes = nHostPerPop; par->beta = beta; par->nstart = nStart; par->t1 = t1; par->t2 = t2; par->t_sample = Tsample; par->n_sample = Nsample; par->duration = duration; par->cn_nb_nb = nbnb; par->cn_list_nb = listnb; par->cn_weights = pdisp; /* check/print parameters */ check_param(par); print_param(par); /* dispersal matrix */ struct network *cn = create_network(par); /* group sizes */ struct ts_groupsizes * grpsizes = create_ts_groupsizes(par); /* initiate population */ struct metapopulation * metapop; metapop = create_metapopulation(par); /* get sampling schemes (timestep+effectives) */ translate_dates(par); struct table_int *tabdates = get_table_int(par->t_sample, par->n_sample); printf("\n\nsampling at timesteps:"); print_table_int(tabdates); /* create sample */ struct sample ** samplist = (struct sample **) malloc(tabdates->n * sizeof(struct sample *)); struct sample *samp; /* MAKE METAPOPULATION EVOLVE */ nstep = 0; while(get_total_nsus(metapop)>0 && (get_total_ninf(metapop)+get_total_nexp(metapop))>0 && nstep<par->duration){ nstep++; /* age metapopulation */ age_metapopulation(metapop, par); /* process infections */ for(j=0;j<get_npop(metapop);j++){ process_infections(get_populations(metapop)[j], metapop, cn, par); } /* draw samples */ if((tabidx = int_in_vec(nstep, tabdates->items, tabdates->n)) > -1){ /* TRUE if step must be sampled */ samplist[counter_sample++] = draw_sample(metapop, tabdates->times[tabidx], par); } fill_ts_groupsizes(grpsizes, metapop, nstep); } /* we stopped after 'nstep' steps */ if(nstep < par->duration){ printf("\nEpidemics ended at time %d, before last sampling time (%d).\n", nstep, par->duration); } else { printf("\n\n-- FINAL METAPOPULATION --"); print_metapopulation(metapop, TRUE); /* test samples */ for(i=0;i<tabdates->n;i++) { printf("\nsample %d\n", i); print_sample(samplist[i], TRUE); } samp = merge_samples(samplist, tabdates->n, par) ; print_sample(samp, TRUE); /* test allele listing */ struct snplist *snpbilan; snpbilan = list_snps(samp, par); print_snplist(snpbilan); /* test allele frequencies */ struct allfreq *freq; freq = get_frequencies(samp, par); print_allfreq(freq); /* test Hs*/ double Hs = hs(samp,par); printf("\nHs = %0.3f\n", Hs); /* test Hs full genome */ Hs = hs_full_genome(samp,par); printf("\nHs (full genome) = %0.5f\n", Hs); /* test nb of snps */ int nball = nb_snps(samp,par); printf("\nnumber of SNPs = %d\n", nball); /* test mean nb of snps */ double temp = mean_nb_snps(samp); printf("\nmean number of SNPs = %.2f\n", temp); /* test var nb of snps */ temp = var_nb_snps(samp); printf("\nvariance of number of alleles = %.2f\n", temp); /* test pairwise distances */ struct distmat_int *mat = pairwise_dist(samp, par); print_distmat_int(mat); /* test mean pairwise distances */ temp = mean_pairwise_dist(samp,par); printf("\nmean pairwise distance: %.2f", temp); /* test variance of pairwise distances */ temp = var_pairwise_dist(samp,par); printf("\nvar pairwise distance: %.2f", temp); /* test Fst */ temp = fst(samp,par); printf("\nfst: %.2f", temp); printf("\n\n"); /* free memory */ free_sample(samp); free_snplist(snpbilan); free_allfreq(freq); free_distmat_int(mat); } /* write group sizes to file */ printf("\n\nPrinting group sizes to file 'out-popsize.txt'"); write_ts_groupsizes(grpsizes); /* free memory */ free_metapopulation(metapop); free_param(par); for(i=0;i<counter_sample;i++) free_sample(samplist[i]); free(samplist); free_table_int(tabdates); free_network(cn); free_ts_groupsizes(grpsizes); }
/* Function to be called from R */ void R_monitor_epidemics(int *seqLength, double *mutRate, int *npop, int *nHostPerPop, double *beta, int *nStart, int *t1, int *t2, int *Nsample, int *Tsample, int *duration, int *nbnb, int *listnb, double *pdisp, int *minSize){ int nstep; /* Initialize random number generator */ int j; time_t t; t = time(NULL); // time in seconds, used to change the seed of the random generator gsl_rng * rng; const gsl_rng_type *typ; gsl_rng_env_setup(); typ=gsl_rng_default; rng=gsl_rng_alloc(typ); gsl_rng_set(rng,t); // changes the seed of the random generator /* transfer simulation parameters */ struct param * par; par = (struct param *) malloc(sizeof(struct param)); par->L = *seqLength; par->mu = *mutRate; par->muL = par->mu * par->L; par->rng = rng; par->npop = *npop; par->popsizes = nHostPerPop; par->beta = *beta; par->nstart = *nStart; par->t1 = *t1; par->t2 = *t2; par->t_sample = Tsample; par->n_sample = *Nsample; par->duration = *duration; par->cn_nb_nb = nbnb; par->cn_list_nb = listnb; par->cn_weights = pdisp; /* check/print parameters */ check_param(par); print_param(par); /* dispersal matrix */ struct network *cn = create_network(par); /* print_network(cn, TRUE); */ /* group sizes */ struct ts_groupsizes * grpsizes = create_ts_groupsizes(par); struct ts_sumstat * sumstats = create_ts_sumstat(par); /* initiate population */ struct metapopulation * metapop; metapop = create_metapopulation(par); /* get sampling schemes (timestep+effectives) */ translate_dates(par); struct table_int *tabdates = get_table_int(par->t_sample, par->n_sample); printf("\n\nsampling at timesteps:"); print_table_int(tabdates); /* create sample */ struct sample *samp; /* MAKE METAPOPULATION EVOLVE */ nstep = 0; while(get_total_nsus(metapop)>0 && (get_total_ninf(metapop)+get_total_nexp(metapop))>0 && nstep<par->duration){ nstep++; /* age metapopulation */ age_metapopulation(metapop, par); /* process infections */ for(j=0;j<get_npop(metapop);j++){ process_infections(get_populations(metapop)[j], metapop, cn, par); } /* draw sample */ samp = draw_sample(metapop, par->n_sample, par); /* compute statistics */ if(get_total_ninf(metapop)> *minSize) fill_ts_sumstat(sumstats, samp, nstep, par); /* get group sizes */ fill_ts_groupsizes(grpsizes, metapop, nstep); } /* write group sizes to file */ printf("\n\nWriting results to file..."); write_ts_groupsizes(grpsizes); write_ts_sumstat(sumstats); printf("done.\n\n"); /* free memory */ free_sample(samp); free_metapopulation(metapop); free_param(par); free_network(cn); free_ts_groupsizes(grpsizes); free_ts_sumstat(sumstats); }
FILE* io_handler(FILE* file, int file_num,struct parameters* param) { char command[1000]; char tmp[1000]; //int i = 0; int gzcat = -1; if(access("/usr/bin/gzcat", X_OK) == 0){ gzcat = 1; }else if(access("/bin/gzcat", X_OK) == 0){ gzcat = 1; }else if(access("/usr/bin/zcat", X_OK) == 0){ gzcat = 0; }else if(access("/bin/zcat", X_OK) == 0){ gzcat = 0; } param->gzipped = 0; param->bzipped = 0; param->sam = 0; param->fasta = 0; if(!file_exists(param->infile[file_num])){ sprintf(param->buffer,"ERROR: Cannot find input file: %s\n",param->infile[file_num] ); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } if(!strcmp(".sam", param->infile[file_num] + (strlen(param->infile[file_num] ) - 4))){ param->sam = 1; }else if (!strcmp(".bam", param->infile[file_num] + (strlen(param->infile[file_num] ) - 4))){ param->sam = 2; }else if (!strcmp(".fa", param->infile[file_num] + (strlen(param->infile[file_num] ) - 3))){ param->sam = 0; param->fasta = 1; }else if (!strcmp(".fq", param->infile[file_num] + (strlen(param->infile[file_num] ) - 3))){ param->sam = 0; }else if (!strcmp(".fastq", param->infile[file_num] + (strlen(param->infile[file_num] ) - 6))){ param->sam = 0; }else if (!strcmp(".fastaq", param->infile[file_num] + (strlen(param->infile[file_num] ) - 7))){ param->sam = 0; }else if (!strcmp(".fasta", param->infile[file_num] + (strlen(param->infile[file_num] ) - 6))){ param->sam = 0; param->fasta = 1; }else if(!strcmp(".sam.gz", param->infile[file_num] + (strlen(param->infile[file_num] ) - 7))){ param->sam = 1; param->gzipped = 1; }else if (!strcmp(".bam.gz", param->infile[file_num] + (strlen(param->infile[file_num] ) - 7))){ param->sam = 2; param->gzipped = 1; }else if (!strcmp(".fa.gz", param->infile[file_num] + (strlen(param->infile[file_num] ) - 6))){ param->sam = 0; param->fasta = 1; param->gzipped = 1; }else if (!strcmp(".fq.gz", param->infile[file_num] + (strlen(param->infile[file_num] ) - 6))){ param->sam = 0; param->gzipped = 1; }else if (!strcmp(".fastq.gz", param->infile[file_num] + (strlen(param->infile[file_num] ) - 9))){ param->sam = 0; param->gzipped = 1; }else if (!strcmp(".fastaq.gz", param->infile[file_num] + (strlen(param->infile[file_num] ) - 10))){ param->sam = 0; param->gzipped = 1; }else if (!strcmp(".fasta.gz", param->infile[file_num] + (strlen(param->infile[file_num] ) - 9))){ param->sam = 0; param->gzipped = 1; }else if (!strcmp(".fastq.bz2", param->infile[file_num] + (strlen(param->infile[file_num] ) - 10))){ param->sam = 0; param->bzipped = 1; }else if (!strcmp(".fq.bz2", param->infile[file_num] + (strlen(param->infile[file_num] ) - 7))){ param->sam = 0; param->bzipped = 1; }else{ sprintf(param->buffer,"ERROR: Cannot recognize format for file: %s\n", param->infile[file_num]); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); param->sam = -1; } if(param->gzipped && gzcat == -1){ sprintf(param->buffer,"ERROR: Cannot find gzcat / zcat on your system. Try gzcat <infile> | samstat -f sam/bam/fa/fq\n"); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } if(file_num == -1){ if(param->sam == 2){ command[0] = 0; if(!param->filter){ strcat ( command, "samtools view -F 768 "); }else{ strcat ( command, "samtools view -F "); sprintf (tmp, "%s ",param->filter); strcat ( command, tmp); } sprintf (tmp, "%s ","-"); strcat ( command, tmp); if (!(file = popen(command, "r"))) { sprintf(param->buffer,"ERROR: Cannot open bam file '%s' with command:%s\n",param->infile[file_num],command); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } }else if(param->sam == 1){ command[0] = 0; if(!param->filter){ strcat ( command, "samtools view -SF 768 "); }else{ strcat ( command, "samtools view -SF "); sprintf (tmp, "%s ",param->filter); strcat ( command, tmp); } sprintf (tmp, "%s ", "-"); strcat ( command, tmp); if (!(file = popen(command, "r"))) { sprintf(param->buffer,"ERROR: Cannot open bam file '%s' with command:%s\n",param->infile[file_num],command); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } }else{ file = stdin; } }else{ if(param->sam == 2){ command[0] = 0; if(param->bzipped){ strcat ( command, "bzcat "); if(!param->filter){ sprintf (tmp, "%s | samtools view -F 768 - ", param->infile[file_num]); strcat ( command, tmp); }else{ sprintf (tmp, "%s | samtools view -F ", param->infile[file_num]); strcat ( command, tmp); sprintf (tmp, "%s - ",param->filter); strcat ( command, tmp); } }else if(param->gzipped){ if(gzcat == 1){ strcat ( command, "gzcat "); }else{ strcat ( command, "zcat "); } if(!param->filter){ sprintf (tmp, "%s | samtools view -F 768 - ", param->infile[file_num]); strcat ( command, tmp); }else{ sprintf (tmp, "%s | samtools view -F ", param->infile[file_num]); strcat ( command, tmp); sprintf (tmp, "%s - ",param->filter); strcat ( command, tmp); } }else{ if(!param->filter){ strcat ( command, "samtools view -F 768 "); }else{ strcat ( command, "samtools view -F "); sprintf (tmp, "%s ",param->filter); strcat ( command, tmp); } sprintf (tmp, "%s ", param->infile[file_num]); strcat ( command, tmp); } if (!(file = popen(command, "r"))) { sprintf(param->buffer,"Cannot open bam file '%s' with command:%s\n",param->infile[file_num],command); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } }else if(param->sam == 1){ command[0] = 0; if(param->gzipped){ if(gzcat == 1){ strcat ( command, "gzcat "); }else{ strcat ( command, "zcat "); } if(!param->filter){ sprintf (tmp, "%s | samtools view -SF 768 - ", param->infile[file_num]); strcat ( command, tmp); }else{ sprintf (tmp, "%s | samtools view -SF ", param->infile[file_num]); strcat ( command, tmp); sprintf (tmp, "%s - ",param->filter); strcat ( command, tmp); } }else{ if(!param->filter){ strcat ( command, "samtools view -SF 768 "); }else{ strcat ( command, "samtools view -SF "); sprintf (tmp, "%s ",param->filter); strcat ( command, tmp); } sprintf (tmp, "%s ", param->infile[file_num]); strcat ( command, tmp); } if (!(file = popen(command, "r"))) { sprintf(param->buffer,"Cannot open bam file '%s' with command:%s\n",param->infile[file_num],command); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } }else{ command[0] = 0; if(param->bzipped){ strcat ( command, "bzcat "); }else if(param->gzipped){ if(gzcat == 1){ strcat ( command, "gzcat "); }else{ strcat ( command, "zcat "); } }else{ strcat ( command, "cat "); } sprintf (tmp, "%s ", param->infile[file_num]); strcat ( command, tmp); //fprintf(stderr,"%s\n",command); if (!(file = popen(command, "r"))) { sprintf(param->buffer,"Cannot open bam file '%s' with command:%s\n",param->infile[file_num],command); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } } } return file; }
int read_fasta_fastq(struct read_info** ri,struct parameters* param,FILE *file) { int park_pos = -1; char line[MAX_LINE]; int i;//,j; int seq_p = 0; int set = 0; int len = 0; int size = 0; ri = clear_read_info(ri, param->num_query); while(fgets(line, MAX_LINE, file)){ if((line[0] == '@' && !set)|| (line[0] == '>' && !set)){ //set sequence length of previous read //check if there is still space.... //if(param->num_query == size){ // fseek (file , - strlen(line) , SEEK_CUR); // return size; //} park_pos++; len = 0; seq_p = 1; for(i = 1;i < MAX_LINE;i++){ len++; if(iscntrl((int)line[i])){ break; } } //ri[park_pos]->hits[0] = 0; //ri[park_pos]->strand[0] = 0; MMALLOC(ri[park_pos]->name,sizeof(unsigned char)* (len+1)); for(i = 1;i < MAX_LINE;i++){ if(iscntrl((int)line[i])){ ri[park_pos]->name[i-1] = 0; break; } if(isspace((int)line[i])){ ri[park_pos]->name[i-1] = ';'; } ri[park_pos]->name[i-1] = line[i]; } //fprintf(stderr,"LEN:%d %s\n",len,ri[park_pos]->name); set = 1; size++; //get ready to read quality if present }else if(line[0] == '+' && !set){ seq_p = 0; set = 1; //reading sequence or quality }else{ if(set){ if(seq_p){ len = 0; for(i = 0;i < MAX_LINE;i++){ len++; if(iscntrl((int)line[i])){ break; } } //fprintf(stderr,"SEQ LEN:%d %s\n",len,line); MMALLOC(ri[park_pos]->seq,sizeof(unsigned char)* (len+1)); MMALLOC(ri[park_pos]->labels, sizeof(unsigned char)* (len+1)); for(i = 0;i < MAX_LINE;i++){ if(iscntrl((int)line[i])){ ri[park_pos]->seq[i] = 0; ri[park_pos]->labels[i] = 0; break; } ri[park_pos]->seq[i] = nuc_code[(int)line[i]]; ri[park_pos]->labels[i] = 0; } ri[park_pos]->len = len-1; }else{ len = 0; for(i = 0;i < MAX_LINE;i++){ len++; if(iscntrl((int)line[i])){ break; } } if(len-1 != ri[park_pos]->len ){ sprintf(param->buffer,"ERROR: Length of sequence and base qualities differ!.\n"); param->messages = append_message(param->messages, param->buffer); free_param(param); exit(EXIT_FAILURE); } //fprintf(stderr,"QUAL LEN:%d\n",len); MMALLOC(ri[park_pos]->qual,sizeof(unsigned char)* (len+1)); for(i = 0;i < MAX_LINE;i++){ if(iscntrl((int)line[i])){ ri[park_pos]->qual[i] = 0; break; } ri[park_pos]->qual[i] = line[i]; } } } set = 0; } if(param->num_query == size ){//here I know I am in the last entry AND filled the quality... if(!param->fasta && ri[park_pos]->qual){ return size; } if(param->fasta && ri[park_pos]->seq){ return size; } } } return size; }
void KalignAdapter::alignUnsafe(const MultipleSequenceAlignment& ma, MultipleSequenceAlignment& res, TaskStateInfo& ti) { ti.progress = 0; int* tree = 0; quint32 a, b, c; struct alignment* aln = 0; struct parameters* param = 0; struct aln_tree_node* tree2 = 0; param = (parameters*)malloc(sizeof(struct parameters)); param = interface(param,0,0); kalign_context *ctx = get_kalign_context(); unsigned int &numseq = ctx->numseq; unsigned int &numprofiles = ctx->numprofiles; if (ma->getNumRows() < 2){ if (!numseq){ k_printf("No sequences found.\n\n"); } else { k_printf("Only one sequence found.\n\n"); } free_param(param); throw KalignException("Can't align less then 2 sequences"); } if(ctx->gpo != -1) { param->gpo = ctx->gpo; } if(ctx->gpe != -1) { param->gpe = ctx->gpe; } if(ctx->tgpe != -1) { param->tgpe = ctx->tgpe; } if(ctx->secret != -1) { param->secret = ctx->secret; } /************************************************************************/ /* Convert MA to aln */ /************************************************************************/ k_printf("Prepare data"); numseq = ma->getNumRows(); numprofiles = (numseq << 1) - 1; aln = aln_alloc(aln); for(quint32 i = 0 ; i < numseq; i++) { const MultipleSequenceAlignmentRow row= ma->getMsaRow(i); aln->sl[i] = row->getUngappedLength(); aln->lsn[i] = row->getName().length(); } for (quint32 i = 0; i < numseq;i++) { try { aln->s[i] = (int*) malloc(sizeof(int)*(aln->sl[i]+1)); checkAllocatedMemory(aln->s[i]); aln->seq[i] = (char*)malloc(sizeof(char)*(aln->sl[i]+1)); checkAllocatedMemory(aln->seq[i]); aln->sn[i] = (char*)malloc(sizeof(char)*(aln->lsn[i]+1)); checkAllocatedMemory(aln->sn[i]); } catch (...) { cleanupMemory(NULL, numseq, NULL, aln, param); throw; } } int aacode[26] = {0,1,2,3,4,5,6,7,8,-1,9,10,11,12,23,13,14,15,16,17,17,18,19,20,21,22}; for(quint32 i = 0; i < numseq; i++) { const MultipleSequenceAlignmentRow row= ma->getMsaRow(i); qstrncpy(aln->sn[i], row->getName().toLatin1(), row->getName().length() + 1); //+1 to include '\0' QString gapless = QString(row->getCore()).remove('-'); qstrncpy(aln->seq[i], gapless.toLatin1(), gapless.length() + 1); //+1 to include '\0' for (quint32 j = 0; j < aln->sl[i]; j++) { if (isalpha((int)aln->seq[i][j])){ aln->s[i][j] = aacode[toupper(aln->seq[i][j])-65]; } else { aln->s[i][j] = -1; } } aln->s[i][aln->sl[i]] = 0; aln->seq[i][aln->sl[i]] = 0; aln->sn[i][aln->lsn[i]] = 0; } /*for(int i=0;i<numseq;i++) { for(int j=0;j<aln->sl[i];j++) printf("%d ", aln->s[i][j]); }*/ //aln_dump(aln); //aln = detect_and_read_sequences(aln,param); if(param->ntree > (int)numseq){ param->ntree = (int)numseq; } //DETECT DNA if(param->dna == -1){ for (quint32 i = 0; i < numseq;i++){ param->dna = byg_detect(aln->s[i],aln->sl[i]); if(param->dna){ break; } } } //param->dna = 0; //k_printf("DNA:%d\n",param->dna); //exit(0); if(param->dna == 1){ //brief sanity check... for (quint32 i = 0; i < numseq;i++){ if(aln->sl[i] < 6){ //k_printf("Dna/Rna alignments are only supported for sequences longer than 6."); free(param); free_aln(aln); throw KalignException("Dna/Rna alignments are only supported for sequences longer than 6."); } } aln = make_dna(aln); } //int j; //fast distance calculation; float** submatrix = 0; submatrix = read_matrix(submatrix,param); // sets gap penalties as well..... //if(byg_start(param->alignment_type,"profPROFprofilePROFILE") != -1){ // profile_alignment_main(aln,param,submatrix); //} float** dm = 0; if(param->ntree > 1){ //if(byg_start(param->distance,"pairclustalPAIRCLUSTAL") != -1){ // if(byg_start(param->tree,"njNJ") != -1){ // dm = protein_pairwise_alignment_distance(aln,dm,param,submatrix,1); // }else{ // dm = protein_pairwise_alignment_distance(aln,dm,param,submatrix,0); // } //}else if(byg_start("wu",param->alignment_type) != -1){ // dm = protein_wu_distance2(aln,dm,param); // // param->feature_type = "wumanber"; if(param->dna == 1){ // if(byg_start(param->tree,"njNJ") != -1){ // dm = dna_distance(aln,dm,param,1); // }else{ dm = dna_distance(aln,dm,param,0); // } }else{ //if(byg_start(param->tree,"njNJ") != -1){ // dm = protein_wu_distance(aln,dm,param,1); //}else{ try { dm = protein_wu_distance(aln,dm,param,0); } catch (const KalignException &) { cleanupMemory(submatrix, numseq, dm, aln, param); throw; } //} } if(check_task_canceled(ctx)) { cleanupMemory(submatrix, numseq, dm, aln, param); throwCancellingException(); } /*int j; for (int i = 0; i< numseq;i++){ for (j = 0; j< numseq;j++){ k_printf("%f ",dm[i][j]); } k_printf("\n"); }*/ //if(byg_start(param->tree,"njNJ") != -1){ // tree2 = real_nj(dm,param->ntree); //}else{ tree2 = real_upgma(dm,param->ntree); //} //if(param->print_tree){ // print_tree(tree2,aln,param->print_tree); //} } tree = (int*) malloc(sizeof(int)*(numseq*3+1)); for (quint32 i = 1; i < (numseq*3)+1;i++){ tree[i] = 0; } tree[0] = 1; if(param->ntree < 2){ tree[0] = 0; tree[1] = 1; c = numseq; tree[2] = c; a = 2; for (quint32 i = 3; i < (numseq-1)*3;i+=3){ tree[i] = c; tree[i+1] = a; c++; tree[i+2] = c; a++; } }else if(param->ntree > 2){ ntreeify(tree2,param->ntree); }else{ tree = readtree(tree2,tree); for (quint32 i = 0; i < (numseq*3);i++){ tree[i] = tree[i+1]; } free(tree2->links); free(tree2->internal_lables); free(tree2); } //get matrices... //struct feature_matrix* fm = 0; //struct ntree_data* ntree_data = 0; int** map = 0; //if(param->ntree > 2){ // ntree_data = (struct ntree_data*)malloc(sizeof(struct ntree_data)); // ntree_data->realtree = tree2; // ntree_data->aln = aln; // ntree_data->profile = 0; // ntree_data->map = 0; // ntree_data->ntree = param->ntree; // ntree_data->submatrix = submatrix; // ntree_data->tree = tree; // ntree_data = ntree_alignment(ntree_data); // map = ntree_data->map; // tree = ntree_data->tree; // for (int i = 0; i < (numseq*3);i++){ // tree[i] = tree[i+1]; // } // free(ntree_data); //}else if (param->feature_type){ // fm = get_feature_matrix(fm,aln,param); // if(!fm){ // for (int i = 32;i--;){ // free(submatrix[i]); // } // free(submatrix); // free_param(param); // free(map); // free(tree); // throw KalignException("getting feature matrix error"); // } // map = feature_hirschberg_alignment(aln,tree,submatrix,map,fm); // //exit(0); // //map = feature_alignment(aln,tree,submatrix, map,fm); //}else if (byg_start("pairwise",param->alignment_type) != -1){ // if(param->dna == 1){ // map = dna_alignment_against_a(aln,tree,submatrix, map,param->gap_inc); // }else{ // map = hirschberg_alignment_against_a(aln,tree,submatrix, map,param->smooth_window,param->gap_inc); // } // //map = default_alignment(aln,tree,submatrix, map); //}else if (byg_start("fast",param->alignment_type) != -1){ // map = default_alignment(aln,tree,submatrix, map); if(param->dna == 1){ map = dna_alignment(aln,tree,submatrix, map,param->gap_inc); // /*}else if (byg_start("test",param->alignment_type) != -1){ // map = test_alignment(aln,tree,submatrix, map,param->internal_gap_weight,param->smooth_window,param->gap_inc); // }else if (param->aa){ // map = aa_alignment(aln,tree,submatrix, map,param->aa); // }else if (param->alter_gaps){ // map = alter_gaps_alignment(aln,tree,submatrix,map,param->alter_gaps,param->alter_range,param->alter_weight); // }else if (byg_start("altergaps",param->alignment_type) != -1){ // map = alter_gaps_alignment(aln,tree,submatrix,map,param->alter_gaps,param->alter_range,param->alter_weight); // }else if(byg_start("simple",param->alignment_type) != -1){ // map = simple_hirschberg_alignment(aln,tree,submatrix, map);*/ //}else if(byg_start("advanced",param->alignment_type) != -1){ // map = advanced_hirschberg_alignment(aln,tree,submatrix, map,param->smooth_window,param->gap_inc,param->internal_gap_weight); }else{ map = hirschberg_alignment(aln,tree,submatrix, map,param->smooth_window,param->gap_inc); } if (map == NULL) { throw KalignException("Failed to build alignment."); } if(check_task_canceled(ctx)) { free_param(param); free_aln(aln); free(map); free(tree); throwCancellingException(); } //clear up sequence array to be reused as gap array.... int *p = 0; for (quint32 i = 0; i < numseq;i++){ p = aln->s[i]; for (a = 0; a < aln->sl[i];a++){ p[a] = 0; } } //clear up for (quint32 i = 0; i < (numseq-1)*3;i +=3){ a = tree[i]; b = tree[i+1]; aln = make_seq(aln,a,b,map[tree[i+2]]); } //for (int i = 0; i < numseq;i++){ // k_printf("%s %d\n",aln->sn[i],aln->nsip[i]); //} for (quint32 i = 0; i < numseq;i++){ aln->nsip[i] = 0; } aln = sort_sequences(aln,tree,param->sort); //for (int i = 0; i < numseq;i++){ // k_printf("%d %d %d\n",i,aln->nsip[i],aln->sip[i][0]); //} /************************************************************************/ /* Convert aln to MA */ /************************************************************************/ res->setAlphabet(ma->getAlphabet()); for (quint32 i = 0; i < numseq;i++){ int f = aln->nsip[i]; QString seq; for(quint32 j=0;j<aln->sl[f];j++) { seq += QString(aln->s[f][j],'-') + aln->seq[f][j]; } seq += QString(aln->s[f][aln->sl[f]],'-'); res->addRow(QString(aln->sn[f]), seq.toLatin1()); } //output(aln,param); /* if(!param->format){ fasta_output(aln,param->outfile); }else{ if (byg_start("msf",param->format) != -1){ msf_output(aln,param->outfile); }else if (byg_start("clustal",param->format) != -1){ clustal_output(aln,param->outfile); }else if (byg_start("macsim",param->format) != -1){ macsim_output(aln,param->outfile,param->infile[0]); } } */ free_param(param); free_aln(aln); free(map); free(tree); //KalignContext* ctx = getKalignContext(); }
static int process(struct cmd_line_options* cmd_line_options) { int result = EXIT_FAILURE; uint32_t entries; struct state state; param_t *par; CSPSA_Handle_t handle; char idstr[BUFSIZ]; int i; int cspsa_written = 0; memset(&state, 0, sizeof(state)); if (cmd_line_options->debug) { printf(PROGRAM ": Processing %s (only stuff below", cmd_line_options->inpath); for (i = 0; i < cmd_line_options->subpaths; i++) printf(" %s", cmd_line_options->subpath[i]); printf("), writing to %s.\n", cmd_line_options->area); } result = init_cspsa(cmd_line_options->area, &handle); if (result != EXIT_SUCCESS) goto error_exit; /* unit id 0 is reserved for the number of entries */ state.entries = 1; /* create entries in a linked list for each file * or directory in the input directory tree */ if (cmd_line_options->subpaths) { for (i = 0; i < cmd_line_options->subpaths; i++) { result |= process_dirtree(cmd_line_options, cmd_line_options->inpath, "/", cmd_line_options->subpath[i], &state); } } else { result = process_dirtree(cmd_line_options, cmd_line_options->inpath, "/", NULL, &state); } if (result != EXIT_SUCCESS) { fprintf(stderr, ERRORSTR_INTERNAL, __LINE__); goto error_exit; } /* Create small header */ result = create_id(PROGRAM, STRINGIFY(VERSION), idstr, BUFSIZ); if (result != EXIT_SUCCESS) { fprintf(stderr, ERRORSTR_INTERNAL, __LINE__); goto error_exit; } /* Wipe key 0, we have no parameter entries in CSPSA yet */ entries = 0; cspsa_written = 1; result = emit_parameter_to_cspsa(cmd_line_options, &handle, 0, sizeof (entries), (unsigned char *) &entries); if (result != EXIT_SUCCESS) { fprintf(stderr, ERRORSTR_INTERNAL, __LINE__); goto error_exit; } /* Make sure key 0 hits storage */ result = flush_cspsa(&handle); if (result != EXIT_SUCCESS) { fprintf(stderr, ERRORSTR_INTERNAL, __LINE__); goto error_exit; } /* Write header */ result = emit_parameter_to_cspsa(cmd_line_options, &handle, MAX_FILES + 1, strlen(idstr) + 1, (unsigned char *) idstr); if (result != EXIT_SUCCESS) { fprintf(stderr, ERRORSTR_INTERNAL, __LINE__); goto error_exit; } /* Dump the stuff, par points to head of linked list */ par = state.par; while (par != NULL) { result = emit_parameter_to_cspsa(cmd_line_options, &handle, par->key, par->size, par->data); if (result != EXIT_SUCCESS) goto error_exit; par = next_param(par); } /* Make sure the parameters hit storage before key 0 */ result = flush_cspsa(&handle); if (result != EXIT_SUCCESS) { fprintf(stderr, ERRORSTR_INTERNAL, __LINE__); goto error_exit; } /* a unit id 0 output the number of files+dirs */ entries = state.entries - 1; result = emit_parameter_to_cspsa(cmd_line_options, &handle, 0, sizeof (entries), (unsigned char *) &entries); printf(PROGRAM ": Read %d entries from %s, wrote to %s\n", entries, cmd_line_options->inpath, cmd_line_options->area); error_exit: if (state.par) free_param(state.par); result |= exit_cspsa(&handle); if (result != EXIT_SUCCESS) { if (cspsa_written) fprintf(stderr, ERRORSTR PROGRAM ": WARNING: CSPSA was most likely modified!\n"); else fprintf(stderr, ERRORSTR PROGRAM ": CSPSA was NOT modified.\n"); } return result; }
int main (int argc,char * argv[]) { struct parameters* param = 0; struct seq_stats* seq_stats = 0; FILE* outfile =0; int i; init_nuc_code(); param = interface(param,argc,argv); if(param->summary){ if ((outfile = fopen(param->summary, "w")) == NULL){ fprintf(stderr,"can't open output\n"); exit(-1); } } if(param->format || (!param->infiles && !isatty(0))){ if(!param->format){ fprintf(stderr,"No format specified. Use -f <sam | bam | fa | fq > \n"); exit(-1); } if(!strcmp("sam", param->format)){ param->sam = 1; //}else if (byg_end(".bam", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp("bam", param->format)){ param->sam = 2; //}else if (byg_end(".fa", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp("fa", param->format)){ param->sam = 0; //}else if (byg_end(".fq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp("fq", param->format)){ param->sam = 0; //}else if (byg_end(".fastq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp("fastq", param->format)){ param->sam = 0; //}else if (byg_end(".fastaq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp("fastaq", param->format)){ param->sam = 0; //}else if (byg_end(".fasta", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp("fasta", param->format)){ param->sam = 0; }else{ param->sam = -1; } if(param->sam != -1 && !param->infiles && !isatty(0)){ fprintf(stdout,"Working on: stdin\n"); seq_stats = init_seq_stats(param->kmer_size); seq_stats->sam = param->sam; if(param->sam == 0){ seq_stats = collect_data(seq_stats,param,&read_fasta_fastq,-1); }else if(param->sam == 2){ seq_stats = collect_data(seq_stats,param,&read_sam_chunk,-1); }else{ seq_stats = collect_data(seq_stats,param,&read_sam_chunk,-1); } if(sanity_check(seq_stats)){ if(param->summary){ print_summary(seq_stats,param,-1,outfile); }else{ print_html_page(seq_stats,param,-1); } } free_seq_stats(seq_stats); } } for(i = 0; i < param->infiles;i++){ if(!param->format || param->sam == -1){ param->sam = 0; //if(byg_end(".sam", param->infile[i]) == strlen(param->infile[i])){ if(!strcmp(".sam", param->infile[i] + (strlen(param->infile[i] ) - 4))){ param->sam = 1; //}else if (byg_end(".bam", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".bam", param->infile[i] + (strlen(param->infile[i] ) - 4))){ param->sam = 2; //}else if (byg_end(".fa", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fa", param->infile[i] + (strlen(param->infile[i] ) - 3))){ param->sam = 0; //}else if (byg_end(".fq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fq", param->infile[i] + (strlen(param->infile[i] ) - 3))){ param->sam = 0; //}else if (byg_end(".fastq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fastq", param->infile[i] + (strlen(param->infile[i] ) - 6))){ param->sam = 0; //}else if (byg_end(".fastaq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fastaq", param->infile[i] + (strlen(param->infile[i] ) - 7))){ param->sam = 0; //}else if (byg_end(".fasta", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fasta", param->infile[i] + (strlen(param->infile[i] ) - 6))){ param->sam = 0; }else if(!strcmp(".sam.gz", param->infile[i] + (strlen(param->infile[i] ) - 7))){ param->sam = 1; param->gzipped = 1; //}else if (byg_end(".bam", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".bam.gz", param->infile[i] + (strlen(param->infile[i] ) - 7))){ param->sam = 2; param->gzipped = 1; //}else if (byg_end(".fa", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fa.gz", param->infile[i] + (strlen(param->infile[i] ) - 6))){ param->sam = 0; param->gzipped = 1; //}else if (byg_end(".fq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fq.gz", param->infile[i] + (strlen(param->infile[i] ) - 6))){ param->sam = 0; param->gzipped = 1; //}else if (byg_end(".fastq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fastq.gz", param->infile[i] + (strlen(param->infile[i] ) - 9))){ param->sam = 0; param->gzipped = 1; //}else if (byg_end(".fastaq", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fastaq.gz", param->infile[i] + (strlen(param->infile[i] ) - 10))){ param->sam = 0; param->gzipped = 1; //}else if (byg_end(".fasta", param->infile[i]) == strlen(param->infile[i])){ }else if (!strcmp(".fasta.gz", param->infile[i] + (strlen(param->infile[i] ) - 9))){ param->sam = 0; param->gzipped = 1; }else{ param->sam = -1; } } //fprintf(stdout,"Loking at on:%s %d\n",param->infile[i],sam); if(param->sam != -1){ fprintf(stdout,"Working on:%s\n",param->infile[i]); seq_stats = init_seq_stats(param->kmer_size); seq_stats->sam = param->sam; if(param->sam == 0){ seq_stats = collect_data(seq_stats,param,&read_fasta_fastq,i); }else if(param->sam == 2){ seq_stats = collect_data(seq_stats,param,&read_sam_chunk,i); }else{ seq_stats = collect_data(seq_stats,param,&read_sam_chunk,i); } if(sanity_check(seq_stats)){ if(param->summary){ print_summary(seq_stats,param,i,outfile); }else{ print_html_page(seq_stats,param,i); } } free_seq_stats(seq_stats); } } if(param->summary){ fclose(outfile); } free_param(param); return 0; }
// Frees an expression by verifying it only frees no_param once. void free_expr(c16_expr *expr){ free_param(expr->param_1); free_param(expr->param_2); free_param(expr->param_3); free(expr); }