2 * chimeraseqscommand.cpp
5 * Created by Sarah Westcott on 6/29/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "chimeraseqscommand.h"
12 //***************************************************************************************************************
14 ChimeraSeqsCommand::ChimeraSeqsCommand(string option){
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"fasta", "filter", "correction", "processors", "method" };
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string,string> parameters = parser.getParameters();
29 ValidParameters validParameter;
31 //check to make sure all parameters are valid for command
32 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
33 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
36 //check for required parameters
37 fastafile = validParameter.validFile(parameters, "fasta", true);
38 if (fastafile == "not open") { abort = true; }
39 else if (fastafile == "not found") { fastafile = ""; mothurOut("fasta is a required parameter for the chimera.seqs command."); mothurOutEndLine(); abort = true; }
42 temp = validParameter.validFile(parameters, "filter", false); if (temp == "not found") { temp = "F"; }
43 filter = isTrue(temp);
45 temp = validParameter.validFile(parameters, "correction", false); if (temp == "not found") { temp = "T"; }
46 correction = isTrue(temp);
48 temp = validParameter.validFile(parameters, "processors", true); if (temp == "not found") { temp = "1"; }
49 convert(temp, processors);
51 method = validParameter.validFile(parameters, "method", false); if (method == "not found") { method = "bellerophon"; }
56 errorOut(e, "ChimeraSeqsCommand", "ChimeraSeqsCommand");
60 //**********************************************************************************************************************
62 void ChimeraSeqsCommand::help(){
64 mothurOut("The chimera.seqs command reads a fastafile and creates a sorted priority score list of potentially chimeric sequences (ideally, the sequences should already be aligned).\n");
65 mothurOut("The chimera.seqs command parameters are fasta, filter, correction, processors and method. fasta is required.\n");
66 mothurOut("The filter parameter allows you to specify if you would like to apply a 50% soft filter. The default is false. \n");
67 mothurOut("The correction parameter allows you to ..... The default is true. \n");
68 mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
69 mothurOut("The method parameter allows you to specify the method for finding chimeric sequences. The default is bellerophon. \n");
70 mothurOut("The chimera.seqs command should be in the following format: \n");
71 mothurOut("chimera.seqs(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors, method=bellerophon) \n");
72 mothurOut("Example: chimera.seqs(fasta=AD.align, filter=True, correction=true, processors=2, method=yourMethod) \n");
73 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
76 errorOut(e, "ChimeraSeqsCommand", "help");
81 //***************************************************************************************************************
83 ChimeraSeqsCommand::~ChimeraSeqsCommand(){ /* do nothing */ }
85 //***************************************************************************************************************
87 int ChimeraSeqsCommand::execute(){
90 if (abort == true) { return 0; }
94 string optionString = "fasta=" + fastafile + ", soft=50.0, vertical=F";
95 filterSeqs = new FilterSeqsCommand(optionString);
96 filterSeqs->execute();
99 //reset fastafile to filtered file
100 fastafile = getRootName(fastafile) + "filter.fasta";
106 //int numSeqs = seqs.size();
108 //find average midpoint of seqs
109 midpoint = findAverageMidPoint();
111 //this should be parallelized
112 //generatePreferences();
115 //output results to screen
117 mothurOut("\t\t"); mothurOutEndLine();
118 //mothurOut("Minimum:\t" + toString(startPosition[0]) + "\t" + toString(endPosition[0]) + "\t" + toString(seqLength[0]) + "\t" + toString(ambigBases[0]) + "\t" + toString(longHomoPolymer[0])); mothurOutEndLine();
119 //mothurOut("2.5%-tile:\t" + toString(startPosition[ptile0_25]) + "\t" + toString(endPosition[ptile0_25]) + "\t" + toString(seqLength[ptile0_25]) + "\t" + toString(ambigBases[ptile0_25]) + "\t"+ toString(longHomoPolymer[ptile0_25])); mothurOutEndLine();
120 //mothurOut("25%-tile:\t" + toString(startPosition[ptile25]) + "\t" + toString(endPosition[ptile25]) + "\t" + toString(seqLength[ptile25]) + "\t" + toString(ambigBases[ptile25]) + "\t" + toString(longHomoPolymer[ptile25])); mothurOutEndLine();
121 //mothurOut("Median: \t" + toString(startPosition[ptile50]) + "\t" + toString(endPosition[ptile50]) + "\t" + toString(seqLength[ptile50]) + "\t" + toString(ambigBases[ptile50]) + "\t" + toString(longHomoPolymer[ptile50])); mothurOutEndLine();
122 //mothurOut("75%-tile:\t" + toString(startPosition[ptile75]) + "\t" + toString(endPosition[ptile75]) + "\t" + toString(seqLength[ptile75]) + "\t" + toString(ambigBases[ptile75]) + "\t" + toString(longHomoPolymer[ptile75])); mothurOutEndLine();
123 //mothurOut("97.5%-tile:\t" + toString(startPosition[ptile97_5]) + "\t" + toString(endPosition[ptile97_5]) + "\t" + toString(seqLength[ptile97_5]) + "\t" + toString(ambigBases[ptile97_5]) + "\t" + toString(longHomoPolymer[ptile97_5])); mothurOutEndLine();
124 //mothurOut("Maximum:\t" + toString(startPosition[ptile100]) + "\t" + toString(endPosition[ptile100]) + "\t" + toString(seqLength[ptile100]) + "\t" + toString(ambigBases[ptile100]) + "\t" + toString(longHomoPolymer[ptile100])); mothurOutEndLine();
125 //mothurOut("# of Seqs:\t" + toString(numSeqs)); mothurOutEndLine();
127 //outSummary.close();
130 catch(exception& e) {
131 errorOut(e, "ChimeraSeqsCommand", "execute");
136 //***************************************************************************************************************
137 void ChimeraSeqsCommand::readSeqs(){
140 openInputFile(fastafile, inFASTA);
142 //read in seqs and store in vector
143 while(!inFASTA.eof()){
144 Sequence current(inFASTA);
146 seqs.push_back(current);
153 catch(exception& e) {
154 errorOut(e, "ChimeraSeqsCommand", "readSeqs");
160 //***************************************************************************************************************
161 int ChimeraSeqsCommand::findAverageMidPoint(){
166 //loop through the seqs and find midpoint
167 for (int i = 0; i < seqs.size(); i++) {
169 //get unaligned sequence
170 seqs[i].setUnaligned(seqs[i].getUnaligned()); //if you read an aligned file the unaligned is really aligned, so we need to make sure its unaligned
172 string unaligned = seqs[i].getUnaligned();
173 string aligned = seqs[i].getAligned();
175 //find midpoint of this seq
178 for (int j = 0; j < aligned.length(); j++) {
182 //if you are part of the unaligned sequence increment
183 if (isalpha(aligned[j])) { count++; }
185 //if you have reached the halfway point stop
186 if (count >= (unaligned.length() / 2)) { break; }
189 //add this mid to total
190 totalMids += thismid;
194 averageMid = (totalMids / seqs.size());
200 catch(exception& e) {
201 errorOut(e, "ChimeraSeqsCommand", "findAverageMidPoint");
206 /***************************************************************************************************************
207 int ChimeraSeqsCommand::createSparseMatrix(int startLine, int endLine, SparseMatrix* sparse){
210 for(int i=startLine; i<endLine; i++){
212 for(int j=0;j<i;j++){
214 distCalculator->calcDist(seqs.get(i), seqs.get(j));
215 double dist = distCalculator->getDist();
224 catch(exception& e) {
225 errorOut(e, "ChimeraSeqsCommand", "createSparseMatrix");
229 /**************************************************************************************************/