This file is indexed.

/usr/lib/python2.7/dist-packages/cogent/format/mage.py is in python-cogent 1.9-9.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#!/usr/bin/env python
"""
MAGE format writer, especially useful for writing the RNA/DNA simplex.

The MAGE format is documented here:
    ftp://kinemage.biochem.duke.edu/pub/kinfiles/docsDemos/KinFmt6.19.txt

Implementation notes

Mage seems to have a dynamic range of 'only' 2-3 orders of magnitude for balls
and spheres. Consequently, although we have to truncate small values when
writing in the RNA simplex, no additional output accuracy is gained by scaling
everything by a large constant factor (e.g. by 10,000). Consequently, this code
preserves the property of writing the simplex in the interval (0,1) since the
numbers reflect the fractions of A, C, G and U directly.
"""

from __future__ import division
from numpy import array, fabs

from copy import deepcopy
from cogent.util.misc import extract_delimited
from cogent.maths.stats.util import Freqs
from cogent.maths.stats.special import fix_rounding_error

__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Rob Knight", "Gavin Huttley", "Sandra Smit", "Daniel McDonald"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Development"

def is_essentially(ref_value, value, round_error=1e-14):
    """Returns True if value is within round_error distance from ref_value.

    ref_value -- int or float, number
    value -- int or float, number
    round_error -- limit on deviation from ref_value, default=1e-14

    This function differs from fix_rounding_error:
    - it returns a boolean, not 1 or 0. 
    - it checks the lower bound as well, not only if 
        ref_value < value < ref_value+round_error
    """
    if ref_value-round_error < value < ref_value+round_error:
        return True
    return False

#Note: in addition to the following 24 colors, there's also deadwhite and
#deadblack (force white or black always). The color names cannot be changed.
#MAGE uses an internal color table to look up 4 'steps' in each color 
#corresponding to distance from the camera, and varies the colors depending
#on whether the background is black or white. A @fullrgbpalette declaration
#can be used to reassign colors by number, but it seems to be tricky to
#get it right.
MageColors = [ 'red', 'orange', 'gold', 'yellow', 'lime', 'green', 'sea',
               'cyan', 'sky', 'blue', 'purple', 'magenta', 'hotpink', 'pink',
               'lilac', 'peach', 'peachtint', 'yellowtint', 'greentint',
               'bluetint', 'lilactint', 'pinktint', 'white', 'gray', 'brown']

#use these for 7-point scales
RainbowColors = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple']

#use these for matched series of up to 3 items. The first 5 series work pretty
#well, but the last series doesn't really match so only use it if desperate.
MatchedColors = [['red', 'green', 'blue'],
                 ['pinktint', 'greentint', 'bluetint'],
                 ['orange', 'lime', 'sky'],
                 ['magenta', 'yellow', 'cyan'],
                 ['peach', 'sea', 'purple'],
                 ['white', 'gray', 'brown']]
    
