// readFromCurFile - reads one .ms2 file
void SpectraSTMs2LibImporter::readFromFile(string& impFileName) {

  
  ifstream fin;
  if (!myFileOpen(fin, impFileName)) {
    g_log->error("CREATE", "Cannot open .ms2 file \"" + impFileName + "\" for reading. File skipped.");
    return;
  }
  
  g_log->log("MS2 IMPORT", "Importing .ms2 file \"" + impFileName + "\"."); 
  
  if (g_verbose) {
    cout << "\nImporting spectra from .ms2 library file..." << endl;
  } 
  
  // start the progress count
  ProgressCount pc(!g_quiet && !g_verbose, 500, 0);
  pc.start("\nImporting spectra from .ms2 library file");
  
  string line("");
  unsigned int scan1 = 0;
  unsigned int scan2 = 0;
  double precursorMz = 0.0;
  int charge = 1;
  double mw = 0.0;
  
  string dummy("");
  string seq("");
  string modifiedSeq("");
  
  SpectraSTPeakList* peakList = NULL;
   
  // looks like their cysteines are actually C[160]'s
  // Peptide::addModTokenToTables("C", "Carbamidomethyl");
  
  while (nextLine(fin, line)) {
  
    if (line.empty()) continue;
    
    string::size_type pos = 0; 
    
    if (line[0] == 'H') {
      continue;
    
    } else if (line[0] == 'S') {
      if (peakList) {
        if (peakList->getNumPeaks() == 0 || seq.empty()) {
          delete peakList;
        } else {
          
          pc.increment();
	  if (!(modifiedSeq.empty())) seq = modifiedSeq;
          Peptide* pep = new Peptide(seq, charge);
	  
	  // check legality of peptide and mod strings -- will not insert later if illegal (parsing will continue anyway)
          if (pep->hasUnknownMod) {
	    g_log->error("MS2 IMPORT", "Peptide ID with unknown modification: \"" + seq + "\". Entry skipped.");
	  }
	  if (pep->illegalPeptideStr && !pep->hasUnknownMod) {
	    g_log->error("MS2 IMPORT", "Illegal peptide ID string: \"" + seq + "\". Entry skipped.");
          }
	  
          stringstream commentss;
          commentss << "Fullname=X." << seq << ".X/" << charge;
          commentss << " ScanNum=" << scan1 << '.' << scan2;
          commentss << " Spec=Raw";
          
          SpectraSTLibEntry* entry = new SpectraSTLibEntry(pep, commentss.str(), "Normal", peakList); 
          
          if (g_verbose) {
            cout << "Importing record " << m_count << ": " << pep->interactStyleWithCharge() << endl;
          }
          m_count++;

          if (passAllFilters(entry)) {
	    entry->annotatePeaks();
	    m_lib->insertEntry(entry);
          }
          delete (entry);
        }
        
      } 
      
      seq = "";
      modifiedSeq = "";
      peakList = NULL;
      scan1 = atoi(nextToken(line, 1, pos, " \t\r\n", " \t\r\n").c_str());
      scan2 = atoi(nextToken(line, pos, pos, " \t\r\n", " \t\r\n").c_str());
      precursorMz = atof(nextToken(line, pos, pos, " \t\r\n", " \t\r\n").c_str());
      
    } else if (line[0] == 'Z') {
      
      charge = atoi(nextToken(line, 1, pos, " \t\r\n", " \t\r\n").c_str());
      mw = atof(nextToken(line, pos, pos, " \t\r\n", " \t\r\n").c_str());
  
    } else if (line[0] == 'D') {
      dummy = nextToken(line, 1, pos, "\t\r\n", " \t\r\n");
      if (dummy == "seq") {
        seq = nextToken(line, pos, pos, "\r\n", " \t\r\n");
      } else if (dummy == "modified seq") {
        modifiedSeq = nextToken(line, pos, pos, "\r\n", " \t\r\n");
      }
 	
    } else {
      // should be a peak
      if (!peakList) {
        peakList = new SpectraSTPeakList(precursorMz, 0);
      }
      double mz = atof(nextToken(line, 0, pos, " \t\r\n", " \t\r\n").c_str());
      float intensity = atof(nextToken(line, pos, pos, " \t\r\n", " \t\r\n").c_str());
      peakList->insert(mz, intensity, "", "");
        
    }
    
    
  }
    	
  // finish last record
  if (peakList) {
    if (peakList->getNumPeaks() == 0 || seq.empty()) {
      delete peakList;
    } else {
          
      pc.increment();
      if (!(modifiedSeq.empty())) seq = modifiedSeq;
      Peptide* pep = new Peptide(seq, charge);
      if (pep->hasUnknownMod) {
	g_log->error("MS2 IMPORT", "Peptide ID with unknown modification: \"" + seq + "\". Entry skipped.");
      }
      if (pep->illegalPeptideStr && !pep->hasUnknownMod) {
	g_log->error("MS2 IMPORT", "Illegal peptide ID string: \"" + seq + "\". Entry skipped.");
      }
      stringstream commentss;
      commentss << "Fullname=X." << seq << ".X/" << charge;
      commentss << " ScanNum=" << scan1 << '.' << scan2;
      commentss << " Spec=Raw";
          
      SpectraSTLibEntry* entry = new SpectraSTLibEntry(pep, commentss.str(), "Normal", peakList); 
          
      if (g_verbose) {
        cout << "Importing record " << m_count << ": " << pep->interactStyleWithCharge() << endl;
      }
      m_count++;

      if (passAllFilters(entry)) {
	entry->annotatePeaks();
        m_lib->insertEntry(entry);
      }
      delete (entry);
    }
        
  } 
      
 
  pc.done();
}