Example #1
0
seqio_status seqio_read_all(seqio_sequence sequence,
                            char **buffer,
                            uint64_t *buffer_length,
                            uint64_t *read_length) {
    check_null(sequence);
    check_null(buffer);
    check_null(buffer_length);
    check_null(read_length);

    ISequence *iseq = (ISequence *)sequence;

    try {
        if(*buffer == nullptr) {
            *buffer_length = INITIAL_READ_ALL_BUFFER_LENGTH;
            *buffer = (char *)malloc(*buffer_length);
            if(*buffer == nullptr)
                raise_oom("Cannot allocate %zu bytes", size_t(*buffer_length));
        }

        auto grow_to = [=] (uint64_t new_length) {
            char *buffer_ = (char *)realloc(*buffer, new_length);
            if(buffer_ == nullptr) {
                raise_oom("Cannot allocate %zu bytes", size_t(new_length));
            }
            *buffer = buffer_;
            *buffer_length = new_length;
        };

        uint64_t nread;
        uint64_t nread_total = 0;

        while( (nread = iseq->read(*buffer + nread_total, *buffer_length - nread_total)) != 0) {
            nread_total += nread;
            if(nread_total == *buffer_length) {
                grow_to(*buffer_length * 2);
            }
        }

        if(nread_total == *buffer_length) {
            grow_to(*buffer_length + 1);
        }
        (*buffer)[nread_total] = '\0';

        *read_length = nread_total;
    } catch(Exception x) {
        *read_length = 0;
        return err_handler(x.err_info);
    }
    
    return SEQIO_SUCCESS;    
}
SequenceContainer * SequenceApplicationTools::getSequenceContainer(
  const Alphabet * alpha,
  map<string, string> & params,
  const string & suffix,
  bool suffixIsOptional,
  bool verbose)
{
  string sequenceFilePath = ApplicationTools::getAFilePath("sequence.file",params, true, true, suffix, suffixIsOptional);
  string sequenceFormat = ApplicationTools::getStringParameter("sequence.format", params, "Fasta", suffix, suffixIsOptional);
  if(verbose) ApplicationTools::displayResult("Sequence format " + suffix, sequenceFormat);
  ISequence * iSeq = NULL;
  if(sequenceFormat == "Mase")
  {
    iSeq = new Mase();
  }
  else if(sequenceFormat == "Phylip")
  {
    bool sequential = true, extended = true;
    string split = "  ";
    if(params.find("sequence.format_phylip.order") != params.end())
    {
           if(params["sequence.format_phylip.order"] == "sequential" ) sequential = true;
      else if(params["sequence.format_phylip.order"] == "interleaved") sequential = false;
      else ApplicationTools::displayWarning("Argument '" +
             params["sequence.format_phylip.order"] +
             "' for parameter 'sequence.format_phylip.order' is unknown. " +
             "Default used instead: sequential.");
    }
    else ApplicationTools::displayWarning("Argument 'sequence.format_phylip.order' not found. Default used instead: sequential.");
    if(params.find("sequence.format_phylip.ext") != params.end())
    {
           if(params["sequence.format_phylip.ext"] == "extended")
      {
        extended = true;
        split = ApplicationTools::getStringParameter("sequence.format_phylip.extended.split", params, "spaces", suffix, suffixIsOptional);
        if(split == "spaces") split = "  ";
        else if(split == "tab") split = "\t";
        else throw Exception("Unknown option for sequence.format_phylip.extended.split: " + split);
      }
      else if(params["sequence.format_phylip.ext"] == "classic" ) extended = false;
      else ApplicationTools::displayWarning("Argument '" +
             params["sequence.format_phylip.ext"] +
             "' for parameter 'sequence.format_phylip.ext' is unknown. " +
             "Default used instead: extended.");
    }
    else ApplicationTools::displayWarning("Argument 'sequence.format_phylip.ext' not found. Default used instead: extended.");
    iSeq = new Phylip(extended, sequential, 100, true, split);
  }
  else if(sequenceFormat == "Fasta") iSeq = new Fasta();
  else if(sequenceFormat == "Clustal") iSeq = new Clustal();
  else
  {
    ApplicationTools::displayError("Unknown sequence format.");
    exit(-1);
  }
  SequenceContainer * sequences = iSeq->read(sequenceFilePath, alpha);
  delete iSeq;
  
  if(verbose) ApplicationTools::displayResult("Sequence file " + suffix, sequenceFilePath);

  return sequences;
}
VectorSiteContainer * SequenceApplicationTools::getSiteContainer(
  const Alphabet * alpha,
  map<string, string> & params,
  const string & suffix,
  bool suffixIsOptional,
  bool verbose)
{
  string sequenceFilePath = ApplicationTools::getAFilePath("sequence.file",params, true, true, suffix, suffixIsOptional);
  string sequenceFormat = ApplicationTools::getStringParameter("sequence.format", params, "Fasta", suffix, suffixIsOptional);
  if(verbose) ApplicationTools::displayResult("Sequence format " + suffix, sequenceFormat);
  ISequence * iSeq = NULL;
  if(sequenceFormat == "Mase")
  {
    iSeq = new Mase();
  }
  else if(sequenceFormat == "Phylip")
  {
    bool sequential = true, extended = true;
    string split = "  ";
    if(params.find("sequence.format_phylip.order") != params.end())
    {
           if(params["sequence.format_phylip.order"] == "sequential" ) sequential = true;
      else if(params["sequence.format_phylip.order"] == "interleaved") sequential = false;
      else ApplicationTools::displayWarning("Argument '" +
             params["sequence.format_phylip.order"] +
             "' for parameter 'sequence.format_phylip.order' is unknown. " +
             "Default used instead: sequential.");
    }
    else ApplicationTools::displayWarning("Argument 'sequence.format_phylip.order' not found. Default used instead: sequential.");
    if(params.find("sequence.format_phylip.ext") != params.end())
    {
      if(params["sequence.format_phylip.ext"] == "extended")
      {
        extended = true;
        split = ApplicationTools::getStringParameter("sequence.format_phylip.extended.split", params, "spaces", suffix, suffixIsOptional);
        if(split == "spaces") split = "  ";
        else if(split == "tab") split = "\t";
        else throw Exception("Unknown option for sequence.format_phylip.extended.split: " + split);
      }
      else if(params["sequence.format_phylip.ext"] == "classic" ) extended = false;
      else ApplicationTools::displayWarning("Argument '" +
             params["sequence.format_phylip.ext"] +
             "' for parameter 'sequence.format_phylip.ext' is unknown. " +
             "Default used instead: extended.");
    }
    else ApplicationTools::displayWarning("Argument 'sequence.format_phylip.ext' not found. Default used instead: extended.");
    iSeq = new Phylip(extended, sequential, 100, true, split);
  }
  else if(sequenceFormat == "Fasta") iSeq = new Fasta();
  else if(sequenceFormat == "Clustal") iSeq = new Clustal();
  else
  {
    ApplicationTools::displayError("Unknown sequence format.");
    exit(-1);
  }
  const SequenceContainer * seqCont = iSeq->read(sequenceFilePath, alpha);
  VectorSiteContainer * sites = new VectorSiteContainer(* dynamic_cast<const OrderedSequenceContainer *>(seqCont));
  delete seqCont;
  delete iSeq;
  
  if(verbose) ApplicationTools::displayResult("Sequence file " + suffix, sequenceFilePath);

  // Look for site selection:
  if(sequenceFormat == "Mase")
  {
    //getting site set:
    string siteSet = ApplicationTools::getStringParameter("sequence.format_mase.site_selection", params, "none", suffix, suffixIsOptional, false);
    if(siteSet != "none")
    {
      VectorSiteContainer * selectedSites;
      try
      {
        selectedSites = dynamic_cast<VectorSiteContainer *>(MaseTools::getSelectedSites(* sites, siteSet));
        if(verbose) ApplicationTools::displayResult("Set found", TextTools::toString(siteSet) + " sites.");
      }
      catch(IOException ioe)
      {
        ApplicationTools::displayError("Site Set '" + siteSet + "' not found.");
        exit(-1);
      }
      if(selectedSites->getNumberOfSites() == 0)
      {
        ApplicationTools::displayError("Site Set '" + siteSet + "' is empty.");
        exit(-1);
      }
      delete sites;
      sites = selectedSites;
    }
  }
  return sites;
}