File: usage.py

package info (click to toggle)
python-cogent 1.4.1-1.2
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 13,260 kB
  • ctags: 20,087
  • sloc: python: 116,163; ansic: 732; makefile: 74; sh: 9
file content (1157 lines) | stat: -rw-r--r-- 43,052 bytes parent folder | download
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
#!/usr/bin/env python
"""usage.py: usage of symbols, including substitutions on pairwise alphabets.

Revision History

Created 10/12/04 by Rob Knight.

9/14/05 Rob Knight: Changed Usage constructor to allow Alphabet on the instance
level, and to eliminate the precalculated flag which was not used. Added
entropy method.

7/20/07 Mike Robeson: Under PairMatrix.__init__ changed 'if data:' to
        'if data != None:
8/3/07 Daniel McDonald: Code now relies on numpy and cogent with the exception
        of the one scipy function that still needs to be removed
"""
from cogent.maths.scipy_optimize import fmin, brent
from cogent.util.array import scale_trace, norm_diff, \
    has_neg_off_diags, sum_neg_off_diags, with_diag, without_diag

from cogent.core.alphabet import get_array_type
from cogent.core.usage import RnaBases, DnaBases, DnaPairs, RnaPairs, Codons
from cogent.core.sequence import ModelSequence, ModelDnaSequence, \
    ModelRnaSequence
from operator import add, sub, mul, div
from cogent.maths.matrix_logarithm import logm
from cogent.maths.stats.util import FreqsI
from cogent.maths.matrix_exponentiation import FastExponentiator as expm
from numpy import zeros, array, max, diag, log, nonzero, product, cumsum, \
                  searchsorted, exp, diagonal, choose, less, repeat, average,\
                  logical_and, logical_or, logical_not, transpose, compress,\
                  ravel, concatenate, equal, log, dot, identity, \
                  newaxis as NewAxis, sum, take, reshape, any, all, asarray
from numpy.linalg import eig
from numpy.linalg import inv as inverse
from numpy.random import random as randarray

ARRAY_TYPE = type(array([0]))

__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__credits__ = ["Rob Knight", "Mike Robeson", "Daniel McDonald"]
__license__ = "GPL"
__version__ = "1.4.1"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Production"

