]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
added set.current and get.current commands and modified existing commands to update...
[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::getValidParameters(){        
14         try {
15                 string Array[] =  {"fasta", "name","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "DeconvoluteCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 DeconvoluteCommand::DeconvoluteCommand(){       
26         try {
27                 abort = true; calledHelp = true; 
28                 vector<string> tempOutNames;
29                 outputTypes["fasta"] = tempOutNames;
30                 outputTypes["name"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> DeconvoluteCommand::getRequiredParameters(){     
39         try {
40                 string Array[] =  {"fasta"};
41                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "DeconvoluteCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> DeconvoluteCommand::getRequiredFiles(){  
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "DeconvoluteCommand", "getRequiredFiles");
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                         //valid paramters for this command
70                         string Array[] =  {"fasta", "name","outputdir","inputdir"};
71                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
72                         
73                         OptionParser parser(option);
74                         map<string,string> parameters = parser.getParameters();
75                         
76                         ValidParameters validParameter;
77                         map<string, string>::iterator it;
78                 
79                         //check to make sure all parameters are valid for command
80                         for (it = parameters.begin(); it != parameters.end(); it++) { 
81                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
82                         }
83                         
84                         //initialize outputTypes
85                         vector<string> tempOutNames;
86                         outputTypes["fasta"] = tempOutNames;
87                         outputTypes["name"] = tempOutNames;
88                 
89                         //if the user changes the input directory command factory will send this info to us in the output parameter 
90                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
91                         if (inputDir == "not found"){   inputDir = "";          }
92                         else {
93                                 string path;
94                                 it = parameters.find("fasta");
95                                 //user has given a template file
96                                 if(it != parameters.end()){ 
97                                         path = m->hasPath(it->second);
98                                         //if the user has not given a path then, add inputdir. else leave path alone.
99                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
100                                 }
101                                 
102                                 it = parameters.find("name");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
108                                 }
109                         }
110
111                         
112                         //check for required parameters
113                         inFastaName = validParameter.validFile(parameters, "fasta", true);
114                         if (inFastaName == "not open") { abort = true; }
115                         else if (inFastaName == "not found") { inFastaName = ""; m->mothurOut("fasta is a required parameter for the unique.seqs command."); m->mothurOutEndLine(); abort = true;  }    
116                         
117                         //if the user changes the output directory command factory will send this info to us in the output parameter 
118                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
119                                 outputDir = ""; 
120                                 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it     
121                         }
122                         
123                         oldNameMapFName = validParameter.validFile(parameters, "name", true);
124                         if (oldNameMapFName == "not open") { abort = true; }
125                         else if (oldNameMapFName == "not found"){       oldNameMapFName = "";   }
126                 }
127
128         }
129         catch(exception& e) {
130                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
131                 exit(1);
132         }
133 }
134 //**********************************************************************************************************************
135
136 void DeconvoluteCommand::help(){
137         try {
138                 m->mothurOut("The unique.seqs command reads a fastafile and creates a namesfile.\n");
139                 m->mothurOut("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");
140                 m->mothurOut("If the sequence is unique the second column will just contain its name. \n");
141                 m->mothurOut("The unique.seqs command parameter is fasta and it is required.\n");
142                 m->mothurOut("The unique.seqs command should be in the following format: \n");
143                 m->mothurOut("unique.seqs(fasta=yourFastaFile) \n");    
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "DeconvoluteCommand", "help");
147                 exit(1);
148         }
149 }
150
151 /**************************************************************************************/
152 int DeconvoluteCommand::execute() {     
153         try {
154                 
155                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
156
157                 //prepare filenames and open files
158                 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
159                 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
160                 
161                 FastaMap fastamap;
162         
163                 if(oldNameMapFName == "")       {       fastamap.readFastaFile(inFastaName);                                    }
164                 else                                            {       fastamap.readFastaFile(inFastaName, oldNameMapFName);   }
165                 
166                 if (m->control_pressed) { return 0; }
167                 
168                 fastamap.printCondensedFasta(outFastaFile);
169                 fastamap.printNamesFile(outNameFile);
170                 
171                 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
172                 
173                 m->mothurOutEndLine();
174                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
175                 m->mothurOut(outFastaFile); m->mothurOutEndLine();      
176                 m->mothurOut(outNameFile); m->mothurOutEndLine();
177                 outputNames.push_back(outFastaFile);  outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile);  outputTypes["name"].push_back(outNameFile); 
178                 m->mothurOutEndLine();
179
180                 //set fasta file as new current fastafile
181                 string current = "";
182                 itTypes = outputTypes.find("fasta");
183                 if (itTypes != outputTypes.end()) {
184                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
185                 }
186                 
187                 itTypes = outputTypes.find("name");
188                 if (itTypes != outputTypes.end()) {
189                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
190                 }
191                 
192                 return 0;
193         }
194         catch(exception& e) {
195                 m->errorOut(e, "DeconvoluteCommand", "execute");
196                 exit(1);
197         }
198 }
199 /**************************************************************************************/