class MagePoint(object):
    """Holds information for a Mage point: label, attributes, coordinates."""
    MinRadius = 0.001
    DefaultRadius = MinRadius

    def __init__(self, Coordinates=None, Label=None, Color=None, State=None, 
                 Width=None, Radius=None):
        """Returns a new MagePoint with specified coordinates.

        Usage: m = MagePoint(Coordinates=None, Label=None, Color=None,
                   Attributes=None, Width=None, Radius=None)

        Coordinates is a list of floats: x, y, z
        Label is a text label (warning: does not check length).
        Color is the name of the point's color
        State can be one of:
            P   beginning of new point in vectorlist
            U   unpickable
            L   line to this point
            B   ball at this point in a vectorlist
            S   sphere at this point in a vectorlist
            R   ring at this point in a vectorlist
            Q   square at this point in a vectorlist
            A   unknown (arrow?): something to do with vectorlist
            T   interpret as part of triangle in vectorlist
        Width is an integer that defines pen width
        Radius is a float that defines the point radius for balls, spheres, etc.
        
        If Coordinates is None, the Coordinates will be set to [0,0,0]
        """
        if Coordinates is None:
            self.Coordinates = [0,0,0]
        else:
            self.Coordinates = Coordinates
        self.Label = Label
        self.Color = Color
        self.State = State
        self.Width = Width
        self.Radius = Radius
        
        
    def __str__(self):
        """Prints out current point as string."""
        coords = self.Coordinates
        if len(coords) != 3:
            raise ValueError, "Must have exactly 3 coordinates: found %s" \
                % coords
        pieces = []
        #add the pointID
        if self.Label is not None:
            pieces.append('{%s}' % self.Label)
        #collect attributes and add them if necessary
        attr = []
        if self.State:
            attr.append(self.State)
        if self.Color:
            attr.append(self.Color)
        if self.Width:
            attr.append('width%s' % self.Width)
        r = self.Radius
        #NOTE: as of version 6.25, MAGE could not handle radius < 0.001
        if r is not None:
            if r > self.MinRadius:
                attr.append('r=%s' % r)
            else:
                attr.append('r=%s' % self.DefaultRadius)
        if attr:
            pieces += attr
        #add the coordinates
        pieces.extend(map(str, coords))
        #return the result
        return ' '.join(pieces)

    def __cmp__(self, other):
        """Checks equality/inequality on all fields.
        
        Order: Coordinates, Label, Color, State, Width, Radius"""
        try:
            return cmp(self.__dict__, other.__dict__)
        except AttributeError:  #some objects don't have __dict__...
            return cmp(self.__dict__, other)

    def _get_coord(coord_idx):
        """Gets the coordinate asked for; X is first, Y second, Z third coord
        """
        def get_it(self):
            return self.Coordinates[coord_idx]
        return get_it
    
    def _set_coord(coord_idx):
        """Sets the given coordinate; X is first, Y second, Z third coord"""
        def set_it(self,val):
            self.Coordinates[coord_idx] = val
        return set_it

    _lookup = {'x':(_get_coord(0),_set_coord(0)),
               'y':(_get_coord(1),_set_coord(1)),
               'z':(_get_coord(2),_set_coord(2))}

    X = property(*_lookup['x'])
    Y = property(*_lookup['y'])
    Z = property(*_lookup['z'])
       
    def toCartesian(self, round_error=1e-14):
        """Returns new MagePoint with UC,UG,UA coordinates from A,C,G(,U).

        round_error -- float, accepted rounding error

        x=u+c, y=u+g, z=u+a

        This will only work for coordinates in a simplex, where all of them 
        (inlcuding the implicit one) add up to one.
        """
        # all values have to be between 0 and 1
        a = fix_rounding_error(self.X)
        c = fix_rounding_error(self.Y)
        g = fix_rounding_error(self.Z)
        #u = fix_rounding_error(1-a-c-g)
        if is_essentially(1, a+c+g, round_error=round_error):
            u = 0.0
        else:
            u = fix_rounding_error(1-a-c-g)
        
        for coord in [a,c,g,u]:
            if not 0 <= coord <= 1:
                raise ValueError,\
                "%s is not in unit simplex (between 0 and 1)"%(coord)
        cart_x, cart_y, cart_z = u+c, u+g, u+a
        result = deepcopy(self)
        result.Coordinates = [cart_x, cart_y, cart_z]
        return result
    
    def fromCartesian(self):
        """Returns new MagePoint with A,C,G(,U) coordinates from UC,UG,UA.

        From UC,UG,UA to A,C,G(,U).

        This will only work when the original coordinates come from a simplex,
        where U+C+A+G=1
        """
        # x=U+C, y=U+G, z=U+A, U+C+A+G=1
        # U=(1-x-y-z)/-2
        x,y,z = self.X, self.Y, self.Z
        u = fix_rounding_error((1-x-y-z)/-2)
        a, c, g = map(fix_rounding_error,[z-u, x-u, y-u])
        result = deepcopy(self)
        result.Coordinates = [a, c, g]
        return result
        