class Usage(FreqsI):
    """Stores usage on a particular alphabet. Abstract class.
    
    Note: Usage is abstract because most subclasses (e.g. CodonUsage,
    AminoAcidUsage) have specific methods that depend on their alphabets.
    Allowing generic Usage objects is disallowed to enforce use of the
    appropriate Usage object for specific situations.

    Supports most of the Cogent FreqsI interface.
    """
    Alphabet = None # concrete subclasses have specific alphabets
    
    def __init__(self, data=None, Alphabet=None):
        """Returns a new Usage object from array of symbol freqs.
        
        Will interpret many different kinds of data, including precalculated
        frequencies, arrays of symbols, and cogent.core.sequence.ModelSequence 
        objects.

        Warning: it guesses whether you passed in frequencies or symbols based
        on the length of the array, so for example Usage(DnaSequence('ATCG'))
        will _not_ give the result you expect. If you know the data type,
        use the alternative class method constructors.
        """
        if Alphabet is not None:
            self.Alphabet = Alphabet
        if not self.Alphabet:
            raise TypeError, "Usage subclasses must define alphabet."""
        if isinstance(data, Usage):
            self._data = data._data
        else:
            self._data = zeros(len(self), 'float64')
            if any(data):
                self += data

    def __getitem__(self, i):
        """Returns item based on alphabet."""
        return self._data[self.Alphabet.index(i)]

    def __setitem__(self, key, val):
        """Sets item based on alphabet."""
        self._data[self.Alphabet.index(key)] = val

    def __str__(self):
        """Prints as though it were a tuple of key,value pairs."""
        return str(self.items())

    def __repr__(self):
        """String representation of self."""
        return ''.join([self.__class__.__name__, '(', repr(self._data), ')'])

    def __iter__(self):
        """Iterates over keys, like a dict."""
        return iter(self.Alphabet)

    def __eq__(self, other):
        """Tests whether two Usage objects have the same data."""
        if hasattr(other, '_data'):
            return all(self._data == other._data)
        #if we get here, didn't compare equal
        try:
            return all(self._data == self.__class__(other)._data)
        except:
            return False

    def __ne__(self, other):
        """Returns True if self and other are not equal."""
        if hasattr(other, '_data'):
            return any(self._data != other._data)
        #if we get here, didn't compare equal
        try:
            return any(self._data != self.__class__(other)._data)
        except:
            return True

    def __iadd__(self, other):
        """Adds data to self in-place."""
        #check if other is nonzero; skip if it isn't
        try:
            if not other:
                return self
        except ValueError:
            if not any(other):
                return self
        #first, check if it's a Usage object
        if isinstance(other, Usage):
            self._data += other._data
            return self
        #then, check if it's one of our ModelSequence objects
        ac = self.Alphabet.counts
        if isinstance(other, ModelSequence):
            self._data += ac(other._data)
            return self
        #if it's the same length as self, try to add it as frequencies
        try:
            if len(other) == len(self):
                self._data += other
                return self
        except TypeError:
            pass
        #then try to convert it using the alphabet
        #WARNING: this will silently ignore unknown keys!
        #since we know other wasn't nonzero, we won't accept
        #the result if we can't convert anything.
        try:
            other_freqs = ac(other)
            #check if we actually converted anything...
            if any(other_freqs):
                self._data += other_freqs
                return self
        except (IndexError, KeyError, TypeError):
            pass
        #then use the generic conversion function
        f = self._find_conversion_function(other)
        if f:
            f(other, op=add)
            return self
        else:
            raise TypeError, "Could not convert this to freqs: %s" % other

    def __isub__(self, other):
        """Subtracts data from self in-place."""
        #check if other is nonzero; skip if it isn't
        try:
            if not other:
                return self
        except ValueError:
            if not any(other):
                return self
        #first, check if it's a Usage object
        if isinstance(other, Usage):
            self._data -= other._data
            return self
        #then, check if it's one of our ModelSequence objects
        ac = self.Alphabet.counts
        if isinstance(other, ModelSequence):
            self._data -= ac(other._data)
            return self
        #if it's the same length as self, try to add it as frequencies
        try:
            if len(other) == len(self):
                self._data -= other
                return self
        except TypeError:
            pass
        #then try to convert it using the alphabet
        #WARNING: this will silently ignore unknown keys!
        #since we know other wasn't nonzero, we won't accept
        #the result if we can't convert anything.
        try:
            other_freqs = ac(other)
            #check if we actually converted anything...
            if other_freqs.any():
                self._data -= other_freqs
                return self
        except (IndexError, KeyError, TypeError):
            pass
        #then use the generic conversion function
        f = self._find_conversion_function(other)
        if f:
            f(other, op=sub)
            return self
        else:
            raise TypeError, "Could not convert this to freqs: %s" % other
  
    def __mul__(self, other):
        """Multiplies self by other (assumed scalar)."""
        return self.__class__(self._data * other)

    def __imul__(self, other):
        """Multiplies self by other in-place (assumed scalar)."""
        self._data *= other
        
    def __div__(self, other):
        """Divides self by other (assumed scalar). Always true division."""
        return self.__class__(self._data / (other))

    def __idiv__(self, other):
        """Divides self by other (assumed scalar) inplace. Maybe int division."""
        self._data /= other

    def scale_sum(self, sum_=1.0):
        """Returns copy of self scaled to specified sum."""
        return self.__class__(self._data * (sum_/sum(self._data)))

    def scale_max(self, max_=1.0):
        """Returns copy of self scaled to specified maximum (default 1)."""
        return self.__class__(self._data * (max_/max(self._data)))

    def probs(self):
        """Returns copy of self scaled so that the sum is 1."""
        return self.__class__(self._data / (sum(self._data)))

    def randomIndices(self, length, random_vector=None):
        """Produces random indices according to symbol freqs."""
        freqs = cumsum(self._data/sum(self._data))[:-1]
        if random_vector is None:
            random_vector=randarray(length)
        return searchsorted(freqs, random_vector)

    def fromSeqData(cls, seq, Alphabet=None):
        """Returns new Usage object from Sequence object."""
        return cls.fromArray(seq._data, Alphabet=Alphabet)
    
    def fromArray(cls, a, Alphabet=None):
        """Returns new Usage object from array."""
        return cls(cls.Alphabet.counts(a), Alphabet=Alphabet)

    fromSeqData = classmethod(fromSeqData)
    fromArray = classmethod(fromArray)
    
    #following code is to support FreqsI
    def get(self, key, default):
        """Returns self._data[self.Alphabet.index(key) if present, or default."""
        try:
            return self._data[self.Alphabet.index(key)]
        except (KeyError, IndexError, TypeError):
            return default

    def values(self):
        """Returns list of keys in self (i.e. the alphabet)."""
        return list(self._data)

    def keys(self):
        """Returns list of values in self (i.e. the data)."""
        return list(self.Alphabet)

    def items(self):
        """Returns list of (key, value) pairs in self."""
        return zip(self.Alphabet, self._data)

    def isValid(self):
        """Always valid (except for negative numbers), so override."""
        return min(self._data) >= 0

    def copy(self):
        """Return copy of self with same alphabet, not sharing data."""
        return self.__class__(self._data.copy())

    def __delitem__(self, key):
        """Can't really delete items, but raise error if in alphabet."""
        if key in self.Alphabet:
            raise KeyError, "May not delete required key %s" % key

    def purge(self):
        """Can't contain anything not in alphabet, so do nothing."""
        pass

    def normalize(self, total=1.0, purge=True):
        """Converts counts into probabilities, normalized to 1 in-place.
        
        Changes result to Float64. Purge is always treated as True. 
        """
        if self._data is not None and self._data.any():
            self._data = self._data / (total * sum(self._data))

    def choice(self, prob):
        """Returns item corresponding to Pr(prob)."""
        if prob > 1:
            return self.Alphabet[-1]
        summed = cumsum(self._data/sum(self._data))
        return self.Alphabet[searchsorted(summed, prob)]

    def randomSequence(self, n):
        """Returns list of n random choices, with replacement."""
        if not self:
            raise IndexError, "All frequencies are zero."
        return list(choose(self.randomIndices(n), self.Alphabet))

    def subset(self, items, keep=True):
        """Sets all frequencies not in items to 0.

        If keep is False, sets all frequencies in items to 0.
        """
        if keep:
            for i in self.Alphabet:
                if i not in items:
                    self[i] = 0
        else:
            for i in items:
                try:
                    self[i] = 0
                except KeyError:
                    pass

    def scale(self, factor=1, offset=0):
        """Linear transform of values in freqs where val= factor*val + offset."""
        self._data = factor * self._data + offset

    def __len__(self):
        """Returns length of alphabet."""
        return len(self.Alphabet)

    def setdefault(self, key, default):
        """Returns self[key] or sets self[key] to default."""
        if self[key]:
            return self[key]
        else:
            self[key] = default
            return default
        

    def __contains__(self, key):
        """Returns True if key in self."""
        try:
            return key in self.Alphabet
        except TypeError:
            return False
    
    def __nonzero__(self):
        """Returns True if self is nonzero."""
        return bool(sum(self._data) != 0)
       
    def rekey(self, key_map, default=None, constructor=None):
        """Returns new Freqs with keys remapped using key_map.

        key_map should be a dict of {old_key:new_key}.
        
        Values are summed across all keys that map to the same new value.
        Keys that are not in the key_map are omitted (if default is None),
        or set to the default.

        constructor defaults to self.__class__. However, if you're doing
        something like mapping amino acid frequencies onto charge frequencies,
        you probably want to specify the constructor since the result won't
        be valid on the alphabet of the current class.

        Note that the resulting Freqs object is not required to contain
        values for all the possible keys.
        """
        if constructor is None:
            constructor = self.__class__
        result = constructor()
        for key, val in self.items():
            new_key = key_map.get(key, default)
            curr = result.get(new_key, 0)
            try:
                result[new_key] = curr + val
            except KeyError:
                pass
        return result 

    def entropy(self, base=2):
        """Returns Shannon entropy of usage: sum of p log p."""
        ln_base = log(base)
        flat = ravel(self._data)
        total = sum(flat)
        if not total:
            return 0
        flat /= total
        ok_indices = nonzero(flat)[0]
        ok_vals = take(flat, ok_indices, axis=0)
        return -sum(ok_vals * log(ok_vals))/ln_base
   
