本文整理匯總了Python中nexus.NexusReader.read_string方法的典型用法代碼示例。如果您正苦於以下問題:Python NexusReader.read_string方法的具體用法?Python NexusReader.read_string怎麽用?Python NexusReader.read_string使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nexus.NexusReader
的用法示例。
在下文中一共展示了NexusReader.read_string方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: snpMatrixGenerator
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def snpMatrixGenerator(sourceFile, destFile, recordAll=False,
recordRandomSample=True):
if recordAll == recordRandomSample:
print "Invalid Options"
exit()
destNexus = NexusWriter()
block = ""
snpCol = 0
for line in sourceFile:
if all(x in line.lower() for x in {"begin", "data"}):
sourceNexus = NexusReader()
sourceNexus.read_string(block)
if "data" in sourceNexus.blocks:
snpCol = _findDifferences(sourceNexus, destNexus, snpCol,
recordAll, recordRandomSample)
block = line
else:
block += line
sourceNexus = NexusReader()
sourceNexus.read_string(block)
if "data" in sourceNexus.blocks:
snpCol = _findDifferences(sourceNexus, destNexus, snpCol,
recordAll, recordRandomSample)
destFile.write(destNexus.make_nexus() + '\n')
destFile.close()
sourceFile.close()
示例2: test_find_unique_sites_2
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_find_unique_sites_2(self):
nexus = NexusReader()
nexus.read_string("""Begin data;
Dimensions ntax=4 nchar=7;
Format datatype=standard symbols="01" gap=-;
Matrix
Harry 10000?-
Simon 1100011
Betty 1110000
Louise 1111000
;""")
unique = find_unique_sites(nexus)
# site 1 should NOT be in the uniques (3x1 and 1x0)
# - i.e. are we ignoring sites with ONE absent taxon
assert 1 not in unique
# these should also NOT be in unique
assert 0 not in unique
assert 2 not in unique
assert 4 not in unique # constant
# site 3 is a simple unique site - check we found it
assert 3 in unique
# sites 5 and 6 should also be unique
# - are we handling missing data appropriately?
assert 5 in unique
assert 6 in unique
示例3: test_read_string
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_read_string(self):
handle = open(os.path.join(EXAMPLE_DIR, 'example.nex'))
data = handle.read()
handle.close()
nex = NexusReader()
nex.read_string(data)
assert 'data' in nex.blocks
assert 'Simon' in nex.blocks['data'].matrix
示例4: test_notimplemented_exception
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_notimplemented_exception(self):
with self.assertRaises(NotImplementedError):
nex = NexusReader()
nex.read_string(
"""Begin something;
Dimensions ntax=5 nchar=1;
Format datatype=standard symbols="01" gap=-;
Matrix
Harry 1
;""")
anonymise(nex)
示例5: test_combine_with_character_labels
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_combine_with_character_labels(self):
n1 = NexusReader()
n1.read_string(
"""
BEGIN DATA;
DIMENSIONS NTAX=3 NCHAR=3;
FORMAT DATATYPE=STANDARD MISSING=0 GAP=- SYMBOLS="123";
CHARSTATELABELS
1 char1,
2 char2,
3 char3
;
MATRIX
Tax1 123
Tax2 123
Tax3 123
;
"""
)
n2 = NexusReader()
n2.read_string(
"""
BEGIN DATA;
DIMENSIONS NTAX=3 NCHAR=3;
FORMAT DATATYPE=STANDARD MISSING=0 GAP=- SYMBOLS="456";
CHARSTATELABELS
1 char1,
2 char2,
3 char3
;
MATRIX
Tax1 456
Tax2 456
Tax3 456
;
"""
)
newnex = combine_nexuses([n1, n2])
assert re.search(r"""\bNTAX=3\b""", newnex.write())
assert re.search(r"""\bNCHAR=6\b""", newnex.write())
assert re.search(r'\sSYMBOLS="123456"[\s;]', newnex.write())
for tax in [1,2,3]:
assert re.search(r"""\bTax%d\s+123456\b""" % tax, newnex.write())
counter = 1
for nex_id in [1,2]:
for char_id in [1,2,3]:
assert re.search(
r"""\b%d\s+%d.char%d\b""" % (counter, nex_id, char_id),
newnex.write(charblock=True)
)
counter += 1
示例6: test_incorrect_dimensions_warnings_nchar
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_incorrect_dimensions_warnings_nchar(self):
with warnings.catch_warnings(record=True) as w:
nex = NexusReader()
nex.read_string(
"""Begin data;
Dimensions ntax=1 nchar=5;
Format datatype=standard symbols="01" gap=-;
Matrix
Harry 1
;""")
assert len(w) == 1, 'Expected 1 warning, got %r' % w
assert issubclass(w[-1].category, UserWarning)
assert "Expected" in str(w[-1].message)
assert nex.data.nchar == 1
示例7: test_treelabel
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_treelabel(self):
nex = NexusReader()
nex.read_string("""
#NEXUS
begin trees;
translate
0 Tom,
1 Simon,
2 Fred;
tree TREEONE = (0,1,2);
end;
""")
assert len(nex.trees.trees) == 1
assert nex.trees.trees == ['tree TREEONE = (0,1,2);']
示例8: test_labelled_unrooted
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_labelled_unrooted(self):
nex = NexusReader()
nex.read_string("""
#NEXUS
begin trees;
translate
0 Tom,
1 Simon,
2 Fred;
tree unrooted [U] = (0,1,2);
end;
""")
assert len(nex.trees.trees) == 1
assert nex.trees.trees == ['tree unrooted [U] = (0,1,2);']
示例9: Test_Binarise
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
class Test_Binarise(unittest.TestCase):
def setUp(self):
self.nex = NexusReader()
self.nex.read_string(
"""Begin data;
Dimensions ntax=3 nchar=2;
Format datatype=standard symbols="01" gap=-;
Charstatelabels
1 char1, 2 char2;
Matrix
Maori 14
Dutch 25
Latin 36
;""")
self.nex = binarise(self.nex)
def test_to_binary(self):
"""Test Nexus -> Binary: Two Character"""
expected = {
'char1_0': {"Maori": '1', "Dutch": "0", "Latin": "0"},
'char1_1': {"Maori": '0', "Dutch": "1", "Latin": "0"},
'char1_2': {"Maori": '0', "Dutch": "0", "Latin": "1"},
'char2_0': {"Maori": '1', "Dutch": "0", "Latin": "0"},
'char2_1': {"Maori": '0', "Dutch": "1", "Latin": "0"},
'char2_2': {"Maori": '0', "Dutch": "0", "Latin": "1"},
}
for char, data in expected.items():
for taxon, exp_value in data.items():
assert self.nex.data[char][taxon] == exp_value
def test_to_binary_nchar(self):
"""Test Nexus -> Binary: Number of Characters"""
assert len(self.nex.characters) == 6
def test_to_binary_symbollist(self):
"""Test Nexus -> Binary: Update Symbol List"""
# check symbol list was updated
assert len(self.nex.symbols) == 2
assert '1' in self.nex.symbols
assert '0' in self.nex.symbols
def test_to_binary_nexus(self):
"""Test Nexus -> Binary: Nexus"""
nexus = self.nex.make_nexus(interleave=False)
assert re.search("Dutch\s+010010", nexus)
assert re.search("Maori\s+100100", nexus)
assert re.search("Latin\s+001001", nexus)
示例10: test_ok_starting_with_one
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_ok_starting_with_one(self):
nex = NexusReader()
nex.read_string("""
#NEXUS
begin trees;
translate
1 Tom,
2 Simon,
3 Fred;
tree tree = (1,2,3)
end;
""")
assert len(nex.trees.translators) == 3
assert '1' in nex.trees.translators
assert '2' in nex.trees.translators
assert '3' in nex.trees.translators
示例11: test_ok_starting_with_zero
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_ok_starting_with_zero(self):
nex = NexusReader()
nex.read_string("""
#NEXUS
begin trees;
translate
0 Tom,
1 Simon,
2 Fred;
tree tree = (0,1,2)
end;
""")
assert len(nex.trees.translators) == 3
assert '0' in nex.trees.translators
assert '1' in nex.trees.translators
assert '2' in nex.trees.translators
示例12: Test_DataHandler_Regression_Mesquite
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
class Test_DataHandler_Regression_Mesquite(unittest.TestCase):
"""Regression: Test that we can parse MESQUITE data blocks"""
def setUp(self):
self.nex = NexusReader()
self.nex.read_string("""
#NEXUS
Begin data;
TITLE Untitled_Block_of_Taxa;
LINK Taxa = Untitled_Block_of_Taxa;
Dimensions ntax=2 nchar=2;
Format datatype=standard gap=- symbols="01";
Matrix
Harry 00
Simon 01
;
End;
""")
def test_attributes(self):
assert len(self.nex.data.attributes) == 2
assert self.nex.data.attributes[0] == \
"""TITLE Untitled_Block_of_Taxa;"""
assert self.nex.data.attributes[1] == \
"""LINK Taxa = Untitled_Block_of_Taxa;"""
def test_write(self):
expected_patterns = [
'^begin data;$',
'^\s+TITLE Untitled_Block_of_Taxa;$',
'^\s+LINK Taxa = Untitled_Block_of_Taxa;$',
'^\s+dimensions ntax=2 nchar=2;$',
'^\s+format datatype=standard gap=- symbols="01";$',
"^matrix$",
"^Harry\s+00",
"^Simon\s+01$",
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), \
'Expected "%s"' % expected
示例13: Test_TallyBySite
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
class Test_TallyBySite(unittest.TestCase):
def setUp(self):
self.nex = NexusReader()
self.nex.read_string(
"""Begin data;
Dimensions ntax=3 nchar=6;
Format datatype=standard symbols="12" gap=-;
Matrix
Harry 0111-?
Simon 0011-?
Elvis 0001-?
;"""
)
def test_errorcheck(self):
self.assertRaises(TypeError, tally_by_site, "I am a string")
self.assertRaises(TypeError, tally_by_site, 0)
def test_tally_by_site(self):
tally = tally_by_site(self.nex)
# 000
assert 'Harry' in tally[0]['0']
assert 'Simon' in tally[0]['0']
assert 'Elvis' in tally[0]['0']
# 100
assert 'Harry' in tally[1]['1']
assert 'Simon' in tally[1]['0']
assert 'Elvis' in tally[1]['0']
# 110
assert 'Harry' in tally[2]['1']
assert 'Simon' in tally[2]['1']
assert 'Elvis' in tally[2]['0']
# 111
assert 'Harry' in tally[3]['1']
assert 'Simon' in tally[3]['1']
assert 'Elvis' in tally[3]['1']
# ---
assert 'Harry' in tally[4]['-']
assert 'Simon' in tally[4]['-']
assert 'Elvis' in tally[4]['-']
# ???
assert 'Harry' in tally[5]['?']
assert 'Simon' in tally[5]['?']
assert 'Elvis' in tally[5]['?']
示例14: test_regression_include_invisible_taxa
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_regression_include_invisible_taxa(self):
"""Include taxa that have no entries"""
data = """
#NEXUS
BEGIN DATA;
DIMENSIONS NTAX=15 NCHAR=7;
FORMAT DATATYPE=STANDARD MISSING=? GAP=- INTERLEAVE=YES;
MATRIX
Gertrude 0000001
Debbie 0001000
Zarathrustra 0000000
Christie 0010000
Benny 0100000
Bertha 0100000
Craig 0010000
Fannie-May 0000010
Charles 0010000
Annik 1000000
Frank 0000010
Amber 1000000
Andreea 1000000
Edward 0000100
Donald 0001000
;
END;
"""
nex = NexusReader()
nex.read_string(data)
msnex = multistatise(nex)
for taxon, sites in msnex.data.matrix.items():
if taxon[0] == 'Z':
continue # will check later
# first letter of taxa name is the expected character state
assert taxon[0] == sites[0], \
"%s should be %s not %s" % (taxon, taxon[0], sites[0])
# deal with completely missing taxa
assert 'Zarathrustra' in msnex.data.matrix
assert msnex.data.matrix['Zarathrustra'][0] == '?'
示例15: test_count_other_values_two
# 需要導入模塊: from nexus import NexusReader [as 別名]
# 或者: from nexus.NexusReader import read_string [as 別名]
def test_count_other_values_two(self):
expected = {"Harry": 1, "Simon": 2, "Peter": 1, "Betty": 0, "Louise": 0}
nexus = NexusReader()
nexus.read_string(
"""#NEXUS
Begin data;
Dimensions ntax=5 nchar=3;
Format datatype=standard symbols="01" gap=-;
Matrix
Harry 0A0 [No missing]
Simon 0AB [one missing]
Peter 0-B [one gap]
Betty ?-1 [one gap and one missing = 2 missing]
Louise ??? [three missing]
;
End;
"""
)
count = count_site_values(nexus, ["A", "B"])
for taxon in count:
assert count[taxon] == expected[taxon]