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
|
#/*******************************************************************************
#*
#* This file is part of the General Hidden Markov Model Library,
#* GHMM version __VERSION__, see http://ghmm.org
#*
#* Filename: ghmmhelper.py
#* Authors: Benjamin Georgi, Janne Grunau
#*
#* Copyright (C) 1998-2004 Alexander Schliep
#* Copyright (C) 1998-2001 ZAIK/ZPR, Universitaet zu Koeln
#* Copyright (C) 2002-2004 Max-Planck-Institut fuer Molekulare Genetik,
#* Berlin
#*
#* Contact: schliep@ghmm.org
#*
#* This library is free software; you can redistribute it and/or
#* modify it under the terms of the GNU Library General Public
#* License as published by the Free Software Foundation; either
#* version 2 of the License, or (at your option) any later version.
#*
#* This library is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#* Library General Public License for more details.
#*
#* You should have received a copy of the GNU Library General Public
#* License along with this library; if not, write to the Free
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#*
#*
#* This file is version $Revision: 2259 $
#* from $Date: 2009-04-22 04:19:35 -0400 (Wed, 22 Apr 2009) $
#* last change by $Author: grunau $.
#*
#*******************************************************************************/
import ghmmwrapper
import math
import os.path
from modhmmer import *
from random import *
def double_matrix2list(cmatrix, row, col):
llist = []
for i in range(row):
llist.append(ghmmwrapper.double_array2list(ghmmwrapper.double_matrix_get_col(cmatrix, i), col))
return llist
def list2double_matrix(matrix):
""" Allocation and initialization of a double** based on a
two dimensional Python list (list of lists).
The number of elements in each column can vary.
"""
cols = len(matrix)
seq = ghmmwrapper.double_matrix_alloc_row(cols)
col_len = []
for i in range(cols):
col = ghmmwrapper.list2double_array(matrix[i])
ghmmwrapper.double_matrix_set_col(seq, i, col)
col_len.append(len(matrix[i]))
return (seq, col_len)
def list2int_matrix(matrix):
""" Allocation and initialization of an int** based on a
two dimensional Python list (list of lists).
The number of elements in each column can vary.
"""
rows = len(matrix)
seq = ghmmwrapper.int_matrix_alloc_row(rows)
col_len = []
for i in range(rows):
col = ghmmwrapper.list2int_array(matrix[i])
ghmmwrapper.int_matrix_set_col(seq, i, col)
col_len.append(len(matrix[i]))
return (seq, col_len)
def int_matrix2list(cmatrix,row,col):
llist = []
for i in range(row):
llist.append(ghmmwrapper.int_array2list(ghmmwrapper.int_matrix_get_col(cmatrix,i),col))
return llist
def extract_out(lisprobs):
""" Helper function for building HMMs from matrices: Used for
transition matrices without transition classes.
Extract out-/ingoing transitions from the row-vector resp. the
column vector (corresponding to incoming transitions) of the
transition matrix
Allocates: .[out|in]_id and .[out|in]_a vectors
"""
lis = []
for i in range(len(lisprobs)):
if lisprobs[i]!=0:
lis.append(i)
trans_id = ghmmwrapper.int_array_alloc(len(lis))
trans_prob = ghmmwrapper.double_array_alloc(len(lis))
for i in range(len(lis)):
ghmmwrapper.int_array_setitem(trans_id, i, lis[i])
ghmmwrapper.double_array_setitem(trans_prob, i, lisprobs[lis[i]])
return [len(lis),trans_id,trans_prob]
#def extract_out_probs(lisprobs,cos):
# """ Helper function for building HMMs from matrices: Used for
# transition matrices with 'cos' transition classes.
# Extract out-/ingoing transitions from a matric consiting of
# the row-vectors resp. the column vectors (corresponding to
# incoming transitions) of the 'cos' transition matrices.
# Hence, input is a 'cos' x N matrix.
# Allocates: .[out|in]_id vector and .[out|in]_a array (of size cos x N)
# """
# lis = []
# parsing indixes belonging to postive probabilites
# for j in range(cos):
# for i in range(len(lisprobs[0])):
# if lisprobs[j][i] != 0 and i not in lis:
# lis.append(i)
# print "lis: ", lis
# trans_id = ghmmwrapper.int_array_alloc(len(lis))
# probsarray = ghmmwrapper.double_2d_array(cos, len(lis)) # C-function
# creating list with positive probabilities
# for k in range(cos):
# for j in range(len(lis)):
# ghmmwrapper.set_2d_arrayd(probsarray,k,j, lisprobs[k][lis[j]])
# trans_prob = twodim_double_array(probsarray, cos, len(lis)) # python CLASS, C internal
#
# print trans_prob
# initializing c state index array
# for i in range(len(lis)):
# ghmmwrapper.int_array_setitem(trans_id,i,lis[i])
# return [len(lis),trans_id,trans_prob]
def extract_out_cos(transmat, cos, state):
""" Helper function for building HMMs from matrices: Used for
transition matrices with 'cos' transition classes.
Extract outgoing transitions for 'state' from the complete list
of transition matrices
Allocates: .out_id vector and .out_a array (of size cos x N)
"""
lis = []
# parsing indixes belonging to postive probabilites
for j in range(cos):
for i in range(len(transmat[j][state])):
if transmat[j][state][i] != 0.0 and i not in lis:
lis.append(i)
#lis.sort()
#print "lis: ", lis
trans_id = ghmmwrapper.int_array_alloc(len(lis))
probsarray = ghmmwrapper.double_matrix_alloc(cos, len(lis)) # C-function
# creating list with positive probabilities
for k in range(cos):
for j in range(len(lis)):
ghmmwrapper.double_matrix_setitem(probsarray, k, j, transmat[k][state][lis[j]])
# initializing C state index array
for i in range(len(lis)):
ghmmwrapper.int_array_setitem(trans_id, i, lis[i])
return [len(lis),trans_id,probsarray]
def extract_in_cos(transmat, cos, state):
""" Helper function for building HMMs from matrices: Used for
transition matrices with 'cos' transition classes.
Extract ingoing transitions for 'state' from the complete list
of transition matrices
Allocates: .in_id vector and .in_a array (of size cos x N)
"""
lis = []
# parsing indixes belonging to postive probabilites
for j in range(cos):
transmat_col_state = [x[state] for x in transmat[j]]
for i in range(len(transmat_col_state)):
if transmat_col_state[i] != 0.0 and i not in lis:
lis.append(i)
#lis.sort()
#print "lis: ", lis
trans_id = ghmmwrapper.int_array_alloc(len(lis))
probsarray = ghmmwrapper.double_matrix_alloc(cos, len(lis)) # C-function
# creating list with positive probabilities
for k in range(cos):
for j in range(len(lis)):
ghmmwrapper.double_matrix_setitem(probsarray,k,j, transmat[k][lis[j]][state])
# initializing C state index array
for i in range(len(lis)):
ghmmwrapper.int_array_setitem(trans_id, i, lis[i])
return [len(lis),trans_id,probsarray]
class twodim_double_array:
""" Two-dimensional C-Double Array """
def __init__(self,array, rows, columns, rowlabels=None, columnlabels=None):
"""
Constructor
"""
self.array = array
self.rows = rows
self.columns = columns
self.size = (rows,columns)
self.rowlabels =rowlabels
self.columnlabels = columnlabels
def __getitem__(self,index):
"""
defines twodim_double_array[index[0],index[1]]
"""
return ghmmwrapper.double_matrix_getitem(self.array,index[0],index[1])
def __setitem__(self,index,value):
"""
defines twodim_double_array[index[0],index[1]]
"""
if (len(index) == 2):
ghmmwrapper.set_2d_arrayd(self.array,index[0],index[1],value)
def __str__(self):
"""
defines string representation
"""
strout = "\n"
if (self.columnlabels is not None):
for k in range(len(self.columnlabels)):
strout+="\t"
strout+= str(self.columnlabels[k])
strout += "\n"
for i in range(self.rows):
if (self.rowlabels is not None):
strout += str(self.rowlabels[i])
strout += "\t"
for j in range(self.columns):
strout += "%2.4f" % self[i,j]
strout += "\t"
strout += "\n"
return strout
# class double_array:
# """A C-double array"""
# def __init__(self, array, columns, columnlabels=None):
# """Constructor"""
# self.array = array
# self.rows = 1
# self.columns = columns
# self.size = columns
# self.columnlabels = columnlabels
# def __getitem__(self,index):
# """defines double_array[index] """
# return ghmmwrapper.get_arrayd(self.array,index)
# def __setitem__(self,index,value):
# """ double_array[index] = value """
# ghmmwrapper.set_arrayd(self.array,index,value)
# def __str__(self):
# """defines string representation"""
# strout = "\n"
# if (self.columnlabels is not None):
# for k in range(len(self.columnlabels)):
# strout+="\t"
# strout+= str(self.columnlabels[k])
# strout += "\n"
# for i in range(self.columns):
# strout += "%2.4f" % self[i]
# strout += "\t"
# strout += "\n"
# return strout
def classNumber(A):
""" Returns the number of transition classes in the matrix A """
cos = 0
if type(A[0][0]) == list:
cos = len(A)
else:
cos = 1
return cos
|