1 //
2 //  currentfile.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 11/9/17.
6 //  Copyright © 2017 Schloss Lab. All rights reserved.
7 //
8 
9 #include "currentfile.h"
10 
11 /*********************************************************************************************/
getCurrentTypes()12 set<string> CurrentFile::getCurrentTypes()  {
13     try {
14 
15         set<string> types;
16         types.insert("fasta");
17         types.insert("summary");
18         types.insert("file");
19         types.insert("accnos");
20         types.insert("column");
21         types.insert("design");
22         types.insert("group");
23         types.insert("list");
24         types.insert("name");
25         types.insert("oligos");
26         types.insert("order");
27         types.insert("ordergroup");
28         types.insert("phylip");
29         types.insert("qfile");
30         types.insert("relabund");
31         types.insert("clr");
32         types.insert("sabund");
33         types.insert("rabund");
34         types.insert("sff");
35         types.insert("shared");
36         types.insert("taxonomy");
37         types.insert("constaxonomy");
38         types.insert("contigsreport");
39         types.insert("tree");
40         types.insert("flow");
41         types.insert("biom");
42         types.insert("count");
43         types.insert("processors");
44         types.insert("sample");
45 
46         return types;
47     }
48     catch(exception& e) {
49         m->errorOut(e, "CurrentFile", "getCurrentTypes");
50         exit(1);
51     }
52 }
53 /*********************************************************************************************/
printCurrentFiles(string filename)54 void CurrentFile::printCurrentFiles(string filename)  {
55     try {
56         lock_guard<std::mutex> guard(currentProtector);
57 
58         if (filename != "") {
59             ofstream out;
60             util.openOutputFile(filename, out);
61 
62             if (accnosfile != "")		{  m->mothurOut("accnos=" + accnosfile, out); m->mothurOutEndLine(out);           }
63             if (columnfile != "")		{  m->mothurOut("column=" + columnfile, out); m->mothurOutEndLine(out);			}
64             if (designfile != "")		{  m->mothurOut("design=" + designfile, out); m->mothurOutEndLine(out);			}
65             if (fastafile != "")		{  m->mothurOut("fasta=" + fastafile, out); m->mothurOutEndLine(out);				}
66             if (groupfile != "")		{  m->mothurOut("group=" + groupfile, out); m->mothurOutEndLine(out);				}
67             if (listfile != "")			{  m->mothurOut("list=" + listfile, out); m->mothurOutEndLine(out);				}
68             if (namefile != "")			{  m->mothurOut("name=" + namefile, out); m->mothurOutEndLine(out);				}
69             if (oligosfile != "")		{  m->mothurOut("oligos=" + oligosfile, out); m->mothurOutEndLine(out);			}
70             if (orderfile != "")		{  m->mothurOut("order=" + orderfile, out); m->mothurOutEndLine(out);				}
71             if (ordergroupfile != "")	{  m->mothurOut("ordergroup=" + ordergroupfile, out); m->mothurOutEndLine(out);	}
72             if (phylipfile != "")		{  m->mothurOut("phylip=" + phylipfile, out); m->mothurOutEndLine(out);			}
73             if (qualfile != "")			{  m->mothurOut("qfile=" + qualfile, out); m->mothurOutEndLine(out);				}
74             if (rabundfile != "")		{  m->mothurOut("rabund=" + rabundfile, out); m->mothurOutEndLine(out);			}
75             if (relabundfile != "")		{  m->mothurOut("relabund=" + relabundfile, out); m->mothurOutEndLine(out);		}
76             if (clrfile != "")          {  m->mothurOut("clr=" + clrfile, out); m->mothurOutEndLine(out);               }
77             if (sabundfile != "")		{  m->mothurOut("sabund=" + sabundfile, out); m->mothurOutEndLine(out);			}
78             if (sfffile != "")			{  m->mothurOut("sff=" + sfffile, out); m->mothurOutEndLine(out);					}
79             if (sharedfile != "")		{  m->mothurOut("shared=" + sharedfile, out); m->mothurOutEndLine(out);			}
80             if (taxonomyfile != "")		{  m->mothurOut("taxonomy=" + taxonomyfile, out); m->mothurOutEndLine(out);		}
81             if (constaxonomyfile != "")	{  m->mothurOut("constaxonomy=" + constaxonomyfile, out); m->mothurOutEndLine(out);}
82             if (contigsreportfile != ""){  m->mothurOut("contigsreport=" + contigsreportfile, out); m->mothurOutEndLine(out);}
83             if (treefile != "")			{  m->mothurOut("tree=" + treefile, out); m->mothurOutEndLine(out);				}
84             if (flowfile != "")			{  m->mothurOut("flow=" + flowfile, out); m->mothurOutEndLine(out);				}
85             if (biomfile != "")			{  m->mothurOut("biom=" + biomfile, out); m->mothurOutEndLine(out);				}
86             if (countfile != "")        {  m->mothurOut("count=" + countfile, out); m->mothurOutEndLine(out);        }
87             if (processors != "1")		{  m->mothurOut("processors=" + processors, out); m->mothurOutEndLine(out);		}
88             if (summaryfile != "")		{  m->mothurOut("summary=" + summaryfile, out); m->mothurOutEndLine(out);         }
89             if (filefile != "")         {  m->mothurOut("file=" + filefile, out); m->mothurOutEndLine(out);               }
90             if (samplefile != "")       {  m->mothurOut("sample=" + samplefile, out); m->mothurOutEndLine(out);               }
91 
92             out.close();
93 
94         }else {
95             if (accnosfile != "")		{  m->mothurOut("accnos=" + accnosfile); m->mothurOutEndLine();			}
96             if (columnfile != "")		{  m->mothurOut("column=" + columnfile); m->mothurOutEndLine();			}
97             if (designfile != "")		{  m->mothurOut("design=" + designfile); m->mothurOutEndLine();			}
98             if (fastafile != "")		{  m->mothurOut("fasta=" + fastafile); m->mothurOutEndLine();				}
99             if (groupfile != "")		{  m->mothurOut("group=" + groupfile); m->mothurOutEndLine();				}
100             if (listfile != "")			{  m->mothurOut("list=" + listfile); m->mothurOutEndLine();				}
101             if (namefile != "")			{  m->mothurOut("name=" + namefile); m->mothurOutEndLine();				}
102             if (oligosfile != "")		{  m->mothurOut("oligos=" + oligosfile); m->mothurOutEndLine();			}
103             if (orderfile != "")		{  m->mothurOut("order=" + orderfile); m->mothurOutEndLine();				}
104             if (ordergroupfile != "")	{  m->mothurOut("ordergroup=" + ordergroupfile); m->mothurOutEndLine();	}
105             if (phylipfile != "")		{  m->mothurOut("phylip=" + phylipfile); m->mothurOutEndLine();			}
106             if (qualfile != "")			{  m->mothurOut("qfile=" + qualfile); m->mothurOutEndLine();				}
107             if (rabundfile != "")		{  m->mothurOut("rabund=" + rabundfile); m->mothurOutEndLine();			}
108             if (relabundfile != "")		{  m->mothurOut("relabund=" + relabundfile); m->mothurOutEndLine();		}
109             if (clrfile != "")          {  m->mothurOut("clr=" + clrfile); m->mothurOutEndLine();               }
110             if (sabundfile != "")		{  m->mothurOut("sabund=" + sabundfile); m->mothurOutEndLine();			}
111             if (sfffile != "")			{  m->mothurOut("sff=" + sfffile); m->mothurOutEndLine();					}
112             if (sharedfile != "")		{  m->mothurOut("shared=" + sharedfile); m->mothurOutEndLine();			}
113             if (taxonomyfile != "")		{  m->mothurOut("taxonomy=" + taxonomyfile); m->mothurOutEndLine();		}
114             if (constaxonomyfile != "")	{  m->mothurOut("constaxonomy=" + constaxonomyfile); m->mothurOutEndLine();}
115             if (contigsreportfile != ""){  m->mothurOut("contigsreport=" + contigsreportfile); m->mothurOutEndLine();}
116             if (treefile != "")			{  m->mothurOut("tree=" + treefile); m->mothurOutEndLine();				}
117             if (flowfile != "")			{  m->mothurOut("flow=" + flowfile); m->mothurOutEndLine();				}
118             if (biomfile != "")			{  m->mothurOut("biom=" + biomfile); m->mothurOutEndLine();				}
119             if (countfile != "")        {  m->mothurOut("count=" + countfile); m->mothurOutEndLine();        }
120             if (processors != "1")		{  m->mothurOut("processors=" + processors); m->mothurOutEndLine();		}
121             if (summaryfile != "")		{  m->mothurOut("summary=" + summaryfile); m->mothurOutEndLine();         }
122             if (filefile != "")         {  m->mothurOut("file=" + filefile); m->mothurOutEndLine();               }
123             if (samplefile != "")       {  m->mothurOut("sample=" + samplefile); m->mothurOutEndLine();               }
124         }
125 
126     }
127     catch(exception& e) {
128         m->errorOut(e, "CurrentFile", "printCurrentFiles");
129         exit(1);
130     }
131 }
132 /*********************************************************************************************/
hasCurrentFiles()133 bool CurrentFile::hasCurrentFiles()  {
134     try {
135         lock_guard<std::mutex> guard(currentProtector);
136 
137         bool hasCurrent = false;
138 
139         if (accnosfile != "")		{  return true;			}
140         if (columnfile != "")		{  return true;			}
141         if (designfile != "")		{  return true;			}
142         if (fastafile != "")		{  return true;			}
143         if (groupfile != "")		{  return true;			}
144         if (listfile != "")			{  return true;			}
145         if (namefile != "")			{  return true;			}
146         if (oligosfile != "")		{  return true;			}
147         if (orderfile != "")		{  return true;			}
148         if (ordergroupfile != "")	{  return true;			}
149         if (phylipfile != "")		{  return true;			}
150         if (qualfile != "")			{  return true;			}
151         if (rabundfile != "")		{  return true;			}
152         if (relabundfile != "")		{  return true;			}
153         if (clrfile != "")          {  return true;         }
154         if (sabundfile != "")		{  return true;			}
155         if (sfffile != "")			{  return true;			}
156         if (sharedfile != "")		{  return true;			}
157         if (taxonomyfile != "")		{  return true;			}
158         if (constaxonomyfile != "")	{  return true;			}
159         if (contigsreportfile != ""){  return true;			}
160         if (treefile != "")			{  return true;			}
161         if (flowfile != "")			{  return true;			}
162         if (biomfile != "")			{  return true;			}
163         if (countfile != "")        {  return true;			}
164         if (summaryfile != "")      {  return true;			}
165         if (filefile != "")         {  return true;			}
166         if (samplefile != "")       {  return true;         }
167         if (processors != "1")		{  return true;			}
168 
169         return hasCurrent;
170 
171     }
172     catch(exception& e) {
173         m->errorOut(e, "CurrentFile", "hasCurrentFiles");
174         exit(1);
175     }
176 }
177 
178 /*********************************************************************************************/
clearCurrentFiles()179 void CurrentFile::clearCurrentFiles()  {
180     try {
181         lock_guard<std::mutex> guard(currentProtector);
182 
183         phylipfile = "";
184         filefile = "";
185         columnfile = "";
186         listfile = "";
187         rabundfile = "";
188         sabundfile = "";
189         namefile = "";
190         groupfile = "";
191         designfile = "";
192         orderfile = "";
193         treefile = "";
194         sharedfile = "";
195         ordergroupfile = "";
196         contigsreportfile = "";
197         constaxonomyfile = "";
198         relabundfile = "";
199         clrfile = "";
200         fastafile = "";
201         qualfile = "";
202         sfffile = "";
203         oligosfile = "";
204         accnosfile = "";
205         taxonomyfile = "";
206         flowfile = "";
207         biomfile = "";
208         countfile = "";
209         summaryfile = "";
210         samplefile = "";
211         unsigned concurentThreadsSupported = std::thread::hardware_concurrency();
212         if (concurentThreadsSupported < 1) { concurentThreadsSupported = 1; } //in case thread errors
213         processors = toString(concurentThreadsSupported);
214     }
215     catch(exception& e) {
216         m->errorOut(e, "CurrentFile", "clearCurrentFiles");
217         exit(1);
218     }
219 }
220 /*********************************************************************************************/
setProcessors(string p)221 int CurrentFile::setProcessors(string p)  {
222     try {
223         lock_guard<std::mutex> guard(currentProtector);
224 
225         if (!util.isInteger(p)) {
226             unsigned concurentThreadsSupported = std::thread::hardware_concurrency();
227             if (concurentThreadsSupported < 1) { concurentThreadsSupported = 1; } //in case thread errors
228             processors = toString(concurentThreadsSupported);
229             m->mothurOut("[ERROR]: " + p + " is not an integer. Setting processors to " + toString(processors) + "\n");
230         }else { processors = p;  m->mothurOut("\nUsing " + toString(processors) + " processors.\n"); }
231         int numProcessors = 1;
232         util.mothurConvert(p, numProcessors);
233         return numProcessors;
234     }
235     catch(exception& e) {
236         m->errorOut(e, "CurrentFile", "clearCurrentFiles");
237         exit(1);
238     }
239 }
240 /*********************************************************************************************/
setDefaultPath(string pathname)241 void CurrentFile::setDefaultPath(string pathname)  {
242     try {
243         lock_guard<std::mutex> guard(currentProtector);
244 
245         if (pathname != "") { //add / to name if needed
246             string lastChar = pathname.substr(pathname.length()-1);
247             if (lastChar != PATH_SEPARATOR) { pathname += PATH_SEPARATOR; }
248         }
249         defaultPath = util.getFullPathName(pathname);
250     }
251     catch(exception& e) {
252         m->errorOut(e, "CurrentFile", "setDefaultPath");
253         exit(1);
254     }
255 }
256 /*********************************************************************************************/
setTestFilePath(string pathname)257 void CurrentFile::setTestFilePath(string pathname)  {
258     try {
259         lock_guard<std::mutex> guard(currentProtector);
260 
261         if (pathname != "") {
262             //add / to name if needed
263             string lastChar = pathname.substr(pathname.length()-1);
264             if (lastChar != PATH_SEPARATOR) { pathname += PATH_SEPARATOR; }
265         }
266 
267         testFilePath = util.getFullPathName(pathname);
268 
269     }
270     catch(exception& e) {
271         m->errorOut(e, "CurrentFile", "setTestFilePath");
272         exit(1);
273     }
274 }
275 /*********************************************************************************************/
setBlastPath(string pathname)276 void CurrentFile::setBlastPath(string pathname)  {
277     try {
278         lock_guard<std::mutex> guard(currentProtector);
279 
280         if (pathname != "") {
281             //add / to name if needed
282             string lastChar = pathname.substr(pathname.length()-1);
283             if (lastChar != PATH_SEPARATOR) { pathname += PATH_SEPARATOR; }
284         }
285         blastPath = util.getFullPathName(pathname);
286 
287     }
288     catch(exception& e) {
289         m->errorOut(e, "CurrentFile", "setBlastPath");
290         exit(1);
291     }
292 }
293 /*********************************************************************************************/
setHomePath(string pathname)294 void CurrentFile::setHomePath(string pathname)  {
295     try {
296         lock_guard<std::mutex> guard(currentProtector);
297 
298         if (pathname != "") {
299             //add / to name if needed
300             string lastChar = pathname.substr(pathname.length()-1);
301             if (lastChar != PATH_SEPARATOR) { pathname += PATH_SEPARATOR; }
302         }
303         homePath = util.getFullPathName(pathname);
304         m->setHomePath(homePath);
305 
306     }
307     catch(exception& e) {
308         m->errorOut(e, "CurrentFile", "setHomePath");
309         exit(1);
310     }
311 }
312 /*********************************************************************************************/
setPaths(vector<string> pathVariables)313 void CurrentFile::setPaths(vector<string> pathVariables)  {
314     try {
315         lock_guard<std::mutex> guard(currentProtector);
316 
317         for (int i = 0; i < pathVariables.size(); i++) {
318             string pathname = pathVariables[i];
319             if (pathname != "") {
320                 //add / to name if needed
321                 string lastChar = pathname.substr(pathname.length()-1);
322                 if (lastChar != PATH_SEPARATOR) { pathname += PATH_SEPARATOR; }
323             }
324             pathVariables[i] = util.getFullPathName(pathname);
325         }
326 
327         paths = pathVariables;
328         m->setPaths(paths);
329     }
330     catch(exception& e) {
331         m->errorOut(e, "CurrentFile", "setPaths");
332         exit(1);
333     }
334 }
335 /*********************************************************************************************/
setToolsPath(string pathname)336 void CurrentFile::setToolsPath(string pathname)  {
337     try {
338         lock_guard<std::mutex> guard(currentProtector);
339 
340         if (pathname != "") {
341             //add / to name if needed
342             string lastChar = pathname.substr(pathname.length()-1);
343             if (lastChar != PATH_SEPARATOR) { pathname += PATH_SEPARATOR; }
344         }
345         toolsPath = util.getFullPathName(pathname);
346 
347     }
348     catch(exception& e) {
349         m->errorOut(e, "CurrentFile", "setToolsPath");
350         exit(1);
351     }
352 }
353 
354 
355 /*********************************************************************************************/
356