def test_fromCartesian(self): """Kinemage fromCartesian should return Kinemage with A,C,G(,U) coords """ point = MagePoint([.1,.2,.3]) l = MageList([point]*5,Color='red') m1 = MageGroup([l],Radius=0.02,Subgroup=True) m2 = MageGroup([l],Radius=0.02) mg = MageGroup([m1]) k = Kinemage(Count=1,Groups=[mg,m2]) res = k.toCartesian().fromCartesian() self.assertEqual(str(k),str(res))
def setUp(self): self.point = MagePoint([0,0,0],'x') self.ml = MageList([self.point], Label='y',Color='green') self.mg1 = MageGroup([self.ml],Label='z') self.mg2 = MageGroup([self.ml,self.ml],Label='b') self.kin = Kinemage(1) self.kin.Groups = [self.mg1,self.mg2]
def MageParser(infile): """MageParser returns a new kinemage object, created from a string repr. infile: should be an iterable file object The MageParser works only on ONE kinemage object, so files containing more than one kinemage should be split beforehand. This can easily be adjusted if it would be useful in the future. The MageParser handles only certain keywords (@kinemage, @text, @caption, @___group, @____list) and MagePoints at this point in time. All unkown keywords are assumed to be part of the header, so you can find them in the header information. The lists that are part of the Simplex header are treated as normal lists. The 'text' and 'caption' are printed between the header and the first group (see cogent.format.mage). All text found after @text keywords is grouped and outputted as Kinemage.Text. WARNING: MageParser should work on all .kin files generated by the code in cogent.format.mage. There are no guarantees using it on other kinemage files, so always check your output!!! """ text = [] caption = [] header = [] group_pat = re.compile('@(sub)?group') list_pat = re.compile('@\w{3,8}list') last_group = None for rec in KeywordFinder(infile): first = rec[0] other=None if len(rec)>1: other = rec[1:] if first.startswith('@kinemage'): #kinemage k = Kinemage() count = int(first.replace('@kinemage','').strip()) k.Count = count elif first.startswith('@text'): #text if other: text.extend(other) elif first.startswith('@caption'): #caption if other: caption.extend(other) elif group_pat.match(first): #group m = MageGroupFromString(first) if m.Subgroup: #subgroup, should be appended to some other group if last_group is None: raise ValueError,"Subgroup found before first group" last_group.append(m) else: #normal group k.Groups.append(m) last_group = m elif list_pat.match(first): #list l = MageListFromString(first) if other: points = [MagePointFromString(p) for p in other] l.extend(points) last_group.append(l) else: #something else header.append(first) if other: header.extend(other) if text: k.Text = '\n'.join(text) if caption: k.Caption = '\n'.join(caption) if header: k.Header = '\n'.join(header) return k
def test_init(self): """Kinemage should init with any of its usual fields""" k = Kinemage(1) self.assertEqual(str(k), '@kinemage 1') k.Header = '@perspective' self.assertEqual(str(k), '@kinemage 1\n@perspective') k.Count = 2 self.assertEqual(str(k), '@kinemage 2\n@perspective') k.Header = '' k.Caption = 'test caption' self.assertEqual(str(k), '@kinemage 2\n@caption\ntest caption') k.Caption = None k.Text = 'some text' self.assertEqual(str(k), '@kinemage 2\n@text\nsome text') k.Groups = [self.mg1] k.Header = '@test_header' k.Caption = 'This is\nThe caption' k.Text = 'some text here' self.assertEqual(str(k), '@kinemage 2\n@test_header\n@text\n' +\ 'some text here\n' + \ '@caption\nThis is\nThe caption\n@group {z} recessiveon\n' + \ '@dotlist {y} color=green\n{x} 0 0 0')
class KinemageTests(TestCase): """Tests of the overall Kinemage class.""" def setUp(self): self.point = MagePoint([0,0,0],'x') self.ml = MageList([self.point], Label='y',Color='green') self.mg1 = MageGroup([self.ml],Label='z') self.mg2 = MageGroup([self.ml,self.ml],Label='b') self.kin = Kinemage(1) self.kin.Groups = [self.mg1,self.mg2] def test_init_empty(self): """Kinemage empty init should work, but refuse to print""" k = Kinemage() self.assertEqual(k.Count, None) self.assertRaises(ValueError, k.__str__) def test_init(self): """Kinemage should init with any of its usual fields""" k = Kinemage(1) self.assertEqual(str(k), '@kinemage 1') k.Header = '@perspective' self.assertEqual(str(k), '@kinemage 1\n@perspective') k.Count = 2 self.assertEqual(str(k), '@kinemage 2\n@perspective') k.Header = '' k.Caption = 'test caption' self.assertEqual(str(k), '@kinemage 2\n@caption\ntest caption') k.Caption = None k.Text = 'some text' self.assertEqual(str(k), '@kinemage 2\n@text\nsome text') k.Groups = [self.mg1] k.Header = '@test_header' k.Caption = 'This is\nThe caption' k.Text = 'some text here' self.assertEqual(str(k), '@kinemage 2\n@test_header\n@text\n' +\ 'some text here\n' + \ '@caption\nThis is\nThe caption\n@group {z} recessiveon\n' + \ '@dotlist {y} color=green\n{x} 0 0 0') def test_iterGroups(self): """Kinemage iterGroups should behave as expected""" k = self.kin groups = list(k.iterGroups()) self.assertEqual(len(groups),2) self.assertEqual(groups[0],self.mg1) self.assertEqual(groups[1],self.mg2) def test_iterLists(self): """Kinemage iterLists should behave as expected""" k = self.kin lists = list(k.iterLists()) self.assertEqual(len(lists),3) self.assertEqual(lists[0],self.ml) def test_iterPoints(self): """Kinemage iterPoints should behave as expected""" k = self.kin points = list(k.iterPoints()) self.assertEqual(len(points),3) self.assertEqual(points[0],self.point) def test_iterGroupAndLists(self): """Kinemage iterGroupsAndLists should behave as expected""" all = list(self.kin.iterGroupsAndLists()) self.assertEqual(len(all),5) self.assertEqual(all[0],self.mg1) self.assertEqual(all[4],self.ml) def test_toCartesian(self): """Kinemage toCartesian should return new Kinemage with UC,UG,UA coords """ k = self.kin res = k.toCartesian() self.assertEqual(len(k.Groups),len(res.Groups)) self.assertEqual(k.Text,res.Text) self.assertEqual(k.Groups[1].RecessiveOn,res.Groups[1].RecessiveOn) self.assertEqual(res.Groups[0][0][0].Coordinates,[1,1,1]) def test_fromCartesian(self): """Kinemage fromCartesian should return Kinemage with A,C,G(,U) coords """ point = MagePoint([.1,.2,.3]) l = MageList([point]*5,Color='red') m1 = MageGroup([l],Radius=0.02,Subgroup=True) m2 = MageGroup([l],Radius=0.02) mg = MageGroup([m1]) k = Kinemage(Count=1,Groups=[mg,m2]) res = k.toCartesian().fromCartesian() self.assertEqual(str(k),str(res))