基因数据压缩算法(ACTG) C++
2015-09-14 16:14
645 查看
基因数据压缩算法是一个初级的数据压缩算法,基因数据如下所示"ATAGATGCAT",如果使用标准的ASCII编码(每个字符1个字节),这个基因字符串的流长度是10*8=80位。
这些字符是由4种不同的字符组成,所有我们可以使用双位编码压缩,比如 A:00, C:01,
T:10, G11,压缩之后我们就可以在一个字节存放4个基因字符了,当数据比较大时,压缩效率为25%。
完整代码
输出结果为:
参考:
《算法》 第四版 5.5 数据压缩
这些字符是由4种不同的字符组成,所有我们可以使用双位编码压缩,比如 A:00, C:01,
T:10, G11,压缩之后我们就可以在一个字节存放4个基因字符了,当数据比较大时,压缩效率为25%。
完整代码
#include <iostream> #include <string> using namespace std; void PrintBits(const char *pstr, int width = 1); // ACTG A:00, C:01, T:10, G11 class Dna { public: Dna() : sizes(0) {} Dna(const char *str); void AddCompress(const char c) { Compress(c); } void AddCompress(const char *pstr); string Expand(); int Size() const { return sizes; } string Data() const { return data; } string Compress() const { return compressData; } void PrintCompressData() { PrintBits(compressData.c_str()); } void PrintCompressData(int width) { PrintBits(compressData.c_str(), width); } private: int sizes; // 存放数据数 string data; // 存放原始数据 string compressData; // 存放压缩后的二进制数据 void Compress(const char c); unsigned char Index(char c); char Table(int index) const; void PrintBits(const char c); void PrintBits(const char *pstr, int width = 1); }; int main(void) { char str[] = "ACTG"; Dna dna(str); cout << dna.Size() << endl; cout << dna.Expand() << endl; dna.AddCompress("GTCA"); cout << dna.Size() << endl; cout << dna.Expand() << endl; dna.PrintCompressData(); system("pause"); return 0; } Dna::Dna(const char *str) : sizes(0) { if (str) { const char *p = str; while (*p) { Compress(*p); p++; } } } void Dna::AddCompress(const char *pstr) { if (!pstr) return; const char *p = pstr; while (*p) { Compress(*p); p++; } } // class Dna private scope // 压缩字符c,并添加到compressData中 void Dna::Compress(const char c) { data.append(1, c); if (sizes % 4 == 0) compressData.append(1, '\0'); int n = sizes % 4; compressData[sizes / 4] |= Index(c) << (6 - 2 * n); sizes++; } // 解压缩,并以string返回 string Dna::Expand() { string s; for (int i = 0; i < sizes; i++) { int n = i % 4; unsigned char index = ((unsigned char)compressData[i / 4]) >> (6 - 2 * n); s.append(1, Table(index)); } return s; } // 根据char值返回对应的索引,比如'A'为0时返回0 unsigned char Dna::Index(char c) { if (c == 'A') return 0; else if (c == 'C') return 1; else if(c == 'T') return 2; else // if(c == 'G') return 3; } // 根据index值返回对应的字符,比如index为0时返回'A' char Dna::Table(int index) const { static char table[4] = { 'A', 'C', 'T', 'G' }; return table[index % 4]; } void Dna::PrintBits(const char c) { for (int i = 0; i < 8; i++) { if ((c << i) & 0x80) cout << '1'; else cout << '0'; } } void Dna::PrintBits(const char *pstr, int width) { if (!pstr) return; const char *p = pstr; int nums = 0; while (*p) { PrintBits(*p); p++; if (!((++nums) % width)) cout << endl; } cout << endl; }
输出结果为:
参考:
《算法》 第四版 5.5 数据压缩
相关文章推荐
- 顺序表(C++语言实现)
- 设计模式C++实现(10)——桥接模式
- 设计模式C++实现(9)——享元模式
- ffmpeg的安装&&在c++工程中引用ffmpeg类库
- 《Java编程思想》作者:C++不垃圾,只是Java很傲慢
- c++primer plus 第九章-名称空间
- C++中shared_ptr的使用
- C语言获取文件大小
- VC++、MFC中最好的开源项目
- C++中memset()函数的作用
- 剖析C++编程当中指针作为函数参数的用法
- hanoi 并计算移动次数 c语言
- C++编译机制(vc++)
- C语言字符串反转实现【采用头指针和尾指针方法完成】
- C++调用webservice
- QT学习之路-C++类的前置声明
- C++Primer第五版 4.1.2节练习
- C++面向对象高级开发课程(第三周)
- C++ STL — 第6章 STL容器(三)list
- KMP算法的C语言实现