+/**************************************************************************************************/
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+#else
+static DWORD WINAPI MyPreclusterThreadFunction(LPVOID lpParam){
+ preClusterData* pDataArray;
+ pDataArray = (preClusterData*)lpParam;
+
+ try {
+
+ //parse fasta and name file by group
+ SequenceParser* parser;
+ SequenceCountParser* cparser;
+ if (pDataArray->countfile != "") {
+ cparser = new SequenceCountParser(pDataArray->countfile, pDataArray->fastafile);
+ }else {
+ if (pDataArray->namefile != "") { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile); }
+ else { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile); }
+ }
+
+ int numSeqs = 0;
+ vector<seqPNode> alignSeqs;
+ //clear out old files
+ ofstream outF; pDataArray->m->openOutputFile(pDataArray->newFName, outF); outF.close();
+ ofstream outN; pDataArray->m->openOutputFile(pDataArray->newNName, outN); outN.close();
+
+ //precluster each group
+ for (int k = pDataArray->start; k < pDataArray->end; k++) {
+
+ pDataArray->count++;
+
+ int start = time(NULL);
+
+ if (pDataArray->m->control_pressed) { delete parser; return 0; }
+
+ pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
+
+ map<string, string> thisNameMap;
+ vector<Sequence> thisSeqs;
+ if (pDataArray->groupfile != "") {
+ thisSeqs = parser->getSeqs(pDataArray->groups[k]);
+ }else if (pDataArray->countfile != "") {
+ thisSeqs = cparser->getSeqs(pDataArray->groups[k]);
+ }
+ if (pDataArray->namefile != "") { thisNameMap = parser->getNameMap(pDataArray->groups[k]); }
+
+ //fill alignSeqs with this groups info.
+ ////////////////////////////////////////////////////
+ //numSeqs = loadSeqs(thisNameMap, thisSeqs); same function below
+
+ int length = 0;
+ alignSeqs.clear();
+ map<string, string>::iterator it;
+ bool error = false;
+ map<string, int> thisCount;
+ if (pDataArray->countfile != "") { thisCount = cparser->getCountTable(pDataArray->groups[k]); }
+
+
+ for (int i = 0; i < thisSeqs.size(); i++) {
+
+ if (pDataArray->m->control_pressed) { delete parser; return 0; }
+
+ if (pDataArray->namefile != "") {
+ it = thisNameMap.find(thisSeqs[i].getName());
+
+ //should never be true since parser checks for this
+ if (it == thisNameMap.end()) { pDataArray->m->mothurOut(thisSeqs[i].getName() + " is not in your names file, please correct."); pDataArray->m->mothurOutEndLine(); error = true; }
+ else{
+ //get number of reps
+ int numReps = 1;
+ for(int j=0;j<(it->second).length();j++){
+ if((it->second)[j] == ','){ numReps++; }
+ }
+
+ seqPNode tempNode(numReps, thisSeqs[i], it->second);
+ alignSeqs.push_back(tempNode);
+ if (thisSeqs[i].getAligned().length() > length) { length = thisSeqs[i].getAligned().length(); }
+ }
+ }else { //no names file, you are identical to yourself
+ int numRep = 1;
+ if (pDataArray->countfile != "") {
+ map<string, int>::iterator it2 = thisCount.find(thisSeqs[i].getName());
+
+ //should never be true since parser checks for this
+ if (it2 == thisCount.end()) { pDataArray->m->mothurOut(thisSeqs[i].getName() + " is not in your count file, please correct."); pDataArray->m->mothurOutEndLine(); error = true; }
+ else { numRep = it2->second; }
+ }
+ seqPNode tempNode(numRep, thisSeqs[i], thisSeqs[i].getName());
+ alignSeqs.push_back(tempNode);
+ if (thisSeqs[i].getAligned().length() > length) { length = thisSeqs[i].getAligned().length(); }
+ }
+ }
+
+ //sanity check
+ if (error) { pDataArray->m->control_pressed = true; }
+
+ thisSeqs.clear();
+ numSeqs = alignSeqs.size();
+
+ ////////////////////////////////////////////////////
+
+ if (pDataArray->m->control_pressed) { delete parser; return 0; }
+
+ if (pDataArray->diffs > length) { pDataArray->m->mothurOut("Error: diffs is greater than your sequence length."); pDataArray->m->mothurOutEndLine(); pDataArray->m->control_pressed = true; return 0; }
+
+ ////////////////////////////////////////////////////
+ //int count = process(); - same function below
+
+ ofstream out;
+ pDataArray->m->openOutputFile(pDataArray->newMName+pDataArray->groups[k]+".map", out);
+ pDataArray->mapFileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
+
+ //sort seqs by number of identical seqs
+ if (pDataArray->topdown) { sort(alignSeqs.begin(), alignSeqs.end(), comparePriorityTopDown); }
+ else { sort(alignSeqs.begin(), alignSeqs.end(), comparePriorityDownTop); }
+
+ int count = 0;
+
+ if (pDataArray->topdown) {
+ //think about running through twice...
+ for (int i = 0; i < numSeqs; i++) {
+
+ //are you active
+ // itActive = active.find(alignSeqs[i].seq.getName());
+
+ if (alignSeqs[i].active) { //this sequence has not been merged yet
+
+ string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n";
+
+ //try to merge it with all smaller seqs
+ for (int j = i+1; j < numSeqs; j++) {
+
+ if (pDataArray->m->control_pressed) { delete parser; return 0; }
+
+ if (alignSeqs[j].active) { //this sequence has not been merged yet
+ //are you within "diff" bases
+ //int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
+ int mismatch = 0;
+
+ for (int k = 0; k < alignSeqs[i].seq.getAligned().length(); k++) {
+ //do they match
+ if (alignSeqs[i].seq.getAligned()[k] != alignSeqs[j].seq.getAligned()[k]) { mismatch++; }
+ if (mismatch > pDataArray->diffs) { mismatch = length; break; } //to far to cluster
+ }
+
+ if (mismatch <= pDataArray->diffs) {
+ //merge
+ alignSeqs[i].names += ',' + alignSeqs[j].names;
+ alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
+
+ alignSeqs[j].active = 0;
+ alignSeqs[j].numIdentical = 0;
+ alignSeqs[j].diffs = mismatch;
+ count++;
+ chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n";
+ }
+ }//end if j active
+ }//end for loop j
+
+ //remove from active list
+ alignSeqs[i].active = 0;
+
+ out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;
+
+ }//end if active i
+ if(i % 100 == 0) { pDataArray->m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
+ }
+
+ }else {
+ map<int, string> mapFile;
+ map<int, int> originalCount;
+ map<int, int>::iterator itCount;
+ for (int i = 0; i < numSeqs; i++) { mapFile[i] = ""; originalCount[i] = alignSeqs[i].numIdentical; }
+
+ //think about running through twice...
+ for (int i = 0; i < numSeqs; i++) {
+
+ //try to merge it into larger seqs
+ for (int j = i+1; j < numSeqs; j++) {
+
+ if (pDataArray->m->control_pressed) { out.close(); return 0; }
+
+ if (originalCount[j] > originalCount[i]) { //this sequence is more abundant than I am
+ //are you within "diff" bases
+ //int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
+ int mismatch = 0;
+
+ for (int k = 0; k < alignSeqs[i].seq.getAligned().length(); k++) {
+ //do they match
+ if (alignSeqs[i].seq.getAligned()[k] != alignSeqs[j].seq.getAligned()[k]) { mismatch++; }
+ if (mismatch > pDataArray->diffs) { mismatch = length; break; } //to far to cluster
+ }
+
+ if (mismatch <= pDataArray->diffs) {
+ //merge
+ alignSeqs[j].names += ',' + alignSeqs[i].names;
+ alignSeqs[j].numIdentical += alignSeqs[i].numIdentical;
+
+ mapFile[j] = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[i].seq.getAligned() + "\n" + mapFile[i];
+ alignSeqs[i].numIdentical = 0;
+ originalCount.erase(i);
+ mapFile[i] = "";
+ count++;
+ j+=numSeqs; //exit search, we merged this one in.
+ }
+ }//end abundance check
+ }//end for loop j
+
+ if(i % 100 == 0) { pDataArray->m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
+ }
+
+ for (int i = 0; i < numSeqs; i++) {
+ if (alignSeqs[i].numIdentical != 0) {
+ out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n" << mapFile[i] << endl;
+ }
+ }