![]() |
|
#1
|
|||
|
|||
|
Upgma and Newick
Hi,
I realize that for experienced C++ programmers this is a ridiculous question, but as a fortran programmer who has only just begun C++ I submit it in the hope of finding some help. I am trying to write a program that uses Numerical Recipes UPGMA and Newick routines to make a hierarchical clustering of some data and write the output in Newick format. I am not sure how I get the program to call these routines. The code I have written here is supposed to read in a distance matrix (Dmat). The last two lines then attempt to call Phylo_upgma and newick. This is where I am having problems Any help would be greatly appreciated. Regards, John. #include "nr3.h" #include "phylo.h" #include <iostream> // for input/output #include <fstream> // for opening data streams: read/write to files #include <cstring> // to manipulate classic C strings #include <string> // to manipulate C++ strings types - not exactly portable across compilers, try to avoid #include <cstdlib> // to recast from one type to another e.g. character to integer #include <math.h> using namespace std; void runread(char *file); int main() { char out='N'; cout << "\n\nProgram reading in the data file "; cout << "\n\n "; char *datafile="HapCommonality1.txt"; runread(datafile); return 0; } void runread(char *file) { string line; ifstream in; in.open(file,ios::in); int counter = 0; int nHap=410; char **HapNames; HapNames=(char**) malloc(nHap * sizeof(char)); for(int i = 0; i < nHap; i ++) HapNames[i] = (char*) malloc(sizeof(char) * 200); MatDoub Dmat(nHap,nHap); for(int i = 0; i < nHap; i ++) { counter++; getline(in,line); int size=line.length(); char *datline = new char[size]; line.copy(datline,line.length()); char *tabdata=strtok(datline,","); strcpy(HapNames[i],tabdata); int HapCounter=1; for (int j = 0; j < nHap; j ++) { Dmat[i][j]=atof(strtok(NULL,",")); } } in.close(); printf ("\n"); for (int i = 0; i < 10; i++) { cout << i; printf ("\n"); for (int j = 0; j < 10; j++) { cout << j << "\t"; } for (int j = 0; j < 10; j++) { cout << Dmat[i][j] << "\t"; } printf ("\n"); } Phylo_upgma p(Dmat); newick(Phylo_upgma p(Dmat) , Dmat, "Output.txt"); } |
|
#2
|
|||
|
|||
|
Upgma and Newick
Problem solved. Code below. How about cutting the tree (cutree in R)?
Regards, John #include "nr3.h" #include "phylo.h" #include <iostream> // for input/output #include <fstream> // for opening data streams: read/write to files #include <cstring> // to manipulate classic C strings #include <string> // to manipulate C++ strings types - not exactly portable across compilers, try to avoid #include <cstdlib> // to recast from one type to another e.g. character to integer #include <math.h> using namespace std; void runread(char *file); int main() { char out='N'; cout << "\n\nProgram reading in the data file "; cout << "\n\n "; char *datafile="HapCommonality1.txt"; runread(datafile); return 0; } void runread(char *file) { string line; ifstream in; in.open(file,ios::in); int counter = 0; int nHap=410; MatChar HapNames(nHap,200); MatDoub Dmat(nHap,nHap); for(int i = 0; i < nHap; i ++) { counter++; getline(in,line); int size=line.length(); char *datline = new char[size]; line.copy(datline,line.length()); char *tabdata=strtok(datline,","); int HapCounter=1; for (int j = 0; j < nHap; j ++) { Dmat[i][j]=atof(strtok(NULL,",")); } } in.close(); printf ("\n"); for (int i = 0; i < 10; i++) { cout << i; printf ("\n"); for (int j = 0; j < 10; j++) { cout << j << "\t"; } for (int j = 0; j < 10; j++) { cout << Dmat[i][j] << "\t"; } printf ("\n"); } Phylo_upgma p(Dmat); newick(p,HapNames,"NewickOutput.txt"); } |
![]() |
| Thread Tools | |
| Display Modes | |
|
|