从基因组创建树状图

时间:2016-03-14 06:32:15

标签: python bioinformatics hierarchical-clustering dendrogram genome

我想玩基因组数据:

Species_A = ctnngtggaccgacaagaacagtttcgaatcggaagcttgcttaacgtag
Species_B = ctaagtggactgacaggaactgtttcgaatcggaagcttgcttaacgtag
Species_C = ctacgtggaccgacaagaacagtttcgactcggaagcttgcttaacgtag
Species_D = ctacgtggaccgacaagaacagtttcgactcggaagcttgcttaacgccg
Species_E = ctgtgtggancgacaaggacagttccaaatcggaagcttgcttaacacag

我想根据上面的基因组序列,根据这些生物彼此之间的相关程度来创建树形图。我首先要做的是计算每个物种的数量,然后我创建一个数组,然后绘制一个树状图:

gen_size1 = len(Species_A)
a1 = float(Species_A.count('a'))/float(gen_size1)
c1 = float(Species_A.count('c'))/float(gen_size1)
g1 = float(Species_A.count('g'))/float(gen_size1)
t1 = float(Species_A.count('t'))/float(gen_size1)
.
.
.
gen_size5 = len(Species_E)
a5 = float(Species_E.count('a'))/float(gen_size5)
c5 = float(Species_E.count('c'))/float(gen_size5)
g5 = float(Species_E.count('g'))/float(gen_size5)
t5 = float(Species_E.count('t'))/float(gen_size5)

my_genes = np.array([[a1,c1,g1,t1],[a2,c2,g2,t2],[a3,c3,g3,t3],[a4,c4,g4,t4],[a5,c5,g5,t5]])
plt.subplot(1,2,1)
plt.title("Mononucleotide")
linkage_matrix = linkage(my_genes, "single")
print linkage_matrix
dendrogram(linkage_matrix,truncate_mode='lastp', color_threshold=1, labels=[Species_A, Species_B, Species_C, Species_D, Species_E], show_leaf_counts=True)
plt.show()

物种A和B是同一生物体的变种,我期望它们都应该与根部的共同进化枝不同,物种C和D也应该与根部的另一个共同进化枝分开然后与物种E分开从主根分叉,因为它与物种A到D无关。不幸的是,树状图结果与物种A和E混合,从一个普通的进化枝发散,然后是另一个进化枝中的物种C,D和B(相当混乱)。

我已经阅读了基因组序列的层次聚类,但我观察到它只适应二维系统,不幸的是我有4个维度,即a,c,t和g。还有其他策略吗?谢谢您的帮助!

2 个答案:

答案 0 :(得分:3)

这是生物信息学中相当普遍的问题,因此您应该使用内置此功能的BioPython等生物信息库。

首先使用序列创建一个多FASTA文件:

import os
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import generic_dna


sequences = ['ctnngtggaccgacaagaacagtttcgaatcggaagcttgcttaacgtag',
             'ctaagtggactgacaggaactgtttcgaatcggaagcttgcttaacgtag',
             'ctacgtggaccgacaagaacagtttcgactcggaagcttgcttaacgtag',
             'ctacgtggaccgacaagaacagtttcgactcggaagcttgcttaacgccg',
             'ctgtgtggancgacaaggacagttccaaatcggaagcttgcttaacacag']

my_records = [SeqRecord(Seq(sequence, generic_dna),
              id='Species_{}'.format(letter), description='Species_{}'.format(letter))
              for sequence, letter in zip(sequences, 'ABCDE')]

root_dir = r"C:\Users\BioGeek\Documents\temp"
filename = 'my_sequences'
fasta_path = os.path.join(root_dir, '{}.fasta'.format(filename))

SeqIO.write(my_records, fasta_path, "fasta")

这会创建如下所示的文件C:\Users\BioGeek\Documents\temp\my_sequences.fasta

>Species_A
ctnngtggaccgacaagaacagtttcgaatcggaagcttgcttaacgtag
>Species_B
ctaagtggactgacaggaactgtttcgaatcggaagcttgcttaacgtag
>Species_C
ctacgtggaccgacaagaacagtttcgactcggaagcttgcttaacgtag
>Species_D
ctacgtggaccgacaagaacagtttcgactcggaagcttgcttaacgccg
>Species_E
ctgtgtggancgacaaggacagttccaaatcggaagcttgcttaacacag

接下来,使用命令行工具ClustalW进行多序列比对:

from Bio.Align.Applications import ClustalwCommandline
clustalw_exe = r"C:\path\to\clustalw-2.1\clustalw2.exe"
assert os.path.isfile(clustalw_exe), "Clustal W executable missing"
clustalw_cline = ClustalwCommandline(clustalw_exe, infile=fasta_path)
stdout, stderr = clustalw_cline()    
print stdout

打印:

 CLUSTAL 2.1 Multiple Sequence Alignments


Sequence format is Pearson
Sequence 1: Species_A     50 bp
Sequence 2: Species_B     50 bp
Sequence 3: Species_C     50 bp
Sequence 4: Species_D     50 bp
Sequence 5: Species_E     50 bp
Start of Pairwise alignments
Aligning...

Sequences (1:2) Aligned. Score:  90
Sequences (1:3) Aligned. Score:  94
Sequences (1:4) Aligned. Score:  88
Sequences (1:5) Aligned. Score:  84
Sequences (2:3) Aligned. Score:  90
Sequences (2:4) Aligned. Score:  84
Sequences (2:5) Aligned. Score:  78
Sequences (3:4) Aligned. Score:  94
Sequences (3:5) Aligned. Score:  82
Sequences (4:5) Aligned. Score:  82
Guide tree file created:   [C:\Users\BioGeek\Documents\temp\my_sequences.dnd]

There are 4 groups
Start of Multiple Alignment

Aligning...
Group 1: Sequences:   2      Score:912
Group 2: Sequences:   2      Score:921
Group 3: Sequences:   4      Score:865
Group 4: Sequences:   5      Score:855
Alignment Score 2975

CLUSTAL-Alignment file created  [C:\Users\BioGeek\Documents\temp\my_sequences.aln]

my_sequences.dnd文件ClustalW创建,是标准Newick tree fileBio.Phylo可以解析这些:

from Bio import Phylo
newick_path = os.path.join(root_dir, '{}.dnd'.format(filename))
tree = Phylo.read(newick_path, "newick")
Phylo.draw_ascii(tree)

打印哪些:

       ____________ Species_A
  ____|
 |    |_____________________________________ Species_B
 |
_|          ____ Species_C
 |_________|
 |         |_________________________ Species_D
 |
 |__________________________________________________________________ Species_E

或者,如果您安装了matplotlibpylab,则可以使用draw功能创建图形:

tree.rooted = True
Phylo.draw(tree, branch_labels=lambda c: c.branch_length)

产生:

dendrogram

这个树状图清楚地说明了你观察到的东西:物种A和B是同一生物的变种,并且都与根部的共同分支发散。与物种C和D相同,两者都与根部的另一个共同分支不同。最后,物种E偏离主根,因为它与物种A到D无关。

答案 1 :(得分:0)

好吧,使用SciPy你可以使用自定距离(我的投注是在Needleman-Wunsch或Smith-Waterman上作为开始)。 Here是如何使用输入数据的示例。您还应该检查how to define a custom distance in SciPy。设置好后,您可以使用更高级的对齐方法,例如MAFFT。您可以提取基因组之间的关系,并在创建树形图时使用它们。