]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
added citation function to commands
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
68                 
69                 else {
70                         vector<string> myArray = setParameters();
71                         
72                         OptionParser parser(option);
73                         map<string,string> parameters = parser.getParameters();
74                         
75                         ValidParameters validParameter;
76                         map<string, string>::iterator it;
77                 
78                         //check to make sure all parameters are valid for command
79                         for (it = parameters.begin(); it != parameters.end(); it++) { 
80                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
81                         }
82                         
83                         //initialize outputTypes
84                         vector<string> tempOutNames;
85                         outputTypes["fasta"] = tempOutNames;
86                         outputTypes["name"] = tempOutNames;
87                 
88                         //if the user changes the input directory command factory will send this info to us in the output parameter 
89                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
90                         if (inputDir == "not found"){   inputDir = "";          }
91                         else {
92                                 string path;
93                                 it = parameters.find("fasta");
94                                 //user has given a template file
95                                 if(it != parameters.end()){ 
96                                         path = m->hasPath(it->second);
97                                         //if the user has not given a path then, add inputdir. else leave path alone.
98                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
99                                 }
100                                 
101                                 it = parameters.find("name");
102                                 //user has given a template file
103                                 if(it != parameters.end()){ 
104                                         path = m->hasPath(it->second);
105                                         //if the user has not given a path then, add inputdir. else leave path alone.
106                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
107                                 }
108                         }
109
110                         
111                         //check for required parameters
112                         inFastaName = validParameter.validFile(parameters, "fasta", true);
113                         if (inFastaName == "not open") { abort = true; }
114                         else if (inFastaName == "not found") {                          
115                                 inFastaName = m->getFastaFile(); 
116                                 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
117                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
118                         }       
119                         
120                         //if the user changes the output directory command factory will send this info to us in the output parameter 
121                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
122                                 outputDir = ""; 
123                                 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it     
124                         }
125                         
126                         oldNameMapFName = validParameter.validFile(parameters, "name", true);
127                         if (oldNameMapFName == "not open") { abort = true; }
128                         else if (oldNameMapFName == "not found"){       oldNameMapFName = "";   }
129                 }
130
131         }
132         catch(exception& e) {
133                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
134                 exit(1);
135         }
136 }
137 /**************************************************************************************/
138 int DeconvoluteCommand::execute() {     
139         try {
140                 
141                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
142
143                 //prepare filenames and open files
144                 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
145                 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
146                 
147                 FastaMap fastamap;
148         
149                 if(oldNameMapFName == "")       {       fastamap.readFastaFile(inFastaName);                                    }
150                 else                                            {       fastamap.readFastaFile(inFastaName, oldNameMapFName);   }
151                 
152                 if (m->control_pressed) { return 0; }
153                 
154                 fastamap.printCondensedFasta(outFastaFile);
155                 fastamap.printNamesFile(outNameFile);
156                 
157                 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
158                 
159                 m->mothurOutEndLine();
160                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
161                 m->mothurOut(outFastaFile); m->mothurOutEndLine();      
162                 m->mothurOut(outNameFile); m->mothurOutEndLine();
163                 outputNames.push_back(outFastaFile);  outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile);  outputTypes["name"].push_back(outNameFile); 
164                 m->mothurOutEndLine();
165
166                 //set fasta file as new current fastafile
167                 string current = "";
168                 itTypes = outputTypes.find("fasta");
169                 if (itTypes != outputTypes.end()) {
170                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
171                 }
172                 
173                 itTypes = outputTypes.find("name");
174                 if (itTypes != outputTypes.end()) {
175                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
176                 }
177                 
178                 return 0;
179         }
180         catch(exception& e) {
181                 m->errorOut(e, "DeconvoluteCommand", "execute");
182                 exit(1);
183         }
184 }
185 /**************************************************************************************/