]> git.donarmstrong.com Git - mothur.git/blob - degapseqscommand.cpp
working on testing for 1.13
[mothur.git] / degapseqscommand.cpp
1 /*
2  *  degapseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 6/21/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "degapseqscommand.h"
11 #include "sequence.hpp"
12
13 //***************************************************************************************************************
14
15 DegapSeqsCommand::DegapSeqsCommand(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", "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 (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 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 = "";          }
41                         
42                         //check for required parameters
43                         fastafile = validParameter.validFile(parameters, "fasta", false);
44                         if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the degap.seqs command."); m->mothurOutEndLine(); abort = true;  }
45                         else { 
46                                 m->splitAtDash(fastafile, fastaFileNames);
47                                 
48                                 //go through files and make sure they are good, if not, then disregard them
49                                 for (int i = 0; i < fastaFileNames.size(); i++) {
50                                         if (inputDir != "") {
51                                                 string path = m->hasPath(fastaFileNames[i]);
52                                                 //if the user has not given a path then, add inputdir. else leave path alone.
53                                                 if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
54                                         }
55         
56                                         ifstream in;
57                                         int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
58                                 
59                                         //if you can't open it, try default location
60                                         if (ableToOpen == 1) {
61                                                 if (m->getDefaultPath() != "") { //default path is set
62                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
63                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
64                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
65                                                         fastaFileNames[i] = tryPath;
66                                                 }
67                                         }
68                                         in.close();
69                                         
70                                         if (ableToOpen == 1) { 
71                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
72                                                 //erase from file list
73                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
74                                                 i--;
75                                         }
76                                 }
77                                 
78                                 //make sure there is at least one valid file left
79                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
80                         }
81
82                         
83                         //if the user changes the output directory command factory will send this info to us in the output parameter 
84                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
85                                 outputDir = ""; 
86                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
87                         }
88
89                 }
90         }
91         catch(exception& e) {
92                 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
93                 exit(1);
94         }
95 }
96 //**********************************************************************************************************************
97
98 void DegapSeqsCommand::help(){
99         try {
100                 m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n");
101                 m->mothurOut("The degap.seqs command parameter is fasta.\n");
102                 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
103                 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
104                 m->mothurOut("The degap.seqs command should be in the following format: \n");
105                 m->mothurOut("degap.seqs(fasta=yourFastaFile) \n");     
106                 m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n");
107                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");     
108         }
109         catch(exception& e) {
110                 m->errorOut(e, "DegapSeqsCommand", "help");
111                 exit(1);
112         }
113 }
114
115 //***************************************************************************************************************
116
117 DegapSeqsCommand::~DegapSeqsCommand(){  /*      do nothing      */      }
118
119 //***************************************************************************************************************
120
121
122 int DegapSeqsCommand::execute(){
123         try{
124                 
125                 if (abort == true) { return 0; }
126                 
127                 for (int s = 0; s < fastaFileNames.size(); s++) {
128                                 
129                         m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
130                         ifstream inFASTA;
131                         m->openInputFile(fastaFileNames[s], inFASTA);
132                         
133                         ofstream outFASTA;
134                         string tempOutputDir = outputDir;
135                         if (outputDir == "") { tempOutputDir = m->hasPath(fastaFileNames[s]); }
136                         string degapFile = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "ng.fasta";
137                         m->openOutputFile(degapFile, outFASTA);
138                         
139                         while(!inFASTA.eof()){
140                                 if (m->control_pressed) {  inFASTA.close();  outFASTA.close(); remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) {        remove(outputNames[j].c_str()); } return 0; }
141                                  
142                                 Sequence currSeq(inFASTA);  m->gobble(inFASTA);
143                                 if (currSeq.getName() != "") {
144                                         outFASTA << ">" << currSeq.getName() << endl;
145                                         outFASTA << currSeq.getUnaligned() << endl;
146                                 }
147                         }
148                         inFASTA.close();
149                         outFASTA.close();
150                         
151                         outputNames.push_back(degapFile);
152                         
153                         if (m->control_pressed) {  remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); } return 0; }
154                 }
155                 
156                 m->mothurOutEndLine();
157                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
158                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
159                 m->mothurOutEndLine();
160
161                 
162                 return 0;
163                 
164         }
165         catch(exception& e) {
166                 m->errorOut(e, "DegapSeqsCommand", "execute");
167                 exit(1);
168         }
169 }
170
171 //***************************************************************************************************************
172