]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
d431dd32ff17dcef5e267b1e93a47c8fc93b324b
[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::getValidParameters(){  
14         try {
15                 string Array[] =  {"input", "output","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "MergeFileCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 MergeFileCommand::MergeFileCommand(){   
26         try {
27                 //initialize outputTypes
28                 vector<string> tempOutNames;
29                 outputTypes["merge"] = tempOutNames;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 vector<string> MergeFileCommand::getRequiredParameters(){       
38         try {
39                 string Array[] =  {"input","output"};
40                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
41                 return myArray;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "MergeFileCommand", "getRequiredParameters");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 vector<string> MergeFileCommand::getRequiredFiles(){    
50         try {
51                 vector<string> myArray;
52                 return myArray;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "MergeFileCommand", "getRequiredFiles");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60
61 MergeFileCommand::MergeFileCommand(string option)  {
62         try {
63                 abort = false;
64                 
65                 if(option == "help") {
66                         help();
67                         abort = true; 
68                 }
69                 else {
70                         //valid paramters for this command
71                         string Array[] =  {"input", "output","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                         
79                         //check to make sure all parameters are valid for command
80                         for (map<string,string>::iterator 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["merge"] = tempOutNames;
87                         
88                         //if the user changes the input directory command factory will send this info to us in the output parameter 
89                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
90                         if (inputDir == "not found"){   inputDir = "";          }
91                         
92                         string fileList = validParameter.validFile(parameters, "input", false);                 
93                         if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine();  abort=true;  }
94                         else{   m->splitAtDash(fileList, fileNames);    }
95                         
96                         //if the user changes the output directory command factory will send this info to us in the output parameter 
97                         string outputDir = validParameter.validFile(parameters, "outputdir", false);            if (outputDir == "not found")   {       outputDir = "";         }
98                         
99                         
100                         numInputFiles = fileNames.size();
101                         ifstream testFile;
102                         if(numInputFiles == 0){
103                                 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) +  " file names"); m->mothurOutEndLine();
104                                 abort=true;  
105                         }
106                         else{
107                                 for(int i=0;i<numInputFiles;i++){
108                                         if (inputDir != "") {
109                                                 string path = m->hasPath(fileNames[i]);
110                                                 //if the user has not given a path then, add inputdir. else leave path alone.
111                                                 if (path == "") {       fileNames[i] = inputDir + fileNames[i];         }
112                                         }
113                                         
114                                         if(m->openInputFile(fileNames[i], testFile)){   abort = true;   }
115                                         testFile.close();
116                                 }
117                         }   
118                         
119                         outputFileName = validParameter.validFile(parameters, "output", false);                 
120                         if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine();  abort=true;  }
121                         else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName);  }
122                 }
123                         
124         }
125         catch(exception& e) {
126                 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
127                 exit(1);
128         }
129 }
130
131 //**********************************************************************************************************************
132
133 MergeFileCommand::~MergeFileCommand()   {       /*      do nothing      */      }
134
135 //**********************************************************************************************************************
136
137 int MergeFileCommand::execute(){
138         try {
139                 if (abort == true) {    return 0;       }
140                 
141                 ofstream outputFile;
142                 m->openOutputFile(outputFileName, outputFile);
143                 
144                 char c;
145                 for(int i=0;i<numInputFiles;i++){
146                         ifstream inputFile; //declaration must be inside for loop of windows throws an error
147                         
148                         m->openInputFile(fileNames[i], inputFile);
149                         
150                         while(!inputFile.eof()){        
151                                 if (m->control_pressed) { outputTypes.clear(); inputFile.close(); outputFile.close(); remove(outputFileName.c_str()); return 0;  }
152                         
153                                 c = inputFile.get(); 
154                                 //-1 is eof char
155                                 if (int(c) != -1) { outputFile << c; }   
156                         }
157                         
158                         inputFile.close();
159                 }
160                 
161                 outputFile.close();
162                 
163                 if (m->control_pressed) { outputTypes.clear();  remove(outputFileName.c_str()); return 0;  }
164                 
165                 m->mothurOutEndLine();
166                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
167                 m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
168                 m->mothurOutEndLine();
169
170                 return 0;
171         }
172         catch(exception& e) {
173                 m->errorOut(e, "MergeFileCommand", "execute");
174                 exit(1);
175         }
176 }
177
178 //**********************************************************************************************************************
179
180 void MergeFileCommand::help(){
181         try {
182                 m->mothurOut("The merge.file command..."); m->mothurOutEndLine();
183         }
184         catch(exception& e) {
185                 m->errorOut(e, "MergeFileCommand", "help");
186                 exit(1);
187         }
188 }
189
190 //**********************************************************************************************************************