Exemple #1
0
void Mase::writeSequences(ostream& output, const SequenceContainer& sc) const throw (Exception)
{
  // Checking the existence of specified file, and possibility to open it in write mode
  if (!output) { throw IOException ("Mase::write : failed to open file"); }

  Comments comments = sc.getGeneralComments();

  // Writing all general comments in file
  if (comments.size() == 0) {
    output << ";;" << endl;
  }
  for (size_t i = 0 ; i < comments.size() ; i++) {
    output << ";;" << comments[i] << endl;
  }

  string seq, temp = "";  // Initialization

  // Main loop : for all sequences
  vector<string> names = sc.getSequencesNames();
  for (size_t i = 0 ; i < names.size() ; i ++)
  {
    comments = sc.getComments(names[i]);

    // Writing all sequence comments in file
    // If no comments are associated with current sequence, an empy commentary line will be writed
    if (comments.size() == 0)
    {
      output << ";" << endl;
    }
    else
    {
      for (size_t j = 0 ; j < comments.size() ; j++)
      {
        output << ";" << comments[j] << endl;
      }
    }

    // Sequence name writing
    output << names[i] << endl;

    // Sequence cutting to specified characters number per line
    seq = sc.toString(names[i]);
    while (seq != "")
    {
      if (seq.size() > charsByLine_)
      {
        temp = seq;
        temp.erase(temp.begin() + static_cast<ptrdiff_t>(charsByLine_), temp.end());
        output << temp  << endl;
        seq.erase(seq.begin(), seq.begin() + static_cast<ptrdiff_t>(charsByLine_));
      }
      else
      {
        output << seq << endl;
        seq = "";
      }
    }
  }
}
Exemple #2
0
void Mase::appendSequencesFromStream(std::istream& input, SequenceContainer& vsc) const throw (Exception)
{
  if (!input) { throw IOException ("Mase::read : fail to open file"); }
  
  // Initialization
  Comments seqComments, fileComments;
  string temp, name, sequence = "";
  bool comments = false;

  // Get current general comments is VectorSequenceContainer
  fileComments = vsc.getGeneralComments();

  // Main loop : for all file lines
  while (!input.eof())
  {
    getline(input, temp, '\n');  // Copy current line in temporary string
    
    // If first character is ;
    if (temp[0] == ';')
    {
      // If second character is also ;
      if (temp[1] == ';')
      {
        // File comments isolation
        temp.erase(0,2);  // Characters ;; deletion
        if(temp != "") fileComments.push_back(temp);
      }
      else
      {
        // If a name and a sequence were founded
        if ((name != "") && (sequence != ""))
        {
          // New sequence creation, and addition in existing VectorSequenceContainer
          vsc.addSequence(BasicSequence(name, sequence, seqComments, vsc.getAlphabet()), checkNames_);
          name = "";
          sequence = "";
          seqComments.clear();
        }
        
        // Sequence commentaries isolation
        temp.erase(temp.begin());  // Character ; deletion
        if (temp != "") seqComments.push_back(temp);
        comments = true;
      }
    }
    else
    {
      // If sequence commentaries were just isolated
      if (comments)
      {
        // Sequence name isolation
        name = temp;
        comments = false;
      }
      else sequence += temp;  // Sequence isolation
    }
  }
  
  // Addition of the last sequence in file
  if ((name != "") && (sequence != ""))
  {
    vsc.addSequence(BasicSequence(name, sequence, seqComments, vsc.getAlphabet()), checkNames_);
  }

  // Set new general comments in VectorSequenceContainer (old + new comments)
  vsc.setGeneralComments(fileComments);
}