]> git.donarmstrong.com Git - mothur.git/blob - chopseqscommand.cpp
modified chop.seqs
[mothur.git] / chopseqscommand.cpp
1 /*
2  *  chopseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/10/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "chopseqscommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14
15 ChopSeqsCommand::ChopSeqsCommand(string option)  {
16         try {
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"fasta","numbases","keep","outputdir","inputdir"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         map<string,string>::iterator it;
32                         
33                         //check to make sure all parameters are valid for command
34                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
36                         }
37                         
38                         //if the user changes the output directory command factory will send this info to us in the output parameter 
39                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
40                         
41                         //if the user changes the input directory command factory will send this info to us in the output parameter 
42                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
43                         if (inputDir == "not found"){   inputDir = "";          }
44                         else {
45                                 string path;
46                                 it = parameters.find("fasta");
47                                 //user has given a template file
48                                 if(it != parameters.end()){ 
49                                         path = hasPath(it->second);
50                                         //if the user has not given a path then, add inputdir. else leave path alone.
51                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
52                                 }
53                         }
54
55                         //check for required parameters
56                         fastafile = validParameter.validFile(parameters, "fasta", true);
57                         if (fastafile == "not open") { abort = true; }
58                         else if (fastafile == "not found") {  m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }    
59                         
60                         string temp = validParameter.validFile(parameters, "numbases", false);  if (temp == "not found") { temp = "0"; } 
61                         convert(temp, numbases);   
62                 
63                         keep = validParameter.validFile(parameters, "keep", false);             if (keep == "not found") { keep = "front"; } 
64                                 
65                         if (numbases == 0)  { m->mothurOut("You must provide the number of bases you want to keep for the chops.seqs command."); m->mothurOutEndLine(); abort = true;  }
66                 }
67
68         }
69         catch(exception& e) {
70                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
71                 exit(1);
72         }
73 }
74 //**********************************************************************************************************************
75
76 void ChopSeqsCommand::help(){
77         try {
78                 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences.\n");
79                 m->mothurOut("The chop.seqs command parameters are fasta, numbases, and keep. fasta and numbases are required required.\n");
80                 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n");
81                 m->mothurOut("The numbases parameter allows you to specify the number of bases you want to keep.\n");
82                 m->mothurOut("The keep parameter allows you to specify whether you want to keep the front or the back of your sequence, default=front.\n");
83                 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n");
84                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
85         }
86         catch(exception& e) {
87                 m->errorOut(e, "ChopSeqsCommand", "help");
88                 exit(1);
89         }
90 }
91
92 //**********************************************************************************************************************
93
94 int ChopSeqsCommand::execute(){
95         try {
96                 
97                 if (abort == true) { return 0; }
98                 
99                 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
100                 string outputFileNameAccnos = outputDir + getRootName(getSimpleName(fastafile)) + "chop.accnos";
101                 
102                 ofstream out;
103                 openOutputFile(outputFileName, out);
104                 
105                 ofstream outAcc;
106                 openOutputFile(outputFileNameAccnos, outAcc);
107                 
108                 ifstream in;
109                 openInputFile(fastafile, in);
110                 
111                 bool wroteAccnos = false;
112                 
113                 while (!in.eof()) {
114                         
115                         Sequence seq(in);
116                         
117                         if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0;  }
118                         
119                         if (seq.getName() != "") {
120                                 string newSeqString = getChopped(seq);
121                                 
122                                 //output trimmed sequence
123                                 if (newSeqString != "") {
124                                         out << ">" << seq.getName() << endl << newSeqString << endl;
125                                 }else{
126                                         outAcc << seq.getName() << endl;
127                                         wroteAccnos = true;
128                                 }
129                         }
130                 }
131                 in.close();
132                 out.close();
133                 outAcc.close();
134                 
135                 m->mothurOutEndLine();
136                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
137                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
138                 
139                 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine();  }
140                 else {  remove(outputFileNameAccnos.c_str());  }
141                 
142                 m->mothurOutEndLine();
143                 
144                 return 0;               
145         }
146
147         catch(exception& e) {
148                 m->errorOut(e, "ChopSeqsCommand", "execute");
149                 exit(1);
150         }
151 }
152 //**********************************************************************************************************************
153 string ChopSeqsCommand::getChopped(Sequence seq) {
154         try {
155                 string temp = seq.getAligned();
156                 string tempUnaligned = seq.getUnaligned();
157                                 
158                 //if needed trim sequence
159                 if (keep == "front") {//you want to keep the beginning
160                         int tempLength = tempUnaligned.length();
161
162                         if (tempLength > numbases) { //you have enough bases to remove some
163                                 
164                                 int stopSpot = 0;
165                                 int numBasesCounted = 0;
166                                 
167                                 for (int i = 0; i < temp.length(); i++) {
168                                         //eliminate N's
169                                         if (toupper(temp[i]) == 'N') { 
170                                                 temp[i] == '.'; 
171                                                 tempLength--;
172                                                 if (tempLength < numbases) { stopSpot = 0; break; }
173                                         }
174                                         
175                                         if(isalpha(temp[i])) { numBasesCounted++; }
176                                         
177                                         if (numBasesCounted >= numbases) { stopSpot = i; break; }
178                                 }
179                                 
180                                 if (stopSpot == 0) { temp = ""; }
181                                 else {  temp = temp.substr(0, stopSpot);  }
182                                                 
183                         }else { temp = ""; } //sequence too short
184                         
185                 }else { //you are keeping the back
186                         int tempLength = tempUnaligned.length();
187                         if (tempLength > numbases) { //you have enough bases to remove some
188                                 
189                                 int stopSpot = 0;
190                                 int numBasesCounted = 0;
191                                 
192                                 for (int i = (temp.length()-1); i >= 0; i--) {
193                                         //eliminate N's
194                                         if (toupper(temp[i]) == 'N') { 
195                                                 temp[i] == '.'; 
196                                                 tempLength--;
197                                                 if (tempLength < numbases) { stopSpot = 0; break; }
198                                         }
199                                         
200                                         if(isalpha(temp[i])) { numBasesCounted++; }
201
202                                         if (numBasesCounted >= numbases) { stopSpot = i; break; }
203                                 }
204                         
205                                 if (stopSpot == 0) { temp = ""; }
206                                 else {  temp = temp.substr(stopSpot+1);  }
207                         }
208                         else {  temp = ""; } //sequence too short
209                 }
210
211                 return temp;
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "ChopSeqsCommand", "getChopped");
215                 exit(1);
216         }
217 }
218 //**********************************************************************************************************************
219
220