class DnaUsage(Usage):
    """Stores usage on the DNA alphabet."""
    Alphabet = DnaBases
        
class RnaUsage(Usage):
    """Stores usage on the RNA alphabet."""
    Alphabet = RnaBases

class CodonUsage(Usage):
    """Stores usage on the Codon alphabet."""
    Alphabet = Codons

class DnaPairUsage(Usage):
    """Stores usage on the DnaPairs alphabet."""
    Alphabet = DnaPairs

class RnaPairUsage(Usage):
    """Stores usage on the RnaPairs alphabet."""
    Alphabet = RnaPairs

class PairMatrix(object):
    """Base class for Counts, Probs, and Rates matrices. Immutable.
    
    Holds any numeric relationship between pairs of objects on a JointAlphabet.
    Note that the two SubEnumerations of the JointAlphabet need not be the same,
    although many subclasses of PairMatrix will require that the two
    SubEnumerations _are_ the same because their methods assume square matrices.
    """
    def __init__(self, data, Alphabet, Name=None):
        """Returns new PairMatrix object containing data.
        
        WARNING: Alphabet must be a JointAlphabet where the two SubEnumerations
        are the same.
        """
        self.Alphabet = Alphabet
        if any(data):
            self._data = reshape(array(data, 'd'), Alphabet.Shape)
        else:
            self._data = zeros(Alphabet.Shape, 'd')
        self.Name = Name
 
    def toMatlab(self):
        """Returns Matlab-formatted string representation."""
        if self.Name is None:
            name = 'm'
        else:
            name = str(self.Name)
        return ''.join([name, '=', '[', \
            ';\n'.join([' '.join(map(str, r)) for r in self._data]), '];\n'])

    def __str__(self):
        """Returns string representation of array held in self."""
        return str(self._data)

    def __repr__(self):
        """Returns string representation of self."""
        return ''.join([self.__class__.__name__, '(', repr(self._data), \
            ',', repr(self.Alphabet), ',', repr(self.Name), ')'])

    def __getitem__(self, args):
        """__getitem__ passes everything to internal array.
        
        WARNING: m[a,b] will work where a and b are symbols in the alphabet,
        but m[a][b] will fail. This is because m[a] produces an array object
        with the corresponding row, which is then passed b as an index. Because
        the array object doesn't have the alphabet, it can't map the index into
        a number.

        Slicing is not supported.
        """
        # First, test whether args are in the JointAlphabet. Will always be tuple.
        if isinstance(args, tuple):
            try:
                return ravel(self._data)[self.Alphabet.index(tuple(args))]
            except (KeyError, TypeError):
                pass
        return self._data[self.Alphabet.SubEnumerations[0].index(args)]

    def __len__(self):
        """Returns number of rows."""
        return len(self._data)

    def empty(cls, Alphabet):
        """Class method: returns empty matrix sized for alphabet."""
        return cls(zeros(Alphabet.Shape), Alphabet)

    empty = classmethod(empty)

    def __eq__(self, other):
        """Tests whether two Usage objects have the same data."""
        try:
            return all(self._data == other._data)
            #return not bool(all(self._data != other._data))
        except:
            return False

    def __ne__(self, other):
        """Returns True if self and other are not equal."""
        try:
            return any(self._data != other._data)
            #return bool(all(self._data != other._data))
        except:
            return False

    def __iter__(self):
        """Iterates over rows in data."""
        return iter(self._data)

