fa.cpp 2.2 KB
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
#include <cstring>
#include <sys/stat.h>

#include "fa.h"
#include "rna.h"



Fasta::Fasta() : path_(), name_(), seq_()
{
}

Fasta::Fasta(const std::string& path, const std::vector<std::string>& name, const std::vector<std::string>& seq)
{
    path_ = path;
    name_ = name;
    seq_ = seq;
}

Fasta::Fasta(const Fasta& fa): path_(fa.path_), name_(fa.name_), seq_(fa.seq_)
{
}

Fasta& Fasta::operator=(const Fasta& fa)
{
  if (this != &fa) {
    path_ = fa.path_;
    name_ = fa.name_;
    seq_ = fa.seq_;
  }
  return *this;
}

const std::vector<std::string>& Fasta::name() const
{
    return name_;
}

const std::vector<std::string>& Fasta::seq() const
{
    return seq_;
}

const std::string& Fasta::path() const
{
    return path_;
}

void Fasta::setName(std::vector<std::string> name)
{
    name_ = name;
}

void Fasta::load(const char* file)
{
    path_ = file;

    std::string line, name, seq;
    struct stat buf;
    if( (stat(file, &buf) == 0))
    {
        std::ifstream ifs(file);
        while (std::getline(ifs, line)) {
            if (line[0]=='>') {         /* header */
                name=line.substr(1);
                if(seq != "")
                {
                    for(std::string::size_type i = 0; (i = seq.find("\n", i)) != std::string::npos;)
                    {
                            seq.replace(i, std::string("\n").length(), std::string(""));
                            i += std::string("").length();
                    }
                    seq_.push_back(seq);
                }
                name_.push_back(name);
                seq = "";
            }
            else if (line[0] != '\0' and line[0] != '\n')
            {
                seq += line;
            }
        }
        if(seq != "")
        {
            for(std::string::size_type i = 0; (i = seq.find("\n", i)) != std::string::npos;)
            {
                    seq.replace(i, std::string("\n").length(), std::string(""));
                    i += std::string("").length();
            }
            seq_.push_back(seq);
        }
    }
    else
    {
        throw std::string("The file " + path_ + " doesn't exist.");
    }
}