]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[mothur.git] / deconvolutecommand.cpp
1 /*
2  *  deconvolute.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/21/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "deconvolutecommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> DeconvoluteCommand::setParameters(){     
14         try {
15                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
16                 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
17                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
18                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
19                 
20                 vector<string> myArray;
21                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
22                 return myArray;
23         }
24         catch(exception& e) {
25                 m->errorOut(e, "DeconvoluteCommand", "setParameters");
26                 exit(1);
27         }
28 }
29 //**********************************************************************************************************************
30 string DeconvoluteCommand::getHelpString(){     
31         try {
32                 string helpString = "";
33                 helpString += "The unique.seqs command reads a fastafile and creates a namesfile.\n";
34                 helpString += "It creates a file where the first column is the groupname and the second column is a list of sequence names who have the same sequence. \n";
35                 helpString += "If the sequence is unique the second column will just contain its name. \n";
36                 helpString += "The unique.seqs command parameters are fasta and name.  fasta is required, unless there is a valid current fasta file.\n";
37                 helpString += "The unique.seqs command should be in the following format: \n";
38                 helpString += "unique.seqs(fasta=yourFastaFile) \n";    
39                 return helpString;
40         }
41         catch(exception& e) {
42                 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
43                 exit(1);
44         }
45 }
46 //**********************************************************************************************************************
47 DeconvoluteCommand::DeconvoluteCommand(){       
48         try {
49                 abort = true; calledHelp = true; 
50                 setParameters();
51                 vector<string> tempOutNames;
52                 outputTypes["fasta"] = tempOutNames;
53                 outputTypes["name"] = tempOutNames;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
57                 exit(1);
58         }
59 }
60 /**************************************************************************************/
61 DeconvoluteCommand::DeconvoluteCommand(string option)  {        
62         try {
63                 abort = false; calledHelp = false;   
64                 
65                 //allow user to run help
66                 if(option == "help") { help(); abort = true; calledHelp = true; }
67                 
68                 else {
69                         vector<string> myArray = setParameters();
70                         
71                         OptionParser parser(option);
72                         map<string,string> parameters = parser.getParameters();
73                         
74                         ValidParameters validParameter;
75                         map<string, string>::iterator it;
76                 
77                         //check to make sure all parameters are valid for command
78                         for (it = parameters.begin(); it != parameters.end(); it++) { 
79                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
80                         }
81                         
82                         //initialize outputTypes
83                         vector<string> tempOutNames;
84                         outputTypes["fasta"] = tempOutNames;
85                         outputTypes["name"] = tempOutNames;
86                 
87                         //if the user changes the input directory command factory will send this info to us in the output parameter 
88                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
89                         if (inputDir == "not found"){   inputDir = "";          }
90                         else {
91                                 string path;
92                                 it = parameters.find("fasta");
93                                 //user has given a template file
94                                 if(it != parameters.end()){ 
95                                         path = m->hasPath(it->second);
96                                         //if the user has not given a path then, add inputdir. else leave path alone.
97                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
98                                 }
99                                 
100                                 it = parameters.find("name");
101                                 //user has given a template file
102                                 if(it != parameters.end()){ 
103                                         path = m->hasPath(it->second);
104                                         //if the user has not given a path then, add inputdir. else leave path alone.
105                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
106                                 }
107                         }
108
109                         
110                         //check for required parameters
111                         inFastaName = validParameter.validFile(parameters, "fasta", true);
112                         if (inFastaName == "not open") { abort = true; }
113                         else if (inFastaName == "not found") {                          
114                                 inFastaName = m->getFastaFile(); 
115                                 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
116                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
117                         }       
118                         
119                         //if the user changes the output directory command factory will send this info to us in the output parameter 
120                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
121                                 outputDir = ""; 
122                                 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it     
123                         }
124                         
125                         oldNameMapFName = validParameter.validFile(parameters, "name", true);
126                         if (oldNameMapFName == "not open") { abort = true; }
127                         else if (oldNameMapFName == "not found"){       oldNameMapFName = "";   }
128                 }
129
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
133                 exit(1);
134         }
135 }
136 /**************************************************************************************/
137 int DeconvoluteCommand::execute() {     
138         try {
139                 
140                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
141
142                 //prepare filenames and open files
143                 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
144                 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
145                 
146                 FastaMap fastamap;
147         
148                 if(oldNameMapFName == "")       {       fastamap.readFastaFile(inFastaName);                                    }
149                 else                                            {       fastamap.readFastaFile(inFastaName, oldNameMapFName);   }
150                 
151                 if (m->control_pressed) { return 0; }
152                 
153                 fastamap.printCondensedFasta(outFastaFile);
154                 fastamap.printNamesFile(outNameFile);
155                 
156                 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
157                 
158                 m->mothurOutEndLine();
159                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
160                 m->mothurOut(outFastaFile); m->mothurOutEndLine();      
161                 m->mothurOut(outNameFile); m->mothurOutEndLine();
162                 outputNames.push_back(outFastaFile);  outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile);  outputTypes["name"].push_back(outNameFile); 
163                 m->mothurOutEndLine();
164
165                 //set fasta file as new current fastafile
166                 string current = "";
167                 itTypes = outputTypes.find("fasta");
168                 if (itTypes != outputTypes.end()) {
169                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
170                 }
171                 
172                 itTypes = outputTypes.find("name");
173                 if (itTypes != outputTypes.end()) {
174                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
175                 }
176                 
177                 return 0;
178         }
179         catch(exception& e) {
180                 m->errorOut(e, "DeconvoluteCommand", "execute");
181                 exit(1);
182         }
183 }
184 /**************************************************************************************/