]> git.donarmstrong.com Git - mothur.git/blob - aligncommand.h
paralellized chimera.uchime for windows for both by group and with a template.
[mothur.git] / aligncommand.h
1 #ifndef ALIGNCOMMAND_H
2 #define ALIGNCOMMAND_H
3
4 /*
5  *  aligncommand.h
6  *  Mothur
7  *
8  *  Created by Sarah Westcott on 5/15/09.
9  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10  *
11  */
12
13 #include "mothur.h"
14 #include "command.hpp"
15 #include "database.hpp"
16 #include "alignment.hpp"
17 #include "alignmentdb.h"
18 #include "sequence.hpp"
19
20 #include "gotohoverlap.hpp"
21 #include "needlemanoverlap.hpp"
22 #include "blastalign.hpp"
23 #include "noalign.hpp"
24
25 #include "nast.hpp"
26 #include "nastreport.hpp"
27
28
29 class AlignCommand : public Command {
30         
31 public:
32         AlignCommand(string);   
33         AlignCommand();
34         ~AlignCommand();
35         
36         vector<string> setParameters();
37         string getCommandName()                 { return "align.seqs";                  }
38         string getCommandCategory()             { return "Sequence Processing"; }
39         string getHelpString(); 
40         string getCitation() { return "DeSantis TZ, Jr., Hugenholtz P, Keller K, Brodie EL, Larsen N, Piceno YM, Phan R, Andersen GL (2006). NAST: a multiple sequence alignment server for comparative analysis of 16S rRNA genes. Nucleic Acids Res 34: W394-9.\nSchloss PD (2009). A high-throughput DNA sequence aligner for microbial ecology studies. PLoS ONE 4: e8230.\nSchloss PD (2010). The effects of alignment quality, distance calculation method, sequence filtering, and region on the analysis of 16S rRNA gene-based studies. PLoS Comput Biol 6: e1000844.\nhttp://www.mothur.org/wiki/Align.seqs http://www.mothur.org/wiki/Align.seqs"; }
41         string getDescription()         { return "align sequences"; }
42         
43         int execute(); 
44         void help() { m->mothurOut(getHelpString()); }  
45         
46 private:
47         struct linePair {
48                 unsigned long long start;
49                 unsigned long long end;
50                 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
51         };
52         vector<int> processIDS;   //processid
53         vector<linePair*> lines;
54         bool MPIWroteAccnos;
55         
56         AlignmentDB* templateDB;
57         
58         int driver(linePair*, string, string, string, string);
59         int createProcesses(string, string, string, string);
60         void appendAlignFiles(string, string); 
61         void appendReportFiles(string, string);
62         
63         #ifdef USE_MPI
64         int driverMPI(int, int, MPI_File&, MPI_File&, MPI_File&, MPI_File&, vector<unsigned long long>&);
65         #endif
66         
67         string candidateFileName, templateFileName, distanceFileName, search, align, outputDir;
68         float match, misMatch, gapOpen, gapExtend, threshold;
69         int processors, kmerSize;
70         vector<string> candidateFileNames;
71         vector<string> outputNames;
72         
73         bool abort, flip, calledHelp, save;
74
75 };
76
77 /**************************************************************************************************/
78 //custom data structure for threads to use.
79 // This is passed by void pointer so it can be any data type
80 // that can be passed using a single void pointer (LPVOID).
81 struct alignData {
82         string alignFName; 
83         string reportFName; 
84         string accnosFName;
85         string filename;
86         string align;
87         string search;
88         bool flip;
89         unsigned long long start;
90         unsigned long long end;
91         MothurOut* m;
92         //AlignmentDB* templateDB;
93         float match, misMatch, gapOpen, gapExtend, threshold;
94         int count, kmerSize, threadID;
95         
96         alignData(){}
97         alignData(string a, string r, string ac, string f, string al, string se, int ks, MothurOut* mout, unsigned long long st, unsigned long long en, bool fl, float ma, float misMa, float gapO, float gapE, float thr, int tid) {
98                 alignFName = a;
99                 reportFName = r;
100                 accnosFName = ac;
101                 filename = f;
102                 flip = fl;
103                 m = mout;
104                 start = st;
105                 end = en;
106                 //templateDB = tdb;
107                 match = ma; 
108                 misMatch = misMa;
109                 gapOpen = gapO; 
110                 gapExtend = gapE; 
111                 threshold = thr;
112                 align = al;
113                 search = se;
114                 count = 0;
115                 kmerSize = ks;
116                 threadID = tid;
117         }
118 };
119
120 /**************************************************************************************************/
121 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
122 #else
123 static DWORD WINAPI MyAlignThreadFunction(LPVOID lpParam){ 
124         alignData* pDataArray;
125         pDataArray = (alignData*)lpParam;
126         
127         try {
128                 ofstream alignmentFile;
129                 pDataArray->m->openOutputFile(pDataArray->alignFName, alignmentFile);
130                 
131                 ofstream accnosFile;
132                 pDataArray->m->openOutputFile(pDataArray->accnosFName, accnosFile);
133                 
134                 NastReport report(pDataArray->reportFName);
135                 
136                 ifstream inFASTA;
137                 pDataArray->m->openInputFile(pDataArray->filename, inFASTA);
138                 
139                 //print header if you are process 0
140                 if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
141                         inFASTA.seekg(0);
142                 }else { //this accounts for the difference in line endings. 
143                         inFASTA.seekg(pDataArray->start-1); pDataArray->m->gobble(inFASTA); 
144                 }
145                 
146                 pDataArray->count = pDataArray->end;
147                 
148                 AlignmentDB* templateDB = new AlignmentDB("saved-silent", pDataArray->search, pDataArray->kmerSize, pDataArray->gapOpen, pDataArray->gapExtend, pDataArray->match, pDataArray->misMatch, pDataArray->threadID);
149                 
150                 //moved this into driver to avoid deep copies in windows paralellized version
151                 Alignment* alignment;
152                 int longestBase = templateDB->getLongestBase();
153                 if(pDataArray->align == "gotoh")                        {       alignment = new GotohOverlap(pDataArray->gapOpen, pDataArray->gapExtend, pDataArray->match, pDataArray->misMatch, longestBase);                 }
154                 else if(pDataArray->align == "needleman")       {       alignment = new NeedlemanOverlap(pDataArray->gapOpen, pDataArray->match, pDataArray->misMatch, longestBase);                            }
155                 else if(pDataArray->align == "blast")           {       alignment = new BlastAlignment(pDataArray->gapOpen, pDataArray->gapExtend, pDataArray->match, pDataArray->misMatch);            }
156                 else if(pDataArray->align == "noalign")         {       alignment = new NoAlign();                                                                                                      }
157                 else {
158                         pDataArray->m->mothurOut(pDataArray->align + " is not a valid alignment option. I will run the command using needleman.");
159                         pDataArray->m->mothurOutEndLine();
160                         alignment = new NeedlemanOverlap(pDataArray->gapOpen, pDataArray->match, pDataArray->misMatch, longestBase);
161                 }
162                 
163                 int count = 0;
164                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
165                         
166                         if (pDataArray->m->control_pressed) {  break; }
167                         
168                         Sequence* candidateSeq = new Sequence(inFASTA);  pDataArray->m->gobble(inFASTA);
169                         report.setCandidate(candidateSeq);
170                         
171                         int origNumBases = candidateSeq->getNumBases();
172                         string originalUnaligned = candidateSeq->getUnaligned();
173                         int numBasesNeeded = origNumBases * pDataArray->threshold;
174                         
175                         if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
176                                 if (candidateSeq->getUnaligned().length() > alignment->getnRows()) {
177                                         alignment->resize(candidateSeq->getUnaligned().length()+1);
178                                 }
179                                 
180                                 Sequence temp = templateDB->findClosestSequence(candidateSeq);
181                                 Sequence* templateSeq = &temp;
182                                 
183                                 float searchScore = templateDB->getSearchScore();
184                                 
185                                 Nast* nast = new Nast(alignment, candidateSeq, templateSeq);
186                                 
187                                 Sequence* copy;
188                                 
189                                 Nast* nast2;
190                                 bool needToDeleteCopy = false;  //this is needed in case you have you enter the ifs below
191                                 //since nast does not make a copy of hte sequence passed, and it is used by the reporter below
192                                 //you can't delete the copy sequence til after you report, but you may choose not to create it in the first place
193                                 //so this bool tells you if you need to delete it
194                                 
195                                 //if there is a possibility that this sequence should be reversed
196                                 if (candidateSeq->getNumBases() < numBasesNeeded) {
197                                         
198                                         string wasBetter =  "";
199                                         //if the user wants you to try the reverse
200                                         if (pDataArray->flip) {
201                                                 
202                                                 //get reverse compliment
203                                                 copy = new Sequence(candidateSeq->getName(), originalUnaligned);
204                                                 copy->reverseComplement();
205                                                 
206                                                 //rerun alignment
207                                                 Sequence temp2 = templateDB->findClosestSequence(copy);
208                                                 Sequence* templateSeq2 = &temp2;
209                                                 
210                                                 searchScore = templateDB->getSearchScore();
211                                                 
212                                                 nast2 = new Nast(alignment, copy, templateSeq2);
213                                                 
214                                                 //check if any better
215                                                 if (copy->getNumBases() > candidateSeq->getNumBases()) {
216                                                         candidateSeq->setAligned(copy->getAligned());  //use reverse compliments alignment since its better
217                                                         templateSeq = templateSeq2; 
218                                                         delete nast;
219                                                         nast = nast2;
220                                                         needToDeleteCopy = true;
221                                                         wasBetter = "\treverse complement produced a better alignment, so mothur used the reverse complement.";
222                                                 }else{  
223                                                         wasBetter = "\treverse complement did NOT produce a better alignment so it was not used, please check sequence.";
224                                                         delete nast2;
225                                                         delete copy;    
226                                                 }
227                                         }
228                                         
229                                         //create accnos file with names
230                                         accnosFile << candidateSeq->getName() << wasBetter << endl;
231                                 }
232                                 
233                                 report.setTemplate(templateSeq);
234                                 report.setSearchParameters(pDataArray->search, searchScore);
235                                 report.setAlignmentParameters(pDataArray->align, alignment);
236                                 report.setNastParameters(*nast);
237                                 
238                                 alignmentFile << '>' << candidateSeq->getName() << '\n' << candidateSeq->getAligned() << endl;
239                                 
240                                 report.print();
241                                 delete nast;
242                                 if (needToDeleteCopy) {   delete copy;   }
243                                 
244                                 count++;
245                         }
246                         delete candidateSeq;
247                         
248                         //report progress
249                         if((count) % 100 == 0){ pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine();           }
250                         
251                 }
252                 //report progress
253                 if((count) % 100 != 0){ pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine();           }
254                 
255                 delete alignment;
256                 delete templateDB;
257                 alignmentFile.close();
258                 inFASTA.close();
259                 accnosFile.close();
260         }
261         catch(exception& e) {
262                 pDataArray->m->errorOut(e, "AlignCommand", "MyAlignThreadFunction");
263                 exit(1);
264         }
265
266 #endif
267
268
269
270 #endif