Beispiel #1
0
/**
* Reads all of the names in a file into an array of
* strings. Each element of the array is a char pointer.
*
* If nStrings is non-NULL, the number of strings is
* returned in nStrings.
*
* The array is always one larger than nStrings elements
* long and the last element is a NULL.
*
* @param fileName Name of file to read
* @param nStrings Optional pointer to integer that will on return contain
*                 the count of strings read from the file
* @returns A NULL-terminated list of NULL-terminated strings
*/
ListOfStrings
readListOfStrings (const char *fileName, int *nStrings)
{
    Tokenizer tok;
    void *stringList = NULL;
    FILE *f;
    char *string;
    ListOfStrings array;
    size_t numStrings;

    f = fopen (fileName, "r");
    DEBUG_CHECK (f, "Unable to read file");

    tokenizerInit (&tok, tokenizerStreamReader, f);
    while (!tokenizerEndOfFile (&tok))
    {
        string = strdup (tokenizerGetWord (&tok));
        listAccumulate (&stringList, &string, sizeof (unsigned char *));
    }

    fclose (f);

    /* Add a terminating NULL */
    string = NULL;
    listAccumulate (&stringList, &string, sizeof (unsigned char *));

    array = (ListOfStrings)listToArray (&stringList, sizeof (unsigned char *), &numStrings);
    if (nStrings) *nStrings = numStrings - 1; /* Don't count the NULL */
    return array;
}
Beispiel #2
0
void subTree(int n,int m,int r,int* & P,int* & Suc1,int* & Suc2,double* & B,string* & Support,string* & Labels,list<int> & P1,list<double> & B1,list<string> & Support1,list<string> &Labels1){
     list<int> nodes = sub(r,n,m,P,Suc1,Suc2);
     int *tab=new int[nodes.size()];
     listToArray(nodes,tab);
     sort(tab,(int)nodes.size());
    for (int i=0;i<nodes.size();i++){
         int j = index(tab,P[tab[i]],(int)nodes.size());
         P1.push_back(j);
         B1.push_back(B[tab[i]]);
         Support1.push_back(Support[tab[i]]);
         Labels1.push_back(Labels[tab[i]]);
     }
    delete[] tab;
}
Beispiel #3
0
int main (int argc, char **argv)
{
    double scale = 1.;
    double width = 80.;
    double atom  = 1e-8; /* 1e-8 */
    double d;
    int n;
    gatherOptions(argc, argv, &scale, &width, &atom);
    doubleList_t *list = NULL;
    initList(list);
    while (scanf("%lf", &d) == 1) {
        addToList(&list, d);
    }
    double *array;
    n = countList(list);
    array = listToArray(list, n);
    releaseList(list);
    stem_leaf(array, n, scale, width, atom);
    free(array);
    return EXIT_SUCCESS;
}
Beispiel #4
0
void extrait_outgroup(string inFile,string outFile,list<string> &outgroups,int nb){
     int n;//the number of internal nodes
	 int m;//the number of branches
     int n1,m1;
    counting(inFile,n,m,nb);
    int *P = new int[m+1];//tableau de predecesseurs
    double *B = new double[m+1];//branch lengths
    string *Support = new string[m+1];
    string* Labels = new string[m+1];
	FILE * tree = fopen(inFile.c_str(),"rt");
	if (tree==NULL) cout<<"Can not open the tree file"<<endl;
	else{
         FILE * w = fopen(outFile.c_str(),"wt");
         bool rooted=false;
         for (int y=1;y<=nb;y++){
             if (rooted){
                n++;
                m++;
             }
             tree2dataS(tree,n,m,P,B,Support,Labels);
             int * Suc1= new int[n];
             int * Suc2= new int[n];
             computeSuc(P,Suc1,Suc2,m+1,n);
             list<int> P1;
             list<double> B1;
             list<string> Support1;
             list<string> Labels1;
             list<double> T1;
             int s;
             if (m==2*n){
                rooted=true;
                rooted2unrooted(n,m,P,Suc1,Suc2,B,Labels);
                m--;
                n--;
                computeSuc(P,Suc1,Suc2,m+1,n);
             }
             s= computeSuc_unrooted(P,Suc1,Suc2,m+1,n);
             list<int> out;
             list<int> in;
             for (int i=n;i<=m;i++){
                 bool flag = true;
                 for (list<string>::iterator iter=outgroups.begin();iter!=outgroups.end();iter++){
		             if (Labels[i].compare(*iter)==0){
                        out.push_back(i);                   
                        flag=false;
                        break;
                     }
                 }
                 if (flag) in.push_back(i);
             }
             if (out.size()==0){
                cout<<"Tree "<<y<<": the tree does not contain any outgroup"<<endl;  
                exit( EXIT_SUCCESS );
             }
             else{
                 int *P_new= new int[m+1];
                 int *Suc1_new= new int[n];
                 int *Suc2_new= new int[n];
                 double *B_new= new double[m+1];
                 string* Support_new = new string[m+1];
                 int t=out.front();
                 int t1=in.front();
                  if (out.size()==1){
                     reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                     subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                     int *Po=new int[m];
                     listToArray(P1,Po);
                     double *Bo=new double[m];
                     listToArray(B1,Bo);
                     string* Supporto=new string[m];
                     listToArray(Support1,Supporto);
                     string* Labelso = new string[m];
                     listToArray(Labels1,Labelso);
                     int *Suc1o=new int[n];
                     int *Suc2o=new int[n];
                     computeSuc(Po,Suc1o,Suc2o,m,n);
                     newicktree(n,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                      delete[]  Po;
                      delete[]  Bo;
                      delete[] Labelso;
                      delete[] Suc1o;
                      delete[] Suc2o;
                      delete[] Supporto;
                  }
                  else{
	                   bool flag = false;
	                   while (!flag && P[t]!=-1){
                             t = P[t];
                              flag=true;
                              for (list<int>::iterator ia=out.begin();ia!=out.end();ia++){
                                  int j=*ia; 
                                  if (!isAncestor(P,t,j)) {
                                     flag=false;break;
                                  }
                              }
                       }//t is lca of outgroups
                       if (P[t]!=-1){//lca of outgroups is not the root
                          reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                          subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                       }
                       else{//lca of outgroups is the root
	                        bool flag = false;
	                        while (!flag && P[t1]!=-1){
                                  t1 = P[t1];
                                  flag=true;
                                  for (list<int>::iterator ia=in.begin();ia!=in.end();ia++){
                                      int j=*ia;
                                      if (!isAncestor(P,t1,j)) {flag=false;break;}
                                  }
                            }
                           //t1 is lca of ingroups
                            if (P[t1]==-1){//lca of ingroups is the root
                               cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl; 
                               exit( EXIT_FAILURE );
                            }
                            else{//lca of ingroups is not the root
                                reroot(n,m,t1,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                                subTree(n,m,t1,P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                          }
                      }
                      m1=(int)P1.size()-1;
                      n1=m1/2;
                      if ((n1+out.size())==n+1){
                              int *Po = new int[m1+1];
                              listToArray(P1,Po);
                              double *Bo = new double[m1+1];
                              listToArray(B1,Bo);
                              string* Supporto = new string[m1+1];
                              listToArray(Support1,Supporto);
                              string* Labelso = new string[m1+1];
                              listToArray(Labels1,Labelso);
                              int *Suc1o = new int[n1];
                              int *Suc2o = new int[n1];
                              computeSuc(Po,Suc1o,Suc2o,m1+1,n1);
                              newicktree(n1,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                              delete[]  Po;
                              delete[]  Bo;
                              delete[]  Supporto;
                              delete[] Labelso;
                              delete[] Suc1o;
                              delete[] Suc2o;
                      }
                      else {
                              cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl;
                              exit( EXIT_FAILURE );
                      }
                      delete[] P_new;
                      delete[] Suc1_new;
                      delete[] Suc2_new;
                      delete[] B_new;
                      delete[] Support_new;
                  }
             delete[] Suc1;
             delete[] Suc2;
             }
             
         }
         fclose(tree);
        fclose(w);
    }
    delete[]  P;
    delete[]  B;
    delete[]  Support;
    delete[] Labels;
}
Beispiel #5
0
void
sortSubjectsBySimilarityToProbe (char *probe, ListOfStrings subjects, char *distanceMatrix, int *indices)
{
    int i, j, nDistances;
    DistanceMeasure *distances = NULL;
    DistanceMeasure *toSort = NULL;

    int nSubjects = countStrings (subjects);

    /* If we are using a distance matrix, then we load up the
    * list of distances from the probe image. This allows us
    * to lookup the distances from the probe quickly
    */
    if (distanceMatrix)
    {
        Tokenizer tok;
        FILE *f = fopen (makePath (distanceMatrix, probe), "r");
        void *distanceList = NULL;

        DEBUG_CHECK_1ARG (f, "Unable to open file %s in scores directory", makePath (distanceMatrix, probe));

        tokenizerInit (&tok, tokenizerStreamReader, f);
        while (!tokenizerEndOfFile (&tok))
        {
            DistanceMeasure m;
            m.subject = strdup (tokenizerGetWord (&tok));
            m.distance = atof (tokenizerGetWord (&tok));
            listAccumulate (&distanceList, &m, sizeof (DistanceMeasure));
        }
        fclose (f);
        distances = (DistanceMeasure*) listToArray (&distanceList, sizeof (DistanceMeasure), (size_t*)&nDistances);
    }

    /* Copy the list of names into the intermediate data structure that allows us to sort the
    * subjects by distance to the probe.
    */

    toSort = (DistanceMeasure*) malloc (nSubjects * sizeof (DistanceMeasure));

    for (j = 0; j < nSubjects; j++)
    {
        toSort[j].subject = subjects[j];
        toSort[j].distance = 0.0;
        toSort[j].index = j;
    }

    /* Read distances between probe and every other image in the subject list.
    * As a special case, a subject is said to be infinitely far away from him/herself.
    * Random scores are used when a distanceMatrix is not provided */

    for (j = 0; j < nSubjects; j++)
    {
        if (strcmp (subjects[j], probe) == 0) {

            /* Probe and subject are the same. Say they are far apart since
            * we don't want to treat an image and itself as being two replicates
            * of a person */

            toSort[j].distance = HUGE;

        } else if (distanceMatrix != NULL) {

            /* Look for the subject in the list of distances and return assign the 
            * score */

            toSort[j].distance = HUGE;

            for (i = 0; i < nDistances; ++i)
                if (strcmp (distances[i].subject, toSort[j].subject) == 0)
                    toSort[j].distance = distances[i].distance;

        } else {

            /* If we are not using a distance matrix, then choose
            * a random value */

            toSort[j].distance = ((double) rand()) / RAND_MAX;

        }
    }

    /* Now sort the list by similarity to the probe */

    qsort (toSort, nSubjects, sizeof (DistanceMeasure), distanceMeasureComparator);

    /* Copy the data back into the subject list or return the permuted indices */

    if (indices == NULL)
        for (j = 0; j < nSubjects; j++) {
            subjects[j] = toSort[j].subject;
        }
    else
        for (j = 0; j < nSubjects; j++) {
            indices[j] = toSort[j].index;
        } 

        /* Clean up */

        if (distanceMatrix)
        {
            for (i = 0; i < nDistances; ++i)
                free (distances[i].subject);
            free (distances);
        }

        free (toSort);
}