X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=sequenceparser.cpp;h=fd94b246a43217cc9cbfa131bfe4cd672955e001;hb=36a6b02cf7f09d2bc34376b588944a9ca73429c5;hp=44012d8ba8321fea520fbe613e0dac8f8911c760;hpb=ae57e166b2ed7b475ec3f466106bd76fabadd063;p=mothur.git diff --git a/sequenceparser.cpp b/sequenceparser.cpp index 44012d8..fd94b24 100644 --- a/sequenceparser.cpp +++ b/sequenceparser.cpp @@ -7,7 +7,7 @@ * */ -#include "sequenceParser.h" +#include "sequenceparser.h" /************************************************************/ @@ -63,6 +63,8 @@ SequenceParser::SequenceParser(string groupFile, string fastaFile, string nameFi string first, second; int countName = 0; + set thisnames1; + while(!inName.eof()) { if (m->control_pressed) { break; } @@ -94,10 +96,12 @@ SequenceParser::SequenceParser(string groupFile, string fastaFile, string nameFi it = splitMap.find(group); if (it != splitMap.end()) { //adding seqs to this group (it->second) += "," + names[i]; + thisnames1.insert(names[i]); countName++; }else { //first sighting of this group splitMap[group] = names[i]; countName++; + thisnames1.insert(names[i]); //is this seq in the fasta file? if (i != 0) { //if not then we need to add a duplicate sequence to the seqs for this group so the new "fasta" and "name" files will match @@ -106,6 +110,8 @@ SequenceParser::SequenceParser(string groupFile, string fastaFile, string nameFi } } } + + allSeqsMap[names[i]] = names[0]; } @@ -129,6 +135,15 @@ SequenceParser::SequenceParser(string groupFile, string fastaFile, string nameFi if (error == 1) { m->control_pressed = true; } if (countName != (groupMap->getNumSeqs())) { + vector groupseqsnames = groupMap->getNamesSeqs(); + + for (int i = 0; i < groupseqsnames.size(); i++) { + set::iterator itnamesfile = thisnames1.find(groupseqsnames[i]); + if (itnamesfile == thisnames1.end()){ + cout << "missing name " + groupseqsnames[i] << '\t' << allSeqsMap[groupseqsnames[i]] << endl; + } + } + m->mothurOutEndLine(); m->mothurOut("[ERROR]: Your name file contains " + toString(countName) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct."); m->mothurOutEndLine(); @@ -247,6 +262,79 @@ vector SequenceParser::getSeqs(string g){ exit(1); } } +/************************************************************/ +int SequenceParser::getSeqs(string g, string filename, bool uchimeFormat=false){ + try { + map >::iterator it; + vector seqForThisGroup; + vector nameVector; + + it = seqs.find(g); + if(it == seqs.end()) { + m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine(); + }else { + + ofstream out; + m->openOutputFile(filename, out); + + seqForThisGroup = it->second; + + if (uchimeFormat) { + // format should look like + //>seqName /ab=numRedundantSeqs/ + //sequence + + map nameMapForThisGroup = getNameMap(g); + map::iterator itNameMap; + int error = 0; + + for (int i = 0; i < seqForThisGroup.size(); i++) { + itNameMap = nameMapForThisGroup.find(seqForThisGroup[i].getName()); + + if (itNameMap == nameMapForThisGroup.end()){ + error = 1; + m->mothurOut("[ERROR]: " + seqForThisGroup[i].getName() + " is in your fastafile, but is not in your namesfile, please correct."); m->mothurOutEndLine(); + }else { + int num = m->getNumNames(itNameMap->second); + + seqPriorityNode temp(num, seqForThisGroup[i].getAligned(), seqForThisGroup[i].getName()); + nameVector.push_back(temp); + } + } + + if (error == 1) { out.close(); m->mothurRemove(filename); return 1; } + + //sort by num represented + sort(nameVector.begin(), nameVector.end(), compareSeqPriorityNodes); + + //print new file in order of + for (int i = 0; i < nameVector.size(); i++) { + + if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; } + + out << ">" << nameVector[i].name << "/ab=" << nameVector[i].numIdentical << "/" << endl << nameVector[i].seq << endl; + } + + }else { + //m->mothurOut("Group " + g + " contains " + toString(seqForThisGroup.size()) + " unique seqs.\n"); + for (int i = 0; i < seqForThisGroup.size(); i++) { + + if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; } + + seqForThisGroup[i].printSequence(out); + } + } + out.close(); + } + + return 0; + } + catch(exception& e) { + m->errorOut(e, "SequenceParser", "getSeqs"); + exit(1); + } +} + /************************************************************/ map SequenceParser::getNameMap(string g){ try { @@ -268,6 +356,38 @@ map SequenceParser::getNameMap(string g){ } } /************************************************************/ +int SequenceParser::getNameMap(string g, string filename){ + try { + map >::iterator it; + map nameMapForThisGroup; + + it = nameMapPerGroup.find(g); + if(it == nameMapPerGroup.end()) { + m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine(); + }else { + nameMapForThisGroup = it->second; + + ofstream out; + m->openOutputFile(filename, out); + + for (map::iterator itFile = nameMapForThisGroup.begin(); itFile != nameMapForThisGroup.end(); itFile++) { + + if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; } + + out << itFile->first << '\t' << itFile->second << endl; + } + + out.close(); + } + + return 0; + } + catch(exception& e) { + m->errorOut(e, "SequenceParser", "getNameMap"); + exit(1); + } +} +/************************************************************/