class Counts(PairMatrix):
    """Holds the data for a matrix of counts. Immutable.
    """
    
    def toProbs(self):
        """Returns copy of self where rows sum to 1."""
        return Probs(self._data/ (sum(self._data, 1)[:,NewAxis]), \
            self.Alphabet)

    def fromPair(cls, first, second, Alphabet, average=True):
        """Class method: returns new Counts from two sequences.
        """
        size = len(Alphabet.SubEnumerations[-1])
        #if they're ModelSequence objects, use the _data attribute
        if hasattr(first, '_data'):
            first, second = first._data, second._data

        #figure out what size we need the result to go in: note that the
        #result is on a pair alphabet, so the data type of the single
        #alphabet (that the sequence starts off in) might not work.
        data_type = get_array_type(product(map(len, Alphabet.SubEnumerations)))
        first = asarray(first, data_type)
        second = asarray(second, data_type)
        items = first * size + second
        
        counts = reshape(Alphabet.counts(items), Alphabet.Shape)
        if average:
            return cls((counts + transpose(counts))/2.0, Alphabet)
        else:
            return cls(counts, Alphabet)

    fromPair = classmethod(fromPair)

    def _from_triple_small(cls, first, second, outgroup, Alphabet):
        """Class method: returns new Counts for first from three sequences.

        Sequence order is first, second, outgroup.

        Use this method when the sequences are short and/or the alphabet is
        small: relatively memory intensive because it makes an array the size
        of the seq x the alphabet for each sequence. Fast on short sequences,
        though.

        NOTE: requires input to either all be ModelSequence objects, or all not
        be ModelSequence objects. Could change this if desirable.
        """
        #if they've got data, assume ModelSequence objects. Otherwise, arrays.
        if hasattr(first, '_data'):
            first, second, outgroup = first._data, second._data, outgroup._data

        size = len(Alphabet.SubEnumerations[-1])
        a_eq_b = equal(first, second)
        a_ne_b = logical_not(a_eq_b)
        a_eq_x = equal(first, outgroup)
        b_eq_x = equal(second, outgroup)

        #figure out what size we need the result to go in: note that the
        #result is on a pair alphabet, so the data type of the single
        #alphabet (that the sequence starts off in) might not work.
        data_type = get_array_type(product(map(len, Alphabet.SubEnumerations)))
        first = asarray(first, data_type)
        second = asarray(second, data_type)
       
        b_to_a = second*size + first
        a_to_a = first*size + first

        b_to_a_items = compress(logical_and(b_eq_x, a_ne_b), b_to_a)
        a_to_a_items = compress(logical_or(a_eq_b, a_eq_x), a_to_a)
        items = concatenate((b_to_a_items, a_to_a_items))
        counts = reshape(Alphabet.counts(items), Alphabet.Shape)

        return cls(counts, Alphabet)

    def _from_triple_large(cls, first, second, outgroup, Alphabet):
        """Same as _from_triple except copes with very long sequences.
        
        Specifically, allocates an array for the frequencies of each type,
        walks through the triple one base at a time, and updates the
        appropriate cell. Faster when alphabet and/or sequences are large;
        also avoids memory issues because it doesn't allocate the seq x
        alphabet array.

        NOTE: requires input to either all be ModelSequence objects, or all not
        be ModelSequence objects. Could change this if desirable.

        WARNING: uses float, not int, as datatype in return value.
        """
        #figure out if we already have the data in terms of alphabet indices.
        #if not, we need to convert it.
        if hasattr(first, '_data'):
            first, second, outgroup = first._data, second._data, outgroup._data
        else:
            if hasattr(Alphabet, 'toIndices'):
                converter = Alphabet.toIndices
            else:
                converter = Alphabet.fromSequenceToArray

            # convert to alphabet indices
            first, second, outgroup = map(asarray, map(converter,
                                        [first, second, outgroup]))
        # only include positions where all three not different
        valid_posn = logical_not(logical_and(logical_and(first != outgroup,
                                                        second != outgroup),
                                                        first != second))
        valid_pos = [index for index, val in enumerate(valid_posn) if val]
        first = first.take(valid_pos)
        second = second.take(valid_pos)
        outgroup = outgroup.take(valid_pos)
        out_diffs = logical_and(first == second, first != outgroup)
        counts = zeros((len(Alphabet.SubEnumerations[0]), \
            len(Alphabet.SubEnumerations[0])))
        for x, y, out_diff in zip(outgroup, first,
                                       out_diffs):
            if out_diff:
                counts[y,y] += 1
            else:
                counts[x,y] += 1
        return cls(counts, Alphabet)

    def fromTriple(cls, first, second, outgroup, Alphabet, threshold=1e6):
       """Reads counts from triple of sequences, method chosen by data size."""
       if len(first) * len(Alphabet) > threshold:
           return cls._from_triple_large(first, second, outgroup, Alphabet)
       else:
           return cls._from_triple_small(first, second, outgroup, Alphabet)

    fromTriple = classmethod(fromTriple)
    _from_triple_small = classmethod(_from_triple_small)
    _from_triple_large = classmethod(_from_triple_large)
       
