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