Beispiel #1
0
 def test_find_values(self):
     v1 = cif.read_string('data_a _v1 one')[0].find_values('_v1')
     gc.collect()
     self.assertListEqual(list(v1), ['one'])
     v2 = cif.read_string('data_a loop_ _v2 a b')[0].find_values('_v2')
     gc.collect()
     self.assertListEqual(list(v2), ['a', 'b'])
Beispiel #2
0
 def test_write_style(self):
     doc = cif.read_string('data_one _x y')
     self.assertEqual(doc.as_string(), doc[0].as_string())
     self.assertEqual(doc.as_string().splitlines(), ['data_one', '_x y'])
     self.assertEqual(
         doc.as_string(style=cif.Style.Indent35).splitlines(),
         ['data_one', '_x                                y'])
Beispiel #3
0
 def test_line_endings(self):
     lines = ['data_a', '_a_field', ';', 'text line', '2nd line', ';']
     for eol in ('\r\n', '\n'):
         input_str = eol.join(lines) + eol
         doc = cif.read_string(input_str)
         output_str = doc.as_string()
         self.assertEqual(input_str.replace(eol, '\n'), output_str)
Beispiel #4
0
 def test_set_pair(self):
     doc = cif.read_string('data_a _a 1 _b 2 _c 3')
     block = doc[0]
     block.set_pair('_d', '9')
     block.set_pair('_b', '8')
     self.assertEqual(block.find_pair('_a'), ['_a', '1'])
     self.assertEqual(block.find_value('_b'), '8')
     self.assertEqual(block.find_value('_c'), '3')
     self.assertEqual(block.find_value('_d'), '9')
Beispiel #5
0
 def test_set_loop(self):
     block = cif.read_string('data_a _c.a 1 _c.b 2 _c.c 3 loop_ _cx.b 3')[0]
     block.init_loop('_cx.', ['b']).add_row(['x'])
     self.assertEqual(block.find_value('_c.a'), '1')
     block.init_mmcif_loop('_c.', ['c']).add_row(['y'])
     self.assertEqual(block.find_value('_c.a'), None)
     loop = block.init_loop('_c.', ['c', 'd'])
     loop.set_all_values([list('one'), list('two')])
     self.assertEqual(loop.width(), 2)
     self.assertEqual(loop.length(), 3)
     self.assertEqual(list(block.find(['_c.c', '_c.d'])[1]), ['n', 'w'])
Beispiel #6
0
 def test_setitem(self):
     block = cif.read_string('data_a _a 1 _b 2 _c 3')[0]
     self.assertEqual(block.find_value('_b'), '2')
     col_b = block.find_values('_b')
     col_b[0] = '20'
     self.assertEqual(block.find_value('_b'), '20')
     bc = block.find(['_b', '_a'])
     bc[0][0] = '30'
     self.assertEqual(block.find_value('_b'), '30')
     bc[0][1] = '40'
     self.assertEqual(block.find_value('_a'), '40')
Beispiel #7
0
 def test_relion_syntax_exception(self):
     block = cif.read_string("""\
         data_
         loop_
         _rlnImageName
         _rlnMicrographName
         _rlnDefocusU
         _rlnDefocusV
         mic1/img000001.spi mic1 10000 10500
         mic1/img000002.spi mic1 10000 10500
         """).sole_block()
     self.assertEqual(block.name, '#')
Beispiel #8
0
 def test_cif_as_string(self):
     with open(self.basename + '.cif', 'rb') as f:
         cif_orig = f.read().decode('utf-8').replace('\r\n', '\n')
     cif_doc = cif.read_string(cif_orig)
     formatting_changes = {
         '# comment\n': '',
         '_x _y _z': '_x\n_y\n_z',
         '_b 2 3 4': '_b\n2\n3\n4',
         'pdbx_details    ': 'pdbx_details ',
     }
     for k, v in formatting_changes.items():
         cif_orig = cif_orig.replace(k, v)
     cif_out = cif_doc.as_string()
     self.assertListEqual(nonempty_lines(cif_orig), nonempty_lines(cif_out))
Beispiel #9
0
 def test_slice(self):
     doc = cif.read_string("""
         data_a
         _one 1 _two 2 _three 3
         data_b
         _four 4
         data_c
         _two 2 _four 4 _six 6
     """)
     self.assertEqual([b.name for b in doc[:1]], ['a'])
     self.assertEqual([b.name for b in doc[1:]], ['b', 'c'])
     self.assertEqual([b.name for b in doc[:]], ['a', 'b', 'c'])
     self.assertEqual([b.name for b in doc[1:-1]], ['b'])
     self.assertEqual([b.name for b in doc[1:1]], [])
