]> git.donarmstrong.com Git - mothur.git/blob - preclustercommand.cpp
added processors to pre.cluster for mac and windows. Fixed bug in reNameFile function...
[mothur.git] / preclustercommand.cpp
1 /*
2  *  preclustercommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 12/21/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "preclustercommand.h"
11 #include "deconvolutecommand.h"
12
13 //**********************************************************************************************************************
14 vector<string> PreClusterCommand::setParameters(){      
15         try {
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);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "PreClusterCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string PreClusterCommand::getHelpString(){      
35         try {
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";
47                 return helpString;
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "PreClusterCommand", "getHelpString");
51                 exit(1);
52         }
53 }
54
55 //**********************************************************************************************************************
56 PreClusterCommand::PreClusterCommand(){ 
57         try {
58                 abort = true; calledHelp = true; 
59                 setParameters();
60                 vector<string> tempOutNames;
61                 outputTypes["fasta"] = tempOutNames;
62                 outputTypes["name"] = tempOutNames;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70
71 PreClusterCommand::PreClusterCommand(string option) {
72         try {
73                 abort = false; calledHelp = false;   
74                 
75                 //allow user to run help
76                 if(option == "help") { help(); abort = true; calledHelp = true; }
77                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
78                 
79                 else {
80                         vector<string> myArray = setParameters();
81                         
82                         OptionParser parser(option);
83                         map<string, string> parameters = parser.getParameters();
84                         
85                         ValidParameters validParameter;
86                         map<string, string>::iterator it;
87                 
88                         //check to make sure all parameters are valid for command
89                         for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
90                                 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
91                         }
92                         
93                         //initialize outputTypes
94                         vector<string> tempOutNames;
95                         outputTypes["fasta"] = tempOutNames;
96                         outputTypes["name"] = tempOutNames;
97                 
98                         //if the user changes the input directory command factory will send this info to us in the output parameter 
99                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
100                         if (inputDir == "not found"){   inputDir = "";          }
101                         else {
102                                 string path;
103                                 it = parameters.find("fasta");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
109                                 }
110                                 
111                                 it = parameters.find("name");
112                                 //user has given a template file
113                                 if(it != parameters.end()){ 
114                                         path = m->hasPath(it->second);
115                                         //if the user has not given a path then, add inputdir. else leave path alone.
116                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
117                                 }
118                                 
119                                 it = parameters.find("group");
120                                 //user has given a template file
121                                 if(it != parameters.end()){ 
122                                         path = m->hasPath(it->second);
123                                         //if the user has not given a path then, add inputdir. else leave path alone.
124                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
125                                 }
126                         }
127
128                         //check for required parameters
129                         fastafile = validParameter.validFile(parameters, "fasta", true);
130                         if (fastafile == "not found") {                                 
131                                 fastafile = m->getFastaFile(); 
132                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
133                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
134                         }
135                         else if (fastafile == "not open") { abort = true; }     
136                         else { m->setFastaFile(fastafile); }
137                         
138                         //if the user changes the output directory command factory will send this info to us in the output parameter 
139                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
140                                 outputDir = ""; 
141                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
142                         }
143
144                         //check for optional parameter and set defaults
145                         // ...at some point should added some additional type checking...
146                         namefile = validParameter.validFile(parameters, "name", true);
147                         if (namefile == "not found") { namefile =  "";  }
148                         else if (namefile == "not open") { abort = true; }      
149                         else {  m->setNameFile(namefile); }
150                         
151                         groupfile = validParameter.validFile(parameters, "group", true);
152                         if (groupfile == "not found") { groupfile =  "";  bygroup = false; }
153                         else if (groupfile == "not open") { abort = true; groupfile =  ""; }    
154                         else {   m->setGroupFile(groupfile); bygroup = true;  }
155                         
156                         string temp     = validParameter.validFile(parameters, "diffs", false);         if(temp == "not found"){        temp = "1"; }
157                         convert(temp, diffs); 
158                         
159                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
160                         m->setProcessors(temp);
161                         convert(temp, processors);
162                         
163                         
164                 }
165                                 
166         }
167         catch(exception& e) {
168                 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
169                 exit(1);
170         }
171 }
172 //**********************************************************************************************************************
173
174 int PreClusterCommand::execute(){
175         try {
176                 
177                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
178                 
179                 int start = time(NULL);
180                 
181                 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
182                 string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile);
183                 string newNamesFile = fileroot + "precluster.names";
184                 
185                 if (bygroup) {
186                         //clear out old files
187                         ofstream outFasta; m->openOutputFile(newFastaFile, outFasta); outFasta.close();
188                         ofstream outNames; m->openOutputFile(newNamesFile, outNames);  outNames.close();
189                         
190                         //parse fasta and name file by group
191                         SequenceParser* parser;
192                         if (namefile != "") { parser = new SequenceParser(groupfile, fastafile, namefile);      }
193                         else                            { parser = new SequenceParser(groupfile, fastafile);                    }
194                         
195                         vector<string> groups = parser->getNamesOfGroups();
196                         
197 //#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
198                         if(processors == 1)     {       driverGroups(parser, newFastaFile, newNamesFile, 0, groups.size(), groups);     }
199                         else                            {       createProcessesGroups(parser, newFastaFile, newNamesFile, groups);                      }
200 //#else
201 //                      driverGroups(parser, newFastaFile, newNamesFile, 0, groups.size(), groups);
202 //#endif
203                         
204                         delete parser;
205                         
206                         if (m->control_pressed) { m->mothurRemove(newFastaFile); m->mothurRemove(newNamesFile); return 0; }
207                         
208                         //run unique.seqs for deconvolute results
209                         string inputString = "fasta=" + newFastaFile;
210                         if (namefile != "") { inputString += ", name=" + newNamesFile; }
211                         m->mothurOutEndLine(); 
212                         m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
213                         m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine(); 
214                         
215                         Command* uniqueCommand = new DeconvoluteCommand(inputString);
216                         uniqueCommand->execute();
217                         
218                         map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
219                         
220                         delete uniqueCommand;
221                         
222                         m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
223                         
224                         m->renameFile(filenames["fasta"][0], newFastaFile);
225                         
226                         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run pre.cluster."); m->mothurOutEndLine(); 
227                                 
228                 }else {
229                         if (namefile != "") { readNameFile(); }
230                 
231                         //reads fasta file and return number of seqs
232                         int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
233                 
234                         if (m->control_pressed) { return 0; }
235         
236                         if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0;  }
237                         if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0;  }
238                         
239                         int count = process();
240                         
241                         if (m->control_pressed) { return 0; }   
242                         
243                         m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
244                         m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
245                         printData(newFastaFile, newNamesFile);
246                         
247                         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
248                 }
249                 
250                 if (m->control_pressed) { m->mothurRemove(newFastaFile); m->mothurRemove(newNamesFile); return 0; }
251                 
252                 m->mothurOutEndLine();
253                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
254                 m->mothurOut(newFastaFile); m->mothurOutEndLine();      outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
255                 m->mothurOut(newNamesFile); m->mothurOutEndLine();      outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
256                 m->mothurOutEndLine();
257                 
258                 //set fasta file as new current fastafile
259                 string current = "";
260                 itTypes = outputTypes.find("fasta");
261                 if (itTypes != outputTypes.end()) {
262                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
263                 }
264                 
265                 itTypes = outputTypes.find("name");
266                 if (itTypes != outputTypes.end()) {
267                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
268                 }
269                 
270                 return 0;
271                 
272         }
273         catch(exception& e) {
274                 m->errorOut(e, "PreClusterCommand", "execute");
275                 exit(1);
276         }
277 }
278 /**************************************************************************************************/
279 int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newFName, string newNName, vector<string> groups) {
280         try {
281                 
282                 vector<int> processIDS;
283                 int process = 1;
284                 int num = 0;
285                 
286                 //sanity check
287                 if (groups.size() < processors) { processors = groups.size(); }
288                 
289                 //divide the groups between the processors
290                 vector<linePair> lines;
291                 int numGroupsPerProcessor = groups.size() / processors;
292                 for (int i = 0; i < processors; i++) {
293                         int startIndex =  i * numGroupsPerProcessor;
294                         int endIndex = (i+1) * numGroupsPerProcessor;
295                         if(i == (processors - 1)){      endIndex = groups.size();       }
296                         lines.push_back(linePair(startIndex, endIndex));
297                 }
298                 
299 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
300                 
301                 //loop through and create all the processes you want
302                 while (process != processors) {
303                         int pid = fork();
304                         
305                         if (pid > 0) {
306                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
307                                 process++;
308                         }else if (pid == 0){
309                                 num = driverGroups(parser, newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", lines[process].start, lines[process].end, groups);
310                                 exit(0);
311                         }else { 
312                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
313                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
314                                 exit(0);
315                         }
316                 }
317                 
318                 //do my part
319                 num = driverGroups(parser, newFName, newNName, lines[0].start, lines[0].end, groups);
320                 
321                 //force parent to wait until all the processes are done
322                 for (int i=0;i<processIDS.size();i++) { 
323                         int temp = processIDS[i];
324                         wait(&temp);
325                 }
326                 
327 #else
328                 
329                 //////////////////////////////////////////////////////////////////////////////////////////////////////
330                 //Windows version shared memory, so be careful when passing variables through the preClusterData struct. 
331                 //Above fork() will clone, so memory is separate, but that's not the case with windows, 
332                 //////////////////////////////////////////////////////////////////////////////////////////////////////
333                 
334                 vector<preClusterData*> pDataArray; 
335                 DWORD   dwThreadIdArray[processors-1];
336                 HANDLE  hThreadArray[processors-1]; 
337                 
338                 //Create processor worker threads.
339                 for( int i=1; i<processors; i++ ){
340                         // Allocate memory for thread data.
341                         string extension = toString(i) + ".temp";
342                         
343                         preClusterData* tempPreCluster = new preClusterData(fastafile, namefile, groupfile, (newFName+extension), (newNName+extension), groups, m, lines[i].start, lines[i].end, diffs, i);
344                         pDataArray.push_back(tempPreCluster);
345                         processIDS.push_back(i);
346                         
347                         //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
348                         //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
349                         hThreadArray[i-1] = CreateThread(NULL, 0, MyPreclusterThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);   
350                 }
351                 
352                                 
353                 //using the main process as a worker saves time and memory
354                 num = driverGroups(parser, newFName, newNName, lines[0].start, lines[0].end, groups);
355                 
356                 //Wait until all threads have terminated.
357                 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
358                 
359                 //Close all thread handles and free memory allocations.
360                 for(int i=0; i < pDataArray.size(); i++){
361                         CloseHandle(hThreadArray[i]);
362                         delete pDataArray[i];
363                 }
364                 
365 #endif          
366                 
367                 //append output files
368                 for(int i=0;i<processIDS.size();i++){
369                         m->appendFiles((newFName + toString(processIDS[i]) + ".temp"), newFName);
370                         m->mothurRemove((newFName + toString(processIDS[i]) + ".temp"));
371                         
372                         m->appendFiles((newNName + toString(processIDS[i]) + ".temp"), newNName);
373                         m->mothurRemove((newNName + toString(processIDS[i]) + ".temp"));
374                 }
375                 
376                 return num;     
377                 
378         }
379         catch(exception& e) {
380                 m->errorOut(e, "PreClusterCommand", "createProcessesGroups");
381                 exit(1);
382         }
383 }
384 /**************************************************************************************************/
385 int PreClusterCommand::driverGroups(SequenceParser* parser, string newFFile, string newNFile, int start, int end, vector<string> groups){
386         try {
387                 
388                 int numSeqs = 0;
389                 
390                 //precluster each group
391                 for (int i = start; i < end; i++) {
392                         
393                         start = time(NULL);
394                         
395                         if (m->control_pressed) {  return 0; }
396                         
397                         m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[i] + ":"); m->mothurOutEndLine();
398                         
399                         map<string, string> thisNameMap;
400                         if (namefile != "") { thisNameMap = parser->getNameMap(groups[i]); }
401                         vector<Sequence> thisSeqs = parser->getSeqs(groups[i]);
402                         
403                         //fill alignSeqs with this groups info.
404                         numSeqs = loadSeqs(thisNameMap, thisSeqs);
405                         
406                         if (m->control_pressed) {   return 0; }
407                         
408                         if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); m->control_pressed = true; return 0;  }
409                         
410                         int count = process();
411                         
412                         if (m->control_pressed) {  return 0; }
413                         
414                         m->mothurOut("Total number of sequences before pre.cluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
415                         m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
416                         printData(newFFile, newNFile);
417                         
418                         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
419                         
420                 }
421                 
422                 return numSeqs;
423         }
424         catch(exception& e) {
425                 m->errorOut(e, "PreClusterCommand", "driverGroups");
426                 exit(1);
427         }
428 }
429 /**************************************************************************************************/
430 int PreClusterCommand::process(){
431         try {
432                 
433                 //sort seqs by number of identical seqs
434                 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
435                 
436                 int count = 0;
437                 int numSeqs = alignSeqs.size();
438                 
439                 //think about running through twice...
440                 for (int i = 0; i < numSeqs; i++) {
441                         
442                         //are you active
443                         //                      itActive = active.find(alignSeqs[i].seq.getName());
444                         
445                         if (alignSeqs[i].active) {  //this sequence has not been merged yet
446                                 
447                                 //try to merge it with all smaller seqs
448                                 for (int j = i+1; j < numSeqs; j++) {
449                                         
450                                         if (m->control_pressed) { return 0; }
451                                         
452                                         if (alignSeqs[j].active) {  //this sequence has not been merged yet
453                                                 //are you within "diff" bases
454                                                 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
455                                                 
456                                                 if (mismatch <= diffs) {
457                                                         //merge
458                                                         alignSeqs[i].names += ',' + alignSeqs[j].names;
459                                                         alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
460                                                         
461                                                         alignSeqs[j].active = 0;
462                                                         alignSeqs[j].numIdentical = 0;
463                                                         count++;
464                                                 }
465                                         }//end if j active
466                                 }//end if i != j
467                                 
468                                 //remove from active list 
469                                 alignSeqs[i].active = 0;
470                                 
471                         }//end if active i
472                         if(i % 100 == 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
473                 }
474                 
475                 if(numSeqs % 100 != 0)  { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine();   }       
476                 
477                 return count;
478                 
479         }
480         catch(exception& e) {
481                 m->errorOut(e, "PreClusterCommand", "process");
482                 exit(1);
483         }
484 }
485 /**************************************************************************************************/
486 int PreClusterCommand::readFASTA(){
487         try {
488                 //ifstream inNames;
489                 ifstream inFasta;
490                 
491                 //m->openInputFile(namefile, inNames);
492                 m->openInputFile(fastafile, inFasta);
493                 
494                 //string firstCol, secondCol, nameString;
495                 length = 0;
496                 
497                 while (!inFasta.eof()) {
498                         
499                         if (m->control_pressed) { inFasta.close(); return 0; }
500                         
501                         //inNames >> firstCol >> secondCol;
502                         //nameString = secondCol;
503                         
504                         //m->gobble(inNames);
505                         //int size = 1;
506                         //while (secondCol.find_first_of(',') != -1) { 
507                         //      size++;
508                         //      secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
509                         //}
510                         
511                         Sequence seq(inFasta);  m->gobble(inFasta);
512                         
513                         if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
514                                 if (namefile != "") {
515                                         itSize = sizes.find(seq.getName());
516                                         
517                                         if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
518                                         else{
519                                                 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
520                                                 alignSeqs.push_back(tempNode);
521                                                 if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
522                                         }       
523                                 }else { //no names file, you are identical to yourself 
524                                         seqPNode tempNode(1, seq, seq.getName());
525                                         alignSeqs.push_back(tempNode);
526                                         if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
527                                 }
528                         }
529                 }
530                 inFasta.close();
531                 //inNames.close();
532                 return alignSeqs.size();
533         }
534         
535         catch(exception& e) {
536                 m->errorOut(e, "PreClusterCommand", "readFASTA");
537                 exit(1);
538         }
539 }
540 /**************************************************************************************************/
541 int PreClusterCommand::loadSeqs(map<string, string>& thisName, vector<Sequence>& thisSeqs){
542         try {
543                 length = 0;
544                 alignSeqs.clear();
545                 map<string, string>::iterator it;
546                 bool error = false;
547                         
548                 for (int i = 0; i < thisSeqs.size(); i++) {
549                         
550                         if (m->control_pressed) { return 0; }
551                                                 
552                         if (namefile != "") {
553                                 it = thisName.find(thisSeqs[i].getName());
554                                 
555                                 //should never be true since parser checks for this
556                                 if (it == thisName.end()) { m->mothurOut(thisSeqs[i].getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); error = true; }
557                                 else{
558                                         //get number of reps
559                                         int numReps = 1;
560                                         for(int j=0;j<(it->second).length();j++){
561                                                 if((it->second)[j] == ','){     numReps++;      }
562                                         }
563                                         
564                                         seqPNode tempNode(numReps, thisSeqs[i], it->second);
565                                         alignSeqs.push_back(tempNode);
566                                         if (thisSeqs[i].getAligned().length() > length) {  length = thisSeqs[i].getAligned().length();  }
567                                 }       
568                         }else { //no names file, you are identical to yourself 
569                                 seqPNode tempNode(1, thisSeqs[i], thisSeqs[i].getName());
570                                 alignSeqs.push_back(tempNode);
571                                 if (thisSeqs[i].getAligned().length() > length) {  length = thisSeqs[i].getAligned().length();  }
572                         }
573                 }
574                 
575                 //sanity check
576                 if (error) { m->control_pressed = true; }
577                 
578                 thisSeqs.clear();
579                 
580                 return alignSeqs.size();
581         }
582         
583         catch(exception& e) {
584                 m->errorOut(e, "PreClusterCommand", "loadSeqs");
585                 exit(1);
586         }
587 }
588                                 
589 /**************************************************************************************************/
590
591 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
592         try {
593                 int numBad = 0;
594                 
595                 for (int i = 0; i < seq1.length(); i++) {
596                         //do they match
597                         if (seq1[i] != seq2[i]) { numBad++; }
598                         if (numBad > diffs) { return length;  } //to far to cluster
599                 }
600                 
601                 return numBad;
602         }
603         catch(exception& e) {
604                 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
605                 exit(1);
606         }
607 }
608
609 /**************************************************************************************************/
610
611 void PreClusterCommand::printData(string newfasta, string newname){
612         try {
613                 ofstream outFasta;
614                 ofstream outNames;
615                 
616                 if (bygroup) {
617                         m->openOutputFileAppend(newfasta, outFasta);
618                         m->openOutputFileAppend(newname, outNames);
619                 }else {
620                         m->openOutputFile(newfasta, outFasta);
621                         m->openOutputFile(newname, outNames);
622                 }
623                                 
624                 for (int i = 0; i < alignSeqs.size(); i++) {
625                         if (alignSeqs[i].numIdentical != 0) {
626                                 alignSeqs[i].seq.printSequence(outFasta); 
627                                 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
628                         }
629                 }
630                 
631                 outFasta.close();
632                 outNames.close();
633                 
634         }
635         catch(exception& e) {
636                 m->errorOut(e, "PreClusterCommand", "printData");
637                 exit(1);
638         }
639 }
640 /**************************************************************************************************/
641
642 void PreClusterCommand::readNameFile(){
643         try {
644                 ifstream in;
645                 m->openInputFile(namefile, in);
646                 string firstCol, secondCol;
647                                 
648                 while (!in.eof()) {
649                         in >> firstCol >> secondCol; m->gobble(in);
650                         names[firstCol] = secondCol;
651                         int size = 1;
652
653                         for(int i=0;i<secondCol.size();i++){
654                                 if(secondCol[i] == ','){        size++; }
655                         }
656                         sizes[firstCol] = size;
657                 }
658                 in.close();
659         }
660         catch(exception& e) {
661                 m->errorOut(e, "PreClusterCommand", "readNameFile");
662                 exit(1);
663         }
664 }
665
666 /**************************************************************************************************/
667
668