Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Obsolete Editions Forum > Methods: Chapters 14 and 15

Reply
 
Thread Tools Display Modes
  #1  
Old 07-26-2010, 02:31 PM
JohnHickey JohnHickey is offline
Registered User
 
Join Date: Jul 2010
Posts: 2
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");

}
Reply With Quote
  #2  
Old 07-27-2010, 02:15 PM
JohnHickey JohnHickey is offline
Registered User
 
Join Date: Jul 2010
Posts: 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");
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:09 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.