]> git.donarmstrong.com Git - mothur.git/blob - chimerauchimecommand.h
fixed kw parameter types
[mothur.git] / chimerauchimecommand.h
1 #ifndef CHIMERAUCHIMECOMMAND_H
2 #define CHIMERAUCHIMECOMMAND_H
3
4
5 /*
6  *  chimerauchimecommand.h
7  *  Mothur
8  *
9  *  Created by westcott on 5/13/11.
10  *  Copyright 2011 Schloss Lab. All rights reserved.
11  *
12  */
13
14 #include "mothur.h"
15 #include "command.hpp"
16 #include "sequenceparser.h"
17
18 /***********************************************************/
19
20 class ChimeraUchimeCommand : public Command {
21 public:
22         ChimeraUchimeCommand(string);
23         ChimeraUchimeCommand();
24         ~ChimeraUchimeCommand() {}
25         
26         vector<string> setParameters();
27         string getCommandName()                 { return "chimera.uchime";              }
28         string getCommandCategory()             { return "Sequence Processing"; }
29         string getOutputFileNameTag(string, string);
30         string getHelpString(); 
31         string getCitation() { return "uchime by Robert C. Edgar\nhttp://drive5.com/uchime\nThis code is donated to the public domain.\nhttp://www.mothur.org/wiki/Chimera.uchime\nEdgar,R.C., Haas,B.J., Clemente,J.C., Quince,C. and Knight,R. (2011), UCHIME improves sensitivity and speed of chimera detection, Bioinformatics, in press.\n"; }
32         string getDescription()         { return "detect chimeric sequences"; }
33         
34         int execute(); 
35         void help() { m->mothurOut(getHelpString()); }          
36         
37 private:
38         struct linePair {
39                 int start;
40                 int end;
41                 linePair(int i, int j) : start(i), end(j) {}
42         };
43         
44         vector<int> processIDS;   //processid
45         int driver(string, string, string, string, int&);
46         int createProcesses(string, string, string, string, int&);
47                 
48         bool abort, useAbskew, chimealns, useMinH, useMindiv, useXn, useDn, useXa, useChunks, useMinchunk, useIdsmoothwindow, useMinsmoothid, useMaxp, skipgaps, skipgaps2, useMinlen, useMaxlen, ucl, useQueryfract;
49         string fastafile, groupfile, templatefile, outputDir, namefile, abskew, minh, mindiv, xn, dn, xa, chunks, minchunk, idsmoothwindow, minsmoothid, maxp, minlen, maxlen, queryfract, uchimeLocation;
50         int processors;
51         
52         
53         vector<string> outputNames;
54         vector<string> fastaFileNames;
55         vector<string> nameFileNames;
56         vector<string> groupFileNames;
57         
58         string getNamesFile(string&);
59         int readFasta(string, map<string, string>&);
60         int printFile(vector<seqPriorityNode>&, string);
61         int deconvoluteResults(SequenceParser&, string, string, string);
62         int driverGroups(SequenceParser&, string, string, string, string, int, int, vector<string>);
63         int createProcessesGroups(SequenceParser&, string, string, string, string, vector<string>, string, string, string);
64
65
66 };
67
68 /***********************************************************/
69 /**************************************************************************************************/
70 //custom data structure for threads to use.
71 // This is passed by void pointer so it can be any data type
72 // that can be passed using a single void pointer (LPVOID).
73 struct uchimeData {
74         string fastafile; 
75         string namefile; 
76         string groupfile;
77         string outputFName;
78         string accnos, alns, filename, templatefile, uchimeLocation;
79         MothurOut* m;
80         int start;
81         int end;
82         int threadID, count, numChimeras;
83         vector<string> groups;
84         bool useAbskew, chimealns, useMinH, useMindiv, useXn, useDn, useXa, useChunks, useMinchunk, useIdsmoothwindow, useMinsmoothid, useMaxp, skipgaps, skipgaps2, useMinlen, useMaxlen, ucl, useQueryfract;
85         string abskew, minh, mindiv, xn, dn, xa, chunks, minchunk, idsmoothwindow, minsmoothid, maxp, minlen, maxlen, queryfract;
86         
87         uchimeData(){}
88         uchimeData(string o, string uloc, string t, string file, string f, string n, string g, string ac,  string al, vector<string> gr, MothurOut* mout, int st, int en, int tid) {
89                 fastafile = f;
90                 namefile = n;
91                 groupfile = g;
92                 filename = file;
93                 outputFName = o;
94                 templatefile = t;
95                 accnos = ac;
96                 alns = al;
97                 m = mout;
98                 start = st;
99                 end = en;
100                 threadID = tid;
101                 groups = gr;
102                 count = 0;
103                 numChimeras = 0;
104         uchimeLocation = uloc;
105         }
106         void setBooleans(bool Abskew, bool calns, bool MinH, bool Mindiv, bool Xn, bool Dn, bool Xa, bool Chunks, bool Minchunk, bool Idsmoothwindow, bool Minsmoothid, bool Maxp, bool skipgap, bool skipgap2, bool Minlen, bool Maxlen, bool uc, bool Queryfract) {
107                 useAbskew = Abskew;
108                 chimealns = calns;
109                 useMinH = MinH;
110                 useMindiv = Mindiv;
111                 useXn = Xn;
112                 useDn = Dn;
113                 useXa = Xa;
114                 useChunks = Chunks;
115                 useMinchunk = Minchunk;
116                 useIdsmoothwindow = Idsmoothwindow;
117                 useMinsmoothid = Minsmoothid;
118                 useMaxp = Maxp;
119                 skipgaps = skipgap;
120                 skipgaps2 = skipgap2;
121                 useMinlen = Minlen;
122                 useMaxlen = Maxlen;
123                 ucl = uc;
124                 useQueryfract = Queryfract;
125         }
126         
127         void setVariables(string abske, string min, string mindi, string x, string d, string xa2, string chunk, string minchun, string idsmoothwindo, string minsmoothi, string max, string minle, string maxle, string queryfrac) {
128                 abskew = abske;
129                 minh = min;
130                 mindiv = mindi;
131                 xn = x;
132                 dn = d;
133                 xa = xa2;
134                 chunks = chunk;
135                 minchunk = minchun;
136                 idsmoothwindow = idsmoothwindo;
137                 minsmoothid = minsmoothi;
138                 maxp = max;
139                 minlen = minle;
140                 maxlen = maxle;
141                 queryfract = queryfrac;
142         }
143 };
144
145 /**************************************************************************************************/
146 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
147 #else
148 static DWORD WINAPI MyUchimeThreadFunction(LPVOID lpParam){ 
149         uchimeData* pDataArray;
150         pDataArray = (uchimeData*)lpParam;
151         
152         try {
153                 
154                 pDataArray->outputFName = pDataArray->m->getFullPathName(pDataArray->outputFName);
155                 pDataArray->filename = pDataArray->m->getFullPathName(pDataArray->filename);
156                 pDataArray->alns = pDataArray->m->getFullPathName(pDataArray->alns);
157                 
158                 //clears files
159                 ofstream out, out1, out2;
160                 pDataArray->m->openOutputFile(pDataArray->outputFName, out); out.close(); 
161                 pDataArray->m->openOutputFile(pDataArray->accnos, out1); out1.close();
162                 if (pDataArray->chimealns) { pDataArray->m->openOutputFile(pDataArray->alns, out2); out2.close(); }
163                 
164                 //parse fasta and name file by group
165                 SequenceParser* parser;
166                 if (pDataArray->namefile != "") { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile);      }
167                 else                                                    { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile);                                            }
168                 
169                 int totalSeqs = 0;
170                 int numChimeras = 0;
171                 
172                 for (int i = pDataArray->start; i < pDataArray->end; i++) {
173                         int start = time(NULL);  if (pDataArray->m->control_pressed) {  delete parser; return 0; }
174                         
175                         int error = parser->getSeqs(pDataArray->groups[i], pDataArray->filename, true); if ((error == 1) || pDataArray->m->control_pressed) {  delete parser; return 0; }
176                         
177                         //int numSeqs = driver((outputFName + groups[i]), filename, (accnos+ groups[i]), (alns+ groups[i]), numChimeras);
178                         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
179                         
180                         //to allow for spaces in the path
181                         string outputFName = "\"" + pDataArray->outputFName+pDataArray->groups[i] + "\"";
182                         string filename = "\"" + pDataArray->filename + "\"";
183                         string alns = "\"" + pDataArray->alns+pDataArray->groups[i] + "\"";
184                         string accnos = pDataArray->accnos+pDataArray->groups[i];
185                         
186                         vector<char*> cPara;
187                         
188             string uchimeCommand = pDataArray->uchimeLocation;
189             uchimeCommand = "\"" + uchimeCommand + "\"";
190                         
191                         char* tempUchime;
192                         tempUchime= new char[uchimeCommand.length()+1]; 
193                         *tempUchime = '\0';
194                         strncat(tempUchime, uchimeCommand.c_str(), uchimeCommand.length());
195                         cPara.push_back(tempUchime);
196                         
197                         char* tempIn = new char[8]; 
198                         *tempIn = '\0'; strncat(tempIn, "--input", 7);
199                         //strcpy(tempIn, "--input"); 
200                         cPara.push_back(tempIn);
201                         char* temp = new char[filename.length()+1];
202                         *temp = '\0'; strncat(temp, filename.c_str(), filename.length());
203                         //strcpy(temp, filename.c_str());
204                         cPara.push_back(temp);
205                         
206                         char* tempO = new char[12]; 
207                         *tempO = '\0'; strncat(tempO, "--uchimeout", 11);
208                         //strcpy(tempO, "--uchimeout"); 
209                         cPara.push_back(tempO);
210                         char* tempout = new char[outputFName.length()+1];
211                         //strcpy(tempout, outputFName.c_str());
212                         *tempout = '\0'; strncat(tempout, outputFName.c_str(), outputFName.length());
213                         cPara.push_back(tempout);
214                         
215                         if (pDataArray->chimealns) {
216                                 char* tempA = new char[13]; 
217                                 *tempA = '\0'; strncat(tempA, "--uchimealns", 12);
218                                 //strcpy(tempA, "--uchimealns"); 
219                                 cPara.push_back(tempA);
220                                 char* tempa = new char[alns.length()+1];
221                                 //strcpy(tempa, alns.c_str());
222                                 *tempa = '\0'; strncat(tempa, alns.c_str(), alns.length());
223                                 cPara.push_back(tempa);
224                         }
225                         
226                         if (pDataArray->useAbskew) {
227                                 char* tempskew = new char[9];
228                                 *tempskew = '\0'; strncat(tempskew, "--abskew", 8);
229                                 //strcpy(tempskew, "--abskew"); 
230                                 cPara.push_back(tempskew);
231                                 char* tempSkew = new char[pDataArray->abskew.length()+1];
232                                 //strcpy(tempSkew, abskew.c_str());
233                                 *tempSkew = '\0'; strncat(tempSkew, pDataArray->abskew.c_str(), pDataArray->abskew.length());
234                                 cPara.push_back(tempSkew);
235                         }
236                         
237                         if (pDataArray->useMinH) {
238                                 char* tempminh = new char[7]; 
239                                 *tempminh = '\0'; strncat(tempminh, "--minh", 6);
240                                 //strcpy(tempminh, "--minh"); 
241                                 cPara.push_back(tempminh);
242                                 char* tempMinH = new char[pDataArray->minh.length()+1];
243                                 *tempMinH = '\0'; strncat(tempMinH, pDataArray->minh.c_str(), pDataArray->minh.length());
244                                 //strcpy(tempMinH, minh.c_str());
245                                 cPara.push_back(tempMinH);
246                         }
247                         
248                         if (pDataArray->useMindiv) {
249                                 char* tempmindiv = new char[9]; 
250                                 *tempmindiv = '\0'; strncat(tempmindiv, "--mindiv", 8);
251                                 //strcpy(tempmindiv, "--mindiv"); 
252                                 cPara.push_back(tempmindiv);
253                                 char* tempMindiv = new char[pDataArray->mindiv.length()+1];
254                                 *tempMindiv = '\0'; strncat(tempMindiv, pDataArray->mindiv.c_str(), pDataArray->mindiv.length());
255                                 //strcpy(tempMindiv, mindiv.c_str());
256                                 cPara.push_back(tempMindiv);
257                         }
258                         
259                         if (pDataArray->useXn) {
260                                 char* tempxn = new char[5]; 
261                                 //strcpy(tempxn, "--xn"); 
262                                 *tempxn = '\0'; strncat(tempxn, "--xn", 4);
263                                 cPara.push_back(tempxn);
264                                 char* tempXn = new char[pDataArray->xn.length()+1];
265                                 //strcpy(tempXn, xn.c_str());
266                                 *tempXn = '\0'; strncat(tempXn, pDataArray->xn.c_str(), pDataArray->xn.length());
267                                 cPara.push_back(tempXn);
268                         }
269                         
270                         if (pDataArray->useDn) {
271                                 char* tempdn = new char[5]; 
272                                 //strcpy(tempdn, "--dn"); 
273                                 *tempdn = '\0'; strncat(tempdn, "--dn", 4);
274                                 cPara.push_back(tempdn);
275                                 char* tempDn = new char[pDataArray->dn.length()+1];
276                                 *tempDn = '\0'; strncat(tempDn, pDataArray->dn.c_str(), pDataArray->dn.length());
277                                 //strcpy(tempDn, dn.c_str());
278                                 cPara.push_back(tempDn);
279                         }
280                         
281                         if (pDataArray->useXa) {
282                                 char* tempxa = new char[5]; 
283                                 //strcpy(tempxa, "--xa"); 
284                                 *tempxa = '\0'; strncat(tempxa, "--xa", 4);
285                                 cPara.push_back(tempxa);
286                                 char* tempXa = new char[pDataArray->xa.length()+1];
287                                 *tempXa = '\0'; strncat(tempXa, pDataArray->xa.c_str(), pDataArray->xa.length());
288                                 //strcpy(tempXa, xa.c_str());
289                                 cPara.push_back(tempXa);
290                         }
291                         
292                         if (pDataArray->useChunks) {
293                                 char* tempchunks = new char[9]; 
294                                 //strcpy(tempchunks, "--chunks"); 
295                                 *tempchunks = '\0'; strncat(tempchunks, "--chunks", 8);
296                                 cPara.push_back(tempchunks);
297                                 char* tempChunks = new char[pDataArray->chunks.length()+1];
298                                 *tempChunks = '\0'; strncat(tempChunks, pDataArray->chunks.c_str(), pDataArray->chunks.length());
299                                 //strcpy(tempChunks, chunks.c_str());
300                                 cPara.push_back(tempChunks);
301                         }
302                         
303                         if (pDataArray->useMinchunk) {
304                                 char* tempminchunk = new char[11]; 
305                                 //strcpy(tempminchunk, "--minchunk"); 
306                                 *tempminchunk = '\0'; strncat(tempminchunk, "--minchunk", 10);
307                                 cPara.push_back(tempminchunk);
308                                 char* tempMinchunk = new char[pDataArray->minchunk.length()+1];
309                                 *tempMinchunk = '\0'; strncat(tempMinchunk, pDataArray->minchunk.c_str(), pDataArray->minchunk.length());
310                                 //strcpy(tempMinchunk, minchunk.c_str());
311                                 cPara.push_back(tempMinchunk);
312                         }
313                         
314                         if (pDataArray->useIdsmoothwindow) {
315                                 char* tempidsmoothwindow = new char[17]; 
316                                 *tempidsmoothwindow = '\0'; strncat(tempidsmoothwindow, "--idsmoothwindow", 16);
317                                 //strcpy(tempidsmoothwindow, "--idsmoothwindow"); 
318                                 cPara.push_back(tempidsmoothwindow);
319                                 char* tempIdsmoothwindow = new char[pDataArray->idsmoothwindow.length()+1];
320                                 *tempIdsmoothwindow = '\0'; strncat(tempIdsmoothwindow, pDataArray->idsmoothwindow.c_str(), pDataArray->idsmoothwindow.length());
321                                 //strcpy(tempIdsmoothwindow, idsmoothwindow.c_str());
322                                 cPara.push_back(tempIdsmoothwindow);
323                         }
324                         
325                         if (pDataArray->useMaxp) {
326                                 char* tempmaxp = new char[7]; 
327                                 //strcpy(tempmaxp, "--maxp"); 
328                                 *tempmaxp = '\0'; strncat(tempmaxp, "--maxp", 6);
329                                 cPara.push_back(tempmaxp);
330                                 char* tempMaxp = new char[pDataArray->maxp.length()+1];
331                                 *tempMaxp = '\0'; strncat(tempMaxp, pDataArray->maxp.c_str(), pDataArray->maxp.length());
332                                 //strcpy(tempMaxp, maxp.c_str());
333                                 cPara.push_back(tempMaxp);
334                         }
335                         
336                         if (!pDataArray->skipgaps) {
337                                 char* tempskipgaps = new char[13]; 
338                                 //strcpy(tempskipgaps, "--[no]skipgaps");
339                                 *tempskipgaps = '\0'; strncat(tempskipgaps, "--noskipgaps", 12);
340                                 cPara.push_back(tempskipgaps);
341                         }
342                         
343                         if (!pDataArray->skipgaps2) {
344                                 char* tempskipgaps2 = new char[14]; 
345                                 //strcpy(tempskipgaps2, "--[no]skipgaps2"); 
346                                 *tempskipgaps2 = '\0'; strncat(tempskipgaps2, "--noskipgaps2", 13);
347                                 cPara.push_back(tempskipgaps2);
348                         }
349                         
350                         if (pDataArray->useMinlen) {
351                                 char* tempminlen = new char[9]; 
352                                 *tempminlen = '\0'; strncat(tempminlen, "--minlen", 8);
353                                 //strcpy(tempminlen, "--minlen"); 
354                                 cPara.push_back(tempminlen);
355                                 char* tempMinlen = new char[pDataArray->minlen.length()+1];
356                                 //strcpy(tempMinlen, minlen.c_str());
357                                 *tempMinlen = '\0'; strncat(tempMinlen, pDataArray->minlen.c_str(), pDataArray->minlen.length());
358                                 cPara.push_back(tempMinlen);
359                         }
360                         
361                         if (pDataArray->useMaxlen) {
362                                 char* tempmaxlen = new char[9]; 
363                                 //strcpy(tempmaxlen, "--maxlen"); 
364                                 *tempmaxlen = '\0'; strncat(tempmaxlen, "--maxlen", 8);
365                                 cPara.push_back(tempmaxlen);
366                                 char* tempMaxlen = new char[pDataArray->maxlen.length()+1];
367                                 *tempMaxlen = '\0'; strncat(tempMaxlen, pDataArray->maxlen.c_str(), pDataArray->maxlen.length());
368                                 //strcpy(tempMaxlen, maxlen.c_str());
369                                 cPara.push_back(tempMaxlen);
370                         }
371                         
372                         if (pDataArray->ucl) {
373                                 char* tempucl = new char[5]; 
374                                 strcpy(tempucl, "--ucl"); 
375                                 cPara.push_back(tempucl);
376                         }
377                         
378                         if (pDataArray->useQueryfract) {
379                                 char* tempqueryfract = new char[13]; 
380                                 *tempqueryfract = '\0'; strncat(tempqueryfract, "--queryfract", 12);
381                                 //strcpy(tempqueryfract, "--queryfract"); 
382                                 cPara.push_back(tempqueryfract);
383                                 char* tempQueryfract = new char[pDataArray->queryfract.length()+1];
384                                 *tempQueryfract = '\0'; strncat(tempQueryfract, pDataArray->queryfract.c_str(), pDataArray->queryfract.length());
385                                 //strcpy(tempQueryfract, queryfract.c_str());
386                                 cPara.push_back(tempQueryfract);
387                         }
388                         
389                         
390                         char** uchimeParameters;
391                         uchimeParameters = new char*[cPara.size()];
392                         string commandString = "";
393                         for (int j = 0; j < cPara.size(); j++) {  uchimeParameters[j] = cPara[j];  commandString += toString(cPara[j]) + " "; } 
394                         //int numArgs = cPara.size();
395                         
396                         //uchime_main(numArgs, uchimeParameters); 
397                         //cout << "commandString = " << commandString << endl;
398                         commandString = "\"" + commandString + "\"";
399             
400             if (pDataArray->m->debug) { pDataArray->m->mothurOut("[DEBUG]: uchime command = " + commandString + ".\n"); }
401             
402                         system(commandString.c_str());
403                         
404                         //free memory
405                         for(int j = 0; j < cPara.size(); j++)  {  delete cPara[j];  }
406                         delete[] uchimeParameters; 
407                         
408                         //remove "" from filenames
409                         outputFName = outputFName.substr(1, outputFName.length()-2);
410                         filename = filename.substr(1, filename.length()-2);
411                         alns = alns.substr(1, alns.length()-2);
412                         
413                         if (pDataArray->m->control_pressed) { delete parser; return 0; }
414                         
415                         //create accnos file from uchime results
416                         ifstream in; 
417                         pDataArray->m->openInputFile(outputFName, in);
418                         
419                         ofstream out;
420                         pDataArray->m->openOutputFile(accnos, out);
421                         
422                         int num = 0;
423                         numChimeras = 0;
424                         while(!in.eof()) {
425                                 
426                                 if (pDataArray->m->control_pressed) { break; }
427                                 
428                                 string name = "";
429                                 string chimeraFlag = "";
430                                 in >> chimeraFlag >> name;
431                                 
432                                 //fix name 
433                                 name = name.substr(0, name.length()-1); //rip off last /
434                                 name = name.substr(0, name.find_last_of('/'));
435                                 
436                                 for (int j = 0; j < 15; j++) {  in >> chimeraFlag; }
437                                 pDataArray->m->gobble(in);
438                                 
439                                 if (chimeraFlag == "Y") {  out << name << endl; numChimeras++; }
440                                 num++;
441                         }
442                         in.close();
443                         out.close();
444                         
445                         
446                         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
447                         totalSeqs += num;
448                         pDataArray->numChimeras += numChimeras;
449                         
450                         if (pDataArray->m->control_pressed) { delete parser; return 0; }
451                         
452                         //remove file made for uchime
453                         pDataArray->m->mothurRemove(filename);
454                         
455                         //append files
456                         pDataArray->m->appendFiles(outputFName, pDataArray->outputFName); pDataArray->m->mothurRemove(outputFName);
457                         pDataArray->m->appendFiles(accnos, pDataArray->accnos); pDataArray->m->mothurRemove(accnos);
458                         if (pDataArray->chimealns) { pDataArray->m->appendFiles(alns, pDataArray->alns); pDataArray->m->mothurRemove(alns); }
459                         
460                         pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(num) + " sequences from group " + pDataArray->groups[i] + ".");    pDataArray->m->mothurOutEndLine();                                      
461                         
462                 }       
463                 
464                 pDataArray->count = totalSeqs;
465                 delete parser;
466                 return totalSeqs;
467                 
468         }
469         catch(exception& e) {
470                 pDataArray->m->errorOut(e, "ChimeraUchimeCommand", "MyUchimeThreadFunction");
471                 exit(1);
472         }
473
474 /**************************************************************************************************/
475
476 static DWORD WINAPI MyUchimeSeqsThreadFunction(LPVOID lpParam){ 
477         uchimeData* pDataArray;
478         pDataArray = (uchimeData*)lpParam;
479         
480         try {
481                 
482                 pDataArray->outputFName = pDataArray->m->getFullPathName(pDataArray->outputFName);
483                 pDataArray->filename = pDataArray->m->getFullPathName(pDataArray->filename);
484                 pDataArray->alns = pDataArray->m->getFullPathName(pDataArray->alns);
485                 
486                 int totalSeqs = 0;
487                 int numChimeras = 0;
488         
489                 int start = time(NULL);  if (pDataArray->m->control_pressed) { return 0; }
490                         
491                 //to allow for spaces in the path
492                 string outputFName = "\"" + pDataArray->outputFName + "\"";
493                 string filename = "\"" + pDataArray->filename + "\"";
494                 string alns = "\"" + pDataArray->alns+ "\"";
495                 string templatefile = "\"" + pDataArray->templatefile + "\"";
496                 string accnos = pDataArray->accnos;
497                 
498                 vector<char*> cPara;
499                 
500                 string uchimeCommand = pDataArray->uchimeLocation;
501         uchimeCommand = "\"" + uchimeCommand + "\"";
502         
503         char* tempUchime;
504         tempUchime= new char[uchimeCommand.length()+1]; 
505         *tempUchime = '\0';
506         strncat(tempUchime, uchimeCommand.c_str(), uchimeCommand.length());
507         cPara.push_back(tempUchime);
508                 
509                 char* tempIn = new char[8]; 
510                 *tempIn = '\0'; strncat(tempIn, "--input", 7);
511                 //strcpy(tempIn, "--input"); 
512                 cPara.push_back(tempIn);
513                 char* temp = new char[filename.length()+1];
514                 *temp = '\0'; strncat(temp, filename.c_str(), filename.length());
515                 //strcpy(temp, filename.c_str());
516                 cPara.push_back(temp);
517         
518                 //add reference file
519                 char* tempRef = new char[5]; 
520                 //strcpy(tempRef, "--db"); 
521                 *tempRef = '\0'; strncat(tempRef, "--db", 4);
522                 cPara.push_back(tempRef);  
523                 char* tempR = new char[templatefile.length()+1];
524                 //strcpy(tempR, templatefile.c_str());
525                 *tempR = '\0'; strncat(tempR, templatefile.c_str(), templatefile.length());
526                 cPara.push_back(tempR);
527                 
528                 char* tempO = new char[12]; 
529                 *tempO = '\0'; strncat(tempO, "--uchimeout", 11);
530                 //strcpy(tempO, "--uchimeout"); 
531                 cPara.push_back(tempO);
532                 char* tempout = new char[outputFName.length()+1];
533                 //strcpy(tempout, outputFName.c_str());
534                 *tempout = '\0'; strncat(tempout, outputFName.c_str(), outputFName.length());
535                 cPara.push_back(tempout);
536                 
537                 if (pDataArray->chimealns) {
538                         char* tempA = new char[13]; 
539                         *tempA = '\0'; strncat(tempA, "--uchimealns", 12);
540                         //strcpy(tempA, "--uchimealns"); 
541                         cPara.push_back(tempA);
542                         char* tempa = new char[alns.length()+1];
543                         //strcpy(tempa, alns.c_str());
544                         *tempa = '\0'; strncat(tempa, alns.c_str(), alns.length());
545                         cPara.push_back(tempa);
546                 }
547                 
548                 if (pDataArray->useAbskew) {
549                         char* tempskew = new char[9];
550                         *tempskew = '\0'; strncat(tempskew, "--abskew", 8);
551                         //strcpy(tempskew, "--abskew"); 
552                         cPara.push_back(tempskew);
553                         char* tempSkew = new char[pDataArray->abskew.length()+1];
554                         //strcpy(tempSkew, abskew.c_str());
555                         *tempSkew = '\0'; strncat(tempSkew, pDataArray->abskew.c_str(), pDataArray->abskew.length());
556                         cPara.push_back(tempSkew);
557                 }
558                 
559                 if (pDataArray->useMinH) {
560                         char* tempminh = new char[7]; 
561                         *tempminh = '\0'; strncat(tempminh, "--minh", 6);
562                         //strcpy(tempminh, "--minh"); 
563                         cPara.push_back(tempminh);
564                         char* tempMinH = new char[pDataArray->minh.length()+1];
565                         *tempMinH = '\0'; strncat(tempMinH, pDataArray->minh.c_str(), pDataArray->minh.length());
566                         //strcpy(tempMinH, minh.c_str());
567                         cPara.push_back(tempMinH);
568                 }
569                 
570                 if (pDataArray->useMindiv) {
571                         char* tempmindiv = new char[9]; 
572                         *tempmindiv = '\0'; strncat(tempmindiv, "--mindiv", 8);
573                         //strcpy(tempmindiv, "--mindiv"); 
574                         cPara.push_back(tempmindiv);
575                         char* tempMindiv = new char[pDataArray->mindiv.length()+1];
576                         *tempMindiv = '\0'; strncat(tempMindiv, pDataArray->mindiv.c_str(), pDataArray->mindiv.length());
577                         //strcpy(tempMindiv, mindiv.c_str());
578                         cPara.push_back(tempMindiv);
579                 }
580                 
581                 if (pDataArray->useXn) {
582                         char* tempxn = new char[5]; 
583                         //strcpy(tempxn, "--xn"); 
584                         *tempxn = '\0'; strncat(tempxn, "--xn", 4);
585                         cPara.push_back(tempxn);
586                         char* tempXn = new char[pDataArray->xn.length()+1];
587                         //strcpy(tempXn, xn.c_str());
588                         *tempXn = '\0'; strncat(tempXn, pDataArray->xn.c_str(), pDataArray->xn.length());
589                         cPara.push_back(tempXn);
590                 }
591                 
592                 if (pDataArray->useDn) {
593                         char* tempdn = new char[5]; 
594                         //strcpy(tempdn, "--dn"); 
595                         *tempdn = '\0'; strncat(tempdn, "--dn", 4);
596                         cPara.push_back(tempdn);
597                         char* tempDn = new char[pDataArray->dn.length()+1];
598                         *tempDn = '\0'; strncat(tempDn, pDataArray->dn.c_str(), pDataArray->dn.length());
599                         //strcpy(tempDn, dn.c_str());
600                         cPara.push_back(tempDn);
601                 }
602                 
603                 if (pDataArray->useXa) {
604                         char* tempxa = new char[5]; 
605                         //strcpy(tempxa, "--xa"); 
606                         *tempxa = '\0'; strncat(tempxa, "--xa", 4);
607                         cPara.push_back(tempxa);
608                         char* tempXa = new char[pDataArray->xa.length()+1];
609                         *tempXa = '\0'; strncat(tempXa, pDataArray->xa.c_str(), pDataArray->xa.length());
610                         //strcpy(tempXa, xa.c_str());
611                         cPara.push_back(tempXa);
612                 }
613                 
614                 if (pDataArray->useChunks) {
615                         char* tempchunks = new char[9]; 
616                         //strcpy(tempchunks, "--chunks"); 
617                         *tempchunks = '\0'; strncat(tempchunks, "--chunks", 8);
618                         cPara.push_back(tempchunks);
619                         char* tempChunks = new char[pDataArray->chunks.length()+1];
620                         *tempChunks = '\0'; strncat(tempChunks, pDataArray->chunks.c_str(), pDataArray->chunks.length());
621                         //strcpy(tempChunks, chunks.c_str());
622                         cPara.push_back(tempChunks);
623                 }
624                 
625                 if (pDataArray->useMinchunk) {
626                         char* tempminchunk = new char[11]; 
627                         //strcpy(tempminchunk, "--minchunk"); 
628                         *tempminchunk = '\0'; strncat(tempminchunk, "--minchunk", 10);
629                         cPara.push_back(tempminchunk);
630                         char* tempMinchunk = new char[pDataArray->minchunk.length()+1];
631                         *tempMinchunk = '\0'; strncat(tempMinchunk, pDataArray->minchunk.c_str(), pDataArray->minchunk.length());
632                         //strcpy(tempMinchunk, minchunk.c_str());
633                         cPara.push_back(tempMinchunk);
634                 }
635                 
636                 if (pDataArray->useIdsmoothwindow) {
637                         char* tempidsmoothwindow = new char[17]; 
638                         *tempidsmoothwindow = '\0'; strncat(tempidsmoothwindow, "--idsmoothwindow", 16);
639                         //strcpy(tempidsmoothwindow, "--idsmoothwindow"); 
640                         cPara.push_back(tempidsmoothwindow);
641                         char* tempIdsmoothwindow = new char[pDataArray->idsmoothwindow.length()+1];
642                         *tempIdsmoothwindow = '\0'; strncat(tempIdsmoothwindow, pDataArray->idsmoothwindow.c_str(), pDataArray->idsmoothwindow.length());
643                         //strcpy(tempIdsmoothwindow, idsmoothwindow.c_str());
644                         cPara.push_back(tempIdsmoothwindow);
645                 }
646                 
647                 if (pDataArray->useMaxp) {
648                         char* tempmaxp = new char[7]; 
649                         //strcpy(tempmaxp, "--maxp"); 
650                         *tempmaxp = '\0'; strncat(tempmaxp, "--maxp", 6);
651                         cPara.push_back(tempmaxp);
652                         char* tempMaxp = new char[pDataArray->maxp.length()+1];
653                         *tempMaxp = '\0'; strncat(tempMaxp, pDataArray->maxp.c_str(), pDataArray->maxp.length());
654                         //strcpy(tempMaxp, maxp.c_str());
655                         cPara.push_back(tempMaxp);
656                 }
657                 
658                 if (!pDataArray->skipgaps) {
659                         char* tempskipgaps = new char[13]; 
660                         //strcpy(tempskipgaps, "--[no]skipgaps");
661                         *tempskipgaps = '\0'; strncat(tempskipgaps, "--noskipgaps", 12);
662                         cPara.push_back(tempskipgaps);
663                 }
664                 
665                 if (!pDataArray->skipgaps2) {
666                         char* tempskipgaps2 = new char[14]; 
667                         //strcpy(tempskipgaps2, "--[no]skipgaps2"); 
668                         *tempskipgaps2 = '\0'; strncat(tempskipgaps2, "--noskipgaps2", 13);
669                         cPara.push_back(tempskipgaps2);
670                 }
671                 
672                 if (pDataArray->useMinlen) {
673                         char* tempminlen = new char[9]; 
674                         *tempminlen = '\0'; strncat(tempminlen, "--minlen", 8);
675                         //strcpy(tempminlen, "--minlen"); 
676                         cPara.push_back(tempminlen);
677                         char* tempMinlen = new char[pDataArray->minlen.length()+1];
678                         //strcpy(tempMinlen, minlen.c_str());
679                         *tempMinlen = '\0'; strncat(tempMinlen, pDataArray->minlen.c_str(), pDataArray->minlen.length());
680                         cPara.push_back(tempMinlen);
681                 }
682                 
683                 if (pDataArray->useMaxlen) {
684                         char* tempmaxlen = new char[9]; 
685                         //strcpy(tempmaxlen, "--maxlen"); 
686                         *tempmaxlen = '\0'; strncat(tempmaxlen, "--maxlen", 8);
687                         cPara.push_back(tempmaxlen);
688                         char* tempMaxlen = new char[pDataArray->maxlen.length()+1];
689                         *tempMaxlen = '\0'; strncat(tempMaxlen, pDataArray->maxlen.c_str(), pDataArray->maxlen.length());
690                         //strcpy(tempMaxlen, maxlen.c_str());
691                         cPara.push_back(tempMaxlen);
692                 }
693                 
694                 if (pDataArray->ucl) {
695                         char* tempucl = new char[5]; 
696                         strcpy(tempucl, "--ucl"); 
697                         cPara.push_back(tempucl);
698                 }
699                 
700                 if (pDataArray->useQueryfract) {
701                         char* tempqueryfract = new char[13]; 
702                         *tempqueryfract = '\0'; strncat(tempqueryfract, "--queryfract", 12);
703                         //strcpy(tempqueryfract, "--queryfract"); 
704                         cPara.push_back(tempqueryfract);
705                         char* tempQueryfract = new char[pDataArray->queryfract.length()+1];
706                         *tempQueryfract = '\0'; strncat(tempQueryfract, pDataArray->queryfract.c_str(), pDataArray->queryfract.length());
707                         //strcpy(tempQueryfract, queryfract.c_str());
708                         cPara.push_back(tempQueryfract);
709                 }
710                 
711                 
712                 char** uchimeParameters;
713                 uchimeParameters = new char*[cPara.size()];
714                 string commandString = "";
715                 for (int j = 0; j < cPara.size(); j++) {  uchimeParameters[j] = cPara[j];  commandString += toString(cPara[j]) + " "; } 
716                 //int numArgs = cPara.size();
717                 
718                 //uchime_main(numArgs, uchimeParameters); 
719                 //cout << "commandString = " << commandString << endl;
720         if (pDataArray->m->debug) { pDataArray->m->mothurOut("[DEBUG]: uchime command = " + commandString + ".\n"); }
721                 system(commandString.c_str());
722                 
723                 //free memory
724                 for(int j = 0; j < cPara.size(); j++)  {  delete cPara[j];  }
725                 delete[] uchimeParameters; 
726                 
727                 //remove "" from filenames
728                 outputFName = outputFName.substr(1, outputFName.length()-2);
729                 filename = filename.substr(1, filename.length()-2);
730                 alns = alns.substr(1, alns.length()-2);
731                 
732                 if (pDataArray->m->control_pressed) { return 0; }
733                 
734                 //create accnos file from uchime results
735                 ifstream in; 
736                 pDataArray->m->openInputFile(outputFName, in);
737                 
738                 ofstream out;
739                 pDataArray->m->openOutputFile(accnos, out);
740                 
741                 numChimeras = 0;
742                 while(!in.eof()) {
743                         
744                         if (pDataArray->m->control_pressed) { break; }
745                         
746                         string name = "";
747                         string chimeraFlag = "";
748                         in >> chimeraFlag >> name;
749                         
750                         for (int j = 0; j < 15; j++) {  in >> chimeraFlag; }
751                         pDataArray->m->gobble(in);
752                         
753                         if (chimeraFlag == "Y") {  out << name << endl; numChimeras++; }
754                         totalSeqs++;
755                 }
756                 in.close();
757                 out.close();
758                 
759                 if (pDataArray->m->control_pressed) { return 0; }
760                 
761                 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(totalSeqs) + " sequences.");       pDataArray->m->mothurOutEndLine();                                      
762         
763                 pDataArray->count = totalSeqs;
764                 pDataArray->numChimeras = numChimeras;
765                 return totalSeqs;
766                 
767         }
768         catch(exception& e) {
769                 pDataArray->m->errorOut(e, "ChimeraUchimeCommand", "MyUchimeSeqsThreadFunction");
770                 exit(1);
771         }
772
773
774 #endif
775
776 /**************************************************************************************************/
777
778
779 #endif
780
781