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