【发布时间】:2017-06-15 05:21:38
【问题描述】:
在以下数据中,我尝试运行一个简单的马尔可夫模型。
假设我有一个具有以下结构的数据:
pos M1 M2 M3 M4 M5 M6 M7 M8 hybrid_block S1 S2 S3 S4 S5 S6 S7 S8
1 A T T A A G A C A|C C G C T T A G A
2 T G C T G T T G T|A A T A T C A A T
3 C A A C A G T C C|G G A C G C G C G
4 G T G T A T C T G|T C T T T A T C T
块 M 表示来自一组类别的数据,块 S 也是如此。
数据为strings,由沿位置线连接的字母组成。因此,M1 的字符串值为 A-T-C-G,其他所有块也是如此。
还有一个hybrid block 有两个以相同方式读取的字符串。 问题是我想找出混合块中的哪个字符串最有可能来自哪个块(M vs. S)?
我正在尝试构建一个马尔科夫模型,它可以帮助我识别hybrid block 中的哪个字符串来自哪个块。在这个例子中,我可以看出在混合块中ATCG来自block M,CAGT来自block S。
我将问题分解为不同的部分来读取和挖掘数据:
问题级别 01:
- 首先我阅读第一行(标题)并为所有列创建
unique keys。 - 然后我读取了第二行(
pos,值为 1)并创建另一个密钥。在同一行中,我从hybrid_block读取值并读取其中的字符串值。pipe |只是一个分隔符,所以index 0 and 2中有两个字符串,分别是A和C。所以,我想从这条线上得到一个
defaultdict(<class 'dict'>, {'M1': ['A'], 'M2': ['T'], 'M3': ['T']...., 'hybrid_block': ['A'], ['C']...}
因为,我正在阅读该行,我想从每列附加字符串值并最终创建。
defaultdict(<class 'dict'>, {'M1': ['A', 'T', 'C', 'G'], 'M2': ['T', 'G', 'A', 'T'], 'M3': ['T', 'C', 'A', 'G']...., 'hybrid_block': ['A', 'T', 'C', 'G'], ['C', 'A', 'G', 'T']...}
问题级别 02:
我读取了
hybrid_block中第一行的数据A and C。现在,我想创建
keys' but unlike fixed keys, these key will be generated while reading the data fromhybrid_blocks. For the first line since there are no preceding line thekeyswill simply beAgAandCgCwhich means (A given A, and C given C), and for the values I count the number ofAinblock Mandblock S`。因此,数据将存储为:
defaultdict(<class 'dict'>, {'M': {'AgA': [4], 'CgC': [1]}, 'S': {'AgA': 2, 'CgC': 2}}
作为,我通读了其他行,我想根据hybrid block 中的字符串创建新键,并根据前一行中的字符串计算该字符串在M vs S 块中出现的次数。这意味着阅读line 2 时的keys 在这一行中将是TgA' which means (T given A) and AgC. For the values inside this key I count the number of times I foundT,在上一行中的A 之后and same forAcG`。
阅读 3 行后的defaultdict 将是。
defaultdict(<class 'dict'>, {'M': {'AgA': 4, 'TgA':3, 'CgT':2}, {'CgC': [1], 'AgC':0, 'GgA':0}, 'S': {'AgA': 2, 'TgA':1, 'CgT':0}, {'CgC': 2, 'AgC':2, 'GgA':2}}
我知道这看起来太复杂了。我经历了几个 dictionary 和 defaultdict 教程,但找不到这样做的方法。
高度赞赏任何部分的解决方案(如果不是两者)。
【问题讨论】:
-
也许使用
pandas并将这个文件读取为CSV文件和space as separator。你会得到DataFrame,它可以轻松访问列-df["M1"]-它可以在没有for循环的情况下做很多事情。 -
我想知道
pandas但是如何在给定数据上运行markov chain。我已经阅读了pandas,但还没有看到任何可以用来申请的方法(AgA, TgA, CgT)?
标签: python pandas numpy dictionary defaultdict