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