void similarity_objects(double *x, int *n, int *p, double *S, int *method) { /* similarity per variable */ void (*similarity)(double *, int , int , double *) = NULL; switch(*method) { case NUMERICAL_EUCLIDEAN: Rprintf("LLAsim: the objects are assumed to be described by numerical variables\n"); similarity = similarity_numerical_euclidean; break; case NUMERICAL_COSINUS: Rprintf("LLAsim: the objects are assumed to be described by numerical variables\n"); similarity = similarity_numerical_cosinus; normalize_data(x, *n, *p); break; case CATEGORICAL: Rprintf("LLAsim: the objects are assumed to be described by categorical variables\n"); similarity = similarity_categorical; break; case ORDINAL: Rprintf("LLAsim: the objects are assumed to be described by ranks induced by ordinal variables\n"); similarity = similarity_ordinal; break; case BOOLEAN: Rprintf("LLAsim: the objects are assumed to be described by boolean variables\n"); normalize_data(x, *n, *p); similarity = similarity_numerical_cosinus; break; default: error("invalid similarity method"); } /* construction of the similarity by additive decomposition */ similarity(x,*n,*p, S); /* normalization of the similarity index */ normalize_similarity(S,(*n) * (*n - 1)/2); }
void test_UserItemBased_Predictor() { clock_t begin,end; string train_data = "E:/data/resys/corpus/1/train/base"; string test_data = "E:/data/resys/corpus/1/test/u1.test"; FileDataReader train_reader = FileDataReader(train_data); FileDataReader test_reader = FileDataReader(test_data); int n_user, n_item, n_rating; n_user = 943; n_item = 1682; n_rating = 80000; UserModel userModel(n_user,n_item,n_rating); ItemModel itemModel(n_user,n_item,n_rating); UserModel testModel(n_user,n_item,20000); UserModel resultModel(n_user,n_item,20000); if(train_reader.readData(userModel) && train_reader.readData(itemModel) && test_reader.readData(testModel)) { int min_com = 0; double min_sim = 0.05; int shrink_parameter = 30; Similarity_Shrinking shrinker(min_com,min_sim,shrink_parameter); Pearson_Similarity similarity(shrinker); UserItemBased_Predictor predictor = UserItemBased_Predictor(similarity,15,0); begin = clock(); predictor.train(userModel,itemModel); end = clock(); cout << "Train time: " << (end - begin) * 1.0 / CLOCKS_PER_SEC << endl; begin = end; predictor.predictAll(userModel,itemModel,testModel,resultModel); end = clock(); cout << "Predict time: " << (end - begin) * 1.0 / CLOCKS_PER_SEC << endl; double rmse = evl_rmse(testModel, resultModel); cout << "RMSE: " << rmse << endl; double mae = evl_mae(testModel, resultModel); cout << "MAE: " << mae << endl; } }
static double ssim_8x8(uint8_t *s, int sp, uint8_t *r, int rp) { unsigned long sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0; vp9_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, &sum_sxr); return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64); }
static double ssim_8x8(const uint8_t *s, int sp, const uint8_t *r, int rp) { uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0; aom_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, &sum_sxr); return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64); }
static double ssim_8x8(unsigned char *s, int sp, unsigned char *r, int rp) { uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0; vp8_ssim_parms_8x8_c(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, &sum_sxr); return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64); }
size_t analysis(unsigned short *incidence,size_t ncol,size_t nrow,double *scores,size_t step,size_t max_iter,size_t verbose,unsigned int seed) { set_rand(seed); size_t i,j,kk,n,rand1,rand2; size_t dim=max_iter+1; size_t *from; size_t *to; unsigned short *matrix; size_t a,b,c,d,e=0; size_t index=1; do matrix=(unsigned short *)calloc(nrow*ncol,sizeof(unsigned short)); while(matrix==NULL); for(i=0;i<nrow;++i) { for(j=0;j<ncol;++j) { matrix[i*ncol+j]=incidence[i*ncol+j]; e+=incidence[i*ncol+j]; } } //initialization of score vector overwriting the original //do scores=(double*)calloc(dim,sizeof(double)); while(scores==NULL); for(i=0;i<dim;scores[i++]=0.0); scores[0]=1.0; do from=(size_t*)calloc(e,sizeof(size_t)); while(from==NULL); do to=(size_t*)calloc(e,sizeof(size_t)); while(to==NULL); kk=0; for(i=0;i<nrow;++i) for(j=0;j<ncol;++j) if(matrix[i*ncol+j]==1) { from[kk]=i; to[kk]=j; kk++; } time_t tin,tfin; tin = time (NULL); //GetRNGstate(); for(n=0;n<max_iter;n++) { //random rewiring if(verbose==1) loadBar( n, max_iter, 100, 50); rand1=(size_t) (unif_rand()*e); do rand2=(size_t) (unif_rand()*e); while (rand1==rand2); a=from[rand1]; c=from[rand2]; b=to[rand1]; d=to[rand2]; //printf("%d %d %d %d %d %d %d %d %d\n",e,a,b,c,d,rand1,rand2,t,n); if(a!=c && d!=b && incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0) { to[rand1]=d; to[rand2]=b; incidence[a*ncol+d]=incidence[c*ncol+b]=1; incidence[a*ncol+b]=incidence[c*ncol+d]=0; } if(n%step==0) (scores)[index++]=similarity(incidence,matrix, ncol,nrow, e); } tfin = time (NULL); //PutRNGstate(); if(verbose==1) printf("DONE in %d seconds \n",-(tin-tfin)); return (index-1); }
WeightPtr FilteredQuery::createWeight(SearcherPtr searcher) { WeightPtr weight(query->createWeight(searcher)); SimilarityPtr similarity(query->getSimilarity(searcher)); return newLucene<FilteredQueryWeight>(shared_from_this(), weight, similarity); }
/// <summary> /// Main update routine for the topological map. This should be called after /// all current inputs have been assigned using the setInput method /// </summary> void Ttopmap::update() { int x,y,least_hits; long n=0; float value; float min; WinnerX = -1; WinnerY = -1; min = 9999; average_similarity=0; for (x = 0;x<map_width;x++) { for (y = 0;y<map_height;y++) { if (availability[x][y]) { value = similarity(x, y); average_similarity += value; if (value < Threshold) { if (value < min) { min = value; WinnerX = x; WinnerY = y; } outputs[x][y] = (unsigned char)(((1 - value) * (1 - value))*255); } else outputs[x][y] = 0; } else outputs[x][y] = 0; } } //calculate the average similarity across the map average_similarity /= map_width * map_height; //update hit score if (WinnerX>-1) { hits[WinnerX][WinnerY]++; if (hits[WinnerX][WinnerY] > max_hits) { max_hits = hits[WinnerX][WinnerY]; if (max_hits > 32000) rescaleHits(); } } else { //find the least used unit least_hits = max_hits+1; for (x = 0;x<map_width;x++) { for (y = 0;y<map_height;y++) { if ((hits[x][y]<least_hits) && (classification[x][y]==0)) { least_hits = hits[x][y]; WinnerX = x; WinnerY = y; } } } hits[WinnerX][WinnerY]++; if (hits[WinnerX][WinnerY] > max_hits) { max_hits = hits[WinnerX][WinnerY]; if (max_hits > 32000) rescaleHits(); } } }
///////za długa !!!!!!!!!!!!!!!!!!!!!!!!!111 void NodesFactory::countCos(int i) { // std::cout<<nodes_[i]->getSample()<<std::endl; char lang = nodes_[i]->getLang(); int trans = nodes_[i]->getLinksTrans(); int* v = new int[markers_count_]; int index = nodes_[i]->getIndex(); if (lang == Node::pl){ for (int j=0;j<markers_count_;++j){ v[j] = paths_pl_[index][j]; } }else{ for (int j=0;j<markers_count_;++j){ v[j] = paths_en_[index][j]; } } double cos, tcos, mcos=-10.0; int trans_pos = 1, mkey=1; int trans_in = nodes_[trans]->getIndex(); if (lang == Node::pl){ tcos = similarity(v, paths_en_[trans_in], markers_count_); }else{ tcos = similarity(v, paths_pl_[trans_in], markers_count_); } if (isnan(tcos)){ std::cout<<"nan: "<<trans_in<<", "<<trans<<std::endl; std::cout<<"slowo\ttrans\tmarkers"<<std::endl; if (lang == Node::pl){ for (int i=0;i<markers_count_;++i){ std::cout<<v[i]<<"\t"<<paths_en_[trans_in][i]<<"\t"<<markers_en_[i]<<std::endl; } }else{ for (int i=0;i<markers_count_;++i){ std::cout<<v[i]<<"\t"<<paths_pl_[trans_in][i]<<"\t"<<markers_pl_[i]<<std::endl; } } } if (lang == Node::pl){ for (int j=0;j<count_en_;++j){ cos = similarity(v, paths_en_[j], markers_count_); if (cos > tcos){ trans_pos++; } if (cos > mcos){ mcos = cos; mkey = j; } } }else{ for (int j=0;j<count_pl_;++j){ cos = similarity(v, paths_pl_[j], markers_count_); if (cos > tcos){ trans_pos++; } if (cos > mcos){ mcos = cos; mkey = j; } } } std::cout<<markers_count_<<" markerow, "<<nodes_[i]->getSample()<<std::endl; std::cout<<"pozycja: \twartosc\tmax_cos\tnajlepszy\tprawdziwy"<<std::endl; if (lang == Node::pl){ std::cout<<trans_pos<<"\t"<<tcos<<"\t"<<mcos<<"\t"<<nodes_[keys_en_[mkey]]->getSample()<<"\t"<<nodes_[trans]->getSample()<<std::endl; }else{ std::cout<<trans_pos<<"\t"<<tcos<<"\t"<<mcos<<"\t"<<nodes_[keys_pl_[mkey]]->getSample()<<"\t"<<nodes_[trans]->getSample()<<std::endl; } std::cout<<"wektory: "<<std::endl; std::cout<<"szukany - "<<i<<": "<<nodes_[i]->getSample()<<std::endl; if (lang == Node::pl){ for (int j=0;j<markers_count_; ++j){ std::cout<<paths_pl_[index][j]<<" "; } }else{ for (int j=0;j<markers_count_; ++j){ std::cout<<paths_en_[index][j]<<" "; } } std::cout<<std::endl; if (lang == Node::pl){ std::cout<<"najlepszy - "<<mkey<<": "<<nodes_[keys_en_[mkey]]->getSample()<<std::endl; for (int j=0;j<markers_count_; ++j){ std::cout<<paths_en_[mkey][j]<<" "; } }else{ std::cout<<"najlepszy - "<<mkey<<": "<<nodes_[keys_pl_[mkey]]->getSample()<<std::endl; for (int j=0;j<markers_count_; ++j){ std::cout<<paths_pl_[mkey][j]<<" "; } } std::cout<<std::endl; std::cout<<"prawdziwy - "<<trans<<": "<<nodes_[trans]->getSample()<<std::endl; if (lang == Node::pl){ for (int j=0;j<markers_count_; ++j){ std::cout<<paths_en_[trans_in][j]<<" "; } }else{ for (int j=0;j<markers_count_; ++j){ std::cout<<paths_pl_[trans_in][j]<<" "; } } std::cout<<std::endl; std::cout<<similarity(paths_en_[mkey], paths_pl_[index], markers_count_)<<std::endl; }
int main() { //if the compiler supports c++ 2011 standard, std::unordered_set container is perfect for this scenario due to // the requirement that the replacements should be written in the order of their appearance in the dictionary std::vector<ITEM> dict; int sequence; //A terminating null character is automatically added at the end of the stored sequence. char word[16]; while(1) { scanf("%s", word); sequence++; if(word[0] != '#') { ITEM tmp_item; tmp_item.m_word = word; tmp_item.m_sequence_num = sequence; dict.push_back(tmp_item); } else break; } std::sort(dict.begin(), dict.end()); while(1) { scanf("%s", word); if(word[0] != '#') { printf("%s", word); bool is_correct = false; ITEM equal_item; equal_item.m_word = word; std::pair<std::vector<ITEM>::iterator,std::vector<ITEM>::iterator> bounds = std::equal_range(dict.begin(), dict.end(), equal_item); for(std::vector<ITEM>::iterator equal = bounds.first; equal != bounds.second; ++equal) { if(equal->m_word == word ) { printf(" is correct"); is_correct = true; break; } } if(!is_correct) { printf(":"); std::string lower_str(strlen(word) -1, 'x'); std::string upper_str(strlen(word) +1, 'x'); ITEM lower_item, upper_item; lower_item.m_word = lower_str; upper_item.m_word = upper_str; std::vector<ITEM>::iterator low, up; low = std::lower_bound(dict.begin(), dict.end(), lower_item); up = std::upper_bound(dict.begin(), dict.end(), upper_item); std::vector<ITEM> similar_words; for(low; low != up; ++low) { if(!similarity(low->m_word.c_str(), word) ) similar_words.push_back(*low); } std::sort(similar_words.begin(), similar_words.end(), comp); for(int i = 0; i < similar_words.size(); ++i) printf(" %s", similar_words[i].m_word.c_str()); } printf("\n"); } else break; } return 0; }
int main(int argc, char **argv) { InitVars(argc, argv, "segment.cfg"); if (argc != 2) { DLOG << "Usage: "<<argv[0]<<" INPUT"<<endl; exit(-1); } // Read image ImageBundle image(argv[1]); const int& w = image.nx(); const int& h = image.ny(); image.BuildHSV(); // Convert to HSV and mono WriteHueOnly("out/h_only.png", image.hsv); // Segment FHSegmenter segmenter; TIMED("Segment") segmenter.Compute(image); const int num_segments = segmenter.num_segments; DLOG << "Generated " << num_segments << " segments"; segmenter.OutputSegViz("out/segmentation.png"); // Compute features for each segment HistSegmentFeatures<TextonLabeller<NhdPixelFeatures> > segftrs; segftrs.Compute(image.rgb, image.hsv, segmenter.segmentation, num_segments); segftrs.OutputLabelViz("out/label_map.png"); // Compute similarity matrix MatF similarity(num_segments, num_segments); for (int i = 0; i < num_segments; i++) { similarity[i][i] = 0; for (int j = i+1; j < num_segments; j++) { const double ssd = VectorSSD(segftrs.features[i], segftrs.features[j]); similarity[i][j] = min(1.0/sqrt(ssd), 10); similarity[j][i] = similarity[i][j]; } } similarity /= similarity.MaxValue(); WriteMatrixImageRescaled("out/similarity.png", similarity); // Generate some vizualizations const int kNumViz = 15; ImageRGB<byte> viz(w, h); vector<PixelRGB<byte> > colors(num_segments); vector<int> segment_indices; sorted_order(segmenter.segment_sizes, segment_indices, greater<int>()); for (int i = 0; i < kNumViz; i++) { // Pick a random pixel and use the segment at that pixel so that // larger segments are more likely to be chosen const int root = segment_indices[i]; DREPORT(root); // Compute colors for each other segment for (int j = 0; j < num_segments; j++) { if (j == root) { colors[j].Set(255, 0, 0); } else { const int v = similarity[j][root] * 255; colors[j].Set(v, v, v); } } // Generate the vizualization for (int r = 0; r < h; r++) { PixelRGB<byte>* vizrow = viz[r]; const int* segrow = segmenter.segmentation[r]; for (int c = 0; c < w; c++) { vizrow[c] = colors[segrow[c]]; } } WriteImage("out/sim"+PaddedInt(i,2)+".png", viz); } return 0; }
int bsstrand_func(bam1_t *b, const samfile_t *in, samfile_t *out, void *data) { bsstrand_data_t *d = (bsstrand_data_t*)data; bsstrand_conf_t *conf = d->conf; const bam1_core_t *c = &b->core; if (c->flag & BAM_FUNMAP){ if (out) samwrite(out, b); d->n_unmapped++; return 0; } fetch_refseq(d->rs, in->header->target_name[c->tid], c->pos, c->pos+1); uint32_t rpos=c->pos+1, qpos=0; int i, nC2T = 0, nG2A = 0; uint32_t j; char rbase, qbase; for (i=0; i<c->n_cigar; ++i) { uint32_t op = bam_cigar_op(bam1_cigar(b)[i]); uint32_t oplen = bam_cigar_oplen(bam1_cigar(b)[i]); switch(op) { case BAM_CMATCH: for(j=0; j<oplen; ++j) { rbase = toupper(getbase_refseq(d->rs, rpos+j)); qbase = bscall(bam1_seq(b), qpos+j); if (rbase == 'C' && qbase == 'T') nC2T += 1; if (rbase == 'G' && qbase == 'A') nG2A += 1; /* printf("%c vs %c\n", toupper(rbase), qbase); */ } rpos += oplen; qpos += oplen; break; case BAM_CINS: qpos += oplen; break; case BAM_CDEL: rpos += oplen; break; case BAM_CSOFT_CLIP: qpos += oplen; break; default: fprintf(stderr, "Unknown cigar, %u\n", op); abort(); } } char key[2] = {'Z','S'}; unsigned char *bsstrand = bam_aux_get(b, key); if (bsstrand) { bsstrand++; double s = similarity(nG2A, nC2T); if (nG2A > 1 && nC2T > 1 && s > 0.5) { if (conf->output_read || conf->output_all_read) printf("F\t%s\t%d\t%d\t%d\t%s\t%s\t%1.2f\n", in->header->target_name[c->tid], c->pos, nC2T, nG2A, bam1_qname(b), bsstrand, s); bam_aux_append(b, "OS", 'A', 1, bsstrand); bsstrand[0] = '?'; d->n_fail++; } else if (*bsstrand == '+' && nG2A > nC2T + 2) { if (conf->output_read || conf->output_all_read) printf("W2C\t%s\t%d\t%d\t%d\t%s\t%s\t%1.2f\n", in->header->target_name[c->tid], c->pos, nC2T, nG2A, bam1_qname(b), bsstrand, s); bam_aux_append(b, "OS", 'A', 1, bsstrand); bsstrand[0] = '-'; d->n_corr++; } else if (*bsstrand == '-' && nC2T > nG2A + 2) { if (conf->output_read || conf->output_all_read) printf("C2W\t%s\t%d\t%d\t%d\t%s\t%s\t%1.2f\n", in->header->target_name[c->tid], c->pos, nC2T, nG2A, bam1_qname(b), bsstrand, s); bam_aux_append(b, "OS", 'A', 1, bsstrand); bsstrand[0] = '+'; d->n_corr++; } else if (conf->output_all_read) { printf("N\t%s\t%d\t%d\t%d\t%s\t%s\t%1.2f\n", in->header->target_name[c->tid], c->pos, nC2T, nG2A, bam1_qname(b), bsstrand, s); } } else if (!(c->flag & BAM_FUNMAP) && conf->infer_bsstrand) { char bss[3]; if (similarity(nG2A, nC2T) < 0.5) { strcpy(bss, "??"); } else if (nC2T > nG2A) { strcpy(bss, c->flag & BAM_FREVERSE ? "+-" : "++"); } else { strcpy(bss, c->flag & BAM_FREVERSE ? "-+" : "--"); } bam_aux_append(b, "ZS", 'Z', 3, (uint8_t*) bss); } if (out) samwrite(out, b); d->n_mapped++; return 0; }
static double ssim_16x16(unsigned char *s,int sp, unsigned char *r,int rp) { unsigned long sum_s=0,sum_r=0,sum_sq_s=0,sum_sq_r=0,sum_sxr=0; vp8_ssim_parms_16x16(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, &sum_sxr); return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 256); }
/***** * User created Social Network * * Allows user to simulate their own social network * Hashes user names and creates UID matrix * Assigns interests at random * Creates similarity matricies */ int build_graph(int*** uu, int*** ii, int*** interests, int** uid){ int opt=0, m; node *head = NULL, *cur=NULL, *ref=NULL, *parent=NULL; char** name; while(1){ printf(GRAPH_MENU); scanf("%d", &opt); switch(opt){ case 0: m = list_length(head); // Network Size to_array(head, uid); // UIDs assign_interests(m, interests, END); // Interest Matrix similarity(m, *interests, uu, ii); // Similarities HEAD = head; return m; case 1: // Add Node ref = get_node(); name = get_name(); fill_node(ref,name); head = add_node(head, ref); cur = head; break; case 2: // Add Friend if(cur != NULL) name = get_name(); if(search(head, name, &parent) == NULL){ //Friend Doesn't Exist Yet ref = get_node(); fill_node(ref, name); head = add_node(head, ref); } else ref = search(head, name, &parent); add_friend(cur, name); add_friend(ref, cur->name); break; case 3: // Switch Node cur = search(head, get_name(), &parent); break; case 4: // View Nodes print_list(head); break; case 5: // View Friends printf("%s %s's Friends: \n", cur->name[0], cur->name[1]); if(cur != NULL) view_leaf(cur->root); printf("\n"); break; default: printf("Invalid Option\n"); } } }
void main(){ srand(time(0)); //create random set of points int size = TEST_SIZE; float G1[size][2]; for(int i=0; i < size; i++){ G1[i][0] = randomfloat()/10; G1[i][1] = randomfloat()/10; } //printMatrix(size, 2, G1); //create adjacency matrix to determine where edges exist float adjacent[size][size]; zeros(size, size, adjacent); int edges = 6; makeAdjacency(size, adjacent, edges); //printf("Adjacency Matrix\n"); //printMatrix(size, size, adjacent); //copy original set of points for distortion float G2[size][2]; for(int i=0; i < size; i++){ G2[i][0] = G1[i][0]; G2[i][1] = G1[i][1]; } //distort the graph for testing purposes graphDistortion(size, G1, G2, 0, 0); //printMatrix(size, 2, G2); //calculate the distances to each neighbor float neighborDist1[size][size], neighborDist2[size][size]; neighborDistances(size, G1, neighborDist1, adjacent); neighborDistances(size, G2, neighborDist2, adjacent); //printf("neighbor Distances 1\n"); //printMatrix(size, size, neighborDist1); //printf("neighbor distances 2\n"); //printMatrix(size, size, neighborDist2); int size2; size2 = (size*size)/2 - (size/2); float simMatrix[size2][size2]; zeros(size2, size2, simMatrix); //check to see if the graphs are the same similarity(size, size2, edges, neighborDist1, neighborDist2, simMatrix); //printf("Similarity Scores\n"); //printMatrix(size2, size2, simMatrix); float X[size][size]; float Z[size][size]; float Y[size][size]; graphMatching(size,neighborDist1,size,neighborDist2, 0.001,size,X,Z,Y); printf("X(hard):\n"); printMatrix(size, size, X); printf("Z(soft):\n"); printMatrix(size, size, Z); printf("Y(debug):\n"); printMatrix(size, size, Y); }
int main(int argc, char** argv) { if(argc!=5){ printf("Please provide correct number of input arguments: \n"); printf("1:gap 2:filename sequence_A 3:filename sequence_B 4:chunk_size\n"); exit(0); } gap = atof(argv[1]); // loading gap int i,ii,j,a,b,c,d; double t1,t2,total; int chunk; chunk = atoi(argv[4]); //load sequences FILE *fpa; fpa = fopen(argv[2], "r"); if (fpa == NULL) { printf("Error: could not open seqA file!\n"); exit(0); } unsigned int N_a, num_a; fscanf(fpa, "%d %d\n", &num_a, &N_a); char **seq_a = malloc(num_a * sizeof(char*)); for(a=0; a < num_a; a++) { seq_a[a] = malloc((N_a+1) * sizeof(char)); fscanf(fpa, "%s\n", seq_a[a]); } fclose(fpa); FILE *fpb; fpb = fopen(argv[3], "r"); if (fpb == NULL) { printf("Error: could not open seqB file!\n"); exit(0); } unsigned int N_b, num_b; fscanf(fpb, "%d %d\n", &num_b, &N_b); char **seq_b = malloc(num_b * sizeof(char*)); for(a=0; a < num_b; a++) { seq_b[a] = malloc((N_b+1) * sizeof(char)); fscanf(fpb, "%s\n", seq_b[a]); } //strcpy(seq_b,""); fclose(fpb); /* if( N_a != N_b) { printf("not a square matrix \n"); exit(0); }*/ /* printf("N_a:%d num_a:%d\n", N_a, num_a); printf("printing seq_a: \n"); for(a=0; a < num_a; a++) { for(i = 0;i < N_a; i++) { printf("%c", seq_a[a][i]); } printf("\n"); } printf("N_b:%d num_b:%d\n", N_b, num_b); printf("printing seq_b: \n"); for(a=0; a < num_b; a++) { for(i = 0;i < N_b; i++) { printf("%c", seq_b[a][i]); } printf("\n"); } */ //initialize H // int H[N_a+1][N_b+1]; int **H; H = malloc((N_a + 1) * sizeof(int *)); //#pragma omp parallel for for(i=0; i< N_a + 1; i++) H[i] = malloc((N_b + 1) * sizeof(int)); for(i=0;i<=N_a;i++){ for(j=0;j<=N_b;j++){ H[i][j]=0; } } int temp[4]; //int I_i[N_a+1][N_b+1],I_j[N_a+1][N_b+1]; // Index matrices to remember the 'path' for backtracking int H_max = 0.; int i_max=0,j_max=0; int current_i,current_j; int next_i; int next_j; int tick=0; //char consensus_a[N_a+N_b+2],consensus_b[N_a+N_b+2]; //strcpy(consensus_a,""); //strcpy(consensus_b,""); int waves = N_a + N_b +1; int wave, elements, np, mp; int min = 0; //t1 = GetTickCount(); for(a=0; a < num_a; a++) { //for(a=0; a < 1; a++) { for(c=0;c<=N_a;c++){ for(d=0;d<=N_b;d++){ H[c][d]=0; } } //for(i=0;i< N_a;i++){ // for(j=0;j< N_b;j++){ t1 = omp_get_wtime(); #pragma omp parallel firstprivate(a, gap, waves) private(temp, wave, ii, i) shared(np, mp, elements) { #pragma omp master { for(wave = 0; wave < waves; ++wave) { // 0 <= wave < n-1 if(wave < N_a-1) { elements = wave+1; np = wave+1; mp = 0+1; } // n-1 <= wave < m else if(wave < N_b) { elements = N_a; np = N_a-1+1; mp = wave-(N_a-1)+1; } // m <= wave < m+n-1 else { elements = N_a-1-(wave-N_b); np = N_a-1+1; mp = wave-(N_a-1)+1; } for(ii = 0; ii < elements; ii+=chunk) { min = MIN(elements,ii + chunk); #pragma omp task firstprivate(ii, np, mp, chunk, elements) { for (i = ii; i < min; i++) //for (i = ii; i < MIN(elements,ii + chunk); i++) { //printf("breakpoint 3- %d \n",i); //printf("{%d,(%d,%d)\n",omp_get_thread_num(), (np-i), (mp+i)); //printf("elements=%d\n", elements); //printf("similarity=%d\n",similarity(seq_a[a][(np-i)-1],seq_b[a][(mp+i)-1])); //printf("H= %d\n", H[(np-i)-1][(mp+i)-1]); temp[0] = H[(np-i)-1][(mp+i)-1] + similarity(seq_a[a][(np-i)-1],seq_b[a][(mp+i)-1]); temp[1] = H[(np-i)-1][(mp+i)]-gap; temp[2] = H[(np-i)][(mp+i)-1]-gap; temp[3] = 0; //printf("%d,%d,%d\n", temp[0],temp[1],temp[2]); H[(np-i)][(mp+i)] = find_array_max(temp,4); } } // task } //for loop #pragma omp taskwait } } } t2 = omp_get_wtime(); total += t2 - t1; /* printf("The scoring matrix is given by:\n"); for(i=1;i<=N_a;i++){ for(j=1;j<=N_b;j++){ printf("%d ",H[i][j]); } printf("\n"); } */ /* //search H for maximal score H_max = 0.; i_max=0,j_max=0; for(i=1;i<=N_a;i++){ for(j=1;j<=N_b;j++){ if(H[i][j]>H_max){ H_max = H[i][j]; i_max = i; j_max = j; } } } //printing maximum score cell printf("H_max:%f (%d,%d) \n",H_max,i_max,j_max); //Backtracking from H_max current_i=i_max,current_j=j_max; next_i=I_i[current_i][current_j]; next_j=I_j[current_i][current_j]; tick=0; //char consensus_a[N_a+N_b+2],consensus_b[N_a+N_b+2]; strcpy(consensus_a,""); strcpy(consensus_b,""); while(((current_i!=next_i) || (current_j!=next_j)) && (next_j!=0) && (next_i!=0)) { if(next_i==current_i) { consensus_a[tick] = '-'; } // deletion in A else consensus_a[tick] = seq_a[a][current_i-1]; // match/mismatch in A if(next_j==current_j) { consensus_b[tick] = '-'; } // deletion in B else consensus_b[tick] = seq_b[a][current_j-1]; // match/mismatch in B current_i = next_i; current_j = next_j; next_i = I_i[current_i][current_j]; next_j = I_j[current_i][current_j]; tick++; } //output of consensus printf("***********************************************\n"); printf("tick:%d\n",tick); printf("The alignment of the sequences \n"); for(i=0;i<N_a;i++){printf("%c",seq_a[a][i]);} printf(" and\n"); for(i=0;i<N_b;i++){printf("%c",seq_b[a][i]);} printf("\n\n"); printf("is for the parameter gap = %f \n", gap); for(i=tick-1;i>=0;i--) printf("%c",consensus_a[i]); printf("\n"); for(j=tick-1;j>=0;j--) printf("%c",consensus_b[j]); printf("\n"); printf("-----------------------------------------------------------------------\n"); printf("-----------------------------------------------------------------------\n"); */ } //t2 = GetTickCount(); //printf("Time for Smith-Watterman in secs: %f \n", (t2-t1)/1000000); printf("Time for Smith-Watterman in secs: %f \n", total); } // END of main