本文整理汇总了Python中skbio.Alignment.distances方法的典型用法代码示例。如果您正苦于以下问题:Python Alignment.distances方法的具体用法?Python Alignment.distances怎么用?Python Alignment.distances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skbio.Alignment
的用法示例。
在下文中一共展示了Alignment.distances方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_distance_matrix
# 需要导入模块: from skbio import Alignment [as 别名]
# 或者: from skbio.Alignment import distances [as 别名]
def compute_distance_matrix(msa_file, csvfile="distance_mat.csv"):
"""
load up some aligned sequences, and compute a distance matrix
compute distances between the sequences using the hamming function
see also:
scipy.spatial.distance.hamming
@args msa_file: multiple sequence alignment in fasta format
@type msa_file: str
@args csvfile: output distance matrix file in csv format
@type csvfile: str
"""
records = []
for rec in SeqIO.parse(msa_file, "fasta"):
records.append(RNA(rec.seq, rec.id))
aln = Alignment(records)
master_dm = aln.distances()
## writing the result to a csv file
csv_header_row = [header for header in master_dm.ids]
## result as a list of list
with open(csvfile, "w") as output:
writer = csv.writer(output, lineterminator="\n")
writer.writerows([csv_header_row])
writer.writerows(master_dm)
output.close()
示例2: AlignmentTests
# 需要导入模块: from skbio import Alignment [as 别名]
# 或者: from skbio.Alignment import distances [as 别名]
class AlignmentTests(TestCase):
def setUp(self):
self.d1 = DNA('..ACC-GTTGG..', metadata={'id': "d1"})
self.d2 = DNA('TTACCGGT-GGCC', metadata={'id': "d2"})
self.d3 = DNA('.-ACC-GTTGC--', metadata={'id': "d3"})
self.r1 = RNA('UUAU-', metadata={'id': "r1"})
self.r2 = RNA('ACGUU', metadata={'id': "r2"})
self.seqs1 = [self.d1, self.d2, self.d3]
self.seqs2 = [self.r1, self.r2]
self.a1 = Alignment(self.seqs1)
self.a2 = Alignment(self.seqs2)
self.a3 = Alignment(self.seqs2, score=42.0,
start_end_positions=[(0, 3), (5, 9)])
self.a4 = Alignment(self.seqs2, score=-42.0,
start_end_positions=[(1, 4), (6, 10)])
# no sequences
self.empty = Alignment([])
# sequences, but no positions
self.no_positions = Alignment([RNA('', metadata={'id': 'a'}),
RNA('', metadata={'id': 'b'})])
def test_degap(self):
expected = SequenceCollection([
DNA('ACCGTTGG', metadata={'id': "d1"}),
DNA('TTACCGGTGGCC', metadata={'id': "d2"}),
DNA('ACCGTTGC', metadata={'id': "d3"})])
actual = self.a1.degap()
self.assertEqual(actual, expected)
expected = SequenceCollection([
RNA('UUAU', metadata={'id': "r1"}),
RNA('ACGUU', metadata={'id': "r2"})])
actual = self.a2.degap()
self.assertEqual(actual, expected)
def test_distances(self):
expected = [[0, 6. / 13, 4. / 13],
[6. / 13, 0, 7. / 13],
[4. / 13, 7. / 13, 0]]
expected = DistanceMatrix(expected, ['d1', 'd2', 'd3'])
actual = self.a1.distances()
self.assertEqual(actual, expected)
# alt distance function provided
def dumb_distance(s1, s2):
return 42.
expected = [[0, 42., 42.],
[42., 0, 42.],
[42., 42., 0]]
expected = DistanceMatrix(expected, ['d1', 'd2', 'd3'])
actual = self.a1.distances(dumb_distance)
self.assertEqual(actual, expected)
def test_score(self):
self.assertEqual(self.a3.score(), 42.0)
self.assertEqual(self.a4.score(), -42.0)
def test_start_end_positions(self):
self.assertEqual(self.a3.start_end_positions(), [(0, 3), (5, 9)])
self.assertEqual(self.a4.start_end_positions(), [(1, 4), (6, 10)])
def test_subalignment(self):
# keep seqs by ids
actual = self.a1.subalignment(seqs_to_keep=['d1', 'd3'])
expected = Alignment([self.d1, self.d3])
self.assertEqual(actual, expected)
# keep seqs by indices
actual = self.a1.subalignment(seqs_to_keep=[0, 2])
expected = Alignment([self.d1, self.d3])
self.assertEqual(actual, expected)
# keep seqs by ids (invert)
actual = self.a1.subalignment(seqs_to_keep=['d1', 'd3'],
invert_seqs_to_keep=True)
expected = Alignment([self.d2])
self.assertEqual(actual, expected)
# keep seqs by indices (invert)
actual = self.a1.subalignment(seqs_to_keep=[0, 2],
invert_seqs_to_keep=True)
expected = Alignment([self.d2])
self.assertEqual(actual, expected)
# keep positions
actual = self.a1.subalignment(positions_to_keep=[0, 2, 3])
d1 = DNA('.AC', metadata={'id': "d1"})
d2 = DNA('TAC', metadata={'id': "d2"})
d3 = DNA('.AC', metadata={'id': "d3"})
expected = Alignment([d1, d2, d3])
self.assertEqual(actual, expected)
# keep positions (invert)
actual = self.a1.subalignment(positions_to_keep=[0, 2, 3],
#.........这里部分代码省略.........
示例3: AlignmentTests
# 需要导入模块: from skbio import Alignment [as 别名]
# 或者: from skbio.Alignment import distances [as 别名]
class AlignmentTests(TestCase):
def setUp(self):
self.d1 = DNASequence('..ACC-GTTGG..', id="d1")
self.d2 = DNASequence('TTACCGGT-GGCC', id="d2")
self.d3 = DNASequence('.-ACC-GTTGC--', id="d3")
self.r1 = RNASequence('UUAU-', id="r1")
self.r2 = RNASequence('ACGUU', id="r2")
self.seqs1 = [self.d1, self.d2, self.d3]
self.seqs2 = [self.r1, self.r2]
self.seqs1_t = [('d1', '..ACC-GTTGG..'), ('d2', 'TTACCGGT-GGCC'),
('d3', '.-ACC-GTTGC--')]
self.seqs2_t = [('r1', 'UUAU-'), ('r2', 'ACGUU')]
self.a1 = Alignment(self.seqs1)
self.a2 = Alignment(self.seqs2)
self.a3 = Alignment(self.seqs2, score=42.0,
start_end_positions=[(0, 3), (5, 9)])
self.a4 = Alignment(self.seqs2, score=-42.0,
start_end_positions=[(1, 4), (6, 10)])
self.empty = Alignment([])
def test_degap(self):
"""degap functions as expected
"""
expected = [(id_, seq.replace('.', '').replace('-', ''))
for id_, seq in self.seqs1_t]
expected = SequenceCollection.from_fasta_records(expected, DNASequence)
actual = self.a1.degap()
self.assertEqual(actual, expected)
expected = [(id_, seq.replace('.', '').replace('-', ''))
for id_, seq in self.seqs2_t]
expected = SequenceCollection.from_fasta_records(expected, RNASequence)
actual = self.a2.degap()
self.assertEqual(actual, expected)
def test_distances(self):
"""distances functions as expected
"""
expected = [[0, 6. / 13, 4. / 13],
[6. / 13, 0, 7. / 13],
[4. / 13, 7. / 13, 0]]
expected = DistanceMatrix(expected, ['d1', 'd2', 'd3'])
actual = self.a1.distances()
self.assertEqual(actual, expected)
# alt distance function provided
def dumb_distance(s1, s2):
return 42.
expected = [[0, 42., 42.],
[42., 0, 42.],
[42., 42., 0]]
expected = DistanceMatrix(expected, ['d1', 'd2', 'd3'])
actual = self.a1.distances(dumb_distance)
self.assertEqual(actual, expected)
def test_score(self):
self.assertEqual(self.a3.score(), 42.0)
self.assertEqual(self.a4.score(), -42.0)
def test_start_end_positions(self):
self.assertEqual(self.a3.start_end_positions(), [(0, 3), (5, 9)])
self.assertEqual(self.a4.start_end_positions(), [(1, 4), (6, 10)])
def test_subalignment(self):
"""subalignment functions as expected
"""
# keep seqs by ids
actual = self.a1.subalignment(seqs_to_keep=['d1', 'd3'])
expected = Alignment([self.d1, self.d3])
self.assertEqual(actual, expected)
# keep seqs by indices
actual = self.a1.subalignment(seqs_to_keep=[0, 2])
expected = Alignment([self.d1, self.d3])
self.assertEqual(actual, expected)
# keep seqs by ids (invert)
actual = self.a1.subalignment(seqs_to_keep=['d1', 'd3'],
invert_seqs_to_keep=True)
expected = Alignment([self.d2])
self.assertEqual(actual, expected)
# keep seqs by indices (invert)
actual = self.a1.subalignment(seqs_to_keep=[0, 2],
invert_seqs_to_keep=True)
expected = Alignment([self.d2])
self.assertEqual(actual, expected)
# keep positions
actual = self.a1.subalignment(positions_to_keep=[0, 2, 3])
d1 = DNASequence('.AC', id="d1")
d2 = DNASequence('TAC', id="d2")
d3 = DNASequence('.AC', id="d3")
expected = Alignment([d1, d2, d3])
self.assertEqual(actual, expected)
#.........这里部分代码省略.........