def MagePointFromBaseFreqs(freqs, get_label=None, get_color=None, \
    get_radius=None):
    """Returns a MagePoint from an object with counts for the bases.
    
    get_label should be a function that calculates a label from the freqs.
    If get_label is not supplied, checks freqs.Label, freqs.Species, freqs.Id,
    freqs.Accession, and freqs.Name in that order. If get_label fails or none
    of the attributes is found, no label is written.

    get_color should be a function that calculates a color from the freqs. 
    Default is no color (i.e. the point has the color for the series), which
    will also happen if get_color fails.

    get_radius is similar to get_color.
    """
    label = None
    if get_label:
        try:
            label = get_label(freqs)
        except:
            pass    #label will be assigned None below
    else:
        for attr in ['Label', 'Species', 'Id', 'Accession', 'Name']:
            if hasattr(freqs, attr):
                label = getattr(freqs, attr)
                #keep going if the label is empty
                if label is not None and label != '':
                    break
    if not label and label != 0:
        label = None
    if get_color:
        try:
            color = get_color(freqs)
        except:
            color=None
    else:
        if hasattr(freqs, 'Color'):
            color = freqs.Color
        else:
            color = None
            
    if get_radius:
        try:
            radius = get_radius(freqs)
        except:
            radius=None
    else:
        if hasattr(freqs, 'Radius'):
            try:
                radius = float(freqs.Radius)
            except:
                radius = None
        else:
            radius = None
            
    relevant = Freqs({'A':freqs.get('A',0), 'C':freqs.get('C',0), 
        'G':freqs.get('G',0), 'U':freqs.get('U',0) or  freqs.get('T',0)})
    relevant.normalize()
    return MagePoint((relevant['A'],relevant['C'],relevant['G']), Label=label,\
        Color=color, Radius=radius)

class MageList(list):
    """Holds information about a list of Mage points.
    """
    KnownStyles = ['vector', 'dot', 'label', 'word', 'ball', 'sphere', \
                   'triangle', 'ribbon', 'arrow', 'mark', 'ring', 'fan']
    BooleanOptions = ['Off', 'NoButton']
    KeywordOptions = ['Color','Radius','Angle','Width','Face','Font','Size']
    def __init__(self, Data=None, Label=None, Style=None, Color=None, \
        Off=False, NoButton=False, Radius=None, Angle=None, Width=None, \
        Face=None, Font=None, Size=None):
        """Returns new MageList object. Must initialize with list of MagePoints.
        """
        if Data is None:
            super(MageList,self).__init__([])
        else:
            super(MageList, self).__init__(Data)
        self.Label = Label
        self.Style = Style
        self.Color = Color
        self.Off = Off
        self.NoButton = NoButton
        self.Radius = Radius
        self.Angle = Angle
        self.Width = Width
        self.Face = Face
        self.Font = Font
        self.Size = Size

    def __str__(self):
        """Returns data as a mage-format string, one point to a line."""
        pieces = []
        curr_style = self.Style or 'dot'    #dotlists by default
        pieces.append('@%slist' % curr_style.lower())
        if self.Label:
            pieces.append('{%s}' % self.Label)
        for opt in self.BooleanOptions:
            if getattr(self, opt):
                pieces.append(opt.lower())
        for opt in self.KeywordOptions:
            curr = getattr(self, opt)
            if curr:
                pieces.append('%s=%s' % (opt.lower(), curr))
        lines = [' '.join(pieces)] + [str(d) for d in self]
        return '\n'.join(lines)

    def toArray(self, include_radius=True):
        """Returns an array of the three coordinates and the radius for all MP
        """
        elem = []
        if include_radius:
            for point in self:
                coords = point.Coordinates
                radius = point.Radius or self.Radius
                if radius is None:
                    raise ValueError,\
                        "Radius is not set for point or list, %s"%(str(point))
                else:
                    elem.append(coords + [radius])
        else:
            for point in self:
                coords = point.Coordinates
                elem.append(coords)
        return array(elem)
    
    def iterPoints(self):
        """Iterates over all points in the list"""
        for point in self:
            yield point

    def toCartesian(self, round_error=1e-14):
        """Returns new MageList where points are in Cartesian coordinates"""
        #create new list
        result = MageList()
        #copy all attributes from the original
        result.__dict__ = self.__dict__.copy()
        #fill with new points
        for point in self.iterPoints():
            result.append(point.toCartesian(round_error=round_error))
        return result

    def fromCartesian(self):
        """Returns new MageList where points are in ACG coordinates"""
        #create new list
        result = MageList()
        #copy all attributes from the original
        result.__dict__ = self.__dict__.copy()
        #fill with new points
        for point in self.iterPoints():
            result.append(point.fromCartesian())
        return result

class MageGroup(list):
    """Holds information about a MAGE Group, which has a list of lists/groups.

    Specificially, any nested MageGroups will be treated as MAGE @subgroups, 
    while any dotlists, vectorlists, etc. will be treated directly.
    """
    BooleanOptions = ['Off', 'NoButton', 'RecessiveOn', 'Dominant', 'Lens']
    KeywordOptions = ['Master', 'Instance', 'Clone']
    Cascaders = ['Style', 'Color','Radius','Angle','Width','Face','Font','Size']
    
    def __init__(self, Data=None, Label=None, Subgroup=False, Off=False, \
        NoButton=False, RecessiveOn=True, Dominant=False, Master=None,   \
        Instance=None, Clone=None, Lens=False, Style=None, Color=None,   \
        Radius=None, Angle=None, Width=None, Face=None, Font=None, Size=None):
        """Returns new MageList object. Must initialize with list of MagePoints.
        """
        if Data is None:
            super(MageGroup,self).__init__([])
        else:
            super(MageGroup,self).__init__(Data)
        self.Label = Label
        self.Subgroup = Subgroup
        self.Off = Off
        self.NoButton = NoButton
        self.RecessiveOn = RecessiveOn
        self.Dominant = Dominant
        self.Master = Master
        self.Instance = Instance
        self.Clone = Clone
        self.Lens = Lens
        #properties of lists. Group settings will be expressed when Kinemage
        #object is printed
        self.Style = Style
        self.Color = Color
        self.Radius = Radius
        self.Angle = Angle
        self.Width = Width
        self.Face = Face
        self.Font = Font
        self.Size = Size
 
    def __str__(self):
        """Returns data as a mage-format string, one point to a line.
        
        Children are temporarily mutated to output the correct string. Changes
        are undone afterwards, so the original doesn't change.
        """
        pieces = []
        if self.Subgroup:
            pieces.append('@subgroup')
        else:
            pieces.append('@group')
        if self.Label:
            pieces.append('{%s}' % self.Label)
        for opt in self.BooleanOptions:
            if getattr(self, opt):
                pieces.append(opt.lower())
        for opt in self.KeywordOptions:
            curr = getattr(self, opt)
            if curr:
                pieces.append('%s={%s}' % (opt.lower(), curr))
        for d in self:
            if isinstance(d, MageGroup):
                d.Subgroup = True
        # cascade values 1 level down (to either child lists or groups)
        changed = {}
        for attr in self.Cascaders:
            val = getattr(self,attr)
            if val is not None:
                changed[attr] = []
                for item in self: #either group or list
                    if getattr(item,attr) is None:
                        setattr(item,attr,val)
                        changed[attr].append(item)
        #gather all lines
        lines = [' '.join(pieces)] + [str(d) for d in self]
        #reset cascaded values
        for k,v in changed.items():
            for l in v:
                setattr(l,k,None)
        return '\n'.join(lines)
    
    def iterGroups(self):
        """Iterates over all groups in this group"""
        for item in self:
            if isinstance(item,MageGroup):
                yield item
                for j in item.iterGroups():
                    yield j
                
    def iterLists(self):
        """Iterates over all lists in this group"""
        for item in self:
            if isinstance(item,MageList):
                yield item
            else:
                for j in item.iterLists():
                    yield j
    
    def iterGroupsAndLists(self):
        """Iterates over all groups and lists in this group
        
        Groups and Lists have multiple elements in common. You might want to 
        change a property for both groups and lists. This iterator doesn't 
        distinguish between them, but returns them all. 
        """
        for i in self:
            if isinstance(i,MageGroup):
                yield i
                for j in i.iterGroupsAndLists():
                    yield j
            elif isinstance(i,MageList):
                yield i
        
    def iterPoints(self):
        """Iterates over all points in this group"""
        for item in self.iterLists():
            for p in item.iterPoints():
                yield p

    def toCartesian(self, round_error=1e-14):
        """Returns a new MageGroup where all points are Cartesian coordinates
        """
        #create an empty group
        result = MageGroup()
        #copy the attributes of the original
        result.__dict__ = self.__dict__.copy()
        #fill with new groups and lists
        for item in self:
            result.append(item.toCartesian(round_error=round_error))
        return result
   
    def fromCartesian(self):
        """Returns a new MageGroup where all points are ACG coordinates
        """
        #create an empty group
        result = MageGroup()
        #copy the attributes of the original
        result.__dict__ = self.__dict__.copy()
        #fill with new groups and lists
        for item in self:
            result.append(item.fromCartesian())
        return result

class MageHeader(object):
    """Defines the header for a kinemage.

    For now, just returns the text string it was initialized with.
    """
    def __init__(self, data):
        """Returns a new MageHeader object."""
        self.Data = data

    def __str__(self):
        """Writes the header information out as a string."""
        return str(self.Data)

class SimplexHeader(MageHeader):
    """Defines the header for the RNA simplex.

    May have a bunch of scaling and coloring options later.
    """
    def __init__(self, *args, **kwargs):
        """Returns a new SimplexHeader object.

        May have options added later, but none for now.
        """
        self.Data = \
"""
@viewid {oblique}
@zoom 1.05
@zslab 467
@center 0.500 0.289 0.204
@matrix
-0.55836 -0.72046 -0.41133  0.82346 -0.42101 -0.38036  0.10085 -0.55108
0.82833

@2viewid {top}
@2zoom 0.82
@2zslab 470
@2center 0.500 0.289 0.204
@2matrix
-0.38337  0.43731 -0.81351  0.87217 -0.11840 -0.47466 -0.30389 -0.89148
-0.33602

@3viewid {side}
@3zoom 0.82
@3zslab 470
@3center 0.500 0.289 0.204
@3matrix
-0.49808 -0.81559 -0.29450  0.86714 -0.46911 -0.16738 -0.00164 -0.33875
0.94088

@4viewid {End-O-Line}
@4zoom 1.43
@4zslab 469
@4center 0.500 0.289 0.204
@4matrix
 0.00348 -0.99984 -0.01766  0.57533 -0.01244  0.81784 -0.81792 -0.01301
0.57519

@perspective
@fontsizelabel 24
@onewidth
@zclipoff
@localrotation  1 0 0 .5 .866 0 .5 .289 .816

@group {Tetrahedron}
@vectorlist  {Edges}  color=white     nobutton
P   {0 0 0} 0 0 0
     0.5 0 0
  {1 0 0} 1 0 0
     0.5  0.5  0
  {0 1 0} 0 1 0
P    0 0 0
    0  0.5  0
  {0 1 0} 0 1 0
   0  0.5  0.5
  {0 0 1} 0 0 1
P    0 0 0
   0 0 0.5
  {0 0 1} 0 0 1
   0.5  0 0.5
  {1 0 0} 1 0 0

@labellist {labels} color=white    nobutton
  {U}  0  0  0
   {A}  1.1    0  0
   {C}  0    1.05   0
   {G}   0  0   1.08


@group  {Lines}
@vectorlist {A=U&C=G} color= green    off
 P   0   0.5   0.5
     .1  .4  .4
     .25 .25 .25
     .4  .1  .1
 L   0.500, 0.000, 0.000

@vectorlist {A=G&C=U} color= red    off
 P   0.5   0   0.5
     .25 .25 .25
 L   0, 0.500, 0.000

@vectorlist {A=C&G=U} color= red    off
 P   0.5   0.5   0
     .25 .25 .25
 L   0.000, 0.000, 0.500
"""

class CartesianSimplexHeader(MageHeader):
    """Defines the header for the RNA simplex: coordinates in Cartesian system.

    May have a bunch of scaling and coloring options later.
    """
    def __init__(self, *args, **kwargs):
        """Returns a new CartesianSimplexHeader object.

        May have options added later, but none for now.
        """
        self.Data = \
"""
@1viewid {Oblique}
@1span 2.2
@1zslab 200.0
@1center 0.5 0.5 0.5
@1matrix 0.2264 0.406 0.8854 0.6132 0.6468 -0.4535 -0.7567 0.6456 -0.1025

@2viewid {Down UA axis}
@2span 2.0
@2zslab 200.0
@2center 0.5 0.5 0.5
@2matrix 1 0 0 0 1 0 0 0 1

@3viewid {Down UC axis}
@3span 2.0
@3zslab 200.0
@3center 0.5 0.5 0.5
@3matrix 0 0 1 0 1 0 -1 0 0

@4viewid {Down UG axis}
@4span 2.0
@4zslab 200.0
@4center 0.5 0.5 0.5
@4matrix 0 -1 0 0 0 1 -1 0 0

@5viewid {Cube}
@5span 2.0
@5zslab 200.0
@5center .5 0.5 0.5
@5matrix 0.956 -0.0237 0.2923 0.2933 0.0739 -0.9532 0.001 0.997 0.0776

@group {Tetrahedron}
@vectorlist {Edges} nobutton color= white
{Edges}P 1 1 1
1 0 0
0 0 1
0 1 0
1 0 0
1 1 1
0 0 1
1 1 1
0 1 0

@labellist {labels} nobutton color= white
{U}1.05 1.05 1.05
{A}-0.05 -0.05 1.05
{C}1.05 -0.05 -0.05
{G}-0.05 1.05 -0.05

@group {Lines}
@vectorlist {A=U&C=G} color= green
{A=U&C=G}P 0.5 0.5 1
0.5 0.5 0
@vectorlist {A=G&C=U} color= red
{A=G&C=U}P 0 0.5 0.5
1 0.5 0.5
@vectorlist {A=C&G=U} color= red
{A=C&G=U}P 0.5 0 0.5
0.5 1 0.5

@group {Cube} off
@vectorlist nobutton color=yellow 
{"}P 0 0 0
{"}1 0 0
{"}1 1 0
{"}0 1 0
{"}0 0 0
{"}P 0 0 1
{"}1 0 1
{"}1 1 1
{"}0 1 1
{"}0 0 1
{"}P 0 0 0
{"}0 0 1
{"}P 1 0 0
{"}1 0 1
{"}P 1 1 0
{"}1 1 1
{"}P 0 1 0
{"}0 1 1

@labellist {Cube labels} color= red off
{0,0,0}-0.1 -0.1 -0.1
{1,1,1}1.1 1.1 1.1
{0,0,1} -.1 -.1 1.1
{0,1,1} -.1 1.1 1.1
{0,1,0} -.1 0.9 0.05
{1,0,1} 1.05 -.1 0.95
{1,1,0} 1.1 1.1 -.1
{1,0,0} 1.1 -.1 -.1
{x++}1.03 0.5 0.5
{y++}0.5 1.1 0.475
{z++}0.5 0.5 1.05
{x--}-0.075 0.5 0.5
{y--}0.475 -0.15 0.5
{z--}0.5 0.5 -0.08

"""

class Kinemage(object):
    """Stores information associated with a kinemage: header, caption, groups.

    A single file can have multiple kinemages.
    """
    def __init__(self,Count=None,Header=None,Groups=None,Caption=None,\
        Text=None):
        """Returns a new Kinemage object.

        Usage: k = Kinemage(count, header, groups, caption=None, text=None)
        """
        self.Count = Count  #integer, required for MAGE
        self.Header = Header
        self.Groups = Groups or []
        self.Caption = Caption
        self.Text = Text

    def __str__(self):
        """String representation suitable for writing to file."""
        if not self.Count:
            raise ValueError, "Must set a count to display a kinemage."
        pieces = ['@kinemage %s' % self.Count]
        if self.Header:
            pieces.append(str(self.Header))
        if self.Text:
            pieces.append('@text')
            pieces.append(str(self.Text))
        if self.Caption:
            pieces.append('@caption')
            pieces.append(str(self.Caption))
        for g in self.Groups:
            pieces.append(str(g))
        return '\n'.join(pieces)

    def iterGroups(self):
        """Iterates over all groups in Kinemage object"""
        for i in self.Groups:
            yield i
            for j in i.iterGroups():
                yield j

    def iterLists(self):
        """Iterates over all lists in Kinemage object"""
        for gr in self.Groups:
            for l in gr.iterLists():
                yield l

    def iterPoints(self):
        """Iterates over all points in Kinemage object"""
        for gr in self.Groups:
            for p in gr.iterPoints():
                yield p
   
    def iterGroupsAndLists(self):
        """Iterates over all groups and lists in Kinemage object"""
        for gr in self.Groups:
           yield gr
           for j in gr.iterGroupsAndLists():
               yield j

    def toCartesian(self, round_error=1e-14):
        """Returns a new Kinemage where all coordinates are UC,UG,UA"""
        result = deepcopy(self)
        result.Groups = []
        for item in self.Groups:
            result.Groups.append(item.toCartesian(round_error=round_error))
        return result

    def fromCartesian(self):
        """Returns a new Kinemage where all coordinates are ACG again"""
        result = deepcopy(self)
        result.Groups = []
        for item in self.Groups:
            result.Groups.append(item.fromCartesian())
        return result