2 * preclustercommand.cpp
5 * Created by westcott on 12/21/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "preclustercommand.h"
11 #include "deconvolutecommand.h"
13 //**********************************************************************************************************************
14 vector<string> PreClusterCommand::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
19 CommandParameter pdiffs("diffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pdiffs);
20 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "PreClusterCommand", "setParameters");
33 //**********************************************************************************************************************
34 string PreClusterCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The pre.cluster command groups sequences that are within a given number of base mismatches.\n";
38 helpString += "The pre.cluster command outputs a new fasta and name file.\n";
39 helpString += "The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n";
40 helpString += "The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n";
41 helpString += "The group parameter allows you to provide a group file so you can cluster by group. \n";
42 helpString += "The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n";
43 helpString += "The pre.cluster command should be in the following format: \n";
44 helpString += "pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n";
45 helpString += "Example pre.cluster(fasta=amazon.fasta, diffs=2).\n";
46 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
50 m->errorOut(e, "PreClusterCommand", "getHelpString");
54 //**********************************************************************************************************************
55 string PreClusterCommand::getOutputFileNameTag(string type, string inputName=""){
57 string outputFileName = "";
58 map<string, vector<string> >::iterator it;
60 //is this a type this command creates
61 it = outputTypes.find(type);
62 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
64 if (type == "fasta") { outputFileName = "precluster" + m->getExtension(inputName); }
65 else if (type == "name") { outputFileName = "precluster.names"; }
66 else if (type == "map") { outputFileName = "precluster.map"; }
67 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
69 return outputFileName;
72 m->errorOut(e, "PreClusterCommand", "getOutputFileNameTag");
76 //**********************************************************************************************************************
77 PreClusterCommand::PreClusterCommand(){
79 abort = true; calledHelp = true;
81 vector<string> tempOutNames;
82 outputTypes["fasta"] = tempOutNames;
83 outputTypes["name"] = tempOutNames;
84 outputTypes["map"] = tempOutNames;
87 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
91 //**********************************************************************************************************************
93 PreClusterCommand::PreClusterCommand(string option) {
95 abort = false; calledHelp = false;
97 //allow user to run help
98 if(option == "help") { help(); abort = true; calledHelp = true; }
99 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
102 vector<string> myArray = setParameters();
104 OptionParser parser(option);
105 map<string, string> parameters = parser.getParameters();
107 ValidParameters validParameter;
108 map<string, string>::iterator it;
110 //check to make sure all parameters are valid for command
111 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
112 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
115 //initialize outputTypes
116 vector<string> tempOutNames;
117 outputTypes["fasta"] = tempOutNames;
118 outputTypes["name"] = tempOutNames;
119 outputTypes["map"] = tempOutNames;
121 //if the user changes the input directory command factory will send this info to us in the output parameter
122 string inputDir = validParameter.validFile(parameters, "inputdir", false);
123 if (inputDir == "not found"){ inputDir = ""; }
126 it = parameters.find("fasta");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["fasta"] = inputDir + it->second; }
134 it = parameters.find("name");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["name"] = inputDir + it->second; }
142 it = parameters.find("group");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["group"] = inputDir + it->second; }
151 //check for required parameters
152 fastafile = validParameter.validFile(parameters, "fasta", true);
153 if (fastafile == "not found") {
154 fastafile = m->getFastaFile();
155 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
156 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
158 else if (fastafile == "not open") { abort = true; }
159 else { m->setFastaFile(fastafile); }
161 //if the user changes the output directory command factory will send this info to us in the output parameter
162 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
164 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
167 //check for optional parameter and set defaults
168 // ...at some point should added some additional type checking...
169 namefile = validParameter.validFile(parameters, "name", true);
170 if (namefile == "not found") { namefile = ""; }
171 else if (namefile == "not open") { namefile = ""; abort = true; }
172 else { m->setNameFile(namefile); }
174 groupfile = validParameter.validFile(parameters, "group", true);
175 if (groupfile == "not found") { groupfile = ""; bygroup = false; }
176 else if (groupfile == "not open") { abort = true; groupfile = ""; }
177 else { m->setGroupFile(groupfile); bygroup = true; }
179 string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; }
180 m->mothurConvert(temp, diffs);
182 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
183 m->setProcessors(temp);
184 m->mothurConvert(temp, processors);
186 if (namefile == "") {
187 vector<string> files; files.push_back(fastafile);
188 parser.getNameFile(files);
193 catch(exception& e) {
194 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
198 //**********************************************************************************************************************
200 int PreClusterCommand::execute(){
203 if (abort == true) { if (calledHelp) { return 0; } return 2; }
205 int start = time(NULL);
207 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
208 string newFastaFile = fileroot + getOutputFileNameTag("fasta", fastafile);
209 string newNamesFile = fileroot + getOutputFileNameTag("name");
210 string newMapFile = fileroot + getOutputFileNameTag("map"); //add group name if by group
211 outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
212 outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
216 //clear out old files
217 ofstream outFasta; m->openOutputFile(newFastaFile, outFasta); outFasta.close();
218 ofstream outNames; m->openOutputFile(newNamesFile, outNames); outNames.close();
219 newMapFile = fileroot + "precluster.";
221 //parse fasta and name file by group
222 SequenceParser* parser;
223 if (namefile != "") { parser = new SequenceParser(groupfile, fastafile, namefile); }
224 else { parser = new SequenceParser(groupfile, fastafile); }
226 vector<string> groups = parser->getNamesOfGroups();
228 if(processors == 1) { driverGroups(parser, newFastaFile, newNamesFile, newMapFile, 0, groups.size(), groups); }
229 else { createProcessesGroups(parser, newFastaFile, newNamesFile, newMapFile, groups); }
233 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
235 //run unique.seqs for deconvolute results
236 string inputString = "fasta=" + newFastaFile;
237 if (namefile != "") { inputString += ", name=" + newNamesFile; }
238 m->mothurOutEndLine();
239 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
240 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine();
241 m->mothurCalling = true;
243 Command* uniqueCommand = new DeconvoluteCommand(inputString);
244 uniqueCommand->execute();
246 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
248 delete uniqueCommand;
249 m->mothurCalling = false;
250 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
252 m->renameFile(filenames["fasta"][0], newFastaFile);
253 m->renameFile(filenames["name"][0], newNamesFile);
255 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run pre.cluster."); m->mothurOutEndLine();
258 if (namefile != "") { readNameFile(); }
260 //reads fasta file and return number of seqs
261 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
263 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
265 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
266 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0; }
268 int count = process(newMapFile);
269 outputNames.push_back(newMapFile); outputTypes["map"].push_back(newMapFile);
271 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
273 m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
274 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
275 printData(newFastaFile, newNamesFile);
277 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
280 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
282 m->mothurOutEndLine();
283 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
284 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
285 m->mothurOutEndLine();
287 //set fasta file as new current fastafile
289 itTypes = outputTypes.find("fasta");
290 if (itTypes != outputTypes.end()) {
291 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
294 itTypes = outputTypes.find("name");
295 if (itTypes != outputTypes.end()) {
296 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
302 catch(exception& e) {
303 m->errorOut(e, "PreClusterCommand", "execute");
307 /**************************************************************************************************/
308 int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newFName, string newNName, string newMFile, vector<string> groups) {
311 vector<int> processIDS;
316 if (groups.size() < processors) { processors = groups.size(); }
318 //divide the groups between the processors
319 vector<linePair> lines;
320 int numGroupsPerProcessor = groups.size() / processors;
321 for (int i = 0; i < processors; i++) {
322 int startIndex = i * numGroupsPerProcessor;
323 int endIndex = (i+1) * numGroupsPerProcessor;
324 if(i == (processors - 1)){ endIndex = groups.size(); }
325 lines.push_back(linePair(startIndex, endIndex));
328 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
330 //loop through and create all the processes you want
331 while (process != processors) {
335 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
339 num = driverGroups(parser, newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", newMFile, lines[process].start, lines[process].end, groups);
341 string tempFile = toString(getpid()) + ".outputNames.temp";
343 m->openOutputFile(tempFile, outTemp);
345 outTemp << outputNames.size();
346 for (int i = 0; i < outputNames.size(); i++) { outTemp << outputNames[i] << endl; }
351 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
352 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
358 num = driverGroups(parser, newFName, newNName, newMFile, lines[0].start, lines[0].end, groups);
360 //force parent to wait until all the processes are done
361 for (int i=0;i<processIDS.size();i++) {
362 int temp = processIDS[i];
366 for (int i = 0; i < processIDS.size(); i++) {
367 string tempFile = toString(processIDS[i]) + ".outputNames.temp";
369 m->openInputFile(tempFile, intemp);
373 for (int k = 0; k < num; k++) {
375 intemp >> name; m->gobble(intemp);
377 outputNames.push_back(name); outputTypes["map"].push_back(name);
380 m->mothurRemove(tempFile);
384 //////////////////////////////////////////////////////////////////////////////////////////////////////
385 //Windows version shared memory, so be careful when passing variables through the preClusterData struct.
386 //Above fork() will clone, so memory is separate, but that's not the case with windows,
387 //////////////////////////////////////////////////////////////////////////////////////////////////////
389 vector<preClusterData*> pDataArray;
390 DWORD dwThreadIdArray[processors-1];
391 HANDLE hThreadArray[processors-1];
393 //Create processor worker threads.
394 for( int i=1; i<processors; i++ ){
395 // Allocate memory for thread data.
396 string extension = toString(i) + ".temp";
398 preClusterData* tempPreCluster = new preClusterData(fastafile, namefile, groupfile, (newFName+extension), (newNName+extension), newMFile, groups, m, lines[i].start, lines[i].end, diffs, i);
399 pDataArray.push_back(tempPreCluster);
400 processIDS.push_back(i);
402 //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
403 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
404 hThreadArray[i-1] = CreateThread(NULL, 0, MyPreclusterThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
408 //using the main process as a worker saves time and memory
409 num = driverGroups(parser, newFName, newNName, newMFile, lines[0].start, lines[0].end, groups);
411 //Wait until all threads have terminated.
412 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
414 //Close all thread handles and free memory allocations.
415 for(int i=0; i < pDataArray.size(); i++){
416 for (int j = 0; j < pDataArray[i]->mapFileNames.size(); j++) {
417 outputNames.push_back(pDataArray[i]->mapFileNames[j]); outputTypes["map"].push_back(pDataArray[i]->mapFileNames[j]);
419 CloseHandle(hThreadArray[i]);
420 delete pDataArray[i];
425 //append output files
426 for(int i=0;i<processIDS.size();i++){
427 //newFName = m->getFullPathName(".\\" + newFName);
428 //newNName = m->getFullPathName(".\\" + newNName);
430 m->appendFiles((newFName + toString(processIDS[i]) + ".temp"), newFName);
431 m->mothurRemove((newFName + toString(processIDS[i]) + ".temp"));
433 m->appendFiles((newNName + toString(processIDS[i]) + ".temp"), newNName);
434 m->mothurRemove((newNName + toString(processIDS[i]) + ".temp"));
440 catch(exception& e) {
441 m->errorOut(e, "PreClusterCommand", "createProcessesGroups");
445 /**************************************************************************************************/
446 int PreClusterCommand::driverGroups(SequenceParser* parser, string newFFile, string newNFile, string newMFile, int start, int end, vector<string> groups){
451 //precluster each group
452 for (int i = start; i < end; i++) {
456 if (m->control_pressed) { return 0; }
458 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[i] + ":"); m->mothurOutEndLine();
460 map<string, string> thisNameMap;
461 if (namefile != "") { thisNameMap = parser->getNameMap(groups[i]); }
462 vector<Sequence> thisSeqs = parser->getSeqs(groups[i]);
464 //fill alignSeqs with this groups info.
465 numSeqs = loadSeqs(thisNameMap, thisSeqs);
467 if (m->control_pressed) { return 0; }
469 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
471 int count = process(newMFile+groups[i]+".map");
472 outputNames.push_back(newMFile+groups[i]+".map"); outputTypes["map"].push_back(newMFile+groups[i]+".map");
474 if (m->control_pressed) { return 0; }
476 m->mothurOut("Total number of sequences before pre.cluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
477 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
478 printData(newFFile, newNFile);
480 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
486 catch(exception& e) {
487 m->errorOut(e, "PreClusterCommand", "driverGroups");
491 /**************************************************************************************************/
492 int PreClusterCommand::process(string newMapFile){
495 m->openOutputFile(newMapFile, out);
497 //sort seqs by number of identical seqs
498 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
501 int numSeqs = alignSeqs.size();
503 //think about running through twice...
504 for (int i = 0; i < numSeqs; i++) {
507 // itActive = active.find(alignSeqs[i].seq.getName());
509 if (alignSeqs[i].active) { //this sequence has not been merged yet
511 string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n";
513 //try to merge it with all smaller seqs
514 for (int j = i+1; j < numSeqs; j++) {
516 if (m->control_pressed) { out.close(); return 0; }
518 if (alignSeqs[j].active) { //this sequence has not been merged yet
519 //are you within "diff" bases
520 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
522 if (mismatch <= diffs) {
524 alignSeqs[i].names += ',' + alignSeqs[j].names;
525 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
527 chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n";
529 alignSeqs[j].active = 0;
530 alignSeqs[j].numIdentical = 0;
536 //remove from active list
537 alignSeqs[i].active = 0;
539 out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;;
542 if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
546 if(numSeqs % 100 != 0) { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
551 catch(exception& e) {
552 m->errorOut(e, "PreClusterCommand", "process");
556 /**************************************************************************************************/
557 int PreClusterCommand::readFASTA(){
562 //m->openInputFile(namefile, inNames);
563 m->openInputFile(fastafile, inFasta);
565 //string firstCol, secondCol, nameString;
568 while (!inFasta.eof()) {
570 if (m->control_pressed) { inFasta.close(); return 0; }
572 //inNames >> firstCol >> secondCol;
573 //nameString = secondCol;
575 //m->gobble(inNames);
577 //while (secondCol.find_first_of(',') != -1) {
579 // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
582 Sequence seq(inFasta); m->gobble(inFasta);
584 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
585 if (namefile != "") {
586 itSize = sizes.find(seq.getName());
588 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
590 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
591 alignSeqs.push_back(tempNode);
592 lengths.insert(seq.getAligned().length());
594 }else { //no names file, you are identical to yourself
595 seqPNode tempNode(1, seq, seq.getName());
596 alignSeqs.push_back(tempNode);
597 lengths.insert(seq.getAligned().length());
604 if (lengths.size() > 1) { m->control_pressed = true; m->mothurOut("[ERROR]: your sequences are not all the same length. pre.cluster requires sequences to be aligned."); m->mothurOutEndLine(); }
605 else if (lengths.size() == 1) { length = *(lengths.begin()); }
607 return alignSeqs.size();
610 catch(exception& e) {
611 m->errorOut(e, "PreClusterCommand", "readFASTA");
615 /**************************************************************************************************/
616 int PreClusterCommand::loadSeqs(map<string, string>& thisName, vector<Sequence>& thisSeqs){
620 map<string, string>::iterator it;
623 for (int i = 0; i < thisSeqs.size(); i++) {
625 if (m->control_pressed) { return 0; }
627 if (namefile != "") {
628 it = thisName.find(thisSeqs[i].getName());
630 //should never be true since parser checks for this
631 if (it == thisName.end()) { m->mothurOut(thisSeqs[i].getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); error = true; }
635 for(int j=0;j<(it->second).length();j++){
636 if((it->second)[j] == ','){ numReps++; }
639 seqPNode tempNode(numReps, thisSeqs[i], it->second);
640 alignSeqs.push_back(tempNode);
641 lengths.insert(thisSeqs[i].getAligned().length());
643 }else { //no names file, you are identical to yourself
644 seqPNode tempNode(1, thisSeqs[i], thisSeqs[i].getName());
645 alignSeqs.push_back(tempNode);
646 lengths.insert(thisSeqs[i].getAligned().length());
650 if (lengths.size() > 1) { error = true; m->mothurOut("[ERROR]: your sequences are not all the same length. pre.cluster requires sequences to be aligned."); m->mothurOutEndLine(); }
651 else if (lengths.size() == 1) { length = *(lengths.begin()); }
654 if (error) { m->control_pressed = true; }
658 return alignSeqs.size();
661 catch(exception& e) {
662 m->errorOut(e, "PreClusterCommand", "loadSeqs");
667 /**************************************************************************************************/
669 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
673 for (int i = 0; i < seq1.length(); i++) {
675 if (seq1[i] != seq2[i]) { numBad++; }
676 if (numBad > diffs) { return length; } //to far to cluster
681 catch(exception& e) {
682 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
687 /**************************************************************************************************/
689 void PreClusterCommand::printData(string newfasta, string newname){
695 m->openOutputFileAppend(newfasta, outFasta);
696 m->openOutputFileAppend(newname, outNames);
698 m->openOutputFile(newfasta, outFasta);
699 m->openOutputFile(newname, outNames);
702 for (int i = 0; i < alignSeqs.size(); i++) {
703 if (alignSeqs[i].numIdentical != 0) {
704 alignSeqs[i].seq.printSequence(outFasta);
705 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
713 catch(exception& e) {
714 m->errorOut(e, "PreClusterCommand", "printData");
718 /**************************************************************************************************/
720 void PreClusterCommand::readNameFile(){
723 m->openInputFile(namefile, in);
724 string firstCol, secondCol;
727 in >> firstCol >> secondCol; m->gobble(in);
728 names[firstCol] = secondCol;
731 for(int i=0;i<secondCol.size();i++){
732 if(secondCol[i] == ','){ size++; }
734 sizes[firstCol] = size;
738 catch(exception& e) {
739 m->errorOut(e, "PreClusterCommand", "readNameFile");
744 /**************************************************************************************************/