try{
globaldata = GlobalData::getInstance();
abort = false;
+ format = "";
//allow user to run help
if(option == "help") { help(); abort = true; }
else {
//valid paramters for this command
- string Array[] = {"phylip","column","name","cutoff","precision","method","splitmethod","taxonomy","taxlevel","large","showabund","timing","hard","processors","outputdir","inputdir"};
+ string Array[] = {"fasta","phylip","column","name","cutoff","precision","method","splitmethod","taxonomy","taxlevel","large","showabund","timing","hard","processors","outputdir","inputdir"};
vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
OptionParser parser(option);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
}
+
+ it = parameters.find("fasta");
+ //user has given a template file
+ if(it != parameters.end()){
+ path = hasPath(it->second);
+ //if the user has not given a path then, add inputdir. else leave path alone.
+ if (path == "") { parameters["fasta"] = inputDir + it->second; }
+ }
}
//check for required parameters
if (namefile == "not open") { abort = true; }
else if (namefile == "not found") { namefile = ""; }
+ fastafile = validParameter.validFile(parameters, "fasta", true);
+ if (fastafile == "not open") { abort = true; }
+ else if (fastafile == "not found") { fastafile = ""; }
+ else { splitmethod = "fasta"; }
+
taxFile = validParameter.validFile(parameters, "taxonomy", true);
if (taxFile == "not open") { abort = true; }
else if (taxFile == "not found") { taxFile = ""; }
- if ((phylipfile == "") && (columnfile == "")) { m->mothurOut("When executing a cluster.split command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
- else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a cluster.split command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
+ if ((phylipfile == "") && (columnfile == "") && (fastafile == "")) { m->mothurOut("When executing a cluster.split command you must enter a phylip or a column or fastafile."); m->mothurOutEndLine(); abort = true; }
+ else if ((phylipfile != "") && (columnfile != "") && (fastafile != "")) { m->mothurOut("When executing a cluster.split command you must enter ONLY ONE of the following: fasta, phylip or column."); m->mothurOutEndLine(); abort = true; }
if (columnfile != "") {
if (namefile == "") { m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); abort = true; }
}
+
+ if (fastafile != "") {
+ if (taxFile == "") { m->mothurOut("You need to provide a taxonomy file if you are using a fasta file to generate the split."); m->mothurOutEndLine(); abort = true; }
+ }
//check for optional parameter and set defaults
// ...at some point should added some additional type checking...
temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
convert(temp, processors);
- splitmethod = validParameter.validFile(parameters, "splitmethod", false); if (splitmethod == "not found") { splitmethod = "distance"; }
+ temp = validParameter.validFile(parameters, "splitmethod", false);
+ if (splitmethod != "fasta") {
+ if (temp == "not found") { splitmethod = "distance"; }
+ else { splitmethod = temp; }
+ }
temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
convert(temp, cutoff);
if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); m->mothurOutEndLine(); abort = true; }
- if ((splitmethod == "distance") || (splitmethod == "classify")) { }
- else { m->mothurOut("Not a valid splitting method. Valid splitting algorithms are distance or classify."); m->mothurOutEndLine(); abort = true; }
+ if ((splitmethod == "distance") || (splitmethod == "classify") || (splitmethod == "fasta")) { }
+ else { m->mothurOut(splitmethod + " is not a valid splitting method. Valid splitting algorithms are distance, classify or fasta."); m->mothurOutEndLine(); abort = true; }
if ((splitmethod == "classify") && (taxFile == "")) { m->mothurOut("You need to provide a taxonomy file if you are going to use the classify splitmethod."); m->mothurOutEndLine(); abort = true; }
void ClusterSplitCommand::help(){
try {
- m->mothurOut("The cluster.split command parameter options are phylip, column, name, cutoff, precision, method, splitmethod, taxonomy, taxlevel, showabund, timing, hard, large, processors. Phylip or column and name are required.\n");
+ m->mothurOut("The cluster.split command parameter options are fasta, phylip, column, name, cutoff, precision, method, splitmethod, taxonomy, taxlevel, showabund, timing, hard, large, processors. Fasta or Phylip or column and name are required.\n");
+ m->mothurOut("The cluster.split command can split your files in 3 ways. Splitting by distance file, by classification, or by classification also using a fasta file. \n");
+ m->mothurOut("For the distance file method, you need only provide your distance file and mothur will split the file into distinct groups. \n");
+ m->mothurOut("For the classification method, you need to provide your distance file and taxonomy file, and set the splitmethod to classify. \n");
+ m->mothurOut("You will also need to set the taxlevel you want to split by. mothur will split the sequence into distinct taxonomy groups, and split the distance file based on those groups. \n");
+ m->mothurOut("For the classification method using a fasta file, you need to provide your fasta file and taxonomy file. \n");
+ m->mothurOut("You will also need to set the taxlevel you want to split by. mothur will split the sequence into distinct taxonomy groups, and create distance files for each grouping. \n");
m->mothurOut("The phylip and column parameter allow you to enter your distance file. \n");
+ m->mothurOut("The fasta parameter allows you to enter your aligned fasta file. \n");
m->mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
m->mothurOut("The cutoff parameter allow you to set the distance you want to cluster to, default is 10.0. \n");
m->mothurOut("The precision parameter allows you specify the precision of the precision of the distances outputted, default=100, meaning 2 decimal places. \n");
m->mothurOut("The method allows you to specify what clustering algorythm you want to use, default=furthest, option furthest, nearest, or average. \n");
- m->mothurOut("The splitmethod parameter allows you to specify how you want to split your distance file before you cluster, default=distance, options distance or classify. \n");
+ m->mothurOut("The splitmethod parameter allows you to specify how you want to split your distance file before you cluster, default=distance, options distance, classify or fasta. \n");
m->mothurOut("The taxonomy parameter allows you to enter the taxonomy file for your sequences, this is only valid if you are using splitmethod=classify. Be sure your taxonomy file does not include the probability scores. \n");
m->mothurOut("The taxlevel parameter allows you to specify the taxonomy level you want to use to split the distance file, default=1. \n");
m->mothurOut("The large parameter allows you to indicate that your distance matrix is too large to fit in RAM. The default value is false.\n");
try {
if (abort == true) { return 0; }
-
+ time_t estart;
//****************** file prep work ******************************//
//if user gave a phylip file convert to column file
if (format == "phylip") {
-
+ estart = time(NULL);
+ m->mothurOut("Converting to column format..."); m->mothurOutEndLine();
+
ReadCluster* convert = new ReadCluster(distfile, cutoff, outputDir, false);
NameAssignment* nameMap = NULL;
}
delete listToMakeNameFile;
delete convert;
+
+ m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to convert the distance file."); m->mothurOutEndLine();
}
if (m->control_pressed) { return 0; }
- time_t estart = time(NULL);
- m->mothurOut("Splitting the distance file..."); m->mothurOutEndLine();
+ estart = time(NULL);
+ m->mothurOut("Splitting the file..."); m->mothurOutEndLine();
//split matrix into non-overlapping groups
SplitMatrix* split;
- if (splitmethod == "distance") { split = new SplitMatrix(distfile, namefile, taxFile, cutoff, splitmethod, large); }
- else { split = new SplitMatrix(distfile, namefile, taxFile, taxLevelCutoff, splitmethod, large); }
+ if (splitmethod == "distance") { split = new SplitMatrix(distfile, namefile, taxFile, cutoff, splitmethod, large); }
+ else if (splitmethod == "classify") { split = new SplitMatrix(distfile, namefile, taxFile, taxLevelCutoff, splitmethod, large); }
+ else if (splitmethod == "fasta") { split = new SplitMatrix(fastafile, taxFile, taxLevelCutoff, splitmethod); }
+ else { m->mothurOut("Not a valid splitting method. Valid splitting algorithms are distance, classify or fasta."); m->mothurOutEndLine(); return 0; }
split->split();
listFileNames.clear(); return listFileNames;
}
- remove(thisDistFile.c_str());
- remove(thisNamefile.c_str());
+ //remove(thisDistFile.c_str());
+ //remove(thisNamefile.c_str());
}
vector<int> processIDS; //processid
vector<string> outputNames;
- string method, fileroot, tag, outputDir, phylipfile, columnfile, namefile, distfile, format, showabund, timing, splitmethod, taxFile;
+ string method, fileroot, tag, outputDir, phylipfile, columnfile, namefile, distfile, format, showabund, timing, splitmethod, taxFile, fastafile;
double cutoff, splitcutoff;
int precision, length, processors, taxLevelCutoff;
bool print_start, abort, hard, large;
float de = calcDE(obsi, exp);
float dist = calcDist(query, subject, front, back);
- //o << i << '\t' << j << '\t' << dist << '\t' << de << endl;
+ //cout << i << '\t' << j << '\t' << dist << '\t' << de << endl;
dist = ceil(dist);
quanMember newScore(de, i, j);
if (m->control_pressed) { return -1; }
int c = readNodeChar(f);
-
+ string k;
+k = c;
+ cout << "at beginning = " << k <<endl;
if(c == '('){
- int lc = readNewickInt(f, n, T);
- if (lc == -1) { return -1; } //reports an error in reading
+
+ //to account for multifurcating trees generated by fasttree, we are forcing them to be bifurcating
+ //read all children
+ vector<int> childrenNodes;
+ while(f.peek() != ')'){
+ int child = readNewickInt(f, n, T);
+ if (child == -1) { return -1; } //reports an error in reading
+
+ childrenNodes.push_back(child);
+
+ //after a child you either have , or ), check for both
+ if(f.peek()==')'){ break; }
+ else if (f.peek()==',') { readSpecialChar(f,',',"comma"); }
+ else { string k;
+ k = f.peek();
+ cout << "in here k = " << k << '\t' << f.tellg() <<endl;
+ }
+ }
+ cout << childrenNodes.size() << endl;
+ if (childrenNodes.size() < 2) { m->mothurOut("Error in tree, please correct."); m->mothurOutEndLine(); return -1; }
+
+ //then force into 2 node structure
+ for (int i = 1; i < childrenNodes.size(); i++) {
+
+ int lc, rc;
+ if (i == 1) { lc = childrenNodes[i-1]; rc = childrenNodes[i]; }
+ else { lc = n; rc = childrenNodes[i]; }
+ cout << i << '\t' << lc << '\t' << rc << endl;
+ T->tree[n].setChildren(lc,rc);
+ T->tree[lc].setParent(n);
+ T->tree[rc].setParent(n);
+
+ T->printTree(); cout << endl;
+ n++;
+ }
- readSpecialChar(f,',',"comma");
+ //to account for extra ++ in looping
+ n--;
+ //int lc = readNewickInt(f, n, T);
+ //if (lc == -1) { return -1; } //reports an error in reading
+
+ //readSpecialChar(f,',',"comma");
- int rc = readNewickInt(f, n, T);
- if (rc == -1) { return -1; } //reports an error in reading
+ //int rc = readNewickInt(f, n, T);
+ //if (rc == -1) { return -1; } //reports an error in reading
if(f.peek()==')'){
readSpecialChar(f,')',"right parenthesis");
c=filehandle.get();
while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
filehandle.putback(c);
-
}
if(f.peek() == ':'){
}
//to account for multifurcating trees generated by fasttree, we are forcing them to be bifurcating
- while(f.peek() == ','){
-
+ /*while(f.peek() == ','){
+ string k;
+ k = f.peek();
+ cout << "in here k = " << k << '\t' << f.tellg() <<endl;
//force this node to be left child and read new rc
T->tree[n].setChildren(lc,rc);
T->tree[lc].setParent(n);
readSpecialChar(f,',',"comma");
- int rc = readNewickInt(f, n, T);
- cout << n << '\t' << lc << '\t' << rc << endl;
+ rc = readNewickInt(f, n, T);
+
if (rc == -1) { return -1; } //reports an error in reading
if(f.peek()==')'){
- string k;
- k = f.peek();
- cout << "in here " << k << endl;
readSpecialChar(f,')',"right parenthesis");
//to pass over labels in trees
c=filehandle.get();
filehandle.putback(c);
if(f.peek() == ':'){
- readSpecialChar(f,':',"colon");
+ readSpecialChar(f,':',"colon");
- if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
+ if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
- T->tree[n].setBranchLength(readBranchLength(f));
+ T->tree[n].setBranchLength(readBranchLength(f));
}else{
T->tree[n].setBranchLength(0.0);
}
break;
}
- }
- cout << n << '\t' << lc << '\t' << rc << endl;
- T->tree[n].setChildren(lc,rc);
- T->tree[lc].setParent(n);
- T->tree[rc].setParent(n);
- T->printTree(); cout << endl;
+ }*/
+
+ //T->tree[n].setChildren(lc,rc);
+ //T->tree[lc].setParent(n);
+ //T->tree[rc].setParent(n);
+ //T->printTree(); cout << endl;
return n++;
+
}else{
f.putback(c);
string name = "";
#include "splitmatrix.h"
#include "phylotree.h"
+#include "sequencedb.h"
+#include "onegapdist.h"
+#include "dist.h"
/***********************************************************************/
taxFile = tax;
large = l;
}
+/***********************************************************************/
+
+SplitMatrix::SplitMatrix(string ffile, string tax, float c, string t){
+ m = MothurOut::getInstance();
+ fastafile = ffile;
+ taxFile = tax;
+ cutoff = c;
+ method = t;
+}
/***********************************************************************/
if (method == "distance") {
splitDistance();
- }else if (method == "classify") {
+ }else if ((method == "classify") || (method == "fasta")) {
splitClassify();
}else {
m->mothurOut("Unknown splitting method, aborting split."); m->mothurOutEndLine();
string seqname, tax;
while(!in.eof()){
in >> seqname >> tax; gobble(in);
-
phylo->addSeqToTree(seqname, tax);
}
in.close();
//make sure the cutoff is not greater than maxlevel
if (cutoff > phylo->getMaxLevel()) { m->mothurOut("splitcutoff is greater than the longest taxonomy, using " + toString(phylo->getMaxLevel())); m->mothurOutEndLine(); cutoff = phylo->getMaxLevel(); }
-
+
//for each node in tree
for (int i = 0; i < phylo->getNumNodes(); i++) {
//is this node within the cutoff
TaxNode taxon = phylo->get(i);
-
+
if (taxon.level == cutoff) {//if yes, then create group containing this nodes sequences
if (taxon.accessions.size() > 1) { //if this taxon just has one seq its a singleton
for (int j = 0; j < taxon.accessions.size(); j++) {
}
}
}
-
+
+ delete phylo;
+
+ if (method == "classify") {
+ splitDistanceFileByTax(seqGroup, numGroups);
+ }else {
+ createDistanceFilesFromTax(seqGroup, numGroups);
+ }
+
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SplitMatrix", "splitClassify");
+ exit(1);
+ }
+}
+/***********************************************************************/
+int SplitMatrix::createDistanceFilesFromTax(map<string, int>& seqGroup, int numGroups){
+ try {
+ map<string, int>::iterator it;
+ map<string, int>::iterator it2;
+ map<string, int> seqIndexInFasta;
+
+ //read fastafile
+ SequenceDB alignDB;
+
+ ifstream filehandle;
+ openInputFile(fastafile, filehandle);
+ int numSeqs = 0;
+ while (!filehandle.eof()) {
+ //input sequence info into sequencedb
+ Sequence newSequence(filehandle);
+
+ if (newSequence.getName() != "") {
+ alignDB.push_back(newSequence);
+ seqIndexInFasta[newSequence.getName()] = numSeqs;
+ numSeqs++;
+ }
+
+ //takes care of white space
+ gobble(filehandle);
+ }
+ filehandle.close();
+
+ Dist* distCalculator = new oneGapDist();
+
+
+//still not done....
+
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SplitMatrix", "createDistanceFilesFromTax");
+ exit(1);
+ }
+}
+/***********************************************************************/
+int SplitMatrix::splitDistanceFileByTax(map<string, int>& seqGroup, int numGroups){
+ try {
+ map<string, int>::iterator it;
+ map<string, int>::iterator it2;
+
ifstream dFile;
openInputFile(distFile, dFile);
ofstream outFile;
remove((distFile + "." + toString(i) + ".temp").c_str());
}
-
//for buffering the io to improve speed
//allow for 10 dists to be stored, then output.
vector<string> outputs; outputs.resize(numGroups, "");
vector<int> numOutputs; numOutputs.resize(numGroups, 0);
+ //you can have a group made, but their may be no distances in the file for this group if the taxonomy file and distance file don't match
+ //this can occur if we have converted the phylip to column, since we reduce the size at that step by using the cutoff value
+ vector<bool> validDistances; validDistances.resize(numGroups, false);
+
//for each distance
while(dFile){
string seqA, seqB;
if ((it != seqGroup.end()) && (it2 != seqGroup.end())) { //they are both not singletons
if (it->second == it2->second) { //they are from the same group so add the distance
- if (numOutputs[it->second] > 10) {
+ if (numOutputs[it->second] > 30) {
openOutputFileAppend((distFile + "." + toString(it->second) + ".temp"), outFile);
outFile << outputs[it->second] << seqA << '\t' << seqB << '\t' << dist << endl;
outFile.close();
outputs[it->second] = "";
numOutputs[it->second] = 0;
+ validDistances[it->second] = true;
}else{
outputs[it->second] += seqA + '\t' + seqB + '\t' + toString(dist) + '\n';
numOutputs[it->second]++;
remove((namefile + "." + toString(i) + ".temp").c_str());
//write out any remaining buffers
- if (numOutputs[it->second] > 0) {
+ if (numOutputs[i] > 0) {
openOutputFileAppend((distFile + "." + toString(i) + ".temp"), outFile);
outFile << outputs[i];
outFile.close();
outputs[i] = "";
numOutputs[i] = 0;
+ validDistances[i] = true;
}
}
remove(singleton.c_str());
singleton = "none";
}
-
+
for(int i=0;i<numGroups;i++){
- string tempNameFile = namefile + "." + toString(i) + ".temp";
- string tempDistFile = distFile + "." + toString(i) + ".temp";
+ //if there are valid distances
+ if (validDistances[i]) {
+ string tempNameFile = namefile + "." + toString(i) + ".temp";
+ string tempDistFile = distFile + "." + toString(i) + ".temp";
- map<string, string> temp;
- temp[tempDistFile] = tempNameFile;
- dists.push_back(temp);
+ map<string, string> temp;
+ temp[tempDistFile] = tempNameFile;
+ dists.push_back(temp);
+ }
}
if (m->control_pressed) {
}
return 0;
-
}
catch(exception& e) {
- m->errorOut(e, "SplitMatrix", "splitClassify");
+ m->errorOut(e, "SplitMatrix", "splitDistanceFileByTax");
exit(1);
}
}
public:
- SplitMatrix(string, string, string, float, string, bool); //column formatted distance file, namesfile, cutoff, method
+ SplitMatrix(string, string, string, float, string, bool); //column formatted distance file, namesfile, cutoff, method, large
+ SplitMatrix(string, string, float, string); //fastafile, taxFile, cutoff, method
+
~SplitMatrix();
int split();
vector< map<string, string> > getDistanceFiles(); //returns map of distance files -> namefile sorted by distance file size
private:
MothurOut* m;
- string distFile, namefile, singleton, method, taxFile;
+ string distFile, namefile, singleton, method, taxFile, fastafile;
vector< map< string, string> > dists;
float cutoff;
bool large;
int splitDistanceLarge();
int splitDistanceRAM();
int splitNames(vector<set<string> >& groups);
+ int splitDistanceFileByTax(map<string, int>&, int);
+ int createDistanceFilesFromTax(map<string, int>&, int);
};
/******************************************************/