class Probs(PairMatrix):
    """Holds the data for a probability matrix. Immutable."""
    
    def isValid(self):
        """Returns True if all values positive and each row sums to 1."""
        for row in self:
            if sum(row) != 1.0 or min(row) < 0.0:
                return False
        return True

    def makeModel(self, seq):
        """Returns substitution model for seq based on self's rows."""
        return take(self._data, seq, axis=0)

    def mutate(self, seq, random_vector=None):
        """Returns mutated version of seq, according to self.

        seq should behave like a Numeric array.
        
        random_vector should be vector of 0 and 1 of same length as sequence,
        if supplied.

        Result is always an array, not coerced into seq's class.
        """
        sums = cumsum(self._data, 1)
        model = take(sums, seq, axis=0)
        if random_vector is None:
            random_vector = randarray(seq.shape)
        return sum(transpose(model)[:-1] < random_vector, axis=0)
        #transpose needed to align frames
        

    def toCounts(self, num):
        """Returns count matrix with approximately num counts.

        Rounding error may prevent counts from summing exactly to num.
        """
        num_rows = len(self)
        return Counts(self._data * (num/num_rows), self.Alphabet)

    def toRates(self, normalize=False):
        """Returns rate matrix. Does not normalize by default."""
        return Rates(logm(self._data), self.Alphabet, self.Name, normalize)

    def random(cls, Alphabet, diags=None):
        """Makes random P-matrix with specified diag elements and size.

        diags can be a single float, or vector of values with same number
        of chars as individual alphabet (e.g. list of 4 elements will act
        as elements for the 4 bases).
        """
        shape = Alphabet.Shape
        if diags is None:
            result = randarray(shape)
            return cls(result/sum(result, 1)[:,NewAxis], Alphabet)
        else:
            single_size = shape[0]
            diags = array(diags, 'd')
            #handle scalar case
            if not diags.shape:
                diags = reshape(diags, (1,))
            if len(diags) == 1:
                diags = repeat(diags, single_size)
            temp = randarray((single_size, single_size-1))
            temp *= ((1.0-diags)/sum(temp, 1))[:,NewAxis]
            result = diag(diags)
            for r, row in enumerate(temp):
                result[r][:r] = row[:r]
                result[r][r+1:] = row[r:]
            return cls(result, Alphabet)

    random = classmethod(random)