Beispiel #10
0
    def test_find(self):
        block = cif.read_string("""
            data_test
            _one 1 _two 2 _three 3
            _nonloop_a alpha
            _nonloop_b beta
            loop_ _la _lb _ln A B 1 C D 2
        """).sole_block()
        gc.collect()
        values = block.find_values('_la')
        self.assertEqual(list(values), ['A', 'C'])

        rows = list(block.find(['_lb', '_la']))  # changed order
        gc.collect()
        self.assertEqual(list(rows[0]), ['B', 'A'])
        self.assertEqual(rows[1][1], 'C')

        self.assertEqual(block.get_index('_nonloop_b'), 4)
        self.assertIsNone(block[4].loop)
        self.assertEqual(block[4].pair[0], '_nonloop_b')
        self.assertEqual(block.get_index('_lb'), 5)
        self.assertEqual(block[5].loop.tags[1], '_lb')

        rows = list(block.find('_nonloop_', ['a', 'b']))
        gc.collect()
        self.assertEqual([list(r) for r in rows], [['alpha', 'beta']])

        tab = block.find('_l', ['a', 'n'])
        self.assertEqual(tab.find_row('A')[1], '1')
        self.assertRaises(RuntimeError, tab.find_row, 'B')
        self.assertEqual(tab.find_row('C')[1], '2')
        self.assertEqual(tab.column(0)[1], 'C')
        self.assertEqual(tab.column(1)[0], '1')
        self.assertEqual(tab.find_column('a')[1], 'C')
        self.assertEqual(tab.find_column('_ln')[0], '1')

        tab = block.find(['_lb', '_ln'])
        self.assertRaises(RuntimeError, tab.find_row, 'A')
        self.assertEqual(tab.find_row('B')[1], '1')
        self.assertEqual(tab.find_row('D')[1], '2')

        tab = block.find(['_la', '?_nop', '?_ln'])
        self.assertEqual(len(tab), 2)
        self.assertEqual(tab.width(), 3)
        row = tab[0]
        self.assertIsNotNone(row.get(0))
        self.assertEqual(row[0], 'A')
        self.assertIsNone(row.get(1))
        self.assertEqual(row[2], '1')
        self.assertTrue("None" in repr(row))
Beispiel #11
0
 def test_relion_syntax_exception(self):
     block = cif.read_string("""\
         data_
         loop_
         _rlnImageName
         _rlnMicrographName
         _rlnDefocusU
         _rlnDefocusV
         mic1/img000001.spi mic1 10000 10500
         mic1/img000002.spi mic1 10000 10501
         """).sole_block()
     self.assertEqual(block.name, '#')
     item = block[0]
     self.assertEqual(item.line_number, 2)
     table = block.item_as_table(item)
     self.assertEqual(table[1][3], '10501')
Beispiel #12
0
 def test_add_row(self):
     block = cif.read_string('data_a loop_ _x _y 1 2 3 4')[0]
     loop = block.find_loop('_x').get_loop()
     loop.add_row(['5', '6'])
     loop.add_row(['?', '0'], 0)
     self.assertEqual(list(block.find_values('_y')), '0 2 4 6'.split())
     self.assertEqual(loop.length(), 4)
     block.find(['_x']).append_row(['xa'])
     block.find(['_y']).append_row(['ya'])
     block.find(['_y', '_x']).append_row(['yb', 'xb'])
     block.find(['_x', '_y']).append_row(['xc', 'yc'])
     self.assertEqual(loop.length(), 8)
     self.assertEqual(list(block.find_values('_x')),
                      '? 1 3 5 xa . xb xc'.split())
     self.assertEqual(list(block.find_values('_y')),
                      '0 2 4 6 . ya yb yc'.split())
Beispiel #13
0
    def test_case_sensitivity(self):
        block = cif.read_string("""
            daTA_test
            _One 1 _two 2 _thrEE 3
            _NonLoop_a alpha
            _nonloop_B beta
            loop_ _laAa _lbBb _ln A B 1 C D 2
        """).sole_block()
        values = block.find_values('_laaA')
        self.assertEqual(list(values), ['A', 'C'])

        rows = list(block.find(['_lBbb', '_lAaa']))  # changed order
        self.assertEqual(list(rows[0]), ['B', 'A'])

        self.assertEqual(block.get_index('_nonlOOp_b'), 4)
        self.assertEqual(block.get_index('_lBBB'), 5)
        self.assertEqual(block.find_pair('_Three'), ['_thrEE', '3'])
Beispiel #14
0
    def test_find(self):
        block = cif.read_string("""
            data_test
            _nonloop_a alpha
            _nonloop_b beta
            loop_ _la _lb A B C D
        """).sole_block()
        gc.collect()
        rows = list(block.find('_la'))
        self.assertEqual(list(rows[0]), ['A'])

        rows = list(block.find(['_lb', '_la']))  # changed order
        gc.collect()
        self.assertEqual(list(rows[0]), ['B', 'A'])
        self.assertEqual(rows[1][1], 'C')

        rows = list(block.find('_nonloop_', ['a', 'b']))
        gc.collect()
        self.assertEqual([list(r) for r in rows], [['alpha', 'beta']])
Beispiel #15
0
 def test_options(self):
     doc = cif.read_string('data_OnE loop_ _TwO 2 _ZeRo 0')
     j1 = json.loads(doc.as_json())
     self.assertEqual(j1, {'one': {'_two': [2], '_zero': 0}})
     j2 = json.loads(doc.as_json(lowercase_names=False))
     self.assertEqual(j2, {'OnE': {'_TwO': [2], '_ZeRo': 0}})
Beispiel #16
0
 def test_add_row(self):
     block = cif.read_string('data_a loop_ _x _y 1 2 3 4')[0]
     loop = block.find_loop('_x').get_loop()
     loop.add_row(['5', '6'])
     loop.add_row(['?', '0'], 0)
     self.assertEqual(list(block.find_values('_y')), '0 2 4 6'.split())
Beispiel #17
0
 def test_erase(self):
     block = cif.read_string('data_a _a_a 1 _a_b 2 _a_c 3 loop_ _x 4')[0]
     block.find('_a_', ['a', '?x', '?c', '?bb']).erase()
     self.assertEqual(block.as_string().split(),
                      ['data_a', '_a_b', '2', 'loop_', '_x', '4'])
Beispiel #18
0
 def test_syntax_error(self):
     with self.assertRaises(ValueError):
         cif.read_string('data_a boom')