1 #ifndef Mothur_makecontigscommand_h
2 #define Mothur_makecontigscommand_h
5 // makecontigscommand.h
8 // Created by Sarah Westcott on 5/15/12.
9 // Copyright (c) 2012 Schloss Lab. All rights reserved.
12 #include "command.hpp"
13 #include "sequence.hpp"
14 #include "qualityscores.h"
15 #include "alignment.hpp"
16 #include "gotohoverlap.hpp"
17 #include "needlemanoverlap.hpp"
18 #include "blastalign.hpp"
19 #include "noalign.hpp"
20 #include "trimoligos.h"
27 fastqRead() { name = ""; sequence = ""; scores.clear(); };
28 fastqRead(string n, string s, vector<int> sc) : name(n), sequence(s), scores(sc) {};
32 struct pairFastqRead {
37 pairFastqRead(fastqRead f, fastqRead r) : forward(f), reverse(r){};
40 /**************************************************************************************************/
42 class MakeContigsCommand : public Command {
44 MakeContigsCommand(string);
46 ~MakeContigsCommand(){}
48 vector<string> setParameters();
49 string getCommandName() { return "make.contigs"; }
50 string getCommandCategory() { return "Sequence Processing"; }
51 //commmand category choices: Sequence Processing, OTU-Based Approaches, Hypothesis Testing, Phylotype Analysis, General, Clustering and Hidden
53 string getHelpString();
54 string getOutputPattern(string);
55 string getCitation() { return "http://www.mothur.org/wiki/Make.contigs"; }
56 string getDescription() { return "description"; }
59 void help() { m->mothurOut(getHelpString()); }
62 bool abort, allFiles, createGroup, trimOverlap;
63 string outputDir, ffastqfile, rfastqfile, align, oligosfile, rfastafile, ffastafile, rqualfile, fqualfile, file, format;
64 float match, misMatch, gapOpen, gapExtend;
65 int processors, longestBase, insert, tdiffs, bdiffs, pdiffs, ldiffs, sdiffs, deltaq;
66 vector<string> outputNames;
68 map<int, oligosPair> barcodes;
69 map<int, oligosPair> primers;
70 vector<string> linker;
71 vector<string> spacer;
72 vector<string> primerNameVector;
73 vector<string> barcodeNameVector;
74 vector<char> convertTable;
76 map<string, int> groupCounts;
77 map<string, string> groupMap;
79 vector<int> convertQual(string);
80 fastqRead readFastq(ifstream&, bool&);
81 vector< vector< vector<string> > > preProcessData(unsigned long int&);
82 vector< vector<string> > readFileNames(string);
83 vector< vector<string> > readFastqFiles(unsigned long int&, string, string);
84 vector< vector<string> > readFastaFiles(unsigned long int&, string, string);
85 //bool checkReads(fastqRead&, fastqRead&, string, string);
86 int createProcesses(vector< vector<string> >, string, string, string, vector<vector<string> >);
87 int driver(vector<string>, string, string, string, vector<vector<string> >, int);
88 bool getOligos(vector<vector<string> >&, string);
89 string reverseOligo(string);
90 vector<pairFastqRead> getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map<string, fastqRead>& uniques);
93 /**************************************************************************************************/
95 /**************************************************************************************************/
96 //custom data structure for threads to use.
97 // This is passed by void pointer so it can be any data type
98 // that can be passed using a single void pointer (LPVOID).
101 string outputScrapFasta;
102 string outputMisMatches;
104 vector<string> files;
105 vector<vector<string> > fastaFileNames;
107 float match, misMatch, gapOpen, gapExtend;
108 int count, insert, threadID, pdiffs, bdiffs, tdiffs, deltaq;
109 bool allFiles, createGroup, done, trimOverlap;
110 map<string, int> groupCounts;
111 map<string, string> groupMap;
112 vector<string> primerNameVector;
113 vector<string> barcodeNameVector;
114 map<int, oligosPair> barcodes;
115 map<int, oligosPair> primers;
118 contigsData(vector<string> f, string of, string osf, string om, string al, MothurOut* mout, float ma, float misMa, float gapO, float gapE, int thr, int delt, map<int, oligosPair> br, map<int, oligosPair> pr, vector<vector<string> > ffn, vector<string>bnv, vector<string> pnv, int pdf, int bdf, int tdf, bool cg, bool all, bool to, int tid) {
121 outputMisMatches = om;
130 outputScrapFasta = osf;
131 fastaFileNames = ffn;
134 barcodeNameVector = bnv;
135 primerNameVector = pnv;
148 /**************************************************************************************************/
149 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
151 static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){
152 contigsData* pDataArray;
153 pDataArray = (contigsData*)lpParam;
156 int longestBase = 1000;
157 Alignment* alignment;
158 if(pDataArray->align == "gotoh") { alignment = new GotohOverlap(pDataArray->gapOpen, pDataArray->gapExtend, pDataArray->match, pDataArray->misMatch, longestBase); }
159 else if(pDataArray->align == "needleman") { alignment = new NeedlemanOverlap(pDataArray->gapOpen, pDataArray->match, pDataArray->misMatch, longestBase); }
161 pDataArray->count = 0;
162 string thisffastafile = pDataArray->files[0];
163 string thisfqualfile = pDataArray->files[1];
164 string thisrfastafile = pDataArray->files[2];
165 string thisrqualfile = pDataArray->files[3];
167 if (pDataArray->m->debug) { pDataArray->m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
169 if(pDataArray->allFiles){
170 for (int i = 0; i < pDataArray->fastaFileNames.size(); i++) { //clears old file
171 for (int j = 0; j < pDataArray->fastaFileNames[i].size(); j++) { //clears old file
172 if (pDataArray->fastaFileNames[i][j] != "") {
174 pDataArray->m->openOutputFile(pDataArray->fastaFileNames[i][j], temp); temp.close();
180 ifstream inFFasta, inRFasta, inFQual, inRQual;
181 ofstream outFasta, outMisMatch, outScrapFasta;
182 pDataArray->m->openInputFile(thisffastafile, inFFasta);
183 pDataArray->m->openInputFile(thisrfastafile, inRFasta);
184 if (thisfqualfile != "") {
185 pDataArray->m->openInputFile(thisfqualfile, inFQual);
186 pDataArray->m->openInputFile(thisrqualfile, inRQual);
188 pDataArray->m->openOutputFile(pDataArray->outputFasta, outFasta);
189 pDataArray->m->openOutputFile(pDataArray->outputMisMatches, outMisMatch);
190 pDataArray->m->openOutputFile(pDataArray->outputScrapFasta, outScrapFasta);
192 if (pDataArray->threadID == 0) { outMisMatch << "Name\tLength\tOverlap_Length\tOverlap_Start\tOverlap_End\tMisMatches\tNum_Ns\n"; }
194 TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, 0, 0, pDataArray->primers, pDataArray->barcodes);
196 while ((!inFFasta.eof()) && (!inRFasta.eof())) {
198 if (pDataArray->m->control_pressed) { break; }
201 string trashCode = "";
202 int currentSeqsDiffs = 0;
204 //read seqs and quality info
205 Sequence fSeq(inFFasta); pDataArray->m->gobble(inFFasta);
206 Sequence rSeq(inRFasta); pDataArray->m->gobble(inRFasta);
207 QualityScores* fQual = NULL; QualityScores* rQual = NULL;
208 if (thisfqualfile != "") {
209 fQual = new QualityScores(inFQual); pDataArray->m->gobble(inFQual);
210 rQual = new QualityScores(inRQual); pDataArray->m->gobble(inRQual);
213 int barcodeIndex = 0;
216 if(pDataArray->barcodes.size() != 0){
217 if (thisfqualfile != "") {
218 success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
220 success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
222 if(success > pDataArray->bdiffs) { trashCode += 'b'; }
223 else{ currentSeqsDiffs += success; }
226 if(pDataArray->primers.size() != 0){
227 if (thisfqualfile != "") {
228 success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
230 success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
232 if(success > pDataArray->pdiffs) { trashCode += 'f'; }
233 else{ currentSeqsDiffs += success; }
236 if (currentSeqsDiffs > pDataArray->tdiffs) { trashCode += 't'; }
238 //flip the reverse reads
239 rSeq.reverseComplement();
240 if (thisfqualfile != "") { rQual->flipQScores(); }
243 alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
244 map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
245 map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
246 fSeq.setAligned(alignment->getSeqAAln());
247 rSeq.setAligned(alignment->getSeqBAln());
248 int length = fSeq.getAligned().length();
250 //traverse alignments merging into one contiguous seq
252 int numMismatches = 0;
253 string seq1 = fSeq.getAligned();
254 string seq2 = rSeq.getAligned();
255 vector<int> scores1, scores2;
256 if (thisfqualfile != "") {
257 scores1 = fQual->getQualityScores();
258 scores2 = rQual->getQualityScores();
259 delete fQual; delete rQual;
262 int overlapStart = fSeq.getStartPos();
263 int seq2Start = rSeq.getStartPos();
264 //bigger of the 2 starting positions is the location of the overlapping start
265 if (overlapStart < seq2Start) { //seq2 starts later so take from 0 to seq2Start from seq1
266 overlapStart = seq2Start;
267 for (int i = 0; i < overlapStart; i++) { contig += seq1[i]; }
268 }else { //seq1 starts later so take from 0 to overlapStart from seq2
269 for (int i = 0; i < overlapStart; i++) { contig += seq2[i]; }
272 int seq1End = fSeq.getEndPos();
273 int seq2End = rSeq.getEndPos();
274 int overlapEnd = seq1End;
275 if (seq2End < overlapEnd) { overlapEnd = seq2End; } //smallest end position is where overlapping ends
277 int oStart = contig.length();
278 for (int i = overlapStart; i < overlapEnd; i++) {
279 if (seq1[i] == seq2[i]) { //match, add base and choose highest score
281 }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
282 if (thisfqualfile != "") {
283 if (scores2[BBaseMap[i]] < pDataArray->insert) { } //
284 else { contig += seq2[i]; }
285 }else { contig += seq2[i]; } //with no quality info, then we keep it?
286 }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
287 if (thisfqualfile != "") {
288 if (scores1[ABaseMap[i]] < pDataArray->insert) { } //
289 else { contig += seq1[i]; }
290 }else { contig += seq1[i]; } //with no quality info, then we keep it?
291 }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
292 if (thisfqualfile != "") {
293 if (abs(scores1[ABaseMap[i]] - scores2[BBaseMap[i]]) >= pDataArray->deltaq) { //is the difference in qual scores >= deltaq, if yes choose base with higher score
295 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { c = seq2[i]; }
297 }else { //if no, base becomes n
301 }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
302 }else { //should never get here
303 pDataArray->m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
306 int oend = contig.length();
308 if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
309 for (int i = overlapEnd; i < length; i++) { contig += seq2[i]; }
310 }else { //seq2 ends before seq1 so take from overlap to length from seq1
311 for (int i = overlapEnd; i < length; i++) { contig += seq1[i]; }
314 if (pDataArray->trimOverlap) { contig = contig.substr(overlapStart-1, oend-oStart); if (contig.length() == 0) { trashCode += "l"; } }
316 if(trashCode.length() == 0){
318 if (pDataArray->createGroup) {
319 if(pDataArray->barcodes.size() != 0){
320 string thisGroup = pDataArray->barcodeNameVector[barcodeIndex];
321 if (pDataArray->primers.size() != 0) {
322 if (pDataArray->primerNameVector[primerIndex] != "") {
323 if(thisGroup != "") {
324 thisGroup += "." + pDataArray->primerNameVector[primerIndex];
326 thisGroup = pDataArray->primerNameVector[primerIndex];
331 if (pDataArray->m->debug) { pDataArray->m->mothurOut(", group= " + thisGroup + "\n"); }
333 int pos = thisGroup.find("ignore");
334 if (pos == string::npos) {
335 pDataArray->groupMap[fSeq.getName()] = thisGroup;
337 map<string, int>::iterator it = pDataArray->groupCounts.find(thisGroup);
338 if (it == pDataArray->groupCounts.end()) { pDataArray->groupCounts[thisGroup] = 1; }
339 else { pDataArray->groupCounts[it->first] ++; }
340 }else { ignore = true; }
344 if(pDataArray->allFiles && !ignore){
346 pDataArray->m->openOutputFileAppend(pDataArray->fastaFileNames[barcodeIndex][primerIndex], output);
347 output << ">" << fSeq.getName() << endl << contig << endl;
352 outFasta << ">" << fSeq.getName() << endl << contig << endl;
354 for (int i = 0; i < contig.length(); i++) { if (contig[i] == 'N') { numNs++; } }
355 outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << (oend-oStart) << '\t' << oStart << '\t' << oend << '\t' << numMismatches << '\t' << numNs << endl;
358 outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
363 if((pDataArray->count) % 1000 == 0){ pDataArray->m->mothurOut(toString(pDataArray->count)); pDataArray->m->mothurOutEndLine(); }
367 if((pDataArray->count) % 1000 != 0){ pDataArray->m->mothurOut(toString(pDataArray->count)); pDataArray->m->mothurOutEndLine(); }
373 outScrapFasta.close();
374 if (thisfqualfile != "") {
380 pDataArray->done = true;
381 if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(pDataArray->outputFasta); pDataArray->m->mothurRemove(pDataArray->outputMisMatches); pDataArray->m->mothurRemove(pDataArray->outputScrapFasta); }
386 catch(exception& e) {
387 pDataArray->m->errorOut(e, "AlignCommand", "MyContigsThreadFunction");