使用c++字符串类函数来显示更长的原始基因组字符串中的“基因子字符串”
using c++ string class functions to display “gene substrings” from a longer original Genome string
请提供帮助。我正在用一个序列TGTGTGTATAT测试我的基因发现程序,起始帽ATG添加到前面,结束帽TAA添加到后面,所以我正在测试基因组ATGTGTGTGTATATTAA,因为不包括帽,序列长度不是3个字符的倍数,因此它不包含任何基因,应该显示
"未发现基因"
黑色定制窗口确实弹出但它不显示
//Date:
//purpose: find Genes and cout found genes
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
string genome = "ATGTGTGTGTATATTAA"; //testing this string
/*cout << "Enter a genome string: ";
cin >> genome;*/
int geneCounter = 0;
while(!genome.empty()) //enters loop if strings not empty
{
if(genome.find("ATG",0) == string::npos) //genome.find("ATG",0,3) should return npos if no ATG is found right?
{
if(geneCounter == 0)
{
cout << "no gene is found";
genome.clear();
}
}
else
{
int startGene = genome.find("ATG",0); //ATG is not part of gene just a front endcap to genes
int endGene = min(min(genome.find("TAG"), genome.find("TAA")), genome.find("TGA"));//endcaps are TAG or TAA or TGA
//finds location of (1+ gene end)
string currentGene = genome.substr(startGene + 3, endGene - (startGene +3)); //puts copy of gene in substring
if((currentGene.length() % 3) == 0)
{
geneCounter += 1;
cout << currentGene <<endl;//a gene is a multiple of three characters so if its a gene I cout the gene
}
endGene += 3;
genome.erase(0, endGene); //should erase the gene I just "cout"displayed
//and its front ATG and its endcap and anything before its ATG
//cout << genome; //testing: this should display the genome after the endcap of the last gene cause I erased all coming before
}
}
return 0;
}
这听起来像是一个干净regex解决方案的完美用例。再次验证基因串的确切规则是什么?
从ATG 开始
三个字符的基因。允许使用哪些字符?
以TTA 结束
使用类似的正则表达式
^ATG([A-Z]{3})+TTA$
^是一行的开头。ATG是一条需要严格匹配的字符串。([A-Z]{3})+是一个匹配组,意味着它将在找到时被提取,从A-Z中有三个字符A。TTA再次完全匹配。加号仅在找到该组中的至少一个时有效。.$是行的末尾。
如果[A-Z]是有效字符,则可以使用[ATGE]代替[A-Z]。
这应该能彻底解决你的问题。为所有内部的三个字符子字符串提供一个迭代器,同时还检查长度开始和结束。
http://www.cplusplus.com/reference/regex/
while循环的第一次迭代进入else
块,并以结束
genome.erase(0, endGene);
这会擦除整个基因组,因此当检查while
条件时,
while(!genome.empty())
genome
为空,因此程序退出。
尝试将while条件更改为:
while(true)
这可能不是最终目标,但它可能有助于引导你朝着正确的方向前进。