C ++ - 输出给定长度的所有可能的DNA kmers

时间:2011-04-06 16:00:18

标签: c++ algorithm bioinformatics

“kmer”是长度为K的DNA序列。有效的DNA序列(为了我的目的)只能含有以下4个碱基:A,C,T,G。我正在寻找一种C ++算法,它只是将字母顺序这些碱基的所有可能组合输出到字符串数组中。例如,如果K = 2,程序应该生成以下数组:

kmers[0]  = AA
kmers[1]  = AC
kmers[2]  = AG
kmers[3]  = AT
kmers[4]  = CA
kmers[5]  = CC
kmers[6]  = CG
kmers[7]  = CT
kmers[8]  = GA
kmers[9]  = GC
kmers[10] = GG
kmers[11] = GT
kmers[12] = TA
kmers[13] = TC
kmers[14] = TG
kmers[15] = TT

如果我正确地考虑这个问题,那么问题实际上就是将十进制整数转换为基数4然后替换相应的基数。我以为我可以使用itoa,但itoa不是C标准,我的编译器不支持它。我欢迎任何聪明的想法。这是我的示例代码:

#include <iostream>
#include <string>
#include <math.h>

#define K 3

using namespace std;

int main() {

  int num_kmers = pow(4,K);
  string* kmers = NULL;

  /* Allocate memory for kmers array */
  kmers = new string[num_kmers];

  /* Populate kmers array */
  for (int i=0; i< pow(4,K); i++) {

    // POPULATE THE kmers ARRAY HERE                                                                                                                                                         

  }

  /* Display all possible kmers */
  for (int i=0; i< pow(4,K); i++)
    cout << kmers[i] << "\n";

  delete [] kmers;
}

5 个答案:

答案 0 :(得分:6)

您需要使用递归才能灵活(即,以便您可以轻松更改K)。

void populate(int depth, string base, string* kmers, int* kmers_offset)
{
    if(depth == K)
    {
        kmers[*kmers_offset].assign(base);
        (*kmers_offset)++;
    }
    else
    {
        static char bases[] = { 'A', 'C', 'G', 'T' };
        for(int i = 0; i < 4; ++i)
            populate(depth + 1, base + bases[i], kmers, kmers_offset);
    }
}

然后像这样称呼它:

int kmers_offset = 0;
populate(0, "", kmers, &kmers_offset);

干杯。

答案 1 :(得分:2)

一旦接受了用户输入文本,就不需要将十进制转换为任何内容。

创建一个字符串数组也可能是一个错误,它会随着K呈指数级增长。只需打印输出。

char bases[] = { 'A', 'C', 'T', 'G' };
std::vector< int > sequence( K ); // allow dynamic K
std::vector< char > output( K * ( 2 << K * 2 ) ); // flat sequence of sequences
std::vector< char >::iterator out_it = output.begin();

int i;
do {
    // print current sequence
    for ( i = 0; i < K; ++ i ) std::cout << bases[ sequence[ i ] ];
    std::cout << '\n';

    // store current sequence
    for ( i = 0; i < K; ++ i ) * out_it ++ = bases[ sequence[ i ] ];

    // advance to next sequence
    for ( i = K; i > 0; -- i ) {
        // increment the last base that we can
        if ( sequence[ i - 1 ] != sizeof bases - 1 ) {
            ++ sequence[ i - 1 ];
            break;
        }
        // reset bases that can't be incremented
        sequence[ i - 1 ] = 0;
    }
} while ( i > 0 ); // if i <= 0, failed to increment anything, we're done.

答案 2 :(得分:2)

在我看来,这非常适合自定义迭代器。这样你的主程序可以很简单:

std::vector<std::string> v;
v.reserve(kmerIterator<4>::end() - kmerIterator<4>::begin());
std::copy(kmerIterator<4>::begin(), kmerIterator<4>::end(),
    std::back_inserter(v));

但是,由于我们已经将kmer概念实现为交互器,我们也可以使用所有其他通用算法。由于我们将kmer迭代器实现为随机访问迭代器,因此找到第i个kmer是微不足道的:

kmerIterator<4>::begin()[i]

这是我的完整计划:

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>

template<unsigned int n>
class kmerIterator :
    public std::iterator<std::random_access_iterator_tag,
        std::string,
        unsigned int>
{
private:
    typedef kmerIterator k; // 'cause I'm lazy
    difference_type it;
    kmerIterator(difference_type i) : it(i) {}
public:
    kmerIterator() : it() {}
    static k begin() {
        return 0;
    }
    static k end() {
        return difference_type(1) << n*2;
    }
    k& operator++() { ++it; return *this; }
    k operator++(int) { return it++; }
    k& operator--() { --it; return *this; }
    k operator--(int) { return it--; }
    k operator+(difference_type delta) { return it + delta; }
    k operator-(difference_type delta) { return it - delta; }
    difference_type operator-(const k& rhs) { return it - rhs.it; }
    bool operator<(const k& rhs) const { return it < rhs.it; }
    bool operator>(const k& rhs) const { return it > rhs.it; }
    bool operator<=(const k& rhs) const { return it <= rhs.it; }
    bool operator>=(const k& rhs) const { return it >= rhs.it; }
    k operator+=(difference_type delta) { return it+=delta; }
    k operator-=(difference_type delta) { return it-=delta; }
    std::string operator[](difference_type delta) const { return *k(it+delta); }
    std::string operator*() const {
        std::string rz;
        int i = 2*n;
        do {
            i -= 2;
            rz += "ACGT"[(it>>i)&3];
        } while(i);
        return rz;
    }
};

int main() {
    std::copy(kmerIterator<2>::begin(),
        kmerIterator<2>::end(),
        std::ostream_iterator<std::string>(std::cout, "\n"));

    std::vector<std::string> v;
    v.reserve(kmerIterator<4>::end() - kmerIterator<4>::begin());
    std::copy(kmerIterator<4>::begin(),
        kmerIterator<4>::end(),
        std::back_inserter(v));
    std::cout << v[42] << "\n";
    std::cout << kmerIterator<4>::begin()[56] << "\n";
}

答案 3 :(得分:1)

哇,这不应该那么难。

std::string kmers(int i, int K) {
  static const char* DNA = "ACGT";
  if (K==0) return std::string();
  return DNA[i%4] + kmers(i / 4, K-1);
}

const int K = 2;
int main () {
  for (int i = 0; i != 1<<(2*K); ++i)
    std::cout << "kmers[" << i << "] = " << kmers(i,K) << std::endl;
}

答案 4 :(得分:0)

#include <iostream>
#include <string>
#include <string_view>
#include <vector>

class kmer
{
public:
    kmer();
    kmer(std::string &str, int &k) : k_(k), str_(str)
    {
        for(int i = 0; i <= str_.size() - k_; ++i)
            value_.push_back(str_.substr(i, k_));
    }
    auto value() { return value_; }
    auto size() { return value_.size(); }
private:
    std::vector<std::string_view> value_;
    std::string_view str_;
    int k_;
};

int main()
{
    std::string seq{"AGCTAGCT"};
    kmer k4_mer(seq, 4);

    std::cout << Total " << k4_mer.size() << " mers\n";
    for(auto &i : k4_mer.value())
        std::cout << i << "\n";

    return 0;
}