예제 #1
0
void
aln_init (struct Alignment **aln_loc, unsigned int size)
{
  *aln_loc = (struct Alignment *) malloc_util( sizeof(struct Alignment ));

  (*aln_loc)->numseqs = size;
  (*aln_loc)->seqs = (struct Sequence **) malloc_util( size * sizeof( struct Sequence *));
  (*aln_loc)->length = 0;

  unsigned int i = 0;
  char *buffer = NULL;
  for (i=0; i<size; i++) {
    (*aln_loc)->seqs[i] = empty_Sequence();
    
    if(asprintf(&buffer, "%d", i) == -1)
        fatal_util("asprintf failed");
    (*aln_loc)->seqs[i]->name = buffer;
  }

  return;
}
int main (int argc, char *argv[]) {

  FILE *input;
  char *fname;
  
  char *optname;                /* name of option found by getoption */
  char *optarg;                 /* argument found by getoption       */
  unsigned int optindex;        /* index in argv[]             */

  /**********************************/
  /** process command line options **/				    
  /**********************************/
  clock_t start = clock();
  
  while (get_option( argc, 
		     argv, 
		     options,
		     sizeof(options) / sizeof( struct Option ),
		     usage,
		     &optindex,
		     &optname,
		     &optarg )){
    if (strcmp(optname, "-upgma") == 0) use_upgma = 1;
    else if (strcmp(optname, "-kimura") == 0) use_kimura = 1;
    else if (strcmp(optname, "-boot") == 0) calc_bootstraps = atoi( optarg );
    else if (strcmp(optname, "-in") == 0) {
      if (optarg[0] != 'm' && optarg[0] != 'a')
	fatal_util("Error: Incorrect use of '-in' optio\n%s\n", usage );
      
      if (optarg[0] == 'm')
	input_is_matrix = 1;
      else if (optarg[0] == 'a')
	input_is_matrix = 0;
    }
    else if (strcmp(optname, "-out") == 0) {
      if (optarg[0] != 'm' && optarg[0] != 't')
	fatal_util("Error: Incorrect use of '-out' option\n%s\n", usage );
      
      if (optarg[0] == 'm')
	output_is_matrix = 1;
      else if (optarg[0] == 't')
	output_is_matrix = 0;

    } 
    else if (strcmp(optname, "-h") == 0) {
      fprintf( stderr, "%s", usage );
      exit(0);
    }
  }

  if (argc - optindex != 1) {
    fatal_util("Fatal error: Incorrect number of arguements.\n%s\n", usage );
  }

  fname = argv[optindex];

  /* do some options combination checking - some combinations do not make sense */

  if (input_is_matrix && use_kimura)
    fatal_util("You can only use kimura when giving an aligment\n%s\n", usage );

  if ( (input_is_matrix || output_is_matrix) && calc_bootstraps)
    fatal_util("You can't do bootstrapping in matrix mode. You need an alignment\n%s\n", usage );


  if ((input = fopen( fname, "r" )) == NULL)
    fatal_util( "Could not open file %s for reading", fname );

  quicktree( input );

  printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
 
  exit(0);
}
예제 #3
0
char *
msa2tree (char **mfa, unsigned int num) {

//  fprintf(stderr, "TREELIB: Starting msa2tree [%d]\n", num);

  struct DistanceMatrix *mat = NULL;
  struct Alignment *aln = NULL;
  struct ClusterGroup *group = NULL;
  struct Tree *njTree = NULL;

  char *treestring = NULL;
  assert(num >= 0);
  if (num == 0) {
    return NULL;
  } else if (num == 1) {
    if(asprintf(&treestring, "%d;", 0) == -1)
        fatal_util("asprintf failed");
    return treestring;
  } else if (num == 2) {
    float dist = jcdist(mfa[0], mfa[1]);
    dist *= 0.5;
    if(asprintf(&treestring, "(%d:%g, %d:%g)", 0, dist, 1, dist) == -1)
        fatal_util("asprintf failed");
    return treestring;
  }

//  fprintf(stderr, "TREELIB: Here is the msa\n");
//  unsigned int i = 0;  
//  for (i=0; i<num; i++) {
//    fprintf(stderr, "TREELIB:\t[%s]\n", mfa[i]);
//  }

//  fprintf(stderr, "TREELIB: Building the distance matrix\n");
  mat = mfa2dist(mfa, num);
  aln_init(&aln, num);
  group = alignment_to_ClusterGroup(aln, 0);
  group->matrix = mat;
//  print_DistanceMatrix(stderr, mat);

  fprintf(stderr, "TREELIB: Building the NJ tree\n");
  njTree = neighbour_joining_buildtree(group, 0);

  struct Tnode *tmpNode = NULL;
  tmpNode = njTree->child[2];
  njTree->child[2] = NULL;

//  fprintf(stderr, "TREELIB: Rooting the tree\n");
  struct Tnode *leftNode = NULL;
  leftNode = (struct Tnode *) malloc_util(sizeof(struct Tnode));
  leftNode->left = njTree->child[0];
  leftNode->right = njTree->child[1];
  leftNode->distance = 0.0;

  struct Tnode *rootNode = NULL;
  rootNode = (struct Tnode *) malloc_util(sizeof(struct Tnode));
  rootNode->left = leftNode;
  rootNode->right = tmpNode;

  njTree->child[0] = rootNode;
  njTree->child[1] = NULL;

  fprintf(stderr, "TREELIB: Converting tree to string\n");
  treestring = tree2string(njTree);
//  fprintf(stderr, "TREELIB: [[%s]]\n", treestring);

//  fprintf(stderr, "TREELIB: free aln\n");
  aln = free_Alignment(aln);

//  fprintf(stderr, "TREELIB: free group\n");
  group = free_ClusterGroup(group);

//  fprintf(stderr, "TREELIB: free njTree\n");
//  njTree = free_Tree(njTree);

//  fprintf(stderr, "TREELIB: Ending msa2tree\n");

  return treestring;
}