src/algo/phy_tree/fastme/inputs.cpp

Go to the documentation of this file.
00001 /*  $Id: inputs.cpp 103491 2007-05-04 17:18:18Z kazimird $
00002 * ===========================================================================
00003 *
00004 *                            PUBLIC DOMAIN NOTICE
00005 *               National Center for Biotechnology Information
00006 *
00007 *  This software/database is a "United States Government Work" under the
00008 *  terms of the United States Copyright Act.  It was written as part of
00009 *  the author's official duties as a United States Government employee and
00010 *  thus cannot be copyrighted.  This software/database is freely available
00011 *  to the public for use. The National Library of Medicine and the U.S.
00012 *  Government have not placed any restriction on its use or reproduction.
00013 *
00014 *  Although all reasonable efforts have been taken to ensure the accuracy
00015 *  and reliability of the software and data, the NLM and the U.S.
00016 *  Government do not and cannot warrant the performance or results that
00017 *  may be obtained by using this software or data. The NLM and the U.S.
00018 *  Government disclaim all warranties, express or implied, including
00019 *  warranties of performance, merchantability or fitness for any particular
00020 *  purpose.
00021 *
00022 *  Please cite the author in any work or product based on this material.
00023 *
00024 * ===========================================================================
00025 *
00026 * Author:  Richard Desper
00027 *
00028 * File Description:  inputs.cpp
00029 *
00030 *    A part of the Miminum Evolution algorithm
00031 *
00032 */
00033 
00034 #include <ncbi_pch.hpp>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <math.h>
00038 #include "fastme.h"
00039 #include "graph.h"
00040 
00041 BEGIN_NCBI_SCOPE
00042 BEGIN_SCOPE(fastme)
00043 
00044 boolean leaf(meNode *v);
00045 
00046 meEdge *depthFirstTraverse(meTree *T, meEdge *e);
00047 
00048 void compareSets(meTree *T, meSet *S, FILE *ofile)
00049 {
00050   meEdge *e;
00051   meNode *v,*w;
00052   meSet *X;
00053   e = depthFirstTraverse(T,NULL);
00054   while (NULL != e)
00055     {
00056       v = e->head;
00057       for(X = S; NULL != X; X = X->secondNode)
00058     {
00059       w = X->firstNode;
00060       if (0 == strcmp(v->label,w->label))
00061         {
00062           v->index2 = w->index2;
00063         w->index2 = -1;
00064         break;
00065         }
00066     }
00067       e = depthFirstTraverse(T,e);
00068     }
00069   v = T->root;
00070   for(X = S; NULL != X; X = X->secondNode)
00071     {
00072       w = X->firstNode;
00073       if (0 == strcmp(v->label,w->label))
00074     {
00075       v->index2 = w->index2;
00076       w->index2 = -1;
00077       break;
00078     }
00079     }
00080   if (-1 == v->index2)
00081     {
00082       fprintf(stderr,"Error leaf %s in meTree not in distance matrix.\n",v->label);
00083       exit(EXIT_FAILURE);
00084 }
00085   e = depthFirstTraverse(T,NULL);
00086   while (NULL != e)
00087     {
00088       v = e->head;
00089       if ((leaf(v)) && (-1 == v->index2))
00090     {
00091       fprintf(stderr,"Error leaf %s in meTree not in distance matrix.\n",v->label);
00092       exit(EXIT_FAILURE);
00093     }
00094       e = depthFirstTraverse(T,e);
00095       }
00096   for(X = S; NULL != X; X = X->secondNode)
00097     if (X->firstNode->index2 > -1)
00098       {
00099     fprintf(ofile,"(v1:0.0)v2;");
00100     fclose(ofile);
00101     fprintf(stderr,"Error meNode %s in matrix but not a leaf in tree.\n",X->firstNode->label);
00102     exit(EXIT_FAILURE);
00103       }
00104 }
00105 
00106 void freeMatrix(double **D, int size)
00107 {
00108   int i;
00109   for(i=0;i<size;i++)
00110     free(D[i]);
00111   free(D);
00112 }
00113 
00114 double **loadMatrix(double **table_in, char **labels, int *size_in, meSet *S)
00115 {
00116 /*   char nextString[MAX_EVENT_NAME]; */
00117   meNode *v;
00118   double **table;
00119   int *size;
00120   int i,j;
00121 
00122   size = size_in;
00123   if ((*size < 0) || (*size > MAXSIZE))
00124     {
00125       printf("Problem inputting size.\n");
00126       exit(EXIT_FAILURE);
00127     }
00128   table = (double **) malloc(*size*sizeof(double *));
00129   for(i=0;i<*size;i++)
00130     {
00131       j = 0;
00132       table[i] = (double *) malloc(*size*sizeof(double));
00133 /*       if (!(fscanf(ifile,"%s",nextString))) */
00134 /*       { */
00135 /*           fprintf(stderr,"Error loading label %d.\n",i); */
00136 /*           exit(EXIT_FAILURE); */
00137 /*       } */
00138 /*       v = makeNewNode(nextString,-1); */
00139       v = makeNewNode(labels[i],-1);
00140       v->index2 = i;
00141       S = addToSet(v,S);
00142       while (j < *size)
00143       {
00144 /*           if (!(fscanf(ifile,"%s",nextString))) */
00145 /*        { */
00146 /*               fprintf(stderr,"Error loading (%d,%d)-entry.\n",i,j); */
00147 /*               exit(EXIT_FAILURE); */
00148 /*        } */
00149 /*           table[i][j++] = atof(nextString); */
00150           table[i][j] = table_in[i][j];
00151           j++;
00152       }   
00153     }
00154     return(table);
00155 }
00156 
00157 double **loadMatrixOLD(FILE *ifile, int *size, meSet *S)
00158 {
00159   char nextString[MAX_EVENT_NAME];
00160   meNode *v;
00161   double **table;
00162   int i,j;
00163   if (!(fscanf(ifile,"%s",nextString)))
00164     {
00165       fprintf(stderr,"Error loading input matrix.\n");
00166       exit(EXIT_FAILURE);
00167     }
00168   *size = atoi(nextString);
00169   if ((*size < 0) || (*size > MAXSIZE))
00170     {
00171       printf("Problem inputting size.\n");
00172       exit(EXIT_FAILURE);
00173     }
00174   table = (double **) malloc(*size*sizeof(double *));
00175   for(i=0;i<*size;i++)
00176     {
00177       j = 0;
00178       table[i] = (double *) malloc(*size*sizeof(double));
00179       if (!(fscanf(ifile,"%s",nextString)))
00180     {
00181       fprintf(stderr,"Error loading label %d.\n",i);
00182       exit(EXIT_FAILURE);
00183     }
00184       v = makeNewNode(nextString,-1);
00185       v->index2 = i;
00186       S = addToSet(v,S);
00187       while (j < *size)
00188     {
00189       if (!(fscanf(ifile,"%s",nextString)))
00190         {
00191           fprintf(stderr,"Error loading (%d,%d)-entry.\n",i,j);
00192           exit(EXIT_FAILURE);
00193         }
00194       table[i][j++] = atof(nextString);
00195     }
00196     }
00197   return(table);
00198 }
00199 
00200 void partitionSizes(meTree *T)
00201 {
00202   meEdge *e;
00203   e = depthFirstTraverse(T,NULL);
00204   while (NULL != e)
00205     {
00206       if (leaf(e->head))
00207     e->bottomsize = 1;
00208       else
00209     e->bottomsize = e->head->leftEdge->bottomsize 
00210       + e->head->rightEdge->bottomsize;
00211       e->topsize = (T->size + 2)/2 - e->bottomsize;
00212       e = depthFirstTraverse(T,e);
00213     }
00214 }
00215 
00216 
00217 END_SCOPE(fastme)
00218 END_NCBI_SCOPE
00219 
00220 

Generated on Sun Dec 6 22:18:53 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:20:52 2009 by modify_doxy.py rev. 173732