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 //create 2 vectors of sequences, 1 for left side and one for right side
112 vector<Sequence> left; vector<Sequence> right;
114 for (int i = 0; i < seqs.size(); i++) {
116 string seqLeft = seqs[i].getAligned();
117 seqLeft = seqLeft.substr(0, midpoint);
118 Sequence tempLeft(seqs[i].getName(), seqLeft);
119 left.push_back(tempLeft);
122 string seqRight = seqs[i].getAligned();
123 seqRight = seqRight.substr(midpoint+1, (seqRight.length()-midpoint-1));
124 Sequence tempRight(seqs[i].getName(), seqRight);
125 right.push_back(tempRight);
128 //this should be parallelized
129 //perference = sum of (| distance of my left to sequence j's left - distance of my right to sequence j's right | )
130 //create a matrix containing the distance from left to left and right to right
131 //calculate distances
132 SparseMatrix* SparseLeft = new SparseMatrix();
133 SparseMatrix* SparseRight = new SparseMatrix();
135 createSparseMatrix(0, left.size(), SparseLeft, left);
136 createSparseMatrix(0, right.size(), SparseRight, right);
139 //vector<SeqMap> distMapRight;
140 //vector<SeqMap> distMapLeft;
142 // Create a data structure to quickly access the distance information.
143 // It consists of a vector of distance maps, where each map contains
144 // all distances of a certain sequence. Vector and maps are accessed
145 // via the index of a sequence in the distance matrix
146 //distMapRight = vector<SeqMap>(globaldata->gListVector->size());
147 //distMapLeft = vector<SeqMap>(globaldata->gListVector->size());
148 for (MatData currentCell = SparseLeft->begin(); currentCell != SparseLeft->end(); currentCell++) {
149 //distMapLeft[currentCell->row][currentCell->column] = currentCell->dist;
151 for (MatData currentCell = SparseRight->begin(); currentCell != SparseRight->end(); currentCell++) {
152 //distMapRight[currentCell->row][currentCell->column] = currentCell->dist;
156 //fill preference structure
157 //generatePreferences(distMapLeft, distMapRight);
160 //output results to screen
162 mothurOut("\t\t"); mothurOutEndLine();
163 //mothurOut("Minimum:\t" + toString(startPosition[0]) + "\t" + toString(endPosition[0]) + "\t" + toString(seqLength[0]) + "\t" + toString(ambigBases[0]) + "\t" + toString(longHomoPolymer[0])); mothurOutEndLine();
164 //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();
165 //mothurOut("25%-tile:\t" + toString(startPosition[ptile25]) + "\t" + toString(endPosition[ptile25]) + "\t" + toString(seqLength[ptile25]) + "\t" + toString(ambigBases[ptile25]) + "\t" + toString(longHomoPolymer[ptile25])); mothurOutEndLine();
166 //mothurOut("Median: \t" + toString(startPosition[ptile50]) + "\t" + toString(endPosition[ptile50]) + "\t" + toString(seqLength[ptile50]) + "\t" + toString(ambigBases[ptile50]) + "\t" + toString(longHomoPolymer[ptile50])); mothurOutEndLine();
167 //mothurOut("75%-tile:\t" + toString(startPosition[ptile75]) + "\t" + toString(endPosition[ptile75]) + "\t" + toString(seqLength[ptile75]) + "\t" + toString(ambigBases[ptile75]) + "\t" + toString(longHomoPolymer[ptile75])); mothurOutEndLine();
168 //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();
169 //mothurOut("Maximum:\t" + toString(startPosition[ptile100]) + "\t" + toString(endPosition[ptile100]) + "\t" + toString(seqLength[ptile100]) + "\t" + toString(ambigBases[ptile100]) + "\t" + toString(longHomoPolymer[ptile100])); mothurOutEndLine();
170 //mothurOut("# of Seqs:\t" + toString(numSeqs)); mothurOutEndLine();
172 //outSummary.close();
175 catch(exception& e) {
176 errorOut(e, "ChimeraSeqsCommand", "execute");
181 //***************************************************************************************************************
182 void ChimeraSeqsCommand::readSeqs(){
185 openInputFile(fastafile, inFASTA);
187 //read in seqs and store in vector
188 while(!inFASTA.eof()){
189 Sequence current(inFASTA);
191 seqs.push_back(current);
198 catch(exception& e) {
199 errorOut(e, "ChimeraSeqsCommand", "readSeqs");
205 //***************************************************************************************************************
206 int ChimeraSeqsCommand::findAverageMidPoint(){
211 //loop through the seqs and find midpoint
212 for (int i = 0; i < seqs.size(); i++) {
214 //get unaligned sequence
215 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
217 string unaligned = seqs[i].getUnaligned();
218 string aligned = seqs[i].getAligned();
220 //find midpoint of this seq
223 for (int j = 0; j < aligned.length(); j++) {
227 //if you are part of the unaligned sequence increment
228 if (isalpha(aligned[j])) { count++; }
230 //if you have reached the halfway point stop
231 if (count >= (unaligned.length() / 2)) { break; }
234 //add this mid to total
235 totalMids += thismid;
239 averageMid = (totalMids / seqs.size());
245 catch(exception& e) {
246 errorOut(e, "ChimeraSeqsCommand", "findAverageMidPoint");
251 /***************************************************************************************************************/
252 int ChimeraSeqsCommand::createSparseMatrix(int startSeq, int endSeq, SparseMatrix* sparse, vector<Sequence> s){
255 for(int i=startSeq; i<endSeq; i++){
257 for(int j=0;j<i;j++){
259 //distCalculator->calcDist(s.get(i), s.get(j));
260 float dist = distCalculator->getDist();
262 PCell temp(i, j, dist);
263 sparse->addCell(temp);
271 catch(exception& e) {
272 errorOut(e, "ChimeraSeqsCommand", "createSparseMatrix");
276 /***************************************************************************************************************
277 void ChimeraSeqsCommand::generatePreferences(vector<SeqMap> left, vector<SeqMap> right){
280 for (int i = 0; i < left.size(); i++) {
283 float closestLeft = 100000.0;
284 float closestRight = 100000.0;
286 for (int j = 0; j < left.size(); j++) {
295 catch(exception& e) {
296 errorOut(e, "ChimeraSeqsCommand", "generatePreferences");
300 /**************************************************************************************************/
302 /**************************************************************************************************/