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