]> git.donarmstrong.com Git - mothur.git/blob - getmimarkspackagecommand.cpp
changed int to pid_t type in fork(). roughed in get.mimarkscommand.
[mothur.git] / getmimarkspackagecommand.cpp
1 //
2 //  getmimarkspackagecommand.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 3/25/14.
6 //  Copyright (c) 2014 Schloss Lab. All rights reserved.
7 //
8
9 #include "getmimarkspackagecommand.h"
10
11 //**********************************************************************************************************************
12 vector<string> GetMIMarksPackageCommand::setParameters(){
13         try {
14         //files that have dependancies
15         CommandParameter pgroup("group", "InputTypes", "", "", "groupOligos", "none", "none","",false,false); parameters.push_back(pgroup);
16         CommandParameter poligos("oligos", "InputTypes", "", "", "groupOligos", "none", "none","",false,false); parameters.push_back(poligos);
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, "GetMIMarksPackageCommand", "setParameters");
26                 exit(1);
27         }
28 }
29 //**********************************************************************************************************************
30 string GetMIMarksPackageCommand::getHelpString(){
31         try {
32                 string helpString = "";
33                 helpString += "The get.mimarkspackage command creates a mimarks package form with your groups. The required fields are flagged with * characters.\n";
34         helpString += "Further documentation on the different packages and required formats can be found here, http://www.mothur.org/wiki/MIMarks_Data_Packages.\n";
35                 helpString += "The get.mimarkspackage command parameters are: oligos, group and package. oligos or group is required.\n";
36                 helpString += "The oligos parameter is used to provide your oligos file so mothur can extract your group names.\n";
37         helpString += "The group parameter is used to provide your group file so mothur can extract your group names.\n";
38         helpString += "The package parameter is used to select the mimarks package you would like to use. Default=???\n";
39                 helpString += "The get.mimarkspackage command should be in the following format: get.mimarkspackage(oligos=yourOligosFile, package=yourPackage)\n";
40                 helpString += "get.mimarkspackage(oligos=GQY1XT001.oligos, package=???)\n";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "GetMIMarksPackageCommand", "getHelpString");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 string GetMIMarksPackageCommand::getOutputPattern(string type) {
50     try {
51         string pattern = "";
52         
53         if (type == "tsv") {  pattern = "[filename],tsv"; }
54         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
55         
56         return pattern;
57     }
58     catch(exception& e) {
59         m->errorOut(e, "GetMIMarksPackageCommand", "getOutputPattern");
60         exit(1);
61     }
62 }
63 //**********************************************************************************************************************
64 GetMIMarksPackageCommand::GetMIMarksPackageCommand(){
65         try {
66                 abort = true; calledHelp = true;
67                 setParameters();
68         vector<string> tempOutNames;
69                 outputTypes["tsv"] = tempOutNames;
70         }
71         catch(exception& e) {
72                 m->errorOut(e, "GetMIMarksPackageCommand", "GetMIMarksPackageCommand");
73                 exit(1);
74         }
75 }
76 //**********************************************************************************************************************
77 GetMIMarksPackageCommand::GetMIMarksPackageCommand(string option)  {
78         try {
79         
80                 abort = false; calledHelp = false;
81                 
82                 //allow user to run help
83                 if(option == "help") { help(); abort = true; calledHelp = true; }
84                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85                 
86                 else {
87                         //valid paramters for this command
88                         vector<string> myArray = setParameters();
89                         
90                         OptionParser parser(option);
91                         map<string,string> parameters = parser.getParameters();
92                         
93                         ValidParameters validParameter;
94                         map<string,string>::iterator it;
95                         //check to make sure all parameters are valid for command
96                         for (it = parameters.begin(); it != parameters.end(); it++) {
97                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
98                         }
99                         
100             vector<string> tempOutNames;
101                         outputTypes["tsv"] = tempOutNames;
102             
103                         //if the user changes the input directory command factory will send this info to us in the output parameter
104                         string inputDir = validParameter.validFile(parameters, "inputdir", false);
105                         if (inputDir == "not found"){   inputDir = "";          }
106                         else {
107                 
108                                 string path;
109                                 it = parameters.find("oligos");
110                                 //user has given a template file
111                                 if(it != parameters.end()){
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["oligos"] = inputDir + it->second;           }
115                                 }
116                                 
117                                 it = parameters.find("group");
118                                 //user has given a template file
119                                 if(it != parameters.end()){
120                                         path = m->hasPath(it->second);
121                                         //if the user has not given a path then, add inputdir. else leave path alone.
122                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
123                                 }
124                                 
125             }
126             
127                         groupfile = validParameter.validFile(parameters, "group", true);
128                         if (groupfile == "not open") {  groupfile = "";  abort = true; }
129                         else if (groupfile == "not found") { groupfile = ""; }
130             else {  m->setGroupFile(groupfile); }
131             
132             oligosfile = validParameter.validFile(parameters, "oligos", true);
133                         if (oligosfile == "not found")      {   oligosfile = "";        }
134                         else if(oligosfile == "not open")       {       abort = true;           }
135                         else {  m->setOligosFile(oligosfile); }
136
137             if ((groupfile != "") && (oligosfile != "")) {
138                 m->mothurOut("[ERROR]: You may not use a group file and an oligos file, only one."); m->mothurOutEndLine(); abort = true;
139             }
140
141             if ((groupfile == "") && (oligosfile == "")) {
142                 oligosfile = m->getOligosFile();
143                 if (oligosfile != "") {  m->mothurOut("Using " + oligosfile + " as input file for the oligos parameter."); m->mothurOutEndLine(); }
144                 else {
145                     groupfile = m->getGroupFile();
146                     if (groupfile != "") {  m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
147                     else {
148                         m->mothurOut("[ERROR]: You must provide groupfile or oligos file for the get.mimarkspackage command."); m->mothurOutEndLine(); abort = true;
149                     }
150                 }
151             }
152             
153             package = validParameter.validFile(parameters, "package", false);         if (package == "not found") { package = "package"; }
154             //if (!checkCasesPackage(package)) { abort = true; } //error message in function
155             
156             //turn _ to spaces mothur's work around
157             for (int i = 0; i < package.length(); i++) { if (package[i] == '_') { package[i] = ' '; }  }
158
159
160                 }
161                 
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "GetMIMarksPackageCommand", "GetMIMarksPackageCommand");
165                 exit(1);
166         }
167 }
168 //**********************************************************************************************************************
169
170 int GetMIMarksPackageCommand::execute(){
171         try {
172                 
173                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
174         
175
176         
177         
178                 
179         //output files created by command
180                 m->mothurOutEndLine();
181                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
182                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
183                 m->mothurOutEndLine();
184         return 0;
185                 
186     }
187         catch(exception& e) {
188                 m->errorOut(e, "GetMIMarksPackageCommand", "GetMIMarksPackageCommand");
189                 exit(1);
190         }
191 }
192 //**********************************************************************************************************************
193
194