]> git.donarmstrong.com Git - mothur.git/blob - degapseqscommand.cpp
added mpi code to cluster.split command
[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                                 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 = 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 = openInputFile(fastaFileNames[i], in);
58                                         in.close();
59                                         
60                                         if (ableToOpen == 1) { 
61                                                 m->mothurOut(fastaFileNames[i] + " will be disregarded."); m->mothurOutEndLine(); 
62                                                 //erase from file list
63                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
64                                                 i--;
65                                         }
66                                 }
67                                 
68                                 //make sure there is at least one valid file left
69                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
70                         }
71
72                         
73                         //if the user changes the output directory command factory will send this info to us in the output parameter 
74                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
75                                 outputDir = ""; 
76                                 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it  
77                         }
78
79                 }
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
83                 exit(1);
84         }
85 }
86 //**********************************************************************************************************************
87
88 void DegapSeqsCommand::help(){
89         try {
90                 m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n");
91                 m->mothurOut("The degap.seqs command parameter is fasta.\n");
92                 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
93                 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
94                 m->mothurOut("The degap.seqs command should be in the following format: \n");
95                 m->mothurOut("degap.seqs(fasta=yourFastaFile) \n");     
96                 m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n");
97                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");     
98         }
99         catch(exception& e) {
100                 m->errorOut(e, "DegapSeqsCommand", "help");
101                 exit(1);
102         }
103 }
104
105 //***************************************************************************************************************
106
107 DegapSeqsCommand::~DegapSeqsCommand(){  /*      do nothing      */      }
108
109 //***************************************************************************************************************
110
111
112 int DegapSeqsCommand::execute(){
113         try{
114                 
115                 if (abort == true) { return 0; }
116                 
117                 for (int s = 0; s < fastaFileNames.size(); s++) {
118                                 
119                         m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
120                         ifstream inFASTA;
121                         openInputFile(fastaFileNames[s], inFASTA);
122                         
123                         ofstream outFASTA;
124                         string degapFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "ng.fasta";
125                         openOutputFile(degapFile, outFASTA);
126                         
127                         while(!inFASTA.eof()){
128                                 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; }
129                                  
130                                 Sequence currSeq(inFASTA);  gobble(inFASTA);
131                                 if (currSeq.getName() != "") {
132                                         outFASTA << ">" << currSeq.getName() << endl;
133                                         outFASTA << currSeq.getUnaligned() << endl;
134                                 }
135                         }
136                         inFASTA.close();
137                         outFASTA.close();
138                         
139                         outputNames.push_back(degapFile);
140                         
141                         if (m->control_pressed) {  remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); } return 0; }
142                 }
143                 
144                 m->mothurOutEndLine();
145                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
146                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
147                 m->mothurOutEndLine();
148
149                 
150                 return 0;
151                 
152         }
153         catch(exception& e) {
154                 m->errorOut(e, "DegapSeqsCommand", "execute");
155                 exit(1);
156         }
157 }
158
159 //***************************************************************************************************************
160