def test_dict2xml_and_xml2dict(self): d = {} generated_xml = util.dict2xml(d, name='foo', pretty=False) self.assertEqual(generated_xml, '<?xml version="1.0" ?><foo/>') self.assertEqual(d, util.xml2dict(generated_xml)) d = { 'a': 1, 'b': 2, 'c': 3, } generated_xml = util.dict2xml(d, name='foobar', pretty=False) desired_output = '<?xml version="1.0" ?><foobar><a>__int1</a><b>__int2</b><c>__int3</c></foobar>' generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(len(generated_d), 3) self.assertEqual(generated_d, d) d = {'a': {'aa': 1}} desired_output = '<?xml version="1.0" ?><foo><a><aa>__int1</aa></a></foo>' generated_xml = util.dict2xml(d, name='foo', pretty=False) generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(generated_d, d) d = { 'a': { '11': 1, 12: 1, } } desired_output = '<?xml version="1.0" ?><foo><a><__int12>__int1</__int12><__num11>__int1</__num11></a></foo>' generated_xml = util.dict2xml(d, name='foo', pretty=False) generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(generated_d, d) d = { 'a': { 'aa': 1, 'ab': 1, }, 'b': { 'ba': 2, 'bb': 2, }, } desired_output = '<?xml version="1.0" ?><foo><a><aa>__int1</aa><ab>__int1</ab></a><b><ba>__int2</ba><bb>__int2</bb></b></foo>' generated_xml = util.dict2xml(d, name='foo', pretty=False) generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(generated_d, d)
def main(): # Set up parameter options ap = argparse.ArgumentParser(description=description) ap.add_argument('bedfile', help='Input bed format file', nargs='?', type=argparse.FileType('r'), default=sys.stdin) ap.add_argument('-o', '--out-format', help='Output format', choices=['xml','tsv', 'pkl'], default='pkl') params = ap.parse_args() g2l = defaultdict(int) with params.bedfile as f: for line in f: la = line.strip().split() g2l[la[3]] += int(la[2]) - int(la[1]) # Output results if params.out_format == 'tsv': for g,l in g2l.iteritems(): sys.stdout.write('%s\t%s\n' % (g,l)) # Output in xml format elif params.out_format == 'xml': sys.stdout.write(util.dict2xml(g2l)) # Output in pickle format else: pickle.dump(g2l, sys.stdout, pickle.HIGHEST_PROTOCOL)
def test_dict2xml_and_xml2dict(self): d = {} generated_xml = util.dict2xml(d, name='foo', pretty=False) self.assertEqual(generated_xml, '<?xml version="1.0" ?><foo/>') self.assertEqual(d, util.xml2dict(generated_xml)) d = {'a': 1, 'b': 2, 'c': 3,} generated_xml = util.dict2xml(d, name='foobar', pretty=False) desired_output = '<?xml version="1.0" ?><foobar><a>__int1</a><b>__int2</b><c>__int3</c></foobar>' generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(len(generated_d), 3) self.assertEqual(generated_d, d) d = {'a': {'aa': 1}} desired_output = '<?xml version="1.0" ?><foo><a><aa>__int1</aa></a></foo>' generated_xml = util.dict2xml(d, name='foo', pretty=False) generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(generated_d, d) d = {'a': {'11': 1, 12: 1,}} desired_output = '<?xml version="1.0" ?><foo><a><__int12>__int1</__int12><__num11>__int1</__num11></a></foo>' generated_xml = util.dict2xml(d, name='foo', pretty=False) generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(generated_d, d) d = {'a': {'aa': 1, 'ab': 1,}, 'b': {'ba': 2, 'bb': 2,},} desired_output = '<?xml version="1.0" ?><foo><a><aa>__int1</aa><ab>__int1</ab></a><b><ba>__int2</ba><bb>__int2</bb></b></foo>' generated_xml = util.dict2xml(d, name='foo', pretty=False) generated_d = util.xml2dict(generated_xml) self.assertEqual(generated_xml, desired_output) self.assertEqual(generated_d, d)