1 #ifndef CHIMERAPERSEUSCOMMAND_H
2 #define CHIMERAPERSEUSCOMMAND_H
6 * chimeraperseuscommand.h
9 * Created by westcott on 10/26/11.
10 * Copyright 2011 Schloss Lab. All rights reserved.
17 #include "command.hpp"
18 #include "sequenceparser.h"
19 #include "myPerseus.h"
21 /***********************************************************/
22 class ChimeraPerseusCommand : public Command {
24 ChimeraPerseusCommand(string);
25 ChimeraPerseusCommand();
26 ~ChimeraPerseusCommand() {}
28 vector<string> setParameters();
29 string getCommandName() { return "chimera.perseus"; }
30 string getCommandCategory() { return "Sequence Processing"; }
31 string getHelpString();
32 string getCitation() { return "http://www.mothur.org/wiki/Chimera.perseus\n"; }
33 string getDescription() { return "detect chimeric sequences"; }
36 void help() { m->mothurOut(getHelpString()); }
42 linePair(int i, int j) : start(i), end(j) {}
46 string fastafile, groupfile, outputDir, namefile;
48 double cutoff, alpha, beta;
50 vector<string> outputNames;
51 vector<string> fastaFileNames;
52 vector<string> nameFileNames;
53 vector<string> groupFileNames;
55 string getNamesFile(string&);
56 int driver(string, vector<seqData>&, string, int&);
57 vector<seqData> readFiles(string, string);
58 vector<seqData> loadSequences(SequenceParser&, string);
59 int deconvoluteResults(SequenceParser&, string, string);
60 int driverGroups(SequenceParser&, string, string, int, int, vector<string>);
61 int createProcessesGroups(SequenceParser&, string, string, vector<string>, string, string, string);
64 /**************************************************************************************************/
65 //custom data structure for threads to use.
66 // This is passed by void pointer so it can be any data type
67 // that can be passed using a single void pointer (LPVOID).
77 int threadID, count, numChimeras;
78 double alpha, beta, cutoff;
79 vector<string> groups;
82 perseusData(double a, double b, double c, string o, string f, string n, string g, string ac, vector<string> gr, MothurOut* mout, int st, int en, int tid) {
100 /**************************************************************************************************/
101 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
103 static DWORD WINAPI MyPerseusThreadFunction(LPVOID lpParam){
104 perseusData* pDataArray;
105 pDataArray = (perseusData*)lpParam;
110 ofstream out, out1, out2;
111 pDataArray->m->openOutputFile(pDataArray->outputFName, out); out.close();
112 pDataArray->m->openOutputFile(pDataArray->accnos, out1); out1.close();
114 //parse fasta and name file by group
115 SequenceParser* parser;
116 if (pDataArray->namefile != "") { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile); }
117 else { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile); }
122 for (int i = pDataArray->start; i < pDataArray->end; i++) {
124 int start = time(NULL); if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); return 0; }
126 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Checking sequences from group " + pDataArray->groups[i] + "..."); pDataArray->m->mothurOutEndLine();
128 //vector<seqData> sequences = loadSequences(parser, groups[i]); - same function below
129 ////////////////////////////////////////////////////////////////////////////////////////
130 vector<Sequence> thisGroupsSeqs = parser->getSeqs(pDataArray->groups[i]);
131 map<string, string> nameMap = parser->getNameMap(pDataArray->groups[i]);
132 map<string, string>::iterator it;
134 vector<seqData> sequences;
137 for (int j = 0; j < thisGroupsSeqs.size(); j++) {
139 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); return 0; }
141 it = nameMap.find(thisGroupsSeqs[j].getName());
142 if (it == nameMap.end()) { error = true; pDataArray->m->mothurOut("[ERROR]: " + thisGroupsSeqs[j].getName() + " is in your fasta file and not in your namefile, please correct."); pDataArray->m->mothurOutEndLine(); }
144 int num = pDataArray->m->getNumNames(it->second);
145 sequences.push_back(seqData(thisGroupsSeqs[j].getName(), thisGroupsSeqs[j].getUnaligned(), num));
149 if (error) { pDataArray->m->control_pressed = true; }
152 sort(sequences.rbegin(), sequences.rend());
153 ////////////////////////////////////////////////////////////////////////////////////////
155 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); return 0; }
157 //int numSeqs = driver((outputFName + groups[i]), sequences, (accnos+groups[i]), numChimeras); - same function below
158 ////////////////////////////////////////////////////////////////////////////////////////
159 string chimeraFileName = pDataArray->outputFName+pDataArray->groups[i];
160 string accnosFileName = pDataArray->accnos+pDataArray->groups[i];
162 vector<vector<double> > correctModel(4); //could be an option in the future to input own model matrix
163 for(int j=0;j<4;j++){ correctModel[j].resize(4); }
165 correctModel[0][0] = 0.000000; //AA
166 correctModel[1][0] = 11.619259; //CA
167 correctModel[2][0] = 11.694004; //TA
168 correctModel[3][0] = 7.748623; //GA
170 correctModel[1][1] = 0.000000; //CC
171 correctModel[2][1] = 7.619657; //TC
172 correctModel[3][1] = 12.852562; //GC
174 correctModel[2][2] = 0.000000; //TT
175 correctModel[3][2] = 10.964048; //TG
177 correctModel[3][3] = 0.000000; //GG
179 for(int k=0;k<4;k++){
180 for(int j=0;j<k;j++){
181 correctModel[j][k] = correctModel[k][j];
185 int numSeqs = sequences.size();
186 int alignLength = sequences[0].sequence.size();
188 ofstream chimeraFile;
190 pDataArray->m->openOutputFile(chimeraFileName, chimeraFile);
191 pDataArray->m->openOutputFile(accnosFileName, accnosFile);
194 vector<vector<double> > binMatrix = myPerseus.binomial(alignLength);
196 chimeraFile << "SequenceIndex\tName\tDiffsToBestMatch\tBestMatchIndex\tBestMatchName\tDiffstToChimera\tIndexofLeftParent\tIndexOfRightParent\tNameOfLeftParent\tNameOfRightParent\tDistanceToBestMatch\tcIndex\t(cIndex - singleDist)\tloonIndex\tMismatchesToChimera\tMismatchToTrimera\tChimeraBreakPoint\tLogisticProbability\tTypeOfSequence\n";
198 vector<bool> chimeras(numSeqs, 0);
200 for(int j=0;j<numSeqs;j++){
202 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
204 vector<bool> restricted = chimeras;
206 vector<vector<int> > leftDiffs(numSeqs);
207 vector<vector<int> > leftMaps(numSeqs);
208 vector<vector<int> > rightDiffs(numSeqs);
209 vector<vector<int> > rightMaps(numSeqs);
211 vector<int> singleLeft, bestLeft;
212 vector<int> singleRight, bestRight;
214 int bestSingleIndex, bestSingleDiff;
215 vector<pwAlign> alignments(numSeqs);
217 int comparisons = myPerseus.getAlignments(j, sequences, alignments, leftDiffs, leftMaps, rightDiffs, rightMaps, bestSingleIndex, bestSingleDiff, restricted);
219 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
221 int minMismatchToChimera, leftParentBi, rightParentBi, breakPointBi;
223 string dummyA, dummyB;
225 if(comparisons >= 2){
226 minMismatchToChimera = myPerseus.getChimera(sequences, leftDiffs, rightDiffs, leftParentBi, rightParentBi, breakPointBi, singleLeft, bestLeft, singleRight, bestRight, restricted);
228 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
230 int minMismatchToTrimera = numeric_limits<int>::max();
231 int leftParentTri, middleParentTri, rightParentTri, breakPointTriA, breakPointTriB;
233 if(minMismatchToChimera >= 3 && comparisons >= 3){
234 minMismatchToTrimera = myPerseus.getTrimera(sequences, leftDiffs, leftParentTri, middleParentTri, rightParentTri, breakPointTriA, breakPointTriB, singleLeft, bestLeft, singleRight, bestRight, restricted);
236 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
239 double singleDist = myPerseus.modeledPairwiseAlignSeqs(sequences[j].sequence, sequences[bestSingleIndex].sequence, dummyA, dummyB, correctModel);
241 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
244 string chimeraRefSeq;
246 if(minMismatchToChimera - minMismatchToTrimera >= 3){
248 chimeraRefSeq = myPerseus.stitchTrimera(alignments, leftParentTri, middleParentTri, rightParentTri, breakPointTriA, breakPointTriB, leftMaps, rightMaps);
252 chimeraRefSeq = myPerseus.stitchBimera(alignments, leftParentBi, rightParentBi, breakPointBi, leftMaps, rightMaps);
255 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
257 double chimeraDist = myPerseus.modeledPairwiseAlignSeqs(sequences[j].sequence, chimeraRefSeq, dummyA, dummyB, correctModel);
259 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
261 double cIndex = chimeraDist;//modeledPairwiseAlignSeqs(sequences[j].sequence, chimeraRefSeq);
262 double loonIndex = myPerseus.calcLoonIndex(sequences[j].sequence, sequences[leftParentBi].sequence, sequences[rightParentBi].sequence, breakPointBi, binMatrix);
264 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); chimeraFile.close(); pDataArray->m->mothurRemove(chimeraFileName); accnosFile.close(); pDataArray->m->mothurRemove(accnosFileName); return 0; }
266 chimeraFile << j << '\t' << sequences[j].seqName << '\t' << bestSingleDiff << '\t' << bestSingleIndex << '\t' << sequences[bestSingleIndex].seqName << '\t';
267 chimeraFile << minMismatchToChimera << '\t' << leftParentBi << '\t' << rightParentBi << '\t' << sequences[leftParentBi].seqName << '\t' << sequences[rightParentBi].seqName << '\t';
268 chimeraFile << singleDist << '\t' << cIndex << '\t' << (cIndex - singleDist) << '\t' << loonIndex << '\t';
269 chimeraFile << minMismatchToChimera << '\t' << minMismatchToTrimera << '\t' << breakPointBi << '\t';
271 double probability = myPerseus.classifyChimera(singleDist, cIndex, loonIndex, pDataArray->alpha, pDataArray->beta);
273 chimeraFile << probability << '\t';
275 if(probability > pDataArray->cutoff){
276 chimeraFile << type << endl;
277 accnosFile << sequences[j].seqName << endl;
282 chimeraFile << "good" << endl;
287 chimeraFile << j << '\t' << sequences[j].seqName << "\t0\t0\tNull\t0\t0\t0\tNull\tNull\t0.0\t0.0\t0.0\t0\t0\t0\t0.0\t0.0\tgood" << endl;
290 if((j+1) % 100 == 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(j+1) + "\n"); }
293 if((numSeqs) % 100 != 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(numSeqs) + "\n"); }
297 ////////////////////////////////////////////////////////////////////////////////////////
299 totalSeqs += numSeqs;
302 pDataArray->m->appendFiles(chimeraFileName, pDataArray->outputFName); pDataArray->m->mothurRemove(chimeraFileName);
303 pDataArray->m->appendFiles(accnosFileName, pDataArray->accnos); pDataArray->m->mothurRemove(accnosFileName);
304 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences from group " + pDataArray->groups[i] + "."); pDataArray->m->mothurOutEndLine();
306 if (pDataArray->m->control_pressed) { delete parser; pDataArray->m->mothurRemove(pDataArray->outputFName); pDataArray->m->mothurRemove(pDataArray->accnos); return 0; }
309 pDataArray->count = totalSeqs;
314 catch(exception& e) {
315 pDataArray->m->errorOut(e, "ChimeraUchimeCommand", "MyPerseusThreadFunction");
319 /**************************************************************************************************/