Exemplo n.º 1
0
int MergeGroupsCommand::process(SharedRAbundVectors*& thisLookUp, ofstream& out, bool& printHeaders){
	try {
        vector<string> setNames = designMap->getCategory();
        
        //create sharedRabundVectors
        vector<SharedRAbundVector*> data = thisLookUp->getSharedRAbundVectors();
        
        //create SharedRAbundVectors for the merged groups. Fill with blank rabundFloatVectors
        SharedRAbundVectors* merged; merged = new SharedRAbundVectors();
        for (int i = 0; i < setNames.size(); i++) {
            SharedRAbundVector* myLookup = new SharedRAbundVector(thisLookUp->getNumBins());
            myLookup->setLabel(thisLookUp->getLabel());
            myLookup->setGroup(setNames[i]);
            merged->push_back(myLookup);
        }
 
        //for each OTU
        for (int j = 0; j < data[0]->getNumBins(); j++) {
            if (m->getControl_pressed()) { break; }
            
            map<string, vector<int> > otusGroupAbunds;
            map<string, vector<int> >::iterator itAbunds;
            
            //for each sample
            for (int i = 0; i < data.size(); i++) {
                
                string grouping = designMap->get(data[i]->getGroup());  //what set to your belong to
                if (grouping == "not found") { m->mothurOut("[ERROR]: " + data[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
                else {
                    //Add this OTUs values to sets abunds
                    itAbunds = otusGroupAbunds.find(grouping);
                    if (itAbunds == otusGroupAbunds.end()) { //new group
                        vector<int> temp;
                        temp.push_back(data[i]->get(j));
                        otusGroupAbunds[grouping] = temp;
                    }else {
                        (itAbunds->second).push_back(data[i]->get(j));
                    }
                }
            }
            
            //find results for this bin. Set merged value for this bin in the results
            for (itAbunds = otusGroupAbunds.begin(); itAbunds != otusGroupAbunds.end(); itAbunds++) {
                int abund = mergeAbund(itAbunds->second);
                merged->set(j, abund, itAbunds->first);
            }
        }
        
        //free memory
        for (int i = 0; i < data.size(); i++) {	delete data[i]; 	}
        
        if (m->getControl_pressed()) { delete merged; return 0; }
        
        merged->eliminateZeroOTUS(); // remove any zero OTUs created by median option.
        
        //print new file
        merged->print(out, printHeaders);
        delete merged;
        
		return 0;
		
	}
	catch(exception& e) {
		m->errorOut(e, "MergeGroupsCommand", "process");
		exit(1);
	}
}
Exemplo n.º 2
0
int MergeGroupsCommand::processCountFile(DesignMap*& designMap){
    try {
        CountTable countTable;
        if (!countTable.testGroups(countfile)) { m->mothurOut("[ERROR]: your countfile contains no group information, please correct.\n"); m->setControl_pressed(true); return 0; }
        
        //read countTable
        countTable.readTable(countfile, true, false);
        
        //fill Groups - checks for "all" and for any typo groups
        vector<string> nameGroups = countTable.getNamesOfGroups();
        if (Groups.size() == 0) { Groups = nameGroups; }
        
        
        vector<string> dnamesGroups = designMap->getNamesGroups();
        
        //sanity check
        bool error = false;
        if (nameGroups.size() == dnamesGroups.size()) { //at least there are the same number
            //is every group in counttable also in designmap
            for (int i = 0; i < nameGroups.size(); i++) {
                if (m->getControl_pressed()) { break; }
                if (!util.inUsersGroups(nameGroups[i], dnamesGroups)) { error = true; break; }
            }
            
        }
        if (error) { m->mothurOut("[ERROR]: Your countfile does not contain the same groups as your design file, please correct\n"); m->setControl_pressed(true); return 0; }
        
        //user selected groups - remove some groups from table
        if (Groups.size() != nameGroups.size()) {
            for (int i = 0; i < nameGroups.size(); i++) {
                if (!util.inUsersGroups(nameGroups[i], Groups)) { countTable.removeGroup(nameGroups[i]); }
            }
        }
        //ask again in case order changed
        nameGroups = countTable.getNamesOfGroups();
        int numGroups = nameGroups.size();
        
        //create new table
        CountTable newTable;
        vector<string> treatments = designMap->getCategory();
        map<string, vector<int> > clearedMap;
        for (int i = 0; i < treatments.size(); i++) {
            newTable.addGroup(treatments[i]);
            vector<int> temp;
            clearedMap[treatments[i]] = temp;
        }
        treatments = newTable.getNamesOfGroups();
        
        set<string> namesToRemove;
        vector<string> namesOfSeqs = countTable.getNamesOfSeqs();
        for (int i = 0; i < namesOfSeqs.size(); i++) {
            
            if (m->getControl_pressed()) { break; }
            
            vector<int> thisSeqsCounts = countTable.getGroupCounts(namesOfSeqs[i]);
            map<string, vector<int> > thisSeqsMap = clearedMap;
            
            for (int j = 0; j < numGroups; j++) {
                thisSeqsMap[designMap->get(nameGroups[j])].push_back(thisSeqsCounts[j]);
            }
        
            //create new counts for seq for new table
            vector<int> newCounts; int totalAbund = 0;
            for (int j = 0; j < treatments.size(); j++){
                int abund = mergeAbund(thisSeqsMap[treatments[j]]);
                newCounts.push_back(abund);  //order matters, add in count for each treatment in new table.
                totalAbund += abund;
            }
            
            //add seq to new table
            if(totalAbund == 0) {
                namesToRemove.insert(namesOfSeqs[i]);
            }else { newTable.push_back(namesOfSeqs[i], newCounts); }
        }
        
        if (error) { m->setControl_pressed(true); return 0; }
        
        //remove sequences zeroed out by median method
        if (namesToRemove.size() != 0) {
            //print names
            ofstream out;
            string accnosFile = "accnosFile.temp";
            util.openOutputFile(accnosFile, out);
            
            //output to .accnos file
            for (set<string>::iterator it = namesToRemove.begin(); it != namesToRemove.end(); it++) {
                if (m->getControl_pressed()) {  out.close(); util.mothurRemove(accnosFile); return 0; }
                out << *it << endl;
            }
            out.close();

            //run remove.seqs
            string inputString = "accnos=" + accnosFile + ", fasta=" + fastafile;
            
            m->mothurOut("/******************************************/"); m->mothurOutEndLine();
            m->mothurOut("Running command: remove.seqs(" + inputString + ")"); m->mothurOutEndLine();
            current->setMothurCalling(true);
            
            Command* removeCommand = new RemoveSeqsCommand(inputString);
            removeCommand->execute();
            
            map<string, vector<string> > filenames = removeCommand->getOutputFiles();
            
            delete removeCommand;
            current->setMothurCalling(false);
            m->mothurOut("/******************************************/"); m->mothurOutEndLine();
            
            util.mothurRemove(accnosFile);
        }
    
        string thisOutputDir = outputDir;
        if (outputDir == "") {  thisOutputDir += util.hasPath(countfile);  }
        map<string, string> variables;
        variables["[filename]"] = thisOutputDir + util.getRootName(util.getSimpleName(countfile));
        variables["[extension]"] = util.getExtension(countfile);
        string outputFileName = getOutputFileName("count", variables);
        outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
        
        newTable.printTable(outputFileName);
        
        return 0;
        
    }
    catch(exception& e) {
        m->errorOut(e, "MergeGroupsCommand", "processCountFile");
        exit(1);
    }
}
Exemplo n.º 3
0
int MergeGroupsCommand::process(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
	try {
        vector<string> setNames = designMap->getCategory();
        
        if (method == "average") {
            //create sharedRabundFloatVectors
            vector<SharedRAbundFloatVector*> temp = thisLookUp[0]->getSharedRAbundFloatVectors(thisLookUp);
            
            //follow code below
            map<string, SharedRAbundFloatVector> merged;
            map<string, SharedRAbundFloatVector>::iterator it;
            for (int i = 0; i < setNames.size(); i++) {
                SharedRAbundFloatVector myLookup(thisLookUp[0]->getNumBins());
                myLookup.setLabel(thisLookUp[0]->getLabel());
                merged[setNames[i]] = myLookup;
            }
            
            map<string, vector<int> > clearGroupAbunds;
            map<string, vector<int> >::iterator itAbunds;
            
            for (int i = 0; i < temp.size(); i++) {
                if (m->control_pressed) { return 0; }
                //what grouping does this group belong to
                string grouping = designMap->get(temp[i]->getGroup());
                if (grouping == "not found") { m->mothurOut("[ERROR]: " + temp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
                else {
                    //do we already have a member of this grouping?
                    it = merged.find(grouping);
                    
                    if (it == merged.end()) { //nope, so create it
                        merged[grouping] = *temp[i];
                        merged[grouping].setGroup(grouping);
                        vector<int> temp;
                        clearGroupAbunds[grouping] = temp;
                    }
                }
            }
            
            for (int j = 0; j < temp[0]->getNumBins(); j++) {
                if (m->control_pressed) { return 0; }
                
                map<string, vector<int> > otusGroupAbunds = clearGroupAbunds;
                for (int i = 0; i < temp.size(); i++) {
                    
                    string grouping = designMap->get(temp[i]->getGroup());
                    if (grouping == "not found") { m->mothurOut("[ERROR]: " + temp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
                    else {
                        otusGroupAbunds[grouping].push_back(temp[i]->getAbundance(j));
                    }
                }
                
                for (itAbunds = otusGroupAbunds.begin(); itAbunds != otusGroupAbunds.end(); itAbunds++) {
                    int abund = mergeAbund(itAbunds->second);
                    merged[itAbunds->first].set(j, abund, itAbunds->first);
                }
            }
            
            if (method == "median") {
                vector<SharedRAbundFloatVector*> temp2;
                for (it = merged.begin(); it != merged.end(); it++) {  temp2.push_back(&(it->second)); }
                eliminateZeroOTUS(temp2);
            }
            
            //print new file
            for (it = merged.begin(); it != merged.end(); it++) {
                if (!m->printedSharedHeaders) { (it->second).printHeaders(out); }
                out << (it->second).getLabel() << '\t' << it->first << '\t';
                (it->second).print(out);
            }
        }else {
            map<string, SharedRAbundVector> merged;
            map<string, SharedRAbundVector>::iterator it;
            for (int i = 0; i < setNames.size(); i++) {
                SharedRAbundVector myLookup(thisLookUp[0]->getNumBins());
                myLookup.setLabel(thisLookUp[0]->getLabel());
                merged[setNames[i]] = myLookup;
            }
            
            map<string, vector<int> > clearGroupAbunds;
            map<string, vector<int> >::iterator itAbunds;
            
            for (int i = 0; i < thisLookUp.size(); i++) {
                if (m->control_pressed) { return 0; }
                //what grouping does this group belong to
                string grouping = designMap->get(thisLookUp[i]->getGroup());
                if (grouping == "not found") { m->mothurOut("[ERROR]: " + thisLookUp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
                else {
                    //do we already have a member of this grouping?
                    it = merged.find(grouping);
                    
                    if (it == merged.end()) { //nope, so create it
                        merged[grouping] = *thisLookUp[i];
                        merged[grouping].setGroup(grouping);
                        vector<int> temp;
                        clearGroupAbunds[grouping] = temp;
                    }
                }
            }
            
            for (int j = 0; j < thisLookUp[0]->getNumBins(); j++) {
                if (m->control_pressed) { return 0; }
                
                map<string, vector<int> > otusGroupAbunds = clearGroupAbunds;
                for (int i = 0; i < thisLookUp.size(); i++) {
                    
                    string grouping = designMap->get(thisLookUp[i]->getGroup());
                    if (grouping == "not found") { m->mothurOut("[ERROR]: " + thisLookUp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
                    else {
                        otusGroupAbunds[grouping].push_back(thisLookUp[i]->getAbundance(j));
                    }
                }
                
                for (itAbunds = otusGroupAbunds.begin(); itAbunds != otusGroupAbunds.end(); itAbunds++) {
                    int abund = mergeAbund(itAbunds->second);
                    merged[itAbunds->first].set(j, abund, itAbunds->first);
                }
            }
            
            if (method == "median") {
                vector<SharedRAbundVector*> temp;
                for (it = merged.begin(); it != merged.end(); it++) {  temp.push_back(&(it->second)); }
                eliminateZeroOTUS(temp);
            }
            
            //print new file
            for (it = merged.begin(); it != merged.end(); it++) {
                if (!m->printedSharedHeaders) { (it->second).printHeaders(out); }
                out << (it->second).getLabel() << '\t' << it->first << '\t';
                (it->second).print(out);
            }
        }
		return 0;
		
	}
	catch(exception& e) {
		m->errorOut(e, "MergeGroupsCommand", "process");
		exit(1);
	}
}