[英]creating complement of DNA sequence and reversing it C++
So I am trying to create the complement of the sequence TGAGACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTGC however my output didn't work as expected.所以我试图创建序列 TGAGACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTGC 的补充,但是我的输出没有按预期工作。 The complements for each letter in the sequence are
序列中每个字母的补码是
A -> T A -> T
G -> C G -> C
C -> G C -> G
T -> A T -> A
I've been programming in Java for over a year now so I've gotten really rusty with pointers in C++, I'm guessing the problem lies in the reverse methods and the way to pointers are shifted around through each pass of the function call我已经用 Java 编程一年多了,所以我对 C++ 中的指针真的很生疏,我猜问题在于反向方法,指针的方式在函数调用的每次传递中都发生了变化
#include<stdio.h>
#include<iostream>
using namespace std;
void reverse(char s[]);
int main() {
char s[40] = {'T','G','A','G','A','C','T','T','C','A','G','G','C','T','C','C','T','G','G','G','C','A','A','C','G','T','G','C','T','G','G','T','C','T','G','T','G','T','G'};
cout << "DNA sequence: "<< endl << s << endl;
reverse(s);
cout << "Reverse Compliment: "<< endl << s << endl;
system("pause");
}
void reverse(char s[])
{
char c;
char *p, *q;
p = s;
if (!p)
return;
q = p + 1;
if (*q == '\0')
return;
c = *p;
reverse(q);
switch(c) {
case 'A':
*p = 'T';
break;
case 'G':
*p = 'C';
break;
case 'C':
*p = 'G';
break;
case 'T':
*p = 'A';
break;
}
while (*q != '\0') {
*p = *q;
p++;
q++;
}
*p = c;
return;
}
Standard modern C++ makes this low-level, pointer-oriented programming, unnecessary (in fact, you're effectively writing C).标准的现代 C++ 使这种低级的、面向指针的编程变得不必要(实际上,您正在有效地编写 C)。
Once you have a function, say complement
, which transforms a nucleotide to its complement, you just need to apply some standard library function like transform
.一旦你有了一个函数,比如说
complement
,它将一个核苷酸转换成它的补体,你只需要应用一些标准库函数,比如transform
。
Here is a rewrite of your program in C++11:这是用 C++11 重写的程序:
#include <string>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
char complement(char n)
{
switch(n)
{
case 'A':
return 'T';
case 'T':
return 'A';
case 'G':
return 'C';
case 'C':
return 'G';
}
assert(false);
return ' ';
}
int main()
{
string nucs = "ACAATTGGA";
transform(
begin(nucs),
end(nucs),
begin(nucs),
complement);
cout << nucs << endl;
}
std::string
style, looks easy and clean: I presume it may be useful to OP or others. std::string
样式,看起来简单干净:我认为它可能对 OP 或其他人有用。
creating complement of DNA sequence and reversing it C++
创建 DNA 序列的补充并将其反转 C++
In other words, it is reverse complement of a DNA sequence, which can be easily achieved by reversing the DNA sequence and then getting its complement.换句话说,它是DNA序列的反向互补,可以很容易地通过将DNA序列反向然后得到它的互补来实现。 Or getting the complement and then reversing.
或者得到补码然后反转。 An example is shown below.
一个例子如下所示。
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string DNAseq = "TGAGACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTGC";
reverse(DNAseq.begin(), DNAseq.end());
for (std::size_t i = 0; i < DNAseq.length(); ++i){
switch (DNAseq[i]){
case 'A':
DNAseq[i] = 'T';
break;
case 'C':
DNAseq[i] = 'G';
break;
case 'G':
DNAseq[i] = 'C';
break;
case 'T':
DNAseq[i] = 'A';
break;
}
}
std::cout << "reverse complement : " << DNAseq << std::endl;
return 0;
}
Change the reverse
method like this像这样改变
reverse
方法
void reverse(char s[])
{
while (*s) {
switch(*s) {
case 'A':
*s = 'T';
break;
case 'G':
*s = 'C';
break;
case 'C':
*s = 'G';
break;
case 'T':
*s = 'A';
break;
}
++s;
}
return;
}
...and you will get correct result. ...你会得到正确的结果。
If you don't like pointers, please, don't use them!如果你不喜欢指针,请不要使用它们! In modern C++ pointers aren't often necessary.
在现代 C++ 中,通常不需要指针。 The following code is C++11 (do you have C++11 compiler?) written as I would do it.
以下代码是按照我的方式编写的 C++11(你有 C++11 编译器吗?)。
#include <string>
#include <iostream>
#include <algorithm>
std::string reverse(std::string seq)
{
auto lambda = [](const char c) {
switch (c) {
case 'A':
return 'T';
case 'G':
return 'C';
case 'C':
return 'G';
case 'T':
return 'A';
default:
throw std::domain_error("Invalid nucleotide.");
}
};
std::transform(seq.cbegin(), seq.cend(), seq.begin(), lambda);
return seq;
}
int main()
{
std::string seq("TGAGACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTG");
std::cout << "DNA sequence: " << std::endl << seq << std::endl;
seq = reverse(seq);
std::cout << "Reverse Compliment: " << std::endl << seq << std::endl;
system("pause");
return EXIT_SUCCESS;
}
Some notes:一些注意事项:
default
in switch
statement.switch
语句中使用default
。std::string
to plain char.std::string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.