@@ -8,7 +8,6 @@ import numpy as np
88cimport numpy as np
99
1010from readMutStrings cimport READ, parseLine, fillReadMut, incrementArrays
11- from dSFMT cimport dsfmt_t, dsfmt_init_gen_rand, dsfmt_genrand_close_open
1211
1312
1413# ##########################################################################
@@ -447,151 +446,6 @@ def fillRINGMatrix(char[:,::1] reads, char[:,::1] mutations, char[:] activestatu
447446 return read_arr, comut_arr, inotj_arr
448447
449448
450-
451- def fillRINGMatrix_montecarlo (char[:,::1] reads , char[:,::1] mutations , char[:] activestatus ,
452- double[:,::1] mu , double[:] p , int window , int samplenumber , int subtractwindow ):
453- """ active status is array containing 0/1 whether or not column is to be included
454- posterior prob calculations"""
455-
456-
457- # initialize RING matrices
458- cdef int [:,:,::1 ] read_arr = np.zeros((p.shape[0 ], mu.shape[1 ], mu.shape[1 ]), dtype = np.int32)
459- cdef int [:,:,::1 ] comut_arr = np.zeros((p.shape[0 ], mu.shape[1 ], mu.shape[1 ]), dtype = np.int32)
460- cdef int [:,:,::1 ] inotj_arr = np.zeros((p.shape[0 ], mu.shape[1 ], mu.shape[1 ]), dtype = np.int32)
461-
462-
463- # declare counters
464- cdef int n,i,j,m
465-
466- cdef int pdim = p.shape[0 ]
467- cdef int maxreadidx = reads.shape[0 ]- 1
468-
469- # setup the random number generator
470- cdef dsfmt_t dsfmt
471- dsfmt_init_gen_rand(& dsfmt, np.uint32(time.time()))
472-
473- # compute logp
474- cdef double [:] logp = np.log(p)
475-
476- # compute logmu and clogmu
477- cdef double [:,::1 ] logmu = np.zeros((mu.shape[0 ], mu.shape[1 ]))
478- cdef double [:,::1 ] clogmu = np.zeros((mu.shape[0 ], mu.shape[1 ]))
479- for i in xrange (pdim):
480- for j in xrange (mu.shape[1 ]):
481- if mu[i,j] > 0 :
482- logmu[i,j] = log( mu[i,j] )
483- clogmu[i,j] = log( 1 - mu[i,j] )
484-
485-
486- # declare other needed containers
487- cdef double [:] loglike = np.empty(pdim) # container for read loglike of each model
488- cdef double [:] ll_i = np.empty(pdim) # container for loglike subtracting i
489- cdef double [:] ll_ij = np.empty(pdim) # container for loglike subtracting i & j
490- cdef double [:] weights = np.empty(pdim) # container for normalized probabilties
491- cdef int [:] occupancy = np.empty(pdim, dtype = np.int32)
492-
493- # codes for contigency table
494- cdef int icode
495- cdef int jcode
496-
497- cdef int step = 0
498-
499- for step in xrange (samplenumber):
500-
501- if step% 10000 == 0 :
502- printf(" \r %d " , step)
503- fflush(stdout)
504-
505- # select read (sample w/ replacement)
506- n = lrint(dsfmt_genrand_close_open(& dsfmt)* maxreadidx)
507-
508- # compute overall loglike of the read
509- readloglike(loglike, activestatus, reads[n,:], mutations[n,:], logp, logmu, clogmu)
510-
511- _loglike2prob(loglike, weights)
512-
513- # compute occupancy based on whole read loglike
514- for m in xrange (pdim):
515- occupancy[m] = 0
516- if weights[m] >= dsfmt_genrand_close_open(& dsfmt):
517- occupancy[m] = 1
518-
519-
520- # now iterate through all i/j pairs
521- for i in xrange (read_arr.shape[1 ]- window+ 1 ):
522-
523- # compute mut code, and skip if not read at all
524- icode = _computeMutCode(reads[n,:], mutations[n,:], i, window)
525- if icode < 0 : continue
526-
527- if subtractwindow:
528- # reset ll_i
529- for m in xrange (pdim):
530- ll_i[m] = loglike[m]
531-
532- # subtract window i
533- _subtractloglike(ll_i, i, window, reads[n,:], mutations[n,:], activestatus, logmu, clogmu)
534-
535- # compute weight of read ignoring i
536- _loglike2prob(ll_i, weights)
537-
538-
539- # increment the diagonal for keeping track of overall mutation rate
540- for m in xrange (pdim):
541- if occupancy[m]:
542- read_arr[m,i,i] += 1
543- if icode== 1 :
544- comut_arr[m,i,i] += 1
545-
546-
547- for j in xrange (i+ 1 , read_arr.shape[1 ]- window+ 1 ):
548-
549- jcode = _computeMutCode(reads[n,:], mutations[n,:], j, window)
550- if jcode < 0 : continue
551-
552-
553- if subtractwindow:
554- # reset ll_ij
555- for m in xrange (pdim):
556- ll_ij[m] = ll_i[m]
557-
558- # subtract j
559- _subtractloglike(ll_ij, j, window, reads[n,:], mutations[n,:], activestatus, logmu, clogmu)
560-
561- # compute weight of read ignoring i & j
562- _loglike2prob(ll_ij, weights)
563-
564- # compute occupancy
565- for m in xrange (pdim):
566- occupancy[m] = 0
567- if weights[m] >= dsfmt_genrand_close_open(& dsfmt):
568- occupancy[m] = 1
569-
570-
571- # now iterate through models and increment RING matrices
572- for m in xrange (pdim):
573- # add the read
574- if occupancy[m]:
575- read_arr[m,i,j] += 1
576-
577- if icode == 1 and jcode == 1 :
578- comut_arr[m,i,j] += 1
579- elif icode == 1 and jcode == 0 :
580- inotj_arr[m,i,j] += 1
581- elif icode == 0 and jcode == 1 :
582- inotj_arr[m,j,i] += 1
583-
584-
585-
586- # reset cursor to new line
587- printf(" \n\n " )
588- fflush(stdout)
589-
590- return read_arr, comut_arr, inotj_arr
591-
592-
593-
594-
595449cdef void _subtractloglike(double [:] loglike, int i_index, int window,
596450 char [:] read, char [:] mutation, char [:] activestatus,
597451 double [:,::1 ] logmu, double [:,::1 ] clogmu):
0 commit comments