5 * Created by Pat Schloss on 5/30/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "seqsummarycommand.h"
11 #include "sequence.hpp"
13 //***************************************************************************************************************
15 SeqSummaryCommand::SeqSummaryCommand(string option){
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string,string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 //if the user changes the input directory command factory will send this info to us in the output parameter
39 string inputDir = validParameter.validFile(parameters, "inputdir", false);
40 if (inputDir == "not found"){ inputDir = ""; }
43 it = parameters.find("fasta");
44 //user has given a template file
45 if(it != parameters.end()){
46 path = hasPath(it->second);
47 //if the user has not given a path then, add inputdir. else leave path alone.
48 if (path == "") { parameters["fasta"] = inputDir + it->second; }
52 //check for required parameters
53 fastafile = validParameter.validFile(parameters, "fasta", true);
54 if (fastafile == "not open") { abort = true; }
55 else if (fastafile == "not found") { fastafile = ""; mothurOut("fasta is a required parameter for the summary.seqs command."); mothurOutEndLine(); abort = true; }
57 //if the user changes the output directory command factory will send this info to us in the output parameter
58 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
60 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
66 errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand");
70 //**********************************************************************************************************************
72 void SeqSummaryCommand::help(){
74 mothurOut("The summary.seqs command reads a fastafile and ....\n");
75 mothurOut("The summary.seqs command parameter is fasta and it is required.\n");
76 mothurOut("The summary.seqs command should be in the following format: \n");
77 mothurOut("summary.seqs(fasta=yourFastaFile) \n");
78 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
81 errorOut(e, "SeqSummaryCommand", "help");
86 //***************************************************************************************************************
88 SeqSummaryCommand::~SeqSummaryCommand(){ /* do nothing */ }
90 //***************************************************************************************************************
92 int SeqSummaryCommand::execute(){
95 if (abort == true) { return 0; }
98 openInputFile(fastafile, inFASTA);
102 string summaryFile = outputDir + getSimpleName(fastafile) + ".summary";
103 openOutputFile(summaryFile, outSummary);
105 vector<int> startPosition;
106 vector<int> endPosition;
107 vector<int> seqLength;
108 vector<int> ambigBases;
109 vector<int> longHomoPolymer;
111 outSummary << "seqname\tstart\tend\tnbases\tambigs\tpolymer" << endl;
113 while(!inFASTA.eof()){
114 Sequence current(inFASTA);
115 if (current.getName() != "") {
116 startPosition.push_back(current.getStartPos());
117 endPosition.push_back(current.getEndPos());
118 seqLength.push_back(current.getNumBases());
119 ambigBases.push_back(current.getAmbigBases());
120 longHomoPolymer.push_back(current.getLongHomoPolymer());
122 outSummary << current.getName() << '\t';
123 outSummary << current.getStartPos() << '\t' << current.getEndPos() << '\t';
124 outSummary << current.getNumBases() << '\t' << current.getAmbigBases() << '\t';
125 outSummary << current.getLongHomoPolymer() << endl;
133 sort(startPosition.begin(), startPosition.end());
134 sort(endPosition.begin(), endPosition.end());
135 sort(seqLength.begin(), seqLength.end());
136 sort(ambigBases.begin(), ambigBases.end());
137 sort(longHomoPolymer.begin(), longHomoPolymer.end());
139 int ptile0_25 = int(numSeqs * 0.025);
140 int ptile25 = int(numSeqs * 0.250);
141 int ptile50 = int(numSeqs * 0.500);
142 int ptile75 = int(numSeqs * 0.750);
143 int ptile97_5 = int(numSeqs * 0.975);
144 int ptile100 = numSeqs - 1;
146 //to compensate for blank sequences that would result in startPosition and endPostion equalling -1
147 if (startPosition[0] == -1) { startPosition[0] = 0; }
148 if (endPosition[0] == -1) { endPosition[0] = 0; }
151 mothurOut("\t\tStart\tEnd\tNBases\tAmbigs\tPolymer"); mothurOutEndLine();
152 mothurOut("Minimum:\t" + toString(startPosition[0]) + "\t" + toString(endPosition[0]) + "\t" + toString(seqLength[0]) + "\t" + toString(ambigBases[0]) + "\t" + toString(longHomoPolymer[0])); mothurOutEndLine();
153 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();
154 mothurOut("25%-tile:\t" + toString(startPosition[ptile25]) + "\t" + toString(endPosition[ptile25]) + "\t" + toString(seqLength[ptile25]) + "\t" + toString(ambigBases[ptile25]) + "\t" + toString(longHomoPolymer[ptile25])); mothurOutEndLine();
155 mothurOut("Median: \t" + toString(startPosition[ptile50]) + "\t" + toString(endPosition[ptile50]) + "\t" + toString(seqLength[ptile50]) + "\t" + toString(ambigBases[ptile50]) + "\t" + toString(longHomoPolymer[ptile50])); mothurOutEndLine();
156 mothurOut("75%-tile:\t" + toString(startPosition[ptile75]) + "\t" + toString(endPosition[ptile75]) + "\t" + toString(seqLength[ptile75]) + "\t" + toString(ambigBases[ptile75]) + "\t" + toString(longHomoPolymer[ptile75])); mothurOutEndLine();
157 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();
158 mothurOut("Maximum:\t" + toString(startPosition[ptile100]) + "\t" + toString(endPosition[ptile100]) + "\t" + toString(seqLength[ptile100]) + "\t" + toString(ambigBases[ptile100]) + "\t" + toString(longHomoPolymer[ptile100])); mothurOutEndLine();
159 mothurOut("# of Seqs:\t" + toString(numSeqs)); mothurOutEndLine();
164 catch(exception& e) {
165 errorOut(e, "SeqSummaryCommand", "execute");
170 //***************************************************************************************************************