class Rates(PairMatrix):
    """Holds the data for a rate matrix. Immutable."""

    def __init__(self, data, Alphabet, name=None, normalize=False):
        """Returns new Rates matrix, normalizing trace to -1 if necessary."""
        data = array(data)
        #check for complex input array
        if data.dtype == 'complex128':
            self.imag = data.imag
            data = data.real
        super(Rates, self).__init__(data, Alphabet)
        if normalize:
            self._normalize_inplace()

    def isComplex(self):
        """Returns True if self has a complex component."""
        return hasattr(self, 'imag')

    def isSignificantlyComplex(self, threshold=0.1):
        """Returns True if complex component is above threshold."""
        if hasattr(self, 'imag'):
            return sum(ravel(self.imag)) > threshold
        else:
            return False

    def isValid(self, threshold=1e-7):
        """Rate matrix is valid if rows sum to 0 and no negative off-diags.
        
        threshold gives maximum error allowed in row sums.
        """
        if max(abs(sum(self._data, -1)) > threshold):
            return False
        return not has_neg_off_diags(self._data)
    
    def _normalize_inplace(self):
        """Normalizes trace to -1, in-place.
        
        Should only call during __init__, since it mutates the object.
        WARNING: Only normalizes real component.
        """
        scale_trace(self._data)

    def normalize(self):
        """Returns normalized copy of self where trace is -1.
        
        WARNING: Only normalizes real component.
        """
        return Rates(self._data, self.Alphabet, normalize=True)

    def _get_diagonalized(self):
        """Gets diagonalization of self as u, v, w; caches values."""
        if not hasattr(self, '_diag_cache'):
            error_tolerance = 1e-4  #amount of error allowed in product
            eigenvalues, eigenvectors = eig(self._data)
            u = transpose(eigenvectors)
            v = eigenvalues
            w = inverse(u)
            #check that the diagonalization actually worked by multiplying
            #the results back together
            result = dot(dot(u,v),w)
            if abs(sum(ravel(result))) > error_tolerance:
                raise ValueError, "Diagonalization failed with erroneous result."
            self._diag_cache = u, v, w
        return self._diag_cache

    _diagonalized = property(_get_diagonalized)
        
    def toProbs(self, time=1.0):
        """Returns probs at exp(self*scale_factor).
        
        The way this works is by diagonalizing the rate matrix so that u is
        the matrix with eigenvectors as columns, v is a vector of eigenvalues,
        and w is the inverse of u. u * diag(v) * w reconstructs the original
        rate matrix. u * diag(exp(v*t)) * w exponentiates the rate matrix to
        time t.

        This is more expensive than a single exponentiation if the rate matrix
        is going to be sxponentiated only once, but faster if it is to be
        exponentiated to many different time points.

        Note that the diagonalization is not the same as the svd.

        If the diagonalization fails, we use the naive version of just
        multiplying the rate matrix by the time and exponentiating.
        """
        try:
            u, v, w = self._diagonalized
            #scale v to the right time by exp(v_0*t)
            v = diag(exp(v * time))
            return Probs(dot(dot(u,v), w), self.Alphabet)
        except:
            return Probs(expm(self._data)(time), self.Alphabet)

    def _timeForSimilarity_naive(self, similarity, freqs=None):
        """Returns time exponent so that exp(q*time) diverges to right distance.

        Takes symbol freqs into account if specified; otherwise assumes equal.

        freqs: vector of frequencies, applied to each row successively.

        WARNING: Factor of 5 slower than timeForSimilarity. Included for 
        testing that results are identical.
        """
        q = self._data
        if freqs is None:
            def similarity_f(t):
                return abs(average(diagonal(expm(q)(t)))-similarity)
        else:
            def similarity_f(t):
                return abs(sum(diagonal(expm(q)(t)*freqs)) - similarity)
        initial_guess = array([1.0])
        result = fmin(similarity_f, initial_guess, disp=0)
        #disp=0 turns off fmin messages
        return result

    def timeForSimilarity(self, similarity, freqs=None):
        """Returns time exponent so that exp(q*time) diverges to right distance.

        Takes symbol freqs into account if specified; otherwise assumes equal.

        freqs: vector of frequencies, applied to each row successively.

        NOTE: harder to understand, but a factor of 5 faster than the naive
        version. The nested matrixmultiply calls have the same effect as
        exponentiating the matrix.
        """
        #if there's no change, the time is 0
        if similarity == 1:
            return 0.0
        #try fast version first, but if it fails we'll use the naive version.
        try:
            u, v, w = self._diagonalized
            if freqs is None:
                def similarity_f(t):
                    return abs(average(diagonal(dot(u, \
                    dot(diag(exp(v*t)), w)))) - similarity)
            else:
                def similarity_f(t):
                    return abs(sum(diagonal(dot(u, \
                    dot(diag(exp(v*t)), w)))*freqs) - similarity)
        except (TypeError, ValueError):
            #get here if diagonalization fails
            q = self._data
            if freqs is None:
                def similarity_f(t):
                    return abs(average(diagonal(expm(q)(t)))-similarity)
            else:
                def similarity_f(t):
                    return abs(sum(diagonal(expm(q)(t)*freqs))-similarity)
        return brent(similarity_f)

    def toSimilarProbs(self, similarity, freqs=None):
        """Returns Probs at specified divergence.

        Convenience wrapper for toProbs and timeForSimilarity.
        """
        return self.toProbs(self.timeForSimilarity(similarity, freqs))

    def random(cls, Alphabet, diags=None):
        """Makes random Q-matrix with specified diag elements and size.

        diags can be a single float, or vector of values with same number
        of chars as individual alphabet (e.g. list of 4 elements will act
        as elements for the 4 bases).
        """
        shape = Alphabet.Shape
        single_size = shape[0]
        if diags is None:
            diags = -randarray(single_size)
        else:
            diags = array(diags, 'd')
            #handle scalar case
            if not diags.shape:
                diags = reshape(diags, (1,))
            if len(diags) == 1:
                diags = repeat(diags, single_size)
        temp = randarray((single_size, single_size-1))
        temp *= ((-diags)/sum(temp, 1))[:,NewAxis]
        result = diag(diags)
        for r, row in enumerate(temp):
            result[r][:r] = row[:r]
            result[r][r+1:] = row[r:]
        return cls(result, Alphabet)

    random = classmethod(random)

    def hasNegOffDiags(self):
        """Returns True if any off-diagonal elements negative."""
        return has_neg_off_diags(self._data)

    def sumNegOffDiags(self):
        """Returns sum of negative off-diagonal elements."""
        return sum_neg_off_diags(self._data)

    def fixNegsDiag(self):
        """Returns copy of self w/o negative off-diags, using 'diag' heuristic.

        If a negative off-diagonal element is encountered, sets it to 0.

        Subtracts all the negative off-diagonals from the diagonal to preserve
        row sum = 0.
        """
        m = self._data.copy()
        #clip to 0
        m = choose(less(m, 0.), (m, 0.))
        for i, row in enumerate(m):
            row[i] = -sum(row)
        return self.__class__(m, self.Alphabet)

    def fixNegsEven(self):
        """Returns copy of self w/o negative off-diags, using 'even' heuristic.
        
        If a negative off-diagonal is encountered, sets it to 0.

        Distributes the negative score evenly among the other elements.
        """
        m = without_diag(self._data)
        for i, row in enumerate(m):
            is_neg = row < 0
            if any(is_neg):
                num_negs = sum(is_neg)
                sum_negs = sum(is_neg*row)
                is_not_neg = logical_not(is_neg)
                num_not_neg = sum(is_not_neg)
                new_row = (row + (sum_negs/(num_not_neg+1)))*is_not_neg
                m[i] = new_row
        return self.__class__(with_diag(m, -sum(m,1)), self.Alphabet)

    def _make_error_f(self, to_minimize):
        """Make error function whose minimization estimates q = ln(p)."""
        p = expm(self._data)(t=1)
        BIG = 1e10
        def result(q):
            new_q = reshape(q, (4,4))
            neg_sum = sum_neg_off_diags(new_q)
            p_new = expm(new_q)(t=1)
            return to_minimize(ravel(p), ravel(p_new)) - (BIG * neg_sum) \
                + (BIG * sum(abs(sum(new_q,1))))
        return result

    def fixNegsFmin(self, method=fmin, to_minimize=norm_diff, debug=False):
        """Uses an fmin method to find a good approximate q matrix.

        Possible values for method:
            
            fmin:           simplex method (the default)
            fmin_bfgs:      bfgs optimizer  #always produces negative elements!
            fmin_cg:        cg optimizer    #doesn't work!
            fmin_powell:    powell method   #doesn't work!
        """
        q = self._data
        #bail out if q is already ok to start with
        if not sum_neg_off_diags(q):
            return self
        err_f = self._make_error_f(to_minimize)
        initial_guess = q.copy()
        xmin = method(err_f, initial_guess.flat, disp=0)
        #disp=0 turns off messages
        new_q = reshape(xmin, self.Alphabet.Shape)[:]
        if debug:
            if sum_neg_off_diags(new_q):
                raise Exception, 'Made invalid Q matrix: %s' % q
        return self.__class__(new_q, self.Alphabet)

    def fixNegsConstrainedOpt(self, to_minimize=norm_diff, badness=1e6):
        """Uses constrained minimization to find approx q matrix.

        to_minimize: metric for comparing orig result and new result.

        badness: scale factor for penalizing negative off-diagonal values.
        """
        if not sum_neg_off_diags(self._data):
            return self
        q = ravel(without_diag(self._data))
        p = expm(self._data)(t=1)
        def err_f(q):
            new_q = reshape(array(q), (4,3))
            new_q = with_diag(new_q, -sum(new_q, 1))
            p_new = expm(new_q)(t=1)
            result = to_minimize(ravel(p), ravel(p_new))
            if q.min() < 0:
                result += -q.min() * badness
            return result
        a = array(q)
        xmin = fmin(func=err_f, x0=a, disp=0)
        r = reshape(xmin, (4,3))
        new_q = with_diag(r, -sum(r, 1))
        return self.__class__(new_q, self.Alphabet)

    def fixNegsReflect(self):
        """Fixes negative off-diagonals by subtracting m[i][j] from m[j][i].
        
        Specifically, if m[i][j] is negative, subtracts this value from
        m[i][j] and m[i][i] to keep the row total at 0, and then subtracts
        it from m[j][i] and m[j][j] to convert a negative flux in the forward
        direction into a positive flux in the reverse direction. If both
        m[i][j] and m[j][i] are negative, this algorithm converts them both
        into positive values, effectively exchanging the magnitudes of the
        changes and making the signs positive.

        NOTE: It's important to iterate over the original and make changes to
        the copy to avoid incorrect results in cases where both m[i][j] and
        m[j][i] are negative.
        """
        orig = self._data
        result = orig.copy()
        for i, row in enumerate(orig):
            for j, val in enumerate(row):
                #skip diagonal
                if i == j:
                    continue
                #only make changes if element < 0
                if val < 0:
                    result[i][j] -= val
                    result[i][i] += val
                    result[j][i] -= val
                    result[j][j] += val
        return self.__class__(result, self.Alphabet)

