File: Utilities.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (60 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download | duplicates (3)
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
"""Generic functions which are useful for working with HMMs.

This just collects general functions which you might like to use in
dealing with HMMs.
"""
# standard modules
import string

def pretty_print_prediction(emissions, real_state, predicted_state,
                            emission_title = "Emissions",
                            real_title = "Real State",
                            predicted_title = "Predicted State",
                            line_width = 75):
    """Print out a state sequence prediction in a nice manner.

    Arguments:
    
    o emissions -- The sequence of emissions of the sequence you are
    dealing with.

    o real_state -- The actual state path that generated the emissions.

    o predicted_state -- A state path predicted by some kind of HMM model.
    """
    # calculate the length of the titles and sequences
    title_length = max(len(emission_title), len(real_title),
                       len(predicted_title)) + 1
    seq_length = line_width - title_length

    # set up the titles so they'll print right
    emission_title = string.ljust(emission_title, title_length)
    real_title = string.ljust(real_title, title_length)
    predicted_title = string.ljust(predicted_title, title_length)

    cur_position = 0
    # while we still have more than seq_length characters to print
    while 1:
        if (cur_position + seq_length) < len(emissions):
            extension = seq_length
        else:
            extension = len(emissions) - cur_position
        
        print "%s%s" % (emission_title,
                        emissions.data[cur_position:cur_position + seq_length])
        print "%s%s" % (real_title,
                        real_state.data[cur_position:
                                        cur_position + seq_length])
        print "%s%s\n" % (predicted_title,
                          predicted_state.data[cur_position:
                                               cur_position + seq_length])

        if (len(emissions) < (cur_position + seq_length)):
            break

        cur_position += seq_length