]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
moved utilities out of mothur.h and into mothurOut class.
[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 DeconvoluteCommand::DeconvoluteCommand(string option)  {        
14         try {
15                 abort = false;
16                 
17                 //allow user to run help
18                 if(option == "help") { help(); abort = true; }
19                 
20                 else {
21                         //valid paramters for this command
22                         string Array[] =  {"fasta", "name","outputdir","inputdir"};
23                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
24                         
25                         OptionParser parser(option);
26                         map<string,string> parameters = parser.getParameters();
27                         
28                         ValidParameters validParameter;
29                         map<string, string>::iterator it;
30                 
31                         //check to make sure all parameters are valid for command
32                         for (it = parameters.begin(); it != parameters.end(); it++) { 
33                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
34                         }
35                         
36                         //if the user changes the input directory command factory will send this info to us in the output parameter 
37                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
38                         if (inputDir == "not found"){   inputDir = "";          }
39                         else {
40                                 string path;
41                                 it = parameters.find("fasta");
42                                 //user has given a template file
43                                 if(it != parameters.end()){ 
44                                         path = m->hasPath(it->second);
45                                         //if the user has not given a path then, add inputdir. else leave path alone.
46                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
47                                 }
48                                 
49                                 it = parameters.find("name");
50                                 //user has given a template file
51                                 if(it != parameters.end()){ 
52                                         path = m->hasPath(it->second);
53                                         //if the user has not given a path then, add inputdir. else leave path alone.
54                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
55                                 }
56                         }
57
58                         
59                         //check for required parameters
60                         inFastaName = validParameter.validFile(parameters, "fasta", true);
61                         if (inFastaName == "not open") { abort = true; }
62                         else if (inFastaName == "not found") { inFastaName = ""; m->mothurOut("fasta is a required parameter for the unique.seqs command."); m->mothurOutEndLine(); abort = true;  }    
63                         
64                         //if the user changes the output directory command factory will send this info to us in the output parameter 
65                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
66                                 outputDir = ""; 
67                                 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it     
68                         }
69                         
70                         oldNameMapFName = validParameter.validFile(parameters, "name", true);
71                         if (oldNameMapFName == "not open") { abort = true; }
72                         else if (oldNameMapFName == "not found"){       oldNameMapFName = "";   }
73                 }
74
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
78                 exit(1);
79         }
80 }
81 //**********************************************************************************************************************
82
83 void DeconvoluteCommand::help(){
84         try {
85                 m->mothurOut("The unique.seqs command reads a fastafile and creates a namesfile.\n");
86                 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");
87                 m->mothurOut("If the sequence is unique the second column will just contain its name. \n");
88                 m->mothurOut("The unique.seqs command parameter is fasta and it is required.\n");
89                 m->mothurOut("The unique.seqs command should be in the following format: \n");
90                 m->mothurOut("unique.seqs(fasta=yourFastaFile) \n");    
91         }
92         catch(exception& e) {
93                 m->errorOut(e, "DeconvoluteCommand", "help");
94                 exit(1);
95         }
96 }
97
98 /**************************************************************************************/
99 int DeconvoluteCommand::execute() {     
100         try {
101                 
102                 if (abort == true) { return 0; }
103
104                 //prepare filenames and open files
105                 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
106                 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
107                 
108                 FastaMap fastamap;
109         
110                 if(oldNameMapFName == "")       {       fastamap.readFastaFile(inFastaName);                                    }
111                 else                                            {       fastamap.readFastaFile(inFastaName, oldNameMapFName);   }
112                 
113                 if (m->control_pressed) { return 0; }
114                 
115                 fastamap.printCondensedFasta(outFastaFile);
116                 fastamap.printNamesFile(outNameFile);
117                 
118                 if (m->control_pressed) { remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
119                 
120                 m->mothurOutEndLine();
121                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
122                 m->mothurOut(outFastaFile); m->mothurOutEndLine();      
123                 m->mothurOut(outNameFile); m->mothurOutEndLine();
124                 m->mothurOutEndLine();
125
126
127                 
128                 return 0;
129         }
130         catch(exception& e) {
131                 m->errorOut(e, "DeconvoluteCommand", "execute");
132                 exit(1);
133         }
134 }
135 /**************************************************************************************/