Correctly parse multiple epitopes per file (using C++ streams), regardless of WT...
authorDariusz Murakowski <murakdar@mit.edu>
Mon, 17 Feb 2014 22:33:15 +0000 (17:33 -0500)
committerDariusz Murakowski <murakdar@mit.edu>
Mon, 17 Feb 2014 22:33:15 +0000 (17:33 -0500)
test/importState.test.expected-out
test/newFormat.test.expected-out
test/verbose.test.expected-out
wf.cpp

index d8e8adf..006f7a6 100644 (file)
@@ -1,6 +1,6 @@
 seed = 66314
 1      30      57      60      74      78      90;
-24     25      26      27      28      29      31      32      |       30
+24 25 26 27 28 29 31 32 | 30 
 -----------
 24 25 26 27 28 29 31 32 | 30 
 -----------
index 405dd10..96b8969 100644 (file)
@@ -1,7 +1,7 @@
 seed = 66314
 1      30      57      60      74      78      90;
-24     25      26      27      28      29      31      32      |       30
-0      1       2       4       8       |       3       5       6       7
+24 25 26 27 28 29 31 32 | 30 
+0 1 2 4 8 | 3 5 6 7 
 -----------
 24 25 26 27 28 29 31 32 | 30 
 0 1 2 4 8 | 3 5 6 7 
index 0170b5a..40916e3 100644 (file)
@@ -1,6 +1,6 @@
 seed = 66314
 1      30      57      60      74      78      90;
-24     25      26      27      28      29      31      32      |       30
+24 25 26 27 28 29 31 32 | 30 
 -----------
 24 25 26 27 28 29 31 32 | 30 
 survival probability 0.00240569 number survived 0 total number 0       0       30      57      60      74      78      90
diff --git a/wf.cpp b/wf.cpp
index 245b0ac..16ee39e 100644 (file)
--- a/wf.cpp
+++ b/wf.cpp
@@ -231,63 +231,51 @@ void importState(RunParameters &r) {
 
 void importEpitope(RunParameters &r) {
 
-    FILE *input=fopen(r.epitopeInfile.c_str(),"r");   // <infile>.st
-    if (input == NULL) { perror((std::string("ERROR in importEpitope: ") + r.epitopeInfile).c_str()); exit(1); }
-    
-    char o;
-    //double frac;
-       unsigned int site;
-    
-    // Import targeted epitope
-    
-    while (fscanf(input,"%u",&site)==1) {
-
-        std::cout << site; // XXX
-
-        std::vector<unsigned int> tempEp;
-    
-        tempEp.push_back(site);
-        //r.eWT.push_back(site);
-        
-        while (fscanf(input,"%c",&o) == 1) {
-            std::cout << o; // XXX
-
-            if (fscanf(input,"%u",&site)==1) {
-                std::cout << site; // XXX
-                tempEp.push_back(site);
-            }
-            else {
+    std::ifstream input(r.epitopeInfile.c_str());   // <infile>.st
+    if (!input) { perror((std::string("ERROR in importEpitope: ") + r.epitopeInfile).c_str()); exit(1); }
+
+    std::string readStr;
+    while (std::getline(input,readStr)) {
+
+        std::string word;
+        unsigned int site;
+        std::vector<unsigned int> tmpEp;
+        size_t pos0 = 0;
+        size_t posBar = readStr.find('|',pos0);
+        size_t posEnd = std::string::npos; //readStr.size();
+
+        // could use std::noskipws to keep tab ('\t') characters?
+
+        std::stringstream readStrStrm(std::string(readStr,pos0,posBar-pos0));
+        while (readStrStrm >> word) {
+            std::cout << word << " ";
+            std::istringstream i(word);
+            if (i >> site)
+                tmpEp.push_back(site);
+            else    // must have encountered '|'
                 break;
-            }
         }
+        r.eWT.push_back(tmpEp);
 
-        r.eWT.push_back(tempEp);
+        tmpEp.clear();  // reset temp epitope
+        readStrStrm.str("");  readStrStrm.clear();  // reset stream
 
-        if (fscanf(input,"%c",&o) != 1 || o != '|') { std::cout<<((std::string("ERROR in parsing importEpitope: ") + r.epitopeInfile).c_str()); exit(1); }
-        std::cout << o; // XXX
-        if (fscanf(input,"%c",&o) != 1 || o != '\t') { std::cout<<((std::string("ERROR2 in parsing importEpitope: ") + r.epitopeInfile).c_str()); exit(1); }
-        std::cout << o; // XXX;
+        std::cout << "| ";
 
-        tempEp.clear();
-
-        while (fscanf(input,"%u",&site)==1) {
-
-            std::cout << site; // XXX
-        
-            tempEp.push_back(site);
-            //r.eMut.push_back(site);
-            
-            fscanf(input,"%c",&o);
-            std::cout << o; // XXX
-            if (o=='\n') break;
-            
+        readStrStrm.str(std::string(readStr,posBar+1,posEnd/*-posBar-1*/));
+        while (readStrStrm >> word) {
+            std::cout << word << " ";
+            std::istringstream i(word);
+            if (i >> site)
+                tmpEp.push_back(site);
+            else
+                break;
         }
-
-        r.eMut.push_back(tempEp);
+        r.eMut.push_back(tmpEp);
 
         r.numEpitopes++;
 
-        //if (o=='\n') break;
+        std::cout << "\n";
 
     }