Exemplo n.º 1
0
int BinSeqCommand::execute(){
	try {
		if (abort == true) { if (calledHelp) { return 0; }  return 2;	}
	
		int error = 0;
		
		fasta = new FastaMap();
		if (groupfile != "") {
			groupMap = new GroupMap(groupfile);
			groupMap->readMap();
		}
		
		//read fastafile
		fasta->readFastaFile(fastafile);
		
		//if user gave a namesfile then use it
		if (namesfile != "") {
			readNamesFile();
		}
		
		input = new InputData(listfile, "list");
		list = input->getListVector();
		string lastLabel = list->getLabel();
		
		if (m->control_pressed) {  delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }
		
		//if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
		set<string> processedLabels;
		set<string> userLabels = labels;

				
		while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
			
			if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);		} delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }	
			
			if(allLines == 1 || labels.count(list->getLabel()) == 1){
				
				error = process(list);	
				if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);		} delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }	
							
				processedLabels.insert(list->getLabel());
				userLabels.erase(list->getLabel());
			}
			
			if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
				string saveLabel = list->getLabel();
				
				delete list;
				list = input->getListVector(lastLabel);
				
				error = process(list);	
				if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);		} delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }
													
				processedLabels.insert(list->getLabel());
				userLabels.erase(list->getLabel());
				
				//restore real lastlabel to save below
				list->setLabel(saveLabel);
			}
			
			lastLabel = list->getLabel();			
			
			delete list;
			list = input->getListVector();
		}
		
		if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);		} delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }	

		//output error messages about any remaining user labels
		set<string>::iterator it;
		bool needToRun = false;
		for (it = userLabels.begin(); it != userLabels.end(); it++) {  
			m->mothurOut("Your file does not include the label " + *it); 
			if (processedLabels.count(lastLabel) != 1) {
				m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
				needToRun = true;
			}else {
				m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
			}
		}
		
		//run last label if you need to
		if (needToRun == true)  {
			if (list != NULL) {		delete list;	}
			list = input->getListVector(lastLabel);
				
			error = process(list);	
			if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);		} delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }
			
			delete list;  
		}
		
		delete input;  
		delete fasta; 
		if (groupfile != "") {  delete groupMap;   } 
		
		if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);		}  return 0; }	
		
		m->mothurOutEndLine();
		m->mothurOut("Output File Names: "); m->mothurOutEndLine();
		for (int i = 0; i < outputNames.size(); i++) {	m->mothurOut(outputNames[i]); m->mothurOutEndLine();	}
		m->mothurOutEndLine();

		
		return 0;
	}
	catch(exception& e) {
		m->errorOut(e, "BinSeqCommand", "execute");
		exit(1);
	}
}
Exemplo n.º 2
0
int DeUniqueSeqsCommand::execute() {	
	try {
		
		if (abort == true) { if (calledHelp) { return 0; }  return 2;	}

		//prepare filenames and open files
		ofstream out;
		string outFastaFile = m->getRootName(m->getSimpleName(fastaFile));
		int pos = outFastaFile.find("unique");
		if (pos != string::npos) { outFastaFile = outputDir + outFastaFile.substr(0, pos); }
        else { outFastaFile = outputDir + outFastaFile; }
        map<string, string> variables; 
        variables["[filename]"] = outFastaFile;
        outFastaFile = getOutputFileName("fasta", variables);
		m->openOutputFile(outFastaFile, out);
		
		readNamesFile();
		if (m->control_pressed) {  out.close(); outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
		
		ifstream in;
		m->openInputFile(fastaFile, in);
		
		while (!in.eof()) {
		
			if (m->control_pressed) { in.close(); out.close(); outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
			
			Sequence seq(in); m->gobble(in);
			
			if (seq.getName() != "") {
				
				//look for sequence name in nameMap
				map<string, string>::iterator it = nameMap.find(seq.getName());
				
				if (it == nameMap.end()) {	m->mothurOut("[ERROR]: Your namefile does not contain " + seq.getName() + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
				else {
					vector<string> names;
					m->splitAtComma(it->second, names);
					
					//output sequences
					for (int i = 0; i < names.size(); i++) {
						out << ">" << names[i] << endl;
						out << seq.getAligned() << endl;
					}
					
					//remove seq from name map so we can check for seqs in namefile not in fastafile later
					nameMap.erase(it);
				}
			}
		}
		in.close();
		out.close(); 
		
		if (nameMap.size() != 0) { //then there are names in the namefile not in the fastafile
			for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {  
				m->mothurOut(it->first + " is not in your fasta file, but is in your name file. Please correct."); m->mothurOutEndLine();
			}
		}
				
		if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
		
		m->mothurOutEndLine();
		m->mothurOut("Output File Names: "); m->mothurOutEndLine();
		m->mothurOut(outFastaFile); m->mothurOutEndLine();	
		outputNames.push_back(outFastaFile);  outputTypes["fasta"].push_back(outFastaFile);  
		m->mothurOutEndLine();
		
		//set fasta file as new current fastafile
		string current = "";
		itTypes = outputTypes.find("fasta");
		if (itTypes != outputTypes.end()) {
			if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
		}
		
		return 0;
	}
	catch(exception& e) {
		m->errorOut(e, "DeUniqueSeqsCommand", "execute");
		exit(1);
	}
}
Exemplo n.º 3
0
int main( const int argc, const char *argv[] )
{
     fileplayer_s   *playersFile;
     names_s        *nameList;
     int             next_name;


     srand( time( NULL ) );

     if ( argc < 4 )
     {
	  printf( "Usage: %s <names file> <input file> <output file>\n", argv[0] );

	  return EXIT_SUCCESS;
     }

     if ( (nameList = readNamesFile( argv[1] )) == NULL )
     {
          printf( "Unable to load names file <%s>.\n", argv[1] );

          return EXIT_SUCCESS;
     }

     if ( (playersFile = readPlayersFile( argv[2] )) == NULL )
     {
          printf( "Unable to load players file <%s>.\n", argv[2] );

          free( nameList );

          return EXIT_SUCCESS;
     }

     srand( time( NULL ) );

     next_name = 0;

     for ( int i = 0; i < TOTAL_TEAMS; ++i )
     {
          for ( int j = 0; j < PLAYERS_PER_TEAM; ++j )
          {
               position2_e pos = positions[j];
               int         idx = (i * PLAYERS_PER_TEAM) + j;

               if      ( pos == pos2_Pitcher ) generatePitcher( &playersFile[idx], &nameList[next_name]      );
               else if ( pos == pos2_Closer  ) generateCloser(  &playersFile[idx], &nameList[next_name]      );
               else                            generateBatter(  &playersFile[idx], &nameList[next_name], pos );

               next_name++;
          }
     }

     if ( ! writePlayersFile( argv[3], playersFile ) )
     {
          printf( "Cannot save player changes.\n" );

          free( nameList );
          free( playersFile );

          return EXIT_FAILURE;
     }

     free( nameList );
     free( playersFile );

     return EXIT_SUCCESS;
}