+/**************************************************************************************************/
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+#else
+static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){
+ contigsData* pDataArray;
+ pDataArray = (contigsData*)lpParam;
+
+ try {
+ int longestBase = 1000;
+ Alignment* alignment;
+ if(pDataArray->align == "gotoh") { alignment = new GotohOverlap(pDataArray->gapOpen, pDataArray->gapExtend, pDataArray->match, pDataArray->misMatch, longestBase); }
+ else if(pDataArray->align == "needleman") { alignment = new NeedlemanOverlap(pDataArray->gapOpen, pDataArray->match, pDataArray->misMatch, longestBase); }
+
+ pDataArray->count = 0;
+ string thisffastafile = pDataArray->files[0];
+ string thisfqualfile = pDataArray->files[1];
+ string thisrfastafile = pDataArray->files[2];
+ string thisrqualfile = pDataArray->files[3];
+
+ if (pDataArray->m->debug) { pDataArray->m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
+
+ if(pDataArray->allFiles){
+ for (int i = 0; i < pDataArray->fastaFileNames.size(); i++) { //clears old file
+ for (int j = 0; j < pDataArray->fastaFileNames[i].size(); j++) { //clears old file
+ if (pDataArray->fastaFileNames[i][j] != "") {
+ ofstream temp;
+ pDataArray->m->openOutputFile(pDataArray->fastaFileNames[i][j], temp); temp.close();
+ }
+ }
+ }
+ }
+
+ ifstream inFFasta, inRFasta, inFQual, inRQual;
+ ofstream outFasta, outMisMatch, outScrapFasta;
+ pDataArray->m->openInputFile(thisffastafile, inFFasta);
+ pDataArray->m->openInputFile(thisrfastafile, inRFasta);
+ if (thisfqualfile != "") {
+ pDataArray->m->openInputFile(thisfqualfile, inFQual);
+ pDataArray->m->openInputFile(thisrqualfile, inRQual);
+ }
+ pDataArray->m->openOutputFile(pDataArray->outputFasta, outFasta);
+ pDataArray->m->openOutputFile(pDataArray->outputMisMatches, outMisMatch);
+ pDataArray->m->openOutputFile(pDataArray->outputScrapFasta, outScrapFasta);
+
+ outMisMatch << "Name\tLength\tOverlap_Length\tOverlap_Start\tOverlap_End\tMisMatches\tNum_Ns\n";
+
+ TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, 0, 0, pDataArray->primers, pDataArray->barcodes);
+
+ while ((!inFFasta.eof()) && (!inRFasta.eof())) {
+
+ if (pDataArray->m->control_pressed) { break; }
+
+ int success = 1;
+ string trashCode = "";
+ int currentSeqsDiffs = 0;
+
+ //read seqs and quality info
+ Sequence fSeq(inFFasta); pDataArray->m->gobble(inFFasta);
+ Sequence rSeq(inRFasta); pDataArray->m->gobble(inRFasta);
+ QualityScores* fQual = NULL; QualityScores* rQual = NULL;
+ if (thisfqualfile != "") {
+ fQual = new QualityScores(inFQual); pDataArray->m->gobble(inFQual);
+ rQual = new QualityScores(inRQual); pDataArray->m->gobble(inRQual);
+ }
+
+ int barcodeIndex = 0;
+ int primerIndex = 0;
+
+ if(pDataArray->barcodes.size() != 0){
+ if (thisfqualfile != "") {
+ success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
+ }else {
+ success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
+ }
+ if(success > pDataArray->bdiffs) { trashCode += 'b'; }
+ else{ currentSeqsDiffs += success; }
+ }
+
+ if(pDataArray->primers.size() != 0){
+ if (thisfqualfile != "") {
+ success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
+ }else {
+ success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
+ }
+ if(success > pDataArray->pdiffs) { trashCode += 'f'; }
+ else{ currentSeqsDiffs += success; }
+ }
+
+ if (currentSeqsDiffs > pDataArray->tdiffs) { trashCode += 't'; }
+
+ //flip the reverse reads
+ rSeq.reverseComplement();
+ if (thisfqualfile != "") { rQual->flipQScores(); }
+
+ //pairwise align
+ alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
+ map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
+ map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
+ fSeq.setAligned(alignment->getSeqAAln());
+ rSeq.setAligned(alignment->getSeqBAln());
+ int length = fSeq.getAligned().length();
+
+ //traverse alignments merging into one contiguous seq
+ string contig = "";
+ int numMismatches = 0;
+ string seq1 = fSeq.getAligned();
+ string seq2 = rSeq.getAligned();
+ vector<int> scores1, scores2;
+ if (thisfqualfile != "") {
+ scores1 = fQual->getQualityScores();
+ scores2 = rQual->getQualityScores();
+ delete fQual; delete rQual;
+ }
+
+ int overlapStart = fSeq.getStartPos();
+ int seq2Start = rSeq.getStartPos();
+ //bigger of the 2 starting positions is the location of the overlapping start
+ if (overlapStart < seq2Start) { //seq2 starts later so take from 0 to seq2Start from seq1
+ overlapStart = seq2Start;
+ for (int i = 0; i < overlapStart; i++) { contig += seq1[i]; }
+ }else { //seq1 starts later so take from 0 to overlapStart from seq2
+ for (int i = 0; i < overlapStart; i++) { contig += seq2[i]; }
+ }
+
+ int seq1End = fSeq.getEndPos();
+ int seq2End = rSeq.getEndPos();
+ int overlapEnd = seq1End;
+ if (seq2End < overlapEnd) { overlapEnd = seq2End; } //smallest end position is where overlapping ends
+
+ int oStart = contig.length();
+ for (int i = overlapStart; i < overlapEnd; i++) {
+ if (seq1[i] == seq2[i]) { //match, add base and choose highest score
+ contig += seq1[i];
+ }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below insert. In that case eliminate base
+ if (thisfqualfile != "") {
+ if (scores2[BBaseMap[i]] < pDataArray->insert) { } //
+ else { contig += seq2[i]; }
+ }else { contig += seq2[i]; } //with no quality info, then we keep it?
+ }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below insert. In that case eliminate base
+ if (thisfqualfile != "") {
+ if (scores1[ABaseMap[i]] < pDataArray->insert) { } //
+ else { contig += seq1[i]; }
+ }else { contig += seq1[i]; } //with no quality info, then we keep it?
+ }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
+ if (thisfqualfile != "") {
+ if (abs(scores1[ABaseMap[i]] - scores2[BBaseMap[i]]) >= pDataArray->deltaq) { //is the difference in qual scores >= deltaq, if yes choose base with higher score
+ char c = seq1[i];
+ if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { c = seq2[i]; }
+ contig += c;
+ }else { //if no, base becomes n
+ contig += 'N';
+ }
+ numMismatches++;
+ }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
+ }else { //should never get here
+ pDataArray->m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
+ }
+ }
+ int oend = contig.length();
+
+ if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
+ for (int i = overlapEnd; i < length; i++) { contig += seq2[i]; }
+ }else { //seq2 ends before seq1 so take from overlap to length from seq1
+ for (int i = overlapEnd; i < length; i++) { contig += seq1[i]; }
+ }