[3] | 1 | """ |
---|
| 2 | Rudimentary support for PHAST's tree model file format (a simple format for |
---|
| 3 | storing trees and rate matrices). |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | from numpy import * |
---|
| 7 | |
---|
| 8 | class TreeModel: |
---|
| 9 | def __init__( self ): |
---|
| 10 | self.alphabet = None |
---|
| 11 | self.radix = 0 |
---|
| 12 | self.order = 0 |
---|
| 13 | self.subst_mod = None |
---|
| 14 | self.background = None |
---|
| 15 | self.tree = None |
---|
| 16 | self.matrix = None |
---|
| 17 | ## TODO: Need scipy for this method |
---|
| 18 | ## def matrix_for_time( self, t ): |
---|
| 19 | ## return expm( self.matrix * t ) |
---|
| 20 | ## matrix_for_time = cachedmethod( matrix_for_time ) |
---|
| 21 | @staticmethod |
---|
| 22 | def from_file( f ): |
---|
| 23 | input = iter( f ) |
---|
| 24 | tm = TreeModel() |
---|
| 25 | for line in input: |
---|
| 26 | if line.startswith( "ALPHABET:" ): |
---|
| 27 | tm.alphabet = tuple( line.split()[1:] ) |
---|
| 28 | tm.radix = len( tm.alphabet ) |
---|
| 29 | if line.startswith( "ORDER:" ): |
---|
| 30 | tm.order = int( line.split()[1] ) |
---|
| 31 | if line.startswith( "SUBST_MOD:" ): |
---|
| 32 | tm.subst_mod = line[11:].rstrip() |
---|
| 33 | if line.startswith( "BACKGROUND:" ): |
---|
| 34 | tm.background = tuple( map( float, line.split()[1:] ) ) |
---|
| 35 | if line.startswith( "TREE:" ): |
---|
| 36 | tm.tree = line[6:].strip() |
---|
| 37 | if line.startswith( "RATE_MAT:" ): |
---|
| 38 | matrix = zeros( (tm.radix,tm.radix), float ) |
---|
| 39 | for i in range( len( tm.alphabet ) ): |
---|
| 40 | matrix[i] = map( float, input.next().split() ) |
---|
| 41 | tm.matrix = matrix |
---|
| 42 | return tm |
---|