]> git.donarmstrong.com Git - mothur.git/blob - chimeraperseuscommand.cpp
working on chimera.perseus. made removeConfidences function smarter. Fixed bug in...
[mothur.git] / chimeraperseuscommand.cpp
1 /*
2  *  chimeraperseuscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/26/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "chimeraperseuscommand.h"
11 #include "deconvolutecommand.h"
12 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> ChimeraPerseusCommand::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,true); parameters.push_back(pname);
18                 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
19                 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
20                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22                 CommandParameter pcutoff("cutoff", "Number", "", "0.5", "", "", "",false,false); parameters.push_back(pcutoff);
23                 CommandParameter palpha("alpha", "Number", "", "-5.54", "", "", "",false,false); parameters.push_back(palpha);
24                 CommandParameter pbeta("beta", "Number", "", "0.33", "", "", "",false,false); parameters.push_back(pbeta);
25                         
26                 vector<string> myArray;
27                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
28                 return myArray;
29         }
30         catch(exception& e) {
31                 m->errorOut(e, "ChimeraPerseusCommand", "setParameters");
32                 exit(1);
33         }
34 }
35 //**********************************************************************************************************************
36 string ChimeraPerseusCommand::getHelpString(){  
37         try {
38                 string helpString = "";
39                 helpString += "The chimera.perseus command reads a fastafile and namefile and outputs potentially chimeric sequences.\n";
40                 helpString += "The chimera.perseus command parameters are fasta, name, group, cutoff, processors, alpha and beta.\n";
41                 helpString += "The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required, unless you have a valid current fasta file. \n";
42                 helpString += "The name parameter allows you to provide a name file associated with your fasta file. If none is given unique.seqs will be run to generate one. \n";
43                 helpString += "You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amazon.fasta \n";
44                 helpString += "The group parameter allows you to provide a group file.  When checking sequences, only sequences from the same group as the query sequence will be used as the reference. \n";
45                 helpString += "The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n";
46                 helpString += "The alpha parameter ....  The default is -5.54. \n";
47                 helpString += "The beta parameter ....  The default is 0.33. \n";
48                 helpString += "The cutoff parameter ....  The default is 0.50. \n";
49                 helpString += "The chimera.perseus command should be in the following format: \n";
50                 helpString += "chimera.perseus(fasta=yourFastaFile, name=yourNameFile) \n";
51                 helpString += "Example: chimera.perseus(fasta=AD.align, name=AD.names) \n";
52                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";       
53                 return helpString;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "ChimeraPerseusCommand", "getHelpString");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61 ChimeraPerseusCommand::ChimeraPerseusCommand(){ 
62         try {
63                 abort = true; calledHelp = true;
64                 setParameters();
65                 vector<string> tempOutNames;
66                 outputTypes["chimera"] = tempOutNames;
67                 outputTypes["accnos"] = tempOutNames;
68         }
69         catch(exception& e) {
70                 m->errorOut(e, "ChimeraPerseusCommand", "ChimeraPerseusCommand");
71                 exit(1);
72         }
73 }
74 //***************************************************************************************************************
75 ChimeraPerseusCommand::ChimeraPerseusCommand(string option)  {
76         try {
77                 abort = false; calledHelp = false; 
78                 
79                 //allow user to run help
80                 if(option == "help") { help(); abort = true; calledHelp = true; }
81                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
82                 
83                 else {
84                         vector<string> myArray = setParameters();
85                         
86                         OptionParser parser(option);
87                         map<string,string> parameters = parser.getParameters();
88                         
89                         ValidParameters validParameter("chimera.uchime");
90                         map<string,string>::iterator it;
91                         
92                         //check to make sure all parameters are valid for command
93                         for (it = parameters.begin(); it != parameters.end(); it++) { 
94                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
95                         }
96                         
97                         vector<string> tempOutNames;
98                         outputTypes["chimera"] = tempOutNames;
99                         outputTypes["accnos"] = tempOutNames;
100                         
101                         //if the user changes the input directory command factory will send this info to us in the output parameter 
102                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
103                         if (inputDir == "not found"){   inputDir = "";          }
104                         
105                         //check for required parameters
106                         fastafile = validParameter.validFile(parameters, "fasta", false);
107                         if (fastafile == "not found") {                                 
108                                 //if there is a current fasta file, use it
109                                 string filename = m->getFastaFile(); 
110                                 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
111                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
112                         }else { 
113                                 m->splitAtDash(fastafile, fastaFileNames);
114                                 
115                                 //go through files and make sure they are good, if not, then disregard them
116                                 for (int i = 0; i < fastaFileNames.size(); i++) {
117                                         
118                                         bool ignore = false;
119                                         if (fastaFileNames[i] == "current") { 
120                                                 fastaFileNames[i] = m->getFastaFile(); 
121                                                 if (fastaFileNames[i] != "") {  m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
122                                                 else {  
123                                                         m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
124                                                         //erase from file list
125                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
126                                                         i--;
127                                                 }
128                                         }
129                                         
130                                         if (!ignore) {
131                                                 
132                                                 if (inputDir != "") {
133                                                         string path = m->hasPath(fastaFileNames[i]);
134                                                         //if the user has not given a path then, add inputdir. else leave path alone.
135                                                         if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
136                                                 }
137                                                 
138                                                 int ableToOpen;
139                                                 ifstream in;
140                                                 
141                                                 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
142                                                 
143                                                 //if you can't open it, try default location
144                                                 if (ableToOpen == 1) {
145                                                         if (m->getDefaultPath() != "") { //default path is set
146                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
147                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
148                                                                 ifstream in2;
149                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
150                                                                 in2.close();
151                                                                 fastaFileNames[i] = tryPath;
152                                                         }
153                                                 }
154                                                 
155                                                 if (ableToOpen == 1) {
156                                                         if (m->getOutputDir() != "") { //default path is set
157                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
158                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
159                                                                 ifstream in2;
160                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
161                                                                 in2.close();
162                                                                 fastaFileNames[i] = tryPath;
163                                                         }
164                                                 }
165                                                 
166                                                 in.close();
167                                                 
168                                                 if (ableToOpen == 1) { 
169                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
170                                                         //erase from file list
171                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
172                                                         i--;
173                                                 }else {
174                                                         m->setFastaFile(fastaFileNames[i]);
175                                                 }
176                                         }
177                                 }
178                                 
179                                 //make sure there is at least one valid file left
180                                 if (fastaFileNames.size() == 0) { m->mothurOut("[ERROR]: no valid files."); m->mothurOutEndLine(); abort = true; }
181                         }
182                         
183                         
184                         //check for required parameters
185                         bool hasName = true;
186                         namefile = validParameter.validFile(parameters, "name", false);
187                         if (namefile == "not found") { 
188                                 //if there is a current fasta file, use it
189                                 string filename = m->getNameFile(); 
190                                 if (filename != "") { nameFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the name parameter."); m->mothurOutEndLine(); }
191                                 else {  m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }                                
192                                 hasName = false;
193                         }else { 
194                                 m->splitAtDash(namefile, nameFileNames);
195                                 
196                                 //go through files and make sure they are good, if not, then disregard them
197                                 for (int i = 0; i < nameFileNames.size(); i++) {
198                                         
199                                         bool ignore = false;
200                                         if (nameFileNames[i] == "current") { 
201                                                 nameFileNames[i] = m->getNameFile(); 
202                                                 if (nameFileNames[i] != "") {  m->mothurOut("Using " + nameFileNames[i] + " as input file for the name parameter where you had given current."); m->mothurOutEndLine(); }
203                                                 else {  
204                                                         m->mothurOut("You have no current namefile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
205                                                         //erase from file list
206                                                         nameFileNames.erase(nameFileNames.begin()+i);
207                                                         i--;
208                                                 }
209                                         }
210                                         
211                                         if (!ignore) {
212                                                 
213                                                 if (inputDir != "") {
214                                                         string path = m->hasPath(nameFileNames[i]);
215                                                         //if the user has not given a path then, add inputdir. else leave path alone.
216                                                         if (path == "") {       nameFileNames[i] = inputDir + nameFileNames[i];         }
217                                                 }
218                                                 
219                                                 int ableToOpen;
220                                                 ifstream in;
221                                                 
222                                                 ableToOpen = m->openInputFile(nameFileNames[i], in, "noerror");
223                                                 
224                                                 //if you can't open it, try default location
225                                                 if (ableToOpen == 1) {
226                                                         if (m->getDefaultPath() != "") { //default path is set
227                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(nameFileNames[i]);
228                                                                 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
229                                                                 ifstream in2;
230                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
231                                                                 in2.close();
232                                                                 nameFileNames[i] = tryPath;
233                                                         }
234                                                 }
235                                                 
236                                                 if (ableToOpen == 1) {
237                                                         if (m->getOutputDir() != "") { //default path is set
238                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(nameFileNames[i]);
239                                                                 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
240                                                                 ifstream in2;
241                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
242                                                                 in2.close();
243                                                                 nameFileNames[i] = tryPath;
244                                                         }
245                                                 }
246                                                 
247                                                 in.close();
248                                                 
249                                                 if (ableToOpen == 1) { 
250                                                         m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
251                                                         //erase from file list
252                                                         nameFileNames.erase(nameFileNames.begin()+i);
253                                                         i--;
254                                                 }else {
255                                                         m->setNameFile(nameFileNames[i]);
256                                                 }
257                                         }
258                                 }
259                                 
260                                 //make sure there is at least one valid file left
261                                 if (nameFileNames.size() == 0) { m->mothurOut("[ERROR]: no valid name files."); m->mothurOutEndLine(); abort = true; }
262                         }
263                         
264                         if (hasName && (nameFileNames.size() != fastaFileNames.size())) { m->mothurOut("[ERROR]: The number of namefiles does not match the number of fastafiles, please correct."); m->mothurOutEndLine(); abort=true; }
265                         
266                         bool hasGroup = true;
267                         groupfile = validParameter.validFile(parameters, "group", false);
268                         if (groupfile == "not found") { groupfile = "";  hasGroup = false; }
269                         else { 
270                                 m->splitAtDash(groupfile, groupFileNames);
271                                 
272                                 //go through files and make sure they are good, if not, then disregard them
273                                 for (int i = 0; i < groupFileNames.size(); i++) {
274                                         
275                                         bool ignore = false;
276                                         if (groupFileNames[i] == "current") { 
277                                                 groupFileNames[i] = m->getGroupFile(); 
278                                                 if (groupFileNames[i] != "") {  m->mothurOut("Using " + groupFileNames[i] + " as input file for the group parameter where you had given current."); m->mothurOutEndLine(); }
279                                                 else {  
280                                                         m->mothurOut("You have no current namefile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
281                                                         //erase from file list
282                                                         groupFileNames.erase(groupFileNames.begin()+i);
283                                                         i--;
284                                                 }
285                                         }
286                                         
287                                         if (!ignore) {
288                                                 
289                                                 if (inputDir != "") {
290                                                         string path = m->hasPath(groupFileNames[i]);
291                                                         //if the user has not given a path then, add inputdir. else leave path alone.
292                                                         if (path == "") {       groupFileNames[i] = inputDir + groupFileNames[i];               }
293                                                 }
294                                                 
295                                                 int ableToOpen;
296                                                 ifstream in;
297                                                 
298                                                 ableToOpen = m->openInputFile(groupFileNames[i], in, "noerror");
299                                                 
300                                                 //if you can't open it, try default location
301                                                 if (ableToOpen == 1) {
302                                                         if (m->getDefaultPath() != "") { //default path is set
303                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(groupFileNames[i]);
304                                                                 m->mothurOut("Unable to open " + groupFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
305                                                                 ifstream in2;
306                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
307                                                                 in2.close();
308                                                                 groupFileNames[i] = tryPath;
309                                                         }
310                                                 }
311                                                 
312                                                 if (ableToOpen == 1) {
313                                                         if (m->getOutputDir() != "") { //default path is set
314                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(groupFileNames[i]);
315                                                                 m->mothurOut("Unable to open " + groupFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
316                                                                 ifstream in2;
317                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
318                                                                 in2.close();
319                                                                 groupFileNames[i] = tryPath;
320                                                         }
321                                                 }
322                                                 
323                                                 in.close();
324                                                 
325                                                 if (ableToOpen == 1) { 
326                                                         m->mothurOut("Unable to open " + groupFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
327                                                         //erase from file list
328                                                         groupFileNames.erase(groupFileNames.begin()+i);
329                                                         i--;
330                                                 }else {
331                                                         m->setGroupFile(groupFileNames[i]);
332                                                 }
333                                         }
334                                 }
335                                 
336                                 //make sure there is at least one valid file left
337                                 if (groupFileNames.size() == 0) { m->mothurOut("[ERROR]: no valid group files."); m->mothurOutEndLine(); abort = true; }
338                         }
339                         
340                         if (hasGroup && (groupFileNames.size() != fastaFileNames.size())) { m->mothurOut("[ERROR]: The number of groupfiles does not match the number of fastafiles, please correct."); m->mothurOutEndLine(); abort=true; }
341                         
342                         
343                         //if the user changes the output directory command factory will send this info to us in the output parameter 
344                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
345                         
346                         string temp = validParameter.validFile(parameters, "processors", false);        if (temp == "not found"){       temp = m->getProcessors();      }
347                         m->setProcessors(temp);
348                         convert(temp, processors);
349                         
350                         temp = validParameter.validFile(parameters, "cutoff", false);   if (temp == "not found"){       temp = "0.50";  }
351                         convert(temp, cutoff);
352                         
353                         temp = validParameter.validFile(parameters, "alpha", false);    if (temp == "not found"){       temp = "-5.54"; }
354                         convert(temp, alpha);
355                         
356                         temp = validParameter.validFile(parameters, "cutoff", false);   if (temp == "not found"){       temp = "0.33";  }
357                         convert(temp, beta);
358                 }
359         }
360         catch(exception& e) {
361                 m->errorOut(e, "ChimeraPerseusCommand", "ChimeraPerseusCommand");
362                 exit(1);
363         }
364 }
365 //***************************************************************************************************************
366
367 int ChimeraPerseusCommand::execute(){
368         try{
369                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
370                 
371                                 
372                 //process each file
373                 for (int s = 0; s < fastaFileNames.size(); s++) {
374                         
375                         m->mothurOut("Checking sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
376                         
377                         int start = time(NULL); 
378                         if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[s]);  }//if user entered a file with a path then preserve it                               
379                         string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "perseus.chimera";
380                         string accnosFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s]))  + "perseus.accnos";
381                         //string newFasta = m->getRootName(fastaFileNames[s]) + "temp";
382                         
383                         //you provided a groupfile
384                         string groupFile = "";
385                         if (groupFileNames.size() != 0) { groupFile = groupFileNames[s]; }
386                         
387                         string nameFile = "";
388                         if (nameFileNames.size() != 0) { //you provided a namefile and we don't need to create one
389                                 nameFile = nameFileNames[s];
390                         }else { nameFile = getNamesFile(fastaFileNames[s]); }
391                         
392                         if (m->control_pressed) {  for (int j = 0; j < outputNames.size(); j++) {       m->mothurRemove(outputNames[j]);        } return 0;     }                               
393                         
394                         int numSeqs = 0;
395                         int numChimeras = 0;
396                         
397                         if (groupFile != "") {
398                                 //Parse sequences by group
399                                 SequenceParser parser(groupFile, fastaFileNames[s], nameFile);
400                                 vector<string> groups = parser.getNamesOfGroups();
401                                 
402                                 if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) {        m->mothurRemove(outputNames[j]);        }  return 0; }
403                                 
404                                 //clears files
405                                 ofstream out, out1, out2;
406                                 m->openOutputFile(outputFileName, out); out.close(); 
407                                 m->openOutputFile(accnosFileName, out1); out1.close();
408                                 
409                                 if(processors == 1)     {       numSeqs = driverGroups(parser, outputFileName, accnosFileName, 0, groups.size(), groups);       }
410                                 else                            {       numSeqs = createProcessesGroups(parser, outputFileName, accnosFileName, groups, groupFile, fastaFileNames[s], nameFile);                        }
411                                 
412                                 if (m->control_pressed) {  for (int j = 0; j < outputNames.size(); j++) {       m->mothurRemove(outputNames[j]);        }  return 0;    }                               
413                                 
414                                 numChimeras = deconvoluteResults(parser, outputFileName, accnosFileName);
415                                 
416                                 m->mothurOut("The number of sequences checked may be larger than the number of unique sequences because some sequences are found in several samples."); m->mothurOutEndLine(); 
417                                 
418                                 if (m->control_pressed) {  for (int j = 0; j < outputNames.size(); j++) {       m->mothurRemove(outputNames[j]);        }  return 0;    }                               
419                                 
420                         }else{
421                                 if (processors != 1) { m->mothurOut("Without a groupfile, mothur can only use 1 processor, continuing."); m->mothurOutEndLine(); processors = 1; }
422                                 
423                                 //read sequences and store sorted by frequency
424                                 vector<seqData> sequences = readFiles(fastaFileNames[s], nameFile);
425                                 
426                                 if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) {        m->mothurRemove(outputNames[j]);        } return 0; }
427                                 
428                                 numSeqs = driver(outputFileName, sequences, accnosFileName, numChimeras); 
429                         }
430                         
431                         if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) {        m->mothurRemove(outputNames[j]);        } return 0; }
432                         
433                         m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences. " + toString(numChimeras) + " chimeras were found.");      m->mothurOutEndLine();
434                         outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
435                         outputNames.push_back(accnosFileName); outputTypes["accnos"].push_back(accnosFileName);
436                 }
437                 
438                 //set accnos file as new current accnosfile
439                 string current = "";
440                 itTypes = outputTypes.find("accnos");
441                 if (itTypes != outputTypes.end()) {
442                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
443                 }
444                 
445                 m->mothurOutEndLine();
446                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
447                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
448                 m->mothurOutEndLine();
449                 
450                 return 0;
451                 
452         }
453         catch(exception& e) {
454                 m->errorOut(e, "ChimeraPerseusCommand", "execute");
455                 exit(1);
456         }
457 }
458 //**********************************************************************************************************************
459 string ChimeraPerseusCommand::getNamesFile(string& inputFile){
460         try {
461                 string nameFile = "";
462                 
463                 m->mothurOutEndLine(); m->mothurOut("No namesfile given, running unique.seqs command to generate one."); m->mothurOutEndLine(); m->mothurOutEndLine();
464                 
465                 //use unique.seqs to create new name and fastafile
466                 string inputString = "fasta=" + inputFile;
467                 m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
468                 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine(); 
469                 
470                 Command* uniqueCommand = new DeconvoluteCommand(inputString);
471                 uniqueCommand->execute();
472                 
473                 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
474                 
475                 delete uniqueCommand;
476                 
477                 m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
478                 
479                 nameFile = filenames["name"][0];
480                 inputFile = filenames["fasta"][0];
481                 
482                 return nameFile;
483         }
484         catch(exception& e) {
485                 m->errorOut(e, "ChimeraPerseusCommand", "getNamesFile");
486                 exit(1);
487         }
488 }
489 //**********************************************************************************************************************
490 int ChimeraPerseusCommand::driverGroups(SequenceParser& parser, string outputFName, string accnos, int start, int end, vector<string> groups){
491         try {
492                 
493                 int totalSeqs = 0;
494                 int numChimeras = 0;
495                 
496                 for (int i = start; i < end; i++) {
497                         
498                         m->mothurOutEndLine(); m->mothurOut("Checking sequences from group " + groups[i] + "...");      m->mothurOutEndLine();                                  
499                         
500                         int start = time(NULL);  if (m->control_pressed) {  return 0; }
501                         
502                         vector<seqData> sequences = loadSequences(parser, groups[i]);
503                         
504                         if (m->control_pressed) { return 0; }
505                         
506                         int numSeqs = driver((outputFName + groups[i]), sequences, (accnos+groups[i]), numChimeras);
507                         totalSeqs += numSeqs;
508                         
509                         if (m->control_pressed) { return 0; }
510                         
511                         //append files
512                         m->appendFiles((outputFName+groups[i]), outputFName); m->mothurRemove((outputFName+groups[i]));
513                         m->appendFiles((accnos+groups[i]), accnos); m->mothurRemove((accnos+groups[i]));
514                         
515                         m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences from group " + groups[i] + ".");    m->mothurOutEndLine();                                  
516                 }       
517                 
518                 return totalSeqs;
519                 
520         }
521         catch(exception& e) {
522                 m->errorOut(e, "ChimeraPerseusCommand", "driverGroups");
523                 exit(1);
524         }
525 }       
526 //**********************************************************************************************************************
527 vector<seqData> ChimeraPerseusCommand::loadSequences(SequenceParser& parser, string group){
528         try {
529                 
530                 vector<Sequence> thisGroupsSeqs = parser.getSeqs(group);
531                 map<string, string> nameMap = parser.getNameMap(group);
532                 map<string, string>::iterator it;
533                 
534                 vector<seqData> sequences;
535                 bool error = false;
536                 
537                 for (int i = 0; i < thisGroupsSeqs.size(); i++) {
538                 
539                         if (m->control_pressed) {  return sequences; }
540                         
541                         it = nameMap.find(thisGroupsSeqs[i].getName());
542                         if (it == nameMap.end()) { error = true; m->mothurOut("[ERROR]: " + thisGroupsSeqs[i].getName() + " is in your fasta file and not in your namefile, please correct."); m->mothurOutEndLine(); }
543                         else {
544                                 int num = m->getNumNames(it->second);
545                                 sequences.push_back(seqData(thisGroupsSeqs[i].getName(), thisGroupsSeqs[i].getUnaligned(), num));
546                         }
547                 }
548                 
549                 if (error) { m->control_pressed = true; }
550                 
551                 //sort by frequency
552                 sort(sequences.rbegin(), sequences.rend());
553                 
554                 return sequences;
555         }
556         catch(exception& e) {
557                 m->errorOut(e, "ChimeraPerseusCommand", "loadSequences");
558                 exit(1);
559         }
560 }
561
562 //**********************************************************************************************************************
563 vector<seqData> ChimeraPerseusCommand::readFiles(string inputFile, string name){
564         try {
565                 map<string, int>::iterator it;
566                 map<string, int> nameMap = m->readNames(name);
567                 
568                 //read fasta file and create sequenceData structure - checking for file mismatches
569                 vector<seqData> sequences;
570                 bool error = false;
571                 ifstream in;
572                 m->openInputFile(inputFile, in);
573                 
574                 while (!in.eof()) {
575                         
576                         if (m->control_pressed) { in.close(); return sequences; }
577                         
578                         Sequence temp(in); m->gobble(in);
579                         
580                         it = nameMap.find(temp.getName());
581                         if (it == nameMap.end()) { error = true; m->mothurOut("[ERROR]: " + temp.getName() + " is in your fasta file and not in your namefile, please correct."); m->mothurOutEndLine(); }
582                         else {
583                                 sequences.push_back(seqData(temp.getName(), temp.getUnaligned(), it->second));
584                         }
585                 }
586                 in.close();
587                 
588                 if (error) { m->control_pressed = true; }
589                 
590                 //sort by frequency
591                 sort(sequences.rbegin(), sequences.rend());
592                 
593                 return sequences;
594         }
595         catch(exception& e) {
596                 m->errorOut(e, "ChimeraPerseusCommand", "getNamesFile");
597                 exit(1);
598         }
599 }
600 //**********************************************************************************************************************
601 int ChimeraPerseusCommand::driver(string chimeraFileName, vector<seqData>& sequences, string accnosFileName, int& numChimeras){
602         try {
603                 
604                 vector<vector<double> > correctModel(4);        //could be an option in the future to input own model matrix
605                 for(int i=0;i<4;i++){   correctModel[i].resize(4);      }
606                 
607                 correctModel[0][0] = 0.000000;  //AA
608                 correctModel[1][0] = 11.619259; //CA
609                 correctModel[2][0] = 11.694004; //TA
610                 correctModel[3][0] = 7.748623;  //GA
611                 
612                 correctModel[1][1] = 0.000000;  //CC
613                 correctModel[2][1] = 7.619657;  //TC
614                 correctModel[3][1] = 12.852562; //GC
615                 
616                 correctModel[2][2] = 0.000000;  //TT
617                 correctModel[3][2] = 10.964048; //TG
618                 
619                 correctModel[3][3] = 0.000000;  //GG
620                 
621                 for(int i=0;i<4;i++){
622                         for(int j=0;j<i;j++){
623                                 correctModel[j][i] = correctModel[i][j];
624                         }
625                 }
626                 
627                 int numSeqs = sequences.size();
628                 int alignLength = sequences[0].sequence.size();
629                 
630                 ofstream chimeraFile;
631                 ofstream accnosFile;
632                 m->openOutputFile(chimeraFileName, chimeraFile); 
633                 m->openOutputFile(accnosFileName, accnosFile); 
634                 
635                 Perseus myPerseus;
636                 vector<vector<double> > binMatrix = myPerseus.binomial(alignLength);
637                 
638                 chimeraFile << "SequenceIndex\tName\tDiffsToBestMatch\tBestMatchIndex\tBestMatchName\tDiffstToChimera\tIndexofLeftParent\tIndexOfRightParent\tNameOfLeftParent\tNameOfRightParent\tDistanceToBestMatch\tcIndex\t(cIndex - singleDist)\tloonIndex\tMismatchesToChimera\tMismatchToTrimera\tChimeraBreakPoint\tLogisticProbability\tTypeOfSequence\n";
639                 
640                 vector<bool> chimeras(numSeqs, 0);
641                 
642                 for(int i=0;i<numSeqs;i++){     
643                         cout << sequences[i].seqName << endl << sequences[i].sequence << endl << sequences[i].frequency << endl;
644                         if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
645                         
646                         vector<bool> restricted = chimeras;
647                         
648                         vector<vector<int> > leftDiffs(numSeqs);
649                         vector<vector<int> > leftMaps(numSeqs);
650                         vector<vector<int> > rightDiffs(numSeqs);
651                         vector<vector<int> > rightMaps(numSeqs);
652                         
653                         vector<int> singleLeft, bestLeft;
654                         vector<int> singleRight, bestRight;
655                         
656                         int bestSingleIndex, bestSingleDiff;
657                         vector<pwAlign> alignments(numSeqs);
658                         
659                         int comparisons = myPerseus.getAlignments(i, sequences, alignments, leftDiffs, leftMaps, rightDiffs, rightMaps, bestSingleIndex, bestSingleDiff, restricted);
660                         cout << "comparisons = " << comparisons << endl;
661                         if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
662
663                         int minMismatchToChimera, leftParentBi, rightParentBi, breakPointBi;
664                         
665                         string dummyA, dummyB;
666                         
667                         if(comparisons >= 2){   
668                                 minMismatchToChimera = myPerseus.getChimera(sequences, leftDiffs, rightDiffs, leftParentBi, rightParentBi, breakPointBi, singleLeft, bestLeft, singleRight, bestRight, restricted);
669                                 cout << "minMismatchToChimera = " << minMismatchToChimera << endl;
670                                 if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
671
672                                 int minMismatchToTrimera = numeric_limits<int>::max();
673                                 int leftParentTri, middleParentTri, rightParentTri, breakPointTriA, breakPointTriB;
674                                 
675                                 if(minMismatchToChimera >= 3 && comparisons >= 3){
676                                         minMismatchToTrimera = myPerseus.getTrimera(sequences, leftDiffs, leftParentTri, middleParentTri, rightParentTri, breakPointTriA, breakPointTriB, singleLeft, bestLeft, singleRight, bestRight, restricted);
677                                         cout << "minMismatchToTrimera = " << minMismatchToTrimera << endl;
678                                         if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
679                                 }
680                                 
681                                 double singleDist = myPerseus.modeledPairwiseAlignSeqs(sequences[i].sequence, sequences[bestSingleIndex].sequence, dummyA, dummyB, correctModel);
682                                 cout << "singleDist = " << singleDist << endl;
683                                 if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
684
685                                 string type;
686                                 string chimeraRefSeq;
687                                 
688                                 if(minMismatchToChimera - minMismatchToTrimera >= 3){
689                                         type = "trimera";
690                                         chimeraRefSeq = myPerseus.stitchTrimera(alignments, leftParentTri, middleParentTri, rightParentTri, breakPointTriA, breakPointTriB, leftMaps, rightMaps);
691                                 }
692                                 else{
693                                         type = "chimera";
694                                         chimeraRefSeq = myPerseus.stitchBimera(alignments, leftParentBi, rightParentBi, breakPointBi, leftMaps, rightMaps);
695                                 }
696                                 cout << "chimeraRefSeq = " << chimeraRefSeq << endl;
697                                 if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
698                                 
699                                 double chimeraDist = myPerseus.modeledPairwiseAlignSeqs(sequences[i].sequence, chimeraRefSeq, dummyA, dummyB, correctModel);
700                                 cout << "chimeraDist = " << chimeraDist << endl;
701                                 if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
702
703                                 double cIndex = chimeraDist;//modeledPairwiseAlignSeqs(sequences[i].sequence, chimeraRefSeq);
704                                 double loonIndex = myPerseus.calcLoonIndex(sequences[i].sequence, sequences[leftParentBi].sequence, sequences[rightParentBi].sequence, breakPointBi, binMatrix);                
705                                 cout << "loonIndex = " << loonIndex << endl;
706                                 if (m->control_pressed) { chimeraFile.close(); m->mothurRemove(chimeraFileName); accnosFile.close(); m->mothurRemove(accnosFileName); return 0; }
707
708                                 chimeraFile << i << '\t' << sequences[i].seqName << '\t' << bestSingleDiff << '\t' << bestSingleIndex << '\t' << sequences[bestSingleIndex].seqName << '\t';
709                                 chimeraFile << minMismatchToChimera << '\t' << leftParentBi << '\t' << rightParentBi << '\t' << sequences[leftParentBi].seqName << '\t' << sequences[rightParentBi].seqName << '\t';
710                                 chimeraFile << singleDist << '\t' << cIndex << '\t' << (cIndex - singleDist) << '\t' << loonIndex << '\t';
711                                 chimeraFile << minMismatchToChimera << '\t' << minMismatchToTrimera << '\t' << breakPointBi << '\t';
712                                 
713                                 double probability = myPerseus.classifyChimera(singleDist, cIndex, loonIndex, alpha, beta);
714                                 
715                                 chimeraFile << probability << '\t';
716                                 
717                                 if(probability > cutoff){ 
718                                         chimeraFile << type << endl;
719                                         accnosFile << sequences[i].seqName << endl;
720                                         chimeras[i] = 1;
721                                         numChimeras++;
722                                 }
723                                 else{
724                                         chimeraFile << "good" << endl;
725                                 }
726                                 
727                         }
728                         else{
729                                 chimeraFile << i << '\t' << sequences[i].seqName << "\t0\t0\tNull\t0\t0\t0\tNull\tNull\t0.0\t0.0\t0.0\t0\t0\t0\t0.0\t0.0\tgood" << endl;
730                         }
731         
732                         //report progress
733                         if((i+1) % 100 == 0){   m->mothurOut("Processing sequence: " + toString(i+1) + "\n");           }
734                 }
735                 
736                 if((numSeqs) % 100 != 0){       m->mothurOut("Processing sequence: " + toString(numSeqs) + "\n");               }
737                 
738                 chimeraFile.close();
739                 accnosFile.close();
740                 
741                 return numSeqs;
742         }
743         catch(exception& e) {
744                 m->errorOut(e, "ChimeraPerseusCommand", "driver");
745                 exit(1);
746         }
747 }
748 /**************************************************************************************************/
749 int ChimeraPerseusCommand::createProcessesGroups(SequenceParser& parser, string outputFName, string accnos, vector<string> groups, string group, string fasta, string name) {
750         try {
751                 
752                 vector<int> processIDS;
753                 int process = 1;
754                 int num = 0;
755                 
756                 //sanity check
757                 if (groups.size() < processors) { processors = groups.size(); }
758                 
759                 //divide the groups between the processors
760                 vector<linePair> lines;
761                 int numGroupsPerProcessor = groups.size() / processors;
762                 for (int i = 0; i < processors; i++) {
763                         int startIndex =  i * numGroupsPerProcessor;
764                         int endIndex = (i+1) * numGroupsPerProcessor;
765                         if(i == (processors - 1)){      endIndex = groups.size();       }
766                         lines.push_back(linePair(startIndex, endIndex));
767                 }
768                 
769 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
770                 
771                 //loop through and create all the processes you want
772                 while (process != processors) {
773                         int pid = fork();
774                         
775                         if (pid > 0) {
776                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
777                                 process++;
778                         }else if (pid == 0){
779                                 num = driverGroups(parser, outputFName + toString(getpid()) + ".temp", accnos + toString(getpid()) + ".temp", lines[process].start, lines[process].end, groups);
780                                 
781                                 //pass numSeqs to parent
782                                 ofstream out;
783                                 string tempFile = outputFName + toString(getpid()) + ".num.temp";
784                                 m->openOutputFile(tempFile, out);
785                                 out << num << endl;
786                                 out.close();
787                                 
788                                 exit(0);
789                         }else { 
790                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
791                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
792                                 exit(0);
793                         }
794                 }
795                 
796                 //do my part
797                 num = driverGroups(parser, outputFName, accnos, lines[0].start, lines[0].end, groups);
798                 
799                 //force parent to wait until all the processes are done
800                 for (int i=0;i<processIDS.size();i++) { 
801                         int temp = processIDS[i];
802                         wait(&temp);
803                 }
804                 
805                 for (int i = 0; i < processIDS.size(); i++) {
806                         ifstream in;
807                         string tempFile =  outputFName + toString(processIDS[i]) + ".num.temp";
808                         m->openInputFile(tempFile, in);
809                         if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
810                         in.close(); m->mothurRemove(tempFile);
811                 }
812                 
813 #else
814                 //////////////////////////////////////////////////////////////////////////////////////////////////////
815                 //Windows version shared memory, so be careful when passing variables through the preClusterData struct. 
816                 //Above fork() will clone, so memory is separate, but that's not the case with windows, 
817                 //////////////////////////////////////////////////////////////////////////////////////////////////////
818                 
819                 vector<perseusData*> pDataArray; 
820                 DWORD   dwThreadIdArray[processors-1];
821                 HANDLE  hThreadArray[processors-1]; 
822                 
823                 //Create processor worker threads.
824                 for( int i=1; i<processors; i++ ){
825                         // Allocate memory for thread data.
826                         string extension = toString(i) + ".temp";
827                         
828                         perseusData* tempPerseus = new perseusData(alpha, beta, cutoff, outputFName+extension, fasta, name, group, accnos+extension, groups, m, lines[i].start, lines[i].end, i);
829                         
830                         pDataArray.push_back(tempPerseus);
831                         processIDS.push_back(i);
832                         
833                         //MyPerseusThreadFunction is in header. It must be global or static to work with the threads.
834                         //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
835                         hThreadArray[i-1] = CreateThread(NULL, 0, MyPerseusThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);   
836                 }
837                 
838                 
839                 //using the main process as a worker saves time and memory
840                 num = driverGroups(parser, outputFName, accnos, lines[0].start, lines[0].end, groups);
841                 
842                 //Wait until all threads have terminated.
843                 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
844                 cout << "here" << endl; 
845                 //Close all thread handles and free memory allocations.
846                 for(int i=0; i < pDataArray.size(); i++){
847                         num += pDataArray[i]->count;
848                         CloseHandle(hThreadArray[i]);
849                         delete pDataArray[i];
850                 }
851 #endif          
852                 
853                 
854                 //append output files
855                 for(int i=0;i<processIDS.size();i++){
856                         m->appendFiles((outputFName + toString(processIDS[i]) + ".temp"), outputFName);
857                         m->mothurRemove((outputFName + toString(processIDS[i]) + ".temp"));
858                         
859                         m->appendFiles((accnos + toString(processIDS[i]) + ".temp"), accnos);
860                         m->mothurRemove((accnos + toString(processIDS[i]) + ".temp"));
861                 }
862                 
863                 return num;     
864                 
865         }
866         catch(exception& e) {
867                 m->errorOut(e, "ChimeraPerseusCommand", "createProcessesGroups");
868                 exit(1);
869         }
870 }
871 //**********************************************************************************************************************
872 int ChimeraPerseusCommand::deconvoluteResults(SequenceParser& parser, string outputFileName, string accnosFileName){
873         try {
874                 map<string, string> uniqueNames = parser.getAllSeqsMap();
875                 map<string, string>::iterator itUnique;
876                 int total = 0;
877                 
878                 //edit accnos file
879                 ifstream in2; 
880                 m->openInputFile(accnosFileName, in2);
881                 
882                 ofstream out2;
883                 m->openOutputFile(accnosFileName+".temp", out2);
884                 
885                 string name;
886                 set<string> namesInFile; //this is so if a sequence is found to be chimera in several samples we dont write it to the results file more than once
887                 set<string>::iterator itNames;
888                 set<string> chimerasInFile;
889                 set<string>::iterator itChimeras;
890                 
891                 
892                 while (!in2.eof()) {
893                         if (m->control_pressed) { in2.close(); out2.close(); m->mothurRemove(outputFileName); m->mothurRemove((accnosFileName+".temp")); return 0; }
894                         
895                         in2 >> name; m->gobble(in2);
896                         
897                         //find unique name
898                         itUnique = uniqueNames.find(name);
899                         
900                         if (itUnique == uniqueNames.end()) { m->mothurOut("[ERROR]: trouble parsing accnos results. Cannot find "+ name + "."); m->mothurOutEndLine(); m->control_pressed = true; }
901                         else {
902                                 itChimeras = chimerasInFile.find((itUnique->second));
903                                 
904                                 if (itChimeras == chimerasInFile.end()) {
905                                         out2 << itUnique->second << endl;
906                                         chimerasInFile.insert((itUnique->second));
907                                         total++;
908                                 }
909                         }
910                 }
911                 in2.close();
912                 out2.close();
913                 
914                 m->mothurRemove(accnosFileName);
915                 rename((accnosFileName+".temp").c_str(), accnosFileName.c_str());
916                 
917                 //edit chimera file
918                 ifstream in; 
919                 m->openInputFile(outputFileName, in);
920                 
921                 ofstream out;
922                 m->openOutputFile(outputFileName+".temp", out); out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
923                 
924                 int DiffsToBestMatch, BestMatchIndex, DiffstToChimera, IndexofLeftParent, IndexOfRightParent;
925                 float temp1,temp2, temp3, temp4, temp5, temp6, temp7, temp8;
926                 string index, BestMatchName, parent1, parent2, flag;
927                 name = "";
928                 namesInFile.clear();    
929                 //assumptions - in file each read will always look like 
930                 /*                                                                              
931                  SequenceIndex  Name    DiffsToBestMatch        BestMatchIndex  BestMatchName   DiffstToChimera IndexofLeftParent       IndexOfRightParent      NameOfLeftParent        NameOfRightParent       DistanceToBestMatch     cIndex  (cIndex - singleDist)   loonIndex       MismatchesToChimera     MismatchToTrimera       ChimeraBreakPoint       LogisticProbability     TypeOfSequence
932                  0      F01QG4L02JVBQY  0       0       Null    0       0       0       Null    Null    0.0     0.0     0.0     0.0     0       0       0       0.0     0.0     good
933                  1      F01QG4L02ICTC6  0       0       Null    0       0       0       Null    Null    0.0     0.0     0.0     0.0     0       0       0       0.0     0.0     good
934                  2      F01QG4L02JZOEC  48      0       F01QG4L02JVBQY  47      0       0       F01QG4L02JVBQY  F01QG4L02JVBQY  2.0449  2.03545 -0.00944493     0       47      2147483647      138     0       good
935                  3      F01QG4L02G7JEC  42      0       F01QG4L02JVBQY  40      1       0       F01QG4L02ICTC6  F01QG4L02JVBQY  1.87477 1.81113 -0.0636404      5.80145 40      2147483647      25      0       good
936                  */
937                 
938                 //get and print headers
939                 BestMatchName = m->getline(in); m->gobble(in);
940                 out << BestMatchName << endl;
941                 
942                 while (!in.eof()) {
943                         
944                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove((outputFileName+".temp")); return 0; }
945                         
946                         bool print = false;
947                         in >> index;    m->gobble(in);
948                         
949                         if (index != "SequenceIndex") { //if you are not a header line, there will be a header line for each group if group file is given
950                                 in >> name;             m->gobble(in);
951                                 in >> DiffsToBestMatch; m->gobble(in);
952                                 in >> BestMatchIndex; m->gobble(in);
953                                 in >> BestMatchName; m->gobble(in);
954                                 in >> DiffstToChimera; m->gobble(in);
955                                 in >> IndexofLeftParent; m->gobble(in);
956                                 in >> IndexOfRightParent; m->gobble(in);
957                                 in >> parent1;  m->gobble(in);
958                                 in >> parent2;  m->gobble(in);
959                                 in >> temp1 >> temp2 >> temp3 >> temp4 >> temp5 >> temp6 >> temp7 >> temp8 >> flag; m->gobble(in);
960                                 
961                                 //find unique name
962                                 itUnique = uniqueNames.find(name);
963                                 
964                                 if (itUnique == uniqueNames.end()) { m->mothurOut("[ERROR]: trouble parsing chimera results. Cannot find "+ name + "."); m->mothurOutEndLine(); m->control_pressed = true; }
965                                 else {
966                                         name = itUnique->second;
967                                         //is this name already in the file
968                                         itNames = namesInFile.find((name));
969                                         
970                                         if (itNames == namesInFile.end()) { //no not in file
971                                                 if (flag == "good") { //are you really a no??
972                                                         //is this sequence really not chimeric??
973                                                         itChimeras = chimerasInFile.find(name);
974                                                         
975                                                         //then you really are a no so print, otherwise skip
976                                                         if (itChimeras == chimerasInFile.end()) { print = true; }
977                                                 }else{ print = true; }
978                                         }
979                                 }
980                                 
981                                 if (print) {
982                                         out << index << '\t' << name  << '\t' << DiffsToBestMatch << '\t' << BestMatchIndex << '\t';
983                                         namesInFile.insert(name);
984                                         
985                                         if (BestMatchName != "Null") {
986                                                 itUnique = uniqueNames.find(BestMatchName);
987                                                 if (itUnique == uniqueNames.end()) { m->mothurOut("[ERROR]: trouble parsing chimera results. Cannot find BestMatchName "+ BestMatchName + "."); m->mothurOutEndLine(); m->control_pressed = true; }
988                                                 else {  out << itUnique->second << '\t';        }                                       
989                                         }else { out << "Null" << '\t'; }
990                                         
991                                         out << DiffstToChimera << '\t' << IndexofLeftParent << '\t' << IndexOfRightParent << '\t';
992                                         
993                                         if (parent1 != "Null") {
994                                                 itUnique = uniqueNames.find(parent1);
995                                                 if (itUnique == uniqueNames.end()) { m->mothurOut("[ERROR]: trouble parsing chimera results. Cannot find parent1 "+ parent1 + "."); m->mothurOutEndLine(); m->control_pressed = true; }
996                                                 else {  out << itUnique->second << '\t';        }
997                                         }else { out << "Null" << '\t'; }
998                                         
999                                         if (parent1 != "Null") {
1000                                                 itUnique = uniqueNames.find(parent2);
1001                                                 if (itUnique == uniqueNames.end()) { m->mothurOut("[ERROR]: trouble parsing chimera results. Cannot find parent2 "+ parent2 + "."); m->mothurOutEndLine(); m->control_pressed = true; }
1002                                                 else {  out << itUnique->second << '\t';        }
1003                                         }else { out << "Null" << '\t'; }
1004                                         
1005                                         out << temp1 << '\t' << temp2 << '\t' << temp3 << '\t' << temp4 << '\t' << temp5 << '\t' << temp6 << '\t' << temp7 << '\t' << temp8 << '\t' << flag << endl;    
1006                                 }
1007                         }else { index = m->getline(in); m->gobble(in); }
1008                 }
1009                 in.close();
1010                 out.close();
1011                 
1012                 m->mothurRemove(outputFileName);
1013                 rename((outputFileName+".temp").c_str(), outputFileName.c_str());
1014                 
1015                 return total;
1016         }
1017         catch(exception& e) {
1018                 m->errorOut(e, "ChimeraPerseusCommand", "deconvoluteResults");
1019                 exit(1);
1020         }
1021 }       
1022 //**********************************************************************************************************************
1023
1024