]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
added mothurgetpid function. fixed bug with align.seqs related to g++ 4.8 change...
[mothur.git] / mergefilecommand.cpp
1 /*
2  *  mergefilecommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 6/14/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "mergefilecommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> MergeFileCommand::setParameters(){       
14         try {
15                 CommandParameter pinput("input", "String", "", "", "", "", "","",false,true,true); parameters.push_back(pinput);
16                 CommandParameter poutput("output", "String", "", "", "", "", "","",false,true,true); parameters.push_back(poutput);
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, "MergeFileCommand", "setParameters");
26                 exit(1);
27         }
28 }
29 //**********************************************************************************************************************
30 string MergeFileCommand::getHelpString(){       
31         try {
32                 string helpString = "";
33                 helpString += "The merge.file command takes a list of files separated by dashes and merges them into one file."; 
34                 helpString += "The merge.file command parameters are input and output."; 
35                 helpString += "Example merge.file(input=small.fasta-large.fasta, output=all.fasta).";
36                 helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputFileName).\n";
37                 return helpString;
38         }
39         catch(exception& e) {
40                 m->errorOut(e, "MergeFileCommand", "getHelpString");
41                 exit(1);
42         }
43 }
44 //**********************************************************************************************************************
45 MergeFileCommand::MergeFileCommand(){   
46         try {
47                 abort = true; calledHelp = true; 
48                 setParameters();
49                 vector<string> tempOutNames;
50                 outputTypes["merge"] = tempOutNames;
51         }
52         catch(exception& e) {
53                 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
54                 exit(1);
55         }
56 }
57 //**********************************************************************************************************************
58
59 MergeFileCommand::MergeFileCommand(string option)  {
60         try {
61                 abort = false; calledHelp = false;   
62                 
63                 if(option == "help") {
64                         help();
65                         abort = true; calledHelp = true;
66                 }else if(option == "citation") { citation(); abort = true; calledHelp = true;}
67                 else {
68                         vector<string> myArray = setParameters();
69                         
70                         OptionParser parser(option);
71                         map<string,string> parameters = parser.getParameters();
72                         
73                         ValidParameters validParameter;
74                         
75                         //check to make sure all parameters are valid for command
76                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
77                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
78                         }
79                         
80                         //initialize outputTypes
81                         vector<string> tempOutNames;
82                         outputTypes["merge"] = tempOutNames;
83                         
84                         //if the user changes the input directory command factory will send this info to us in the output parameter 
85                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
86                         if (inputDir == "not found"){   inputDir = "";          }
87                         
88                         string fileList = validParameter.validFile(parameters, "input", false);                 
89                         if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine();  abort=true;  }
90                         else{   m->splitAtDash(fileList, fileNames);    }
91                         
92                         //if the user changes the output directory command factory will send this info to us in the output parameter 
93                         string outputDir = validParameter.validFile(parameters, "outputdir", false);            if (outputDir == "not found")   {       outputDir = "";         }
94                         
95                         
96                         numInputFiles = fileNames.size();
97                         ifstream testFile;
98                         if(numInputFiles == 0){
99                                 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) +  " file names"); m->mothurOutEndLine();
100                                 abort=true;  
101                         }
102                         else{
103                                 for(int i=0;i<numInputFiles;i++){
104                                         if (inputDir != "") {
105                                                 string path = m->hasPath(fileNames[i]);
106                                                 //if the user has not given a path then, add inputdir. else leave path alone.
107                                                 if (path == "") {       fileNames[i] = inputDir + fileNames[i];         }
108                                         }
109                                         
110                                         if(m->openInputFile(fileNames[i], testFile)){   abort = true;   }
111                                         testFile.close();
112                                 }
113                         }   
114                         
115                         outputFileName = validParameter.validFile(parameters, "output", false);                 
116                         if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine();  abort=true;  }
117                         else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName);  }
118                 }
119                         
120         }
121         catch(exception& e) {
122                 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
123                 exit(1);
124         }
125 }
126 //**********************************************************************************************************************
127
128 int MergeFileCommand::execute(){
129         try {
130                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
131                 
132                 m->mothurRemove(outputFileName);
133                 for(int i=0;i<numInputFiles;i++){  m->appendFiles(fileNames[i], outputFileName);  }
134                 
135                 if (m->control_pressed) {  m->mothurRemove(outputFileName); return 0;  }
136                 
137                 m->mothurOutEndLine();
138                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
139                 m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
140                 m->mothurOutEndLine();
141
142                 return 0;
143         }
144         catch(exception& e) {
145                 m->errorOut(e, "MergeFileCommand", "execute");
146                 exit(1);
147         }
148 }
149 //**********************************************************************************************************************