def goldman_q_rna_triple(seq1, seq2, outgroup):
    """Returns the Goldman rate matrix for seq1"""
    if len(seq1) != len(seq2) != len(outgroup):
        raise ValueError, "seq1,seq2 and outgroup are not the same length!"

    seq1 = ModelRnaSequence(seq1)
    seq2 = ModelRnaSequence(seq2)
    outgroup = ModelRnaSequence(outgroup)

    m = Counts.fromTriple(seq1, seq2, outgroup, RnaPairs)._data

    q = m / m.sum(axis=1)[:,NewAxis]
    new_diag = -(q.sum(axis=1) - diag(q))

    for i,v in enumerate(new_diag):
        q[i,i] = v

    return q

def goldman_q_dna_triple(seq1, seq2, outgroup):
    """Returns the Goldman rate matrix for seq1"""
    if len(seq1) != len(seq2) != len(outgroup):
        raise ValueError, "seq1,seq2 and outgroup are not the same length!"

    seq1 = ModelDnaSequence(seq1)
    seq2 = ModelDnaSequence(seq2)
    outgroup = ModelDnaSequence(outgroup)

    m = Counts.fromTriple(seq1, seq2, outgroup, DnaPairs)._data

    q = m / m.sum(axis=1)[:,NewAxis]
    new_diag = -(q.sum(axis=1) - diag(q))

    for i,v in enumerate(new_diag):
        q[i,i] = v

    return q

