5 * Created by Sarah Westcott on 5/15/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
8 * This version of nast does everything I think that the greengenes nast server does and then some. I have added the
9 * feature of allowing users to define their database, kmer size for searching, alignment penalty values and alignment
10 * method. This latter feature is perhaps most significant. nastPlus enables a user to use either a Needleman-Wunsch
11 * (non-affine gap penalty) or Gotoh (affine gap penalty) pairwise alignment algorithm. This is significant because it
12 * allows for a global alignment and not the local alignment provided by bLAst. Furthermore, it has the potential to
13 * provide a better alignment because of the banding method employed by blast (I'm not sure about this).
17 #include "aligncommand.h"
18 #include "sequence.hpp"
20 #include "gotohoverlap.hpp"
21 #include "needlemanoverlap.hpp"
22 #include "blastalign.hpp"
23 #include "noalign.hpp"
26 #include "suffixdb.hpp"
27 #include "blastdb.hpp"
30 #include "nastreport.hpp"
33 //**********************************************************************************************************************
35 AlignCommand::AlignCommand(string option){
37 // globaldata = GlobalData::getInstance();
40 //allow user to run help
41 if(option == "help") { help(); abort = true; }
45 //valid paramters for this command
46 string AlignArray[] = {"template","candidate","search","ksize","align","match","mismatch","gapopen","gapextend", "processors"};
47 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
49 OptionParser parser(option);
50 map<string, string> parameters = parser.getParameters();
52 ValidParameters validParameter;
54 //check to make sure all parameters are valid for command
55 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
56 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
59 //check for required parameters
60 templateFileName = validParameter.validFile(parameters, "template", true);
61 if (templateFileName == "not found") {
62 mothurOut("template is a required parameter for the align.seqs command.");
66 else if (templateFileName == "not open") { abort = true; }
68 candidateFileName = validParameter.validFile(parameters, "candidate", true);
69 if (candidateFileName == "not found") {
70 mothurOut("candidate is a required parameter for the align.seqs command.");
74 else if (candidateFileName == "not open") { abort = true; }
76 //check for optional parameter and set defaults
77 // ...at some point should added some additional type checking...
79 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
80 convert(temp, kmerSize);
82 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
85 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
86 convert(temp, misMatch);
88 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
89 convert(temp, gapOpen);
91 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
92 convert(temp, gapExtend);
94 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
95 convert(temp, processors);
97 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
99 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
103 catch(exception& e) {
104 errorOut(e, "AlignCommand", "AlignCommand");
109 //**********************************************************************************************************************
111 AlignCommand::~AlignCommand(){
113 if (abort == false) {
114 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
120 //**********************************************************************************************************************
122 void AlignCommand::help(){
124 mothurOut("The align.seqs command reads a file containing sequences and creates an alignment file and a report file.\n");
125 mothurOut("The align.seqs command parameters are template, candidate, search, ksize, align, match, mismatch, gapopen and gapextend.\n");
126 mothurOut("The template and candidate parameters are required.\n");
127 mothurOut("The search parameter allows you to specify the method to find most similar template. Your options are: suffix, kmer and blast. The default is kmer.\n");
128 mothurOut("The align parameter allows you to specify the alignment method to use. Your options are: gotoh, needleman, blast and noalign. The default is needleman.\n");
129 mothurOut("The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 7.\n");
130 mothurOut("The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n");
131 mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n");
132 mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -1.0.\n");
133 mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -2.0.\n");
134 mothurOut("The align.seqs command should be in the following format: \n");
135 mothurOut("align.seqs(template=yourTemplateFile, candidate=yourCandidateFile, align=yourAlignmentMethod, search=yourSearchmethod, ksize=yourKmerSize, match=yourMatchBonus, mismatch=yourMismatchpenalty, gapopen=yourGapopenPenalty, gapextend=yourGapExtendPenalty) \n");
136 mothurOut("Example align.seqs(candidate=candidate.fasta, template=core.filtered, align=kmer, search=gotoh, ksize=8, match=2.0, mismatch=3.0, gapopen=-2.0, gapextend=-1.0)\n");
137 mothurOut("Note: No spaces between parameter labels (i.e. candidate), '=' and parameters (i.e.yourFastaFile).\n\n");
139 catch(exception& e) {
140 errorOut(e, "AlignCommand", "help");
146 //**********************************************************************************************************************
148 int AlignCommand::execute(){
150 if (abort == true) { return 0; }
152 if(search == "kmer") { templateDB = new KmerDB(templateFileName, kmerSize); }
153 else if(search == "suffix") { templateDB = new SuffixDB(templateFileName); }
154 else if(search == "blast") { templateDB = new BlastDB(templateFileName, gapOpen, gapExtend, match, misMatch); }
156 mothurOut(search + " is not a valid search option. I will run the command using kmer, ksize=8.");
160 templateDB = new KmerDB(templateFileName, kmerSize);
163 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, 3000); }
164 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, 3000); }
165 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
166 else if(align == "noalign") { alignment = new NoAlign(); }
168 mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
170 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, 3000);
173 string alignFileName = candidateFileName.substr(0,candidateFileName.find_last_of(".")+1) + "align";
174 string reportFileName = candidateFileName.substr(0,candidateFileName.find_last_of(".")+1) + "align.report";
176 int numFastaSeqs = 0;
177 int start = time(NULL);
179 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
182 openInputFile(candidateFileName, inFASTA);
183 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
186 lines.push_back(new linePair(0, numFastaSeqs));
188 driver(lines[0], alignFileName, reportFileName);
192 vector<int> positions;
193 processIDS.resize(0);
196 openInputFile(candidateFileName, inFASTA);
198 while(!inFASTA.eof()){
199 char c = inFASTA.get();
200 if(c == '>'){ int pos = inFASTA.tellg(); positions.push_back(pos-1); }
201 while (!inFASTA.eof()) { c = inFASTA.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
205 numFastaSeqs = positions.size();
207 int numSeqsPerProcessor = numFastaSeqs / processors;
209 for (int i = 0; i < processors; i++) {
210 int startPos = positions[ i * numSeqsPerProcessor ];
211 if(i == processors - 1){
212 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
214 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
216 createProcesses(alignFileName, reportFileName);
218 rename((alignFileName + toString(processIDS[0]) + ".temp").c_str(), alignFileName.c_str());
219 rename((reportFileName + toString(processIDS[0]) + ".temp").c_str(), reportFileName.c_str());
221 for(int i=1;i<processors;i++){
222 appendAlignFiles((alignFileName + toString(processIDS[i]) + ".temp"), alignFileName);
223 remove((alignFileName + toString(processIDS[i]) + ".temp").c_str());
225 appendReportFiles((reportFileName + toString(processIDS[i]) + ".temp"), reportFileName);
226 remove((reportFileName + toString(processIDS[i]) + ".temp").c_str());
232 openInputFile(candidateFileName, inFASTA);
233 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
236 lines.push_back(new linePair(0, numFastaSeqs));
238 driver(lines[0], alignFileName, reportFileName);
241 mothurOut("It took " + toString(time(NULL) - start) + " secs to align " + toString(numFastaSeqs) + " sequences.");
247 catch(exception& e) {
248 errorOut(e, "AlignCommand", "execute");
253 //**********************************************************************************************************************
255 int AlignCommand::driver(linePair* line, string alignFName, string reportFName){
257 ofstream alignmentFile;
258 openOutputFile(alignFName, alignmentFile);
259 NastReport report(reportFName);
262 openInputFile(candidateFileName, inFASTA);
263 inFASTA.seekg(line->start);
265 for(int i=0;i<line->numSeqs;i++){
267 Sequence* candidateSeq = new Sequence(inFASTA);
268 report.setCandidate(candidateSeq);
270 Sequence temp = templateDB->findClosestSequence(candidateSeq);
271 Sequence* templateSeq = &temp;
273 report.setTemplate(templateSeq);
274 report.setSearchParameters(search, templateDB->getSearchScore());
276 Nast nast(alignment, candidateSeq, templateSeq);
277 report.setAlignmentParameters(align, alignment);
278 report.setNastParameters(nast);
280 alignmentFile << '>' << candidateSeq->getName() << '\n' << candidateSeq->getAligned() << endl;
287 alignmentFile.close();
292 catch(exception& e) {
293 errorOut(e, "AlignCommand", "driver");
298 /**************************************************************************************************/
300 void AlignCommand::createProcesses(string alignFileName, string reportFileName) {
302 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
304 // processIDS.resize(0);
306 //loop through and create all the processes you want
307 while (process != processors) {
311 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
314 driver(lines[process], alignFileName + toString(getpid()) + ".temp", reportFileName + toString(getpid()) + ".temp");
316 }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
319 //force parent to wait until all the processes are done
320 for (int i=0;i<processors;i++) {
321 int temp = processIDS[i];
326 catch(exception& e) {
327 errorOut(e, "AlignCommand", "createProcesses");
332 /**************************************************************************************************/
334 void AlignCommand::appendAlignFiles(string temp, string filename) {
339 openOutputFileAppend(filename, output);
340 openInputFile(temp, input);
342 while(char c = input.get()){
343 if(input.eof()) { break; }
344 else { output << c; }
350 catch(exception& e) {
351 errorOut(e, "AlignCommand", "appendAlignFiles");
356 /**************************************************************************************************/
358 void AlignCommand::appendReportFiles(string temp, string filename) {
363 openOutputFileAppend(filename, output);
364 openInputFile(temp, input);
366 while (!input.eof()) { char c = input.get(); if (c == 10 || c == 13){ break; } } // get header line
368 while(char c = input.get()){
369 if(input.eof()) { break; }
370 else { output << c; }
376 catch(exception& e) {
377 errorOut(e, "AlignCommand", "appendReportFiles");
382 //**********************************************************************************************************************