+/**************************************************************************************************/
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+#else
+static DWORD WINAPI MyChopThreadFunction(LPVOID lpParam){
+ chopData* pDataArray;
+ pDataArray = (chopData*)lpParam;
+
+ try {
+ ofstream out;
+ pDataArray->m->openOutputFile(pDataArray->outFasta, out);
+
+ ofstream outAcc;
+ pDataArray->m->openOutputFile(pDataArray->outAccnos, outAcc);
+
+ ifstream in;
+ pDataArray->m->openInputFile(pDataArray->filename, in);
+
+ if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
+ in.seekg(0);
+ }else { //this accounts for the difference in line endings.
+ in.seekg(pDataArray->start-1); pDataArray->m->gobble(in);
+ }
+
+ bool done = false;
+ bool wroteAccnos = false;
+ pDataArray->count = 0;
+
+ for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
+
+ if (pDataArray->m->control_pressed) { in.close(); out.close(); outAcc.close(); pDataArray->m->mothurRemove(pDataArray->outFasta); pDataArray->m->mothurRemove(pDataArray->outAccnos); return 0; }
+
+ Sequence seq(in); pDataArray->m->gobble(in);
+
+ if (seq.getName() != "") {
+ //string newSeqString = getChopped(seq);
+ ///////////////////////////////////////////////////////////////////////
+ string temp = seq.getAligned();
+ string tempUnaligned = seq.getUnaligned();
+
+ if (pDataArray->countGaps) {
+ //if needed trim sequence
+ if (pDataArray->keep == "front") {//you want to keep the beginning
+ int tempLength = temp.length();
+
+ if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
+
+ int stopSpot = 0;
+ int numBasesCounted = 0;
+
+ for (int i = 0; i < temp.length(); i++) {
+ //eliminate N's
+ if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
+
+ numBasesCounted++;
+
+ if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
+ }
+
+ if (stopSpot == 0) { temp = ""; }
+ else { temp = temp.substr(0, stopSpot+1); }
+
+ }else {
+ if (!pDataArray->Short) { temp = ""; } //sequence too short
+ }
+ }else { //you are keeping the back
+ int tempLength = temp.length();
+ if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
+
+ int stopSpot = 0;
+ int numBasesCounted = 0;
+
+ for (int i = (temp.length()-1); i >= 0; i--) {
+ //eliminate N's
+ if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
+
+ numBasesCounted++;
+
+ if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
+ }
+
+ if (stopSpot == 0) { temp = ""; }
+ else { temp = temp.substr(stopSpot+1); }
+ }else {
+ if (!pDataArray->Short) { temp = ""; } //sequence too short
+ }
+ }
+
+ }else{
+
+ //if needed trim sequence
+ if (pDataArray->keep == "front") {//you want to keep the beginning
+ int tempLength = tempUnaligned.length();
+
+ if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
+
+ int stopSpot = 0;
+ int numBasesCounted = 0;
+
+ for (int i = 0; i < temp.length(); i++) {
+ //eliminate N's
+ if (toupper(temp[i]) == 'N') {
+ temp[i] = '.';
+ tempLength--;
+ if (tempLength < pDataArray->numbases) { stopSpot = 0; break; }
+ }
+
+ if(isalpha(temp[i])) { numBasesCounted++; }
+
+ if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
+ }
+
+ if (stopSpot == 0) { temp = ""; }
+ else { temp = temp.substr(0, stopSpot+1); }
+
+ }else {
+ if (!pDataArray->Short) { temp = ""; } //sequence too short
+ }
+ }else { //you are keeping the back
+ int tempLength = tempUnaligned.length();
+ if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
+
+ int stopSpot = 0;
+ int numBasesCounted = 0;
+
+ for (int i = (temp.length()-1); i >= 0; i--) {
+ //eliminate N's
+ if (toupper(temp[i]) == 'N') {
+ temp[i] = '.';
+ tempLength--;
+ if (tempLength < pDataArray->numbases) { stopSpot = 0; break; }
+ }
+
+ if(isalpha(temp[i])) { numBasesCounted++; }
+
+ if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
+ }
+
+ if (stopSpot == 0) { temp = ""; }
+ else { temp = temp.substr(stopSpot); }
+ }else {
+ if (!pDataArray->Short) { temp = ""; } //sequence too short
+ }
+ }
+ }
+
+ string newSeqString = temp;
+ ///////////////////////////////////////////////////////////////////////
+
+ //output trimmed sequence
+ if (newSeqString != "") {
+ out << ">" << seq.getName() << endl << newSeqString << endl;
+ }else{
+ outAcc << seq.getName() << endl;
+ pDataArray->wroteAccnos = true;
+ }
+ pDataArray->count++;
+ }
+ //report progress
+ if((pDataArray->count) % 1000 == 0){ pDataArray->m->mothurOut(toString(pDataArray->count)); pDataArray->m->mothurOutEndLine(); }
+
+ }
+ //report progress
+ if((pDataArray->count) % 1000 != 0){ pDataArray->m->mothurOut(toString(pDataArray->count)); pDataArray->m->mothurOutEndLine(); }
+
+
+ in.close();
+ out.close();
+ outAcc.close();
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ pDataArray->m->errorOut(e, "ChopsSeqsCommand", "MyChopThreadFunction");
+ exit(1);
+ }
+}
+#endif
+
+
+