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