]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
04c92fae9a0d2189cb8f1ed69c9c53572fcfee2e
[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); parameters.push_back(pinput);
16                 CommandParameter poutput("output", "String", "", "", "", "", "",false,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 //**********************************************************************************************************************
46 MergeFileCommand::MergeFileCommand(){   
47         try {
48                 abort = true; calledHelp = true; 
49                 setParameters();
50                 vector<string> tempOutNames;
51                 outputTypes["merge"] = tempOutNames;
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
55                 exit(1);
56         }
57 }
58 //**********************************************************************************************************************
59
60 MergeFileCommand::MergeFileCommand(string option)  {
61         try {
62                 abort = false; calledHelp = false;   
63                 
64                 if(option == "help") {
65                         help();
66                         abort = true; calledHelp = true;
67                 }else if(option == "citation") { citation(); abort = true; calledHelp = true;}
68                 else {
69                         vector<string> myArray = setParameters();
70                         
71                         OptionParser parser(option);
72                         map<string,string> parameters = parser.getParameters();
73                         
74                         ValidParameters validParameter;
75                         
76                         //check to make sure all parameters are valid for command
77                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
78                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
79                         }
80                         
81                         //initialize outputTypes
82                         vector<string> tempOutNames;
83                         outputTypes["merge"] = tempOutNames;
84                         
85                         //if the user changes the input directory command factory will send this info to us in the output parameter 
86                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
87                         if (inputDir == "not found"){   inputDir = "";          }
88                         
89                         string fileList = validParameter.validFile(parameters, "input", false);                 
90                         if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine();  abort=true;  }
91                         else{   m->splitAtDash(fileList, fileNames);    }
92                         
93                         //if the user changes the output directory command factory will send this info to us in the output parameter 
94                         string outputDir = validParameter.validFile(parameters, "outputdir", false);            if (outputDir == "not found")   {       outputDir = "";         }
95                         
96                         
97                         numInputFiles = fileNames.size();
98                         ifstream testFile;
99                         if(numInputFiles == 0){
100                                 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) +  " file names"); m->mothurOutEndLine();
101                                 abort=true;  
102                         }
103                         else{
104                                 for(int i=0;i<numInputFiles;i++){
105                                         if (inputDir != "") {
106                                                 string path = m->hasPath(fileNames[i]);
107                                                 //if the user has not given a path then, add inputdir. else leave path alone.
108                                                 if (path == "") {       fileNames[i] = inputDir + fileNames[i];         }
109                                         }
110                                         
111                                         if(m->openInputFile(fileNames[i], testFile)){   abort = true;   }
112                                         testFile.close();
113                                 }
114                         }   
115                         
116                         outputFileName = validParameter.validFile(parameters, "output", false);                 
117                         if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine();  abort=true;  }
118                         else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName);  }
119                 }
120                         
121         }
122         catch(exception& e) {
123                 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
124                 exit(1);
125         }
126 }
127 //**********************************************************************************************************************
128
129 int MergeFileCommand::execute(){
130         try {
131                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
132                 
133                 ofstream outputFile;
134                 m->openOutputFile(outputFileName, outputFile);
135                 
136                 char c;
137                 for(int i=0;i<numInputFiles;i++){
138                         ifstream inputFile; //declaration must be inside for loop of windows throws an error
139                         
140                         m->openInputFile(fileNames[i], inputFile);
141                         
142                         while(!inputFile.eof()){        
143                                 if (m->control_pressed) { outputTypes.clear(); inputFile.close(); outputFile.close(); m->mothurRemove(outputFileName); return 0;  }
144                         
145                                 c = inputFile.get(); 
146                                 //-1 is eof char
147                                 if (int(c) != -1) { outputFile << c; }   
148                         }
149                         
150                         inputFile.close();
151                 }
152                 
153                 outputFile.close();
154                 
155                 if (m->control_pressed) { outputTypes.clear();  m->mothurRemove(outputFileName); return 0;  }
156                 
157                 m->mothurOutEndLine();
158                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
159                 m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
160                 m->mothurOutEndLine();
161
162                 return 0;
163         }
164         catch(exception& e) {
165                 m->errorOut(e, "MergeFileCommand", "execute");
166                 exit(1);
167         }
168 }
169 //**********************************************************************************************************************