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).
18 * for basic instructions on how to run nastPlus type:
22 #include "aligncommand.h"
23 #include "sequence.hpp"
25 #include "alignment.hpp"
26 #include "gotohoverlap.hpp"
27 #include "needlemanoverlap.hpp"
28 #include "blastalign.hpp"
29 #include "noalign.hpp"
31 #include "database.hpp"
33 #include "suffixdb.hpp"
34 #include "blastdb.hpp"
37 #include "nastreport.hpp"
40 //**********************************************************************************************************************
41 AlignCommand::AlignCommand(string option){
43 globaldata = GlobalData::getInstance();
46 //allow user to run help
47 if(option == "help") { help(); abort = true; }
50 //valid paramters for this command
51 string AlignArray[] = {"fasta","candidate","search","ksize","align","match","mismatch","gapopen","gapextend"};
52 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
54 parser = new OptionParser();
56 parser->parse(option, parameters); delete parser;
58 ValidParameters* validParameter = new ValidParameters();
60 //check to make sure all parameters are valid for command
61 for (it = parameters.begin(); it != parameters.end(); it++) {
62 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
65 //check for required parameters
66 templateFileName = validParameter->validFile(parameters, "fasta", true);
67 if (templateFileName == "not found") { cout << "fasta is a required parameter for the align.seqs command." << endl; abort = true; }
68 else if (templateFileName == "not open") { abort = true; }
69 else { globaldata->setFastaFile(templateFileName); }
71 candidateFileName = validParameter->validFile(parameters, "candidate", true);
72 if (candidateFileName == "not found") { cout << "candidate is a required parameter for the align.seqs command." << endl; abort = true; }
73 else if (candidateFileName == "not open") { abort = true; }
75 globaldata->setCandidateFile(candidateFileName);
76 openInputFile(candidateFileName, in);
80 //check for optional parameter and set defaults
81 // ...at some point should added some additional type checking...
83 temp = validParameter->validFile(parameters, "ksize", false); if (temp == "not found") { temp = "8"; }
84 convert(temp, kmerSize);
86 temp = validParameter->validFile(parameters, "match", false); if (temp == "not found") { temp = "1.0"; }
89 temp = validParameter->validFile(parameters, "mismatch", false); if (temp == "not found") { temp = "-1.0"; }
90 convert(temp, misMatch);
92 temp = validParameter->validFile(parameters, "gapopen", false); if (temp == "not found") { temp = "-1.0"; }
93 convert(temp, gapOpen);
95 temp = validParameter->validFile(parameters, "gapextend", false); if (temp == "not found") { temp = "-2.0"; }
96 convert(temp, gapExtend);
98 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"; }
101 delete validParameter;
105 catch(exception& e) {
106 cout << "Standard Error: " << e.what() << " has occurred in the AlignCommand class Function AlignCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110 cout << "An unknown error has occurred in the AlignCommand class function AlignCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
115 //**********************************************************************************************************************
117 AlignCommand::~AlignCommand(){
120 //**********************************************************************************************************************
122 void AlignCommand::help(){
124 cout << "The align.seqs command reads a file containing sequences and creates an alignment file and a report file." << "\n";
125 cout << "The align.seqs command parameters are fasta, candidate, search, ksize, align, match, mismatch, gapopen and gapextend. " << "\n";
126 cout << "The fasta and candidate parameters are required." << "\n";
127 cout << "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 cout << "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 cout << "The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 7." << "\n";
130 cout << "The match parameter allows you to specify the bonus for having the same base. The default is 1.0." << "\n";
131 cout << "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0." << "\n";
132 cout << "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -1.0." << "\n";
133 cout << "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -2.0." << "\n";
134 cout << "The align.seqs command should be in the following format: " << "\n";
135 cout << "align.seqs(fasta=yourTemplateFile, candidate=yourCandidateFile, align=yourAlignmentMethod, search=yourSearchmethod, ksize=yourKmerSize, match=yourMatchBonus, mismatch=yourMismatchpenalty, gapopen=yourGapopenPenalty, gapextend=yourGapExtendPenalty) " << "\n";
136 cout << "Example align.seqs(candidate=candidate.fasta, fasta=core.filtered, align=kmer, search=gotoh, ksize=8, match=2.0, mismatch=3.0, gapopen=-2.0, gapextend=-1.0)" << "\n";
137 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile)." << "\n" << "\n";
139 catch(exception& e) {
140 cout << "Standard Error: " << e.what() << " has occurred in the AlignCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144 cout << "An unknown error has occurred in the AlignCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150 //**********************************************************************************************************************
152 int AlignCommand::execute(){
154 if (abort == true) { return 0; }
156 srand( (unsigned)time( NULL ) ); //needed to assign names to temporary files
158 Database* templateDB;
159 if(search == "kmer") { templateDB = new KmerDB(templateFileName, kmerSize); }
160 else if(search == "suffix") { templateDB = new SuffixDB(templateFileName); }
161 else if(search == "blast") { templateDB = new BlastDB(templateFileName, gapOpen, gapExtend, match, misMatch); }
163 cout << search << " is not a valid search option. I will run the command using kmer, ksize=8." << endl; kmerSize = 8;
164 templateDB = new KmerDB(templateFileName, kmerSize);
167 Alignment* alignment;
168 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, 3000); }
169 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, 3000); }
170 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
171 else if(align == "noalign") { alignment = new NoAlign(); }
173 cout << align << " is not a valid alignment option. I will run the command using needleman." << endl;
174 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, 3000);
177 int numFastaSeqs=count(istreambuf_iterator<char>(in),istreambuf_iterator<char>(), '>');
180 string candidateAligngmentFName = candidateFileName.substr(0,candidateFileName.find_last_of(".")+1) + "align";
181 ofstream candidateAlignmentFile;
182 openOutputFile(candidateAligngmentFName, candidateAlignmentFile);
184 string candidateReportFName = candidateFileName.substr(0,candidateFileName.find_last_of(".")+1) + "align.report";
185 NastReport report(candidateReportFName);
187 cout << "We are going to align the " << numFastaSeqs << " sequences in " << candidateFileName << "..." << endl;
190 int start = time(NULL);
192 for(int i=0;i<numFastaSeqs;i++){
194 Sequence* candidateSeq = new Sequence(in);
195 report.setCandidate(candidateSeq);
197 Sequence* templateSeq = templateDB->findClosestSequence(candidateSeq);
198 report.setTemplate(templateSeq);
199 report.setSearchParameters(search, templateDB->getSearchScore());
202 Nast nast(alignment, candidateSeq, templateSeq);
203 report.setAlignmentParameters(align, alignment);
204 report.setNastParameters(nast);
206 candidateAlignmentFile << '>' << candidateSeq->getName() << '\n' << candidateSeq->getAligned() << endl;
207 candidateAlignmentFile.flush();
209 if((i+1) % 100 == 0){
210 cout << "It has taken " << time(NULL) - start << " secs to align " << i+1 << " sequences" << endl;
217 cout << "It took " << time(NULL) - start << " secs to align " << numFastaSeqs << " sequences" << endl;
226 catch(exception& e) {
227 cout << "Standard Error: " << e.what() << " has occurred in the AlignCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
231 cout << "An unknown error has occurred in the AlignCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236 //**********************************************************************************************************************