X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=fastamap.cpp;h=bf55493faece21cbd6c0043ac89149826c83c166;hp=9b2438f4e18986f7fc362c71f77c3e71caa448a3;hb=d1c97b8c04bb75faca1e76ffad60b37a4d789d3d;hpb=163b300cfd7d4ca4e70c454be20f07b1d8346650 diff --git a/fastamap.cpp b/fastamap.cpp index 9b2438f..bf55493 100644 --- a/fastamap.cpp +++ b/fastamap.cpp @@ -15,39 +15,43 @@ void FastaMap::readFastaFile(string inFileName) { try { ifstream in; - openInputFile(inFileName, in); + m->openInputFile(inFileName, in); string name, sequence, line; sequence = ""; string temp; - + map::iterator itName; + + while(!in.eof()){ + if (m->control_pressed) { break; } + Sequence currSeq(in); name = currSeq.getName(); - if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); } - else { sequence = currSeq.getUnaligned(); } - - seqmap[name] = sequence; - map::iterator it = data.find(sequence); - if (it == data.end()) { //it's unique. - data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found. -// data[sequence].groupnumber = 1; - data[sequence].names = name; - }else { // its a duplicate. - data[sequence].names += "," + name; -// data[sequence].groupnumber++; - } - - gobble(in); + if (name != "") { + if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); } + else { sequence = currSeq.getUnaligned(); } + + itName = seqmap.find(name); + if (itName == seqmap.end()) { seqmap[name] = sequence; } + else { m->mothurOut("You already have a sequence named " + name + ", sequence names must be unique, please correct."); m->mothurOutEndLine(); } + + map::iterator it = data.find(sequence); + if (it == data.end()) { //it's unique. + data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found. + // data[sequence].groupnumber = 1; + data[sequence].names = name; + }else { // its a duplicate. + data[sequence].names += "," + name; + // data[sequence].groupnumber++; + } + } + m->gobble(in); } in.close(); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the FastaMap class function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "FastaMap", "readFastaFile"); exit(1); } } @@ -57,39 +61,50 @@ void FastaMap::readFastaFile(string inFileName) { void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data ifstream oldNameFile; - openInputFile(oldNameFileName, oldNameFile); + m->openInputFile(oldNameFileName, oldNameFile); map oldNameMap; + map::iterator itName; string name, list; while(!oldNameFile.eof()){ - oldNameFile >> name >> list; + if (m->control_pressed) { break; } + + oldNameFile >> name; m->gobble(oldNameFile); + oldNameFile >> list; oldNameMap[name] = list; - gobble(oldNameFile); + m->gobble(oldNameFile); } oldNameFile.close(); ifstream inFASTA; - openInputFile(inFastaFile, inFASTA); + m->openInputFile(inFastaFile, inFASTA); string sequence; while(!inFASTA.eof()){ + if (m->control_pressed) { break; } + Sequence currSeq(inFASTA); name = currSeq.getName(); - if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); } - else { sequence = currSeq.getUnaligned(); } - - seqmap[name] = sequence; - map::iterator it = data.find(sequence); - if (it == data.end()) { //it's unique. - data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found. -// data[sequence].groupnumber = 1; - data[sequence].names = oldNameMap[name]; - }else { // its a duplicate. - data[sequence].names += "," + oldNameMap[name]; -// data[sequence].groupnumber++; - } - - gobble(inFASTA); + if (name != "") { + if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); } + else { sequence = currSeq.getUnaligned(); } + + itName = seqmap.find(name); + if (itName == seqmap.end()) { seqmap[name] = sequence; } + else { m->mothurOut("You already have a sequence named " + name + ", sequence names must be unique, please correct."); m->mothurOutEndLine(); } + + seqmap[name] = sequence; + map::iterator it = data.find(sequence); + if (it == data.end()) { //it's unique. + data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found. + // data[sequence].groupnumber = 1; + data[sequence].names = oldNameMap[name]; + }else { // its a duplicate. + data[sequence].names += "," + oldNameMap[name]; + // data[sequence].groupnumber++; + } + } + m->gobble(inFASTA); } @@ -143,20 +158,17 @@ int FastaMap::sizeUnique(){ //returns datas size which is the number of unique s void FastaMap::printNamesFile(string outFileName){ //prints data try { ofstream outFile; - openOutputFile(outFileName, outFile); + m->openOutputFile(outFileName, outFile); // two column file created with groupname and them list of identical sequence names for (map::iterator it = data.begin(); it != data.end(); it++) { + if (m->control_pressed) { break; } outFile << it->second.groupname << '\t' << it->second.names << endl; } outFile.close(); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "FastaMap", "printNamesFile"); exit(1); } } @@ -166,20 +178,17 @@ void FastaMap::printNamesFile(string outFileName){ //prints data void FastaMap::printCondensedFasta(string outFileName){ //prints data try { ofstream out; - openOutputFile(outFileName, out); + m->openOutputFile(outFileName, out); //creates a fasta file for (map::iterator it = data.begin(); it != data.end(); it++) { + if (m->control_pressed) { break; } out << ">" << it->second.groupname << endl; out << it->first << endl; } out.close(); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "FastaMap", "printCondensedFasta"); exit(1); } }