Exemple #1
0
void clear_tree(tree** t) {
    if(*t == NULL)
        return;

    if((*t)->left != NULL)
        clear_tree(&((*t)->left));

    if((*t)->right != NULL)
        clear_tree(&((*t)->right));

    delete_node(t);
}
Exemple #2
0
int parse_ld_line(ld_line_t line) {
    int rv = PLC_OK;
    if(line == (ld_line_t)NULL)
        return PLC_ERR;    
    
    int c = LD_AND; //default character = '-'
    BYTE n_mode = FALSE;
    
    while (line->status == STATUS_UNRESOLVED 
    && c != LD_NODE) {	//loop	    
		c = read_char(line->buf, line->cursor);
		switch (c) {
		    case LD_NODE://PAUSE
				break;	
			case ERR_BADCHAR:
			case (BYTE)PLC_ERR:
				rv = PLC_ERR;
				line->status = STATUS_ERROR;
				break;
            case OP_END:/*this should happen only if line ends without 
                           a valid coil*/
				line->status = STATUS_RESOLVED;
				line->stmt = NULL;//clear_tree(line->stmt);
				break;
            case LD_OR:
            case LD_BLANK://if blank or '|', empty value for the line.
				line->cursor++;
				line->stmt = NULL;//clear_tree(line->stmt);
				break;
		    case LD_NOT:
				n_mode = TRUE;	//normally closed mode
            case LD_AND:
				line->cursor++;
				break;
			case LD_COIL://see if it is a coil: ()[] 
            case LD_SET:
            case LD_RESET:
            case LD_DOWN:
				rv = handle_coil(c, line);
				break;
            default://otherwise operand is expected(i,q,f,r,m,t,c,b)
                rv = handle_operand(c, n_mode, line);
                n_mode = FALSE;
                break;
        }
    }
    if(rv < PLC_OK)
        line->stmt = clear_tree(line->stmt);
    return rv;
}
Exemple #3
0
void testRBtree_delete_is_root_nil(CuTest *tc){
    RB_tree tree;
    tree.nil = (RB_node*)malloc(sizeof(RB_node));
    tree.nil->color = BLACK;
    tree.nil->key = -10;

    tree.root = tree.nil;
    insert_node(&tree, 5);
    delete_node(&tree, 5);
    CuAssertTrue(tc, tree.root == tree.nil);

    clear_tree(&tree);
    free(tree.nil);
}
Exemple #4
0
void testRBtree_height(CuTest *tc){
    RB_tree tree;
    tree.nil = (RB_node*)malloc(sizeof(RB_node));
    tree.nil->color = BLACK;
    tree.nil->key = -10;

    tree.root = tree.nil;
    int i;
    for(i = 32000; i >= 0; --i){
        insert_node(&tree, i);
    }
    //test

    void seek(RB_node *node, RB_node *nil, int *params, int height){
        if(node->left == nil && node->right == nil){
            if(height < params[0]){
                params[0] = height;
            }
            if(height > params[1]){
                params[1] = height;
            }
        }else{
            if(node->left != nil){
                seek(node->left, nil, params, height + 1);
            }
            if(node->right != nil){
                seek(node->right, nil, params, height + 1);
            }
        }
    }

    void DFS(RB_tree* tree, int *params){
        if(tree->root == tree->nil){
            params[0] = 0;
            params[1] = 1;
        }else{
            seek(tree->root, tree->nil, params, 0);
        }
    };
    int min_max[2];
    min_max[0] = 2000000;
    min_max[1] = 0;

    DFS(&tree, min_max);
    printf("\nRed-Black Tree height test\nfor 32000 nodes, min >= dif:\nmin: %d\nmax: %d\ndif: %d\n", min_max[0] + 1, min_max[1] + 1, min_max[1] - min_max[0]);
    CuAssertTrue(tc, 2 * min_max[0] - min_max[1] >= 0);
    clear_tree(&tree);
    free(tree.nil);
}
Exemple #5
0
int	main()
{
	unsigned int key;
	node *arbre;

	arbre = NULL;
	addnode(&arbre, 30, "la");
	addnode(&arbre, 11, " vie");
	addnode(&arbre, 30, " est");
	addnode(&arbre, 32, " belle");
	addnode(&arbre, 1, " et");
	addnode(&arbre, 4, " la");
	addnode(&arbre, 5, " mort");
	addnode(&arbre, 6, " est");
	addnode(&arbre, 1, " douce");

	ft_putstr("-------------------------------------\n");
	print_tree(arbre);
	ft_putstr("-------------------------------------\n");
	print_reverse_tree(arbre);
	ft_putstr("-------------------------------------\n");

	key = 1;
	if (search_node(arbre, key))
	{
		ft_putstr("la cle existe et vaut : ");
		ft_putnbr(key);
		ft_putstr("\n");
	}
	else
		ft_putstr("la cle n existe pas\n");
	key = 66;
		if (search_node(arbre, key))
	{
		ft_putstr("la cle existe et vaut : ");
		ft_putnbr(key);
		ft_putstr("\n");
	}
	else
		ft_putstr("la cle n existe pas\n");

	clear_tree(&arbre);
	return (0);
}
Exemple #6
0
void testRBtree_in_order(CuTest *tc){
    RB_tree tree;
    tree.nil = (RB_node*)malloc(sizeof(RB_node));
    tree.nil->color = BLACK;
    tree.nil->key = -10;

    tree.root = tree.nil;
    int i;
    for(i = 31; i >= 0; --i){
        insert_node(&tree, i);
    }
    //test
    printf("\nRed-Black Tree simple insert, in_order test\nInserted nodes 0-31, in_order:\n");
    in_order(tree.root, tree.nil);
    printf("\n");

    clear_tree(&tree);
    free(tree.nil);
}
Exemple #7
0
void testRBtree_delete(CuTest *tc){
    RB_tree tree;
    tree.nil = (RB_node*)malloc(sizeof(RB_node));
    tree.nil->color = BLACK;
    tree.nil->key = -10;

    tree.root = tree.nil;
    int i;
    for(i = 63; i > 0; --i){
        insert_node(&tree, (short int)i);
    }
    for(i = 0; i < 63; i += 2){
        delete_node(&tree, (short int)i);
    }
    //test
    printf("\nRed-Black Tree delete test\nInserted nodes 1-63, even numbers from 0 to 62 removed\n(for 0 no error), ");
    RB_display_keys_in_order(&tree);

    clear_tree(&tree);
    free(tree.nil);
}
Exemple #8
0
void build_tree( void )
{
    clear_tree( );

    GtkTree* tree = GTK_TREE( context.tree );

    PAKFile* pak = context.pak;
    PAKTree& pak_tree = pak->getTree( );
    PAKTreeNode& root = pak_tree.getRoot( );

    // Construct the special root node.
    //    GtkWidget* item = gtk_tree_item_new_with_label( const_cast<char*>( pak->getPakName( ) ) );
    GtkWidget* item = gtk_tree_item_new_with_label( context.pak_filename );
    gtk_signal_connect( GTK_OBJECT( item ), "select",
			GTK_SIGNAL_FUNC( tree_item_selected ), NULL );
    gtk_tree_append( tree, item );
    gtk_widget_show( item );
    GtkWidget* root_tree = gtk_tree_new( );
    gtk_tree_item_set_subtree( GTK_TREE_ITEM( item ), root_tree );

    add_children_to_tree( GTK_TREE( root_tree ), root );
}
int main()
{
    Tree *tree = createTree();
    Hashtable *table5, *table50, *table500;
    FILE *fp50, *fp500, *fp5mil;
    clock_t tstart, tend;
    time_t t;
    srand((unsigned) time(&t));
    double favg;
    int data = 0, i = 0, search, value, found;
    int array50k[50000], *array500k, *array5mil;
    array500k = malloc(500001 * sizeof(int));
    array5mil = malloc(5000001 * sizeof(int));
    if(tree == NULL)
        printf("Memory allocation failed...");
    fp50 = fopen("50tus.txt", "r");
    fp500 = fopen("500tus.txt", "r");
    fp5mil = fopen("5mil.txt", "r");
    if(fp50 == NULL || fp500 == NULL|| fp5mil == NULL)
    {
        fprintf(stderr, "error\n");
        return 1;
    }
    system("color 3");
    table5 = create_table(50000000);
    table50 = create_table(500000);
    table500 = create_table(5000000);

    /*50k*/
    printf("\n50k insert, balance and search!\n");
    printf("Insert: 50k ");
    tstart = clock(); // start
    for(i = 0; i < 50000; ++i)
    {
        fscanf(fp50,"%d\n", &data);
        insert(tree, data);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    tree_to_arr(tree);
    printf("Searching 100x in 50k ");
    tstart = clock(); // start
    find_tree(tree);
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    clear_tree(tree);

    /*500k*/
    printf("\n500k insert, balance and search!\n");
    printf("Insert: 500k ");
    tstart = clock(); // start
    for(i = 0; i < 500000; ++i)
    {
        fscanf(fp500,"%d\n", &data);
        insert(tree, data);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    tree_to_arr(tree);
    printf("Searching 100x in 500k ");
    tstart = clock(); // start
    find_tree(tree);
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    clear_tree(tree);

    /*5mille*/
    printf("\n5 million insert, balance and search!\n");
    printf("Insert: 5 million ");
    tstart = clock(); // start
    for(i = 0; i < 5000000; ++i)
    {
        fscanf(fp5mil,"%d\n", &data);
        insert(tree, data);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    tree_to_arr(tree);
    printf("Searching 100x in 5 million ");
    tstart = clock(); // start
    find_tree(tree);
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    clear_tree(tree);

    /*50k sekvens*/
    printf("\nSekvential insert 50k!\n");
    tstart = clock(); // start
    for(i = 0; i<50000; i++)
    {
        fscanf(fp50, "%d\n", &array50k[i]);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    printf("Sekvential search 50k, 100x!\n");
    tstart = clock(); // start
    for(i = 0; i<100; i++)
    {
        search = randomGen();
        sekvential_search(array50k, search, 50000);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    printf("Sekvential search 50k, 100x(o)!\n");
    tstart = clock(); // start
    for(i = 0; i<100; i++)
    {
        search = randomGen();
        sekvent_search(array50k, search, 50000);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);

    /*500k sekvens*/
    printf("\nSekvential insert 500k!\n");
    tstart = clock(); // start
    for(i = 0; i<500000; i++)
    {
        fscanf(fp500, "%d\n", &array500k[i]);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    printf("Sekvential search 500k, 100x!\n");
    tstart = clock(); // start
    for(i = 0; i<100; i++)
    {
        search = randomGen();
        sekvential_search(array500k, search, 500000);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    printf("Sekvential search 500k, 100x(o)!\n");
    tstart = clock(); // start
    for(i = 0; i<100; i++)
    {
        search = randomGen();
        sekvent_search(array500k, search, 500000);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);

    /*5mille seq*/
    printf("\nSekvential insert 5 million!\n");
    tstart = clock(); // start
    for(i = 0; i<5000000; i++)
    {
        fscanf(fp5mil, "%d\n", &array5mil[i]);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    printf("Sekvential search 5 million, 100x!\n");
    tstart = clock(); // start
    for(i = 0; i<100; i++)
    {
        search = randomGen();
        sekvential_search(array5mil, search, 5000000);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);
    printf("Sekvential search 5 million, 100x(o)!\n");
    tstart = clock(); // start
    for(i = 0; i<100; i++)
    {
        search = randomGen();
        sekvent_search(array5mil, search, 5000000);
    }
    tend = clock(); // end
    favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
    printf("Avg. execution time: %g sec\n",favg);

    /*Hash 500k*/
    printf("\nHash insert 50k!\n");
    tstart = clock(); // start
    for(i = 0; i<50000; i++){
        fscanf(fp500, "%d\n", &value);
        insert_hash(table50, value);
    }
        tend = clock(); // end
        favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
        printf("Avg. execution time: %g sec\n",favg);

    /*Hash 500k*/
    printf("\nHash insert 500k!\n");
    tstart = clock(); // start
    for(i = 0; i<500000; i++){
        fscanf(fp500, "%d\n", &value);
        insert_hash(table500, value);
    }
        tend = clock(); // end
        favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
        printf("Avg. execution time: %g sec\n",favg);

    /*Hash 5 mille*/
    printf("\nHash insert 5 million!\n");
    tstart = clock(); // start
    for(i = 0; i<5000000; i++){
        fscanf(fp5mil, "%d\n", &value);
        insert_hash(table5, value);
    }
        tend = clock(); // end
        favg = ((double)(tend - tstart))/CLOCKS_PER_SEC;
        printf("Avg. execution time: %g sec\n",favg);

    destroy_table(table5);
    destroy_table(table50);
    destroy_table(table500);
    destroyTree(tree);
    return 0;
}
void clear_tree_local(){
   clear_tree();
}
Exemple #11
0
sint palign2(char *p1_tree_name,char *p2_tree_name)  /* a profile alignment */
{
   sint 	i,j,sum,entries,status;
   lint 		score;
   sint 	*aligned, *group;
   sint		*maxid,*p1_weight,*p2_weight;
/*   sint dscore; */

   info("Start of Multiple Alignment");

/* get the phylogenetic trees from *.ph */

   if (profile1_nseqs >= 2)
     {
        status = read_tree(p1_tree_name, (sint)0, profile1_nseqs);
        if (status == 0) return(0);
     }

/* calculate sequence weights according to branch lengths of the tree -
   weights in global variable seq_weight normalised to sum to 100 */

   p1_weight = (sint *) ckalloc( (profile1_nseqs) * sizeof(sint) );

   calc_seq_weights((sint)0, profile1_nseqs, p1_weight);

/* clear the memory for the phylogenetic tree */

   if (profile1_nseqs >= 2)
        clear_tree(NULL);

   if (nseqs-profile1_nseqs >= 2)
     {
        status = read_tree(p2_tree_name, profile1_nseqs, nseqs);
        if (status == 0) return(0);
     }

   p2_weight = (sint *) ckalloc( (nseqs) * sizeof(sint) );

   calc_seq_weights(profile1_nseqs,nseqs, p2_weight);


/* clear the memory for the phylogenetic tree */

   if (nseqs-profile1_nseqs >= 2)
        clear_tree(NULL);

/* convert tmat distances to similarities */

   for (i=1;i<nseqs;i++)
        for (j=i+1;j<=nseqs;j++) {
            tmat[i][j]=100.0-tmat[i][j]*100.0;
            tmat[j][i]=tmat[i][j];
        }
     

/* weight sequences with max percent identity with other profile*/

   maxid = (sint *)ckalloc( (nseqs+1) * sizeof (sint));
   for (i=0;i<profile1_nseqs;i++) {
         maxid[i] = 0;
         for (j=profile1_nseqs+1;j<=nseqs;j++) 
                      if(maxid[i]<tmat[i+1][j]) maxid[i] = tmat[i+1][j];
         seq_weight[i] = maxid[i]*p1_weight[i];
   }

   for (i=profile1_nseqs;i<nseqs;i++) {
         maxid[i] = -1;
         for (j=1;j<=profile1_nseqs;j++)
                      if(maxid[i]<tmat[i+1][j]) maxid[i] = tmat[i+1][j];
         seq_weight[i] = maxid[i]*p2_weight[i];
   }
/*
  Normalise the weights, such that the sum of the weights = INT_SCALE_FACTOR
*/

         sum = 0;
         for (j=0;j<nseqs;j++)
            sum += seq_weight[j];
         if (sum == 0)
          {
           for (j=0;j<nseqs;j++)
                seq_weight[j] = 1;
                sum = j;
          }
         for (j=0;j<nseqs;j++)
             {
               seq_weight[j] = (seq_weight[j] * INT_SCALE_FACTOR) / sum;
               if (seq_weight[j] < 1) seq_weight[j] = 1;
             }
if (debug > 1) {
  fprintf(stdout,"new weights\n");
  for (j=0;j<nseqs;j++) fprintf( stdout,"sequence %d: %d\n", j+1,seq_weight[j]);
}


/* do the alignment.........  */

   info("Aligning...");

   group = (sint *)ckalloc( (nseqs+1) * sizeof (sint));

   for(i=1; i<=profile1_nseqs; ++i)
         group[i] = 1;
   for(i=profile1_nseqs+1; i<=nseqs; ++i)
         group[i] = 2;
   entries = nseqs;

   aligned = (sint *)ckalloc( (nseqs+1) * sizeof (sint) );
   for (i=1;i<=nseqs;i++) aligned[i] = 1;

   score = prfalign(group, aligned);
   info("Sequences:%d      Score:%d",(pint)entries,(pint)score);
   group=ckfree((void *)group);
   p1_weight=ckfree((void *)p1_weight);
   p2_weight=ckfree((void *)p2_weight);
   aligned=ckfree((void *)aligned);
   maxid=ckfree((void *)maxid);

/* DES   output_index = (int *)ckalloc( (nseqs+1) * sizeof (int)); */
   for (i=1;i<=nseqs;i++) output_index[i] = i;

   return(nseqs);
}
Exemple #12
0
sint malign(sint istart,char *phylip_name) /* full progressive alignment*/
{
   static 	sint *aligned;
   static 	sint *group;
   static 	sint ix;

   sint 	*maxid, max, sum;
   sint		*tree_weight;
   sint 		i,j,set,iseq=0;
   sint 		status,entries;
   lint		score = 0;


   info("Start of Multiple Alignment");

/* get the phylogenetic tree from *.ph */

   if (nseqs >= 2) 
     {
       status = read_tree(phylip_name, (sint)0, nseqs);
       if (status == 0) return((sint)0);
     }

/* calculate sequence weights according to branch lengths of the tree -
   weights in global variable seq_weight normalised to sum to 100 */

   calc_seq_weights((sint)0, nseqs, seq_weight);

/* recalculate tmat matrix as percent similarity matrix */

   status = calc_similarities(nseqs);
   if (status == 0) return((sint)0);

/* for each sequence, find the most closely related sequence */

   maxid = (sint *)ckalloc( (nseqs+1) * sizeof (sint));
   for (i=1;i<=nseqs;i++)
     {
         maxid[i] = -1;
         for (j=1;j<=nseqs;j++) 
           if (j!=i && maxid[i] < tmat[i][j]) maxid[i] = tmat[i][j];
     }

/* group the sequences according to their relative divergence */

   if (istart == 0)
     {
        sets = (sint **) ckalloc( (nseqs+1) * sizeof (sint *) );
        for(i=0;i<=nseqs;i++)
           sets[i] = (sint *)ckalloc( (nseqs+1) * sizeof (sint) );

        create_sets((sint)0,nseqs);
        info("There are %d groups",(pint)nsets);

/* clear the memory used for the phylogenetic tree */

        if (nseqs >= 2)
             clear_tree(NULL);

/* start the multiple alignments.........  */

        info("Aligning...");

/* first pass, align closely related sequences first.... */

        ix = 0;
        aligned = (sint *)ckalloc( (nseqs+1) * sizeof (sint) );
        for (i=0;i<=nseqs;i++) aligned[i] = 0;

        for(set=1;set<=nsets;++set)
         {
          entries=0;
          for (i=1;i<=nseqs;i++)
            {
               if ((sets[set][i] != 0) && (maxid[i] > divergence_cutoff))
                 {
                    entries++;
                    if  (aligned[i] == 0)
                       {
                          if (output_order==INPUT)
                            {
                              ++ix;
                              output_index[i] = i;
                            }
                          else output_index[++ix] = i;
                          aligned[i] = 1;
                       }
                 }
            }

          if(entries > 0) score = prfalign(sets[set], aligned);
          else score=0.0;


/* negative score means fatal error... exit now!  */

          if (score < 0) 
             {
                return(-1);
             }
          if ((entries > 0) && (score > 0))
             info("Group %d: Sequences:%4d      Score:%d",
             (pint)set,(pint)entries,(pint)score);
          else
             info("Group %d:                     Delayed",
             (pint)set);
        }

        for (i=0;i<=nseqs;i++)
          sets[i]=ckfree((void *)sets[i]);
        sets=ckfree(sets);
     }
   else
     {
/* clear the memory used for the phylogenetic tree */

        if (nseqs >= 2)
             clear_tree(NULL);

        aligned = (sint *)ckalloc( (nseqs+1) * sizeof (sint) );
        ix = 0;
        for (i=1;i<=istart+1;i++)
         {
           aligned[i] = 1;
           ++ix;
           output_index[i] = i;
         }
        for (i=istart+2;i<=nseqs;i++) aligned[i] = 0;
     }

/* second pass - align remaining, more divergent sequences..... */

/* if not all sequences were aligned, for each unaligned sequence,
   find it's closest pair amongst the aligned sequences.  */

    group = (sint *)ckalloc( (nseqs+1) * sizeof (sint));
    tree_weight = (sint *) ckalloc( (nseqs) * sizeof(sint) );
    for (i=0;i<nseqs;i++)
   		tree_weight[i] = seq_weight[i];

/* if we haven't aligned any sequences, in the first pass - align the
two most closely related sequences now */
   if(ix==0)
     {
        max = -1;
	iseq = 0;
        for (i=1;i<=nseqs;i++)
          {
             for (j=i+1;j<=nseqs;j++)
               {
                  if (max < tmat[i][j])
		  {
                     max = tmat[i][j];
                     iseq = i;
                  }
              }
          }
        aligned[iseq]=1;
        if (output_order == INPUT)
          {
            ++ix;
            output_index[iseq] = iseq;
          }
         else
            output_index[++ix] = iseq;
     }

    while (ix < nseqs)
      {
             for (i=1;i<=nseqs;i++) {
                if (aligned[i] == 0)
                  {
                     maxid[i] = -1;
                     for (j=1;j<=nseqs;j++) 
                        if ((maxid[i] < tmat[i][j]) && (aligned[j] != 0))
                            maxid[i] = tmat[i][j];
                  }
              }
/* find the most closely related sequence to those already aligned */

            max = -1;
	    iseq = 0;
            for (i=1;i<=nseqs;i++)
              {
                if ((aligned[i] == 0) && (maxid[i] > max))
                  {
                     max = maxid[i];
                     iseq = i;
                  }
              }


/* align this sequence to the existing alignment */
/* weight sequences with percent identity with profile*/
/* OR...., multiply sequence weights from tree by percent identity with new sequence */
   if(no_weights==FALSE) {
   for (j=0;j<nseqs;j++)
       if (aligned[j+1] != 0)
              seq_weight[j] = tree_weight[j] * tmat[j+1][iseq];
/*
  Normalise the weights, such that the sum of the weights = INT_SCALE_FACTOR
*/

         sum = 0;
         for (j=0;j<nseqs;j++)
           if (aligned[j+1] != 0)
            sum += seq_weight[j];
         if (sum == 0)
          {
           for (j=0;j<nseqs;j++)
                seq_weight[j] = 1;
                sum = j;
          }
         for (j=0;j<nseqs;j++)
           if (aligned[j+1] != 0)
             {
               seq_weight[j] = (seq_weight[j] * INT_SCALE_FACTOR) / sum;
               if (seq_weight[j] < 1) seq_weight[j] = 1;
             }
	}

         entries = 0;
         for (j=1;j<=nseqs;j++)
           if (aligned[j] != 0)
              {
                 group[j] = 1;
                 entries++;
              }
           else if (iseq==j)
              {
                 group[j] = 2;
                 entries++;
              }
         aligned[iseq] = 1;

         score = prfalign(group, aligned);
         info("Sequence:%d     Score:%d",(pint)iseq,(pint)score);
         if (output_order == INPUT)
          {
            ++ix;
            output_index[iseq] = iseq;
          }
         else
            output_index[++ix] = iseq;
      }

   group=ckfree((void *)group);
   aligned=ckfree((void *)aligned);
   maxid=ckfree((void *)maxid);
   tree_weight=ckfree((void *)tree_weight);

   aln_score();

/* make the rest (output stuff) into routine clustal_out in file amenu.c */

   return(nseqs);

}
Exemple #13
0
sint seqalign(sint istart,char *phylip_name) /* sequence alignment to existing profile */
{
   static 	sint *aligned, *tree_weight;
   static 	sint *group;
   static 	sint ix;

   sint 	*maxid, max;
   sint 		i,j,status,iseq=0;
   sint 		sum,entries;
   lint		score = 0;


   info("Start of Multiple Alignment");

/* get the phylogenetic tree from *.ph */

   if (nseqs >= 2) 
     {
       status = read_tree(phylip_name, (sint)0, nseqs);
       if (status == 0) return(0);
     }

/* calculate sequence weights according to branch lengths of the tree -
   weights in global variable seq_weight normalised to sum to 100 */

   calc_seq_weights((sint)0, nseqs, seq_weight);
   
   tree_weight = (sint *) ckalloc( (nseqs) * sizeof(sint) );
   for (i=0;i<nseqs;i++)
   		tree_weight[i] = seq_weight[i];

/* recalculate tmat matrix as percent similarity matrix */

   status = calc_similarities(nseqs);
   if (status == 0) return((sint)0);

/* for each sequence, find the most closely related sequence */

   maxid = (sint *)ckalloc( (nseqs+1) * sizeof (sint));
   for (i=1;i<=nseqs;i++)
     {
         maxid[i] = -1;
         for (j=1;j<=nseqs;j++) 
           if (maxid[i] < tmat[i][j]) maxid[i] = tmat[i][j];
     }

/* clear the memory used for the phylogenetic tree */

        if (nseqs >= 2)
             clear_tree(NULL);

        aligned = (sint *)ckalloc( (nseqs+1) * sizeof (sint) );
        ix = 0;
        for (i=1;i<=istart+1;i++)
         {
           aligned[i] = 1;
           ++ix;
           output_index[i] = i;
         }
        for (i=istart+2;i<=nseqs;i++) aligned[i] = 0;

/* for each unaligned sequence, find it's closest pair amongst the
   aligned sequences.  */

    group = (sint *)ckalloc( (nseqs+1) * sizeof (sint));

    while (ix < nseqs)
      {
        if (ix > 0) 
          {
             for (i=1;i<=nseqs;i++) {
                if (aligned[i] == 0)
                  {
                     maxid[i] = -1;
                     for (j=1;j<=nseqs;j++) 
                        if ((maxid[i] < tmat[i][j]) && (aligned[j] != 0))
                            maxid[i] = tmat[i][j];
                  }
              }
          }

/* find the most closely related sequence to those already aligned */

         max = -1;
         for (i=1;i<=nseqs;i++)
           {
             if ((aligned[i] == 0) && (maxid[i] > max))
               {
                  max = maxid[i];
                  iseq = i;
               }
           }

/* align this sequence to the existing alignment */

         entries = 0;
         for (j=1;j<=nseqs;j++)
           if (aligned[j] != 0)
              {
                 group[j] = 1;
                 entries++;
              }
           else if (iseq==j)
              {
                 group[j] = 2;
                 entries++;
              }
         aligned[iseq] = 1;


/* EITHER....., set sequence weights equal to percent identity with new sequence */
/*
           for (j=0;j<nseqs;j++)
              seq_weight[j] = tmat[j+1][iseq];
*/
/* OR...., multiply sequence weights from tree by percent identity with new sequence */
           for (j=0;j<nseqs;j++)
              seq_weight[j] = tree_weight[j] * tmat[j+1][iseq];
if (debug>1)         
  for (j=0;j<nseqs;j++) if (group[j+1] == 1)fprintf (stdout,"sequence %d: %d\n", j+1,tree_weight[j]);
/*
  Normalise the weights, such that the sum of the weights = INT_SCALE_FACTOR
*/

         sum = 0;
         for (j=0;j<nseqs;j++)
           if (group[j+1] == 1) sum += seq_weight[j];
         if (sum == 0)
          {
           for (j=0;j<nseqs;j++)
                seq_weight[j] = 1;
                sum = j;
          }
         for (j=0;j<nseqs;j++)
             {
               seq_weight[j] = (seq_weight[j] * INT_SCALE_FACTOR) / sum;
               if (seq_weight[j] < 1) seq_weight[j] = 1;
             }

if (debug > 1) {
  fprintf(stdout,"new weights\n");
  for (j=0;j<nseqs;j++) if (group[j+1] == 1)fprintf( stdout,"sequence %d: %d\n", j+1,seq_weight[j]);
}

         score = prfalign(group, aligned);
         info("Sequence:%d     Score:%d",(pint)iseq,(pint)score);
         if (output_order == INPUT)
          {
            ++ix;
            output_index[iseq] = iseq;
          }
         else
            output_index[++ix] = iseq;
      }

   group=ckfree((void *)group);
   aligned=ckfree((void *)aligned);
   maxid=ckfree((void *)maxid);

   aln_score();

/* make the rest (output stuff) into routine clustal_out in file amenu.c */

   return(nseqs);

}