def goldman_q_dna_pair(seq1, seq2):
    """Returns the Goldman rate matrix"""
    if len(seq1) != len(seq2):
        raise ValueError, "seq1 and seq2 are not the same length!"

    seq1, seq2 = ModelDnaSequence(seq1), ModelDnaSequence(seq2)

    m = Counts.fromPair(seq1, seq2, DnaPairs,average=True)._data

    q = m / m.sum(axis=1)[:,NewAxis]
    new_diag = -(q.sum(axis=1) - diag(q))

    for i,v in enumerate(new_diag):
        q[i,i] = v

    return q

def goldman_q_rna_pair(seq1, seq2):
    """Returns the Goldman rate matrix"""
    if len(seq1) != len(seq2):
        raise ValueError, "seq1 and seq2 are not the same length!"

    seq1, seq2 = ModelRnaSequence(seq1), ModelRnaSequence(seq2)

    m = Counts.fromPair(seq1, seq2, RnaPairs,average=True)._data

    q = m / m.sum(axis=1)[:,NewAxis]
    new_diag = -(q.sum(axis=1) - diag(q))

    for i,v in enumerate(new_diag):
        q[i,i] = v

    return q

def make_random_from_file(lines):
    """Simulates array random() using values from an iterator."""
    def result(shape):
        size = product(shape)
        items = map(float, [lines.next() for s in range(size)])
        a = reshape(array(items), shape)
        return a
    return result


#randarray = make_random_from_file(open('/Users/rob/random.txt'))

def test_heuristics(p_range=None, num_to_do=71, heuristics=None):
    if p_range is None:
        p_range = [0.6]
    if heuristics is None:
        heuristics = ['fixNegsDiag', 'fixNegsEven', 'fixNegsReflect', 'fixNegsConstrainedOpt']
    num_heuristics = len(heuristics)
    print '\t'.join(['p'] + heuristics)
    for p in p_range:
        result = zeros((num_to_do, num_heuristics), Float64)
        has_nonzero = 0
        i = 0
        while i < num_to_do:
            curr_row = result[i]
            random_p = Probs.random(DnaPairs, p)
            q = random_p.toRates()
            if not q.hasNegOffDiags():
                continue
            has_nonzero += 1
            #print "P:"
            #print random_p._data
            #print "Q:"
            #print q._data
            i += 1
            for j, h in enumerate(heuristics):
                #print "HEURISTIC: ", h
                q_corr = getattr(q, h)()
                #print "CORRECTED Q: "
                #print q_corr._data
                p_corr = expm(q_corr._data)(t=1)
                #print "CORRECTED P:"
                #print p_corr
                dist = norm_diff(p_corr, random_p._data)
                #print "DISTANCE: ", dist
                curr_row[j] = dist
        averages = average(result)
        print p, '\t', '\t'.join(map(str, averages))

if __name__ == '__main__':
    test_heuristics()