1515from pyatac .utils import smooth , call_peaks , read_chrom_sizes_from_fasta
1616from pyatac .chunkmat2d import FragmentMat2D , BiasMat2D
1717from pyatac .bias import InsertionBiasTrack , PWM
18+ from scipy .special import gamma
1819
1920
2021class FragmentMixDistribution :
@@ -25,24 +26,24 @@ def __init__(self, lower = 0, upper =2000):
2526 def getFragmentSizes (self , bamfile , chunklist = None ):
2627 self .fragmentsizes = FragmentSizes (self .lower , self .upper )
2728 self .fragmentsizes .calculateSizes (bamfile , chunks = chunklist )
28- def modelNFR (self , boundary = 115 ):
29- """Model NFR distribution with exponential distribution"""
30- b = np . where (self .fragmentsizes .get (self . lower , boundary ) == max ( self . fragmentsizes . get ( self . lower , boundary )))[ 0 ][ 0 ] + 10 + self . lower
31- def exp_pdf (x ,* p ): #defines the PDF
32- k = p [0 ]
33- a = p [1 ]
34- x = x - b
35- return a * k * np .exp (- k * x )
36- x = np .array ( range ( b , boundary ))
37- p0 = (.1 , 1 )
38- coeff , var_matrix = optimize .curve_fit (exp_pdf ,x , self .fragmentsizes .get (b , boundary ),
29+ def modelNFR (self , boundaries = ( 45 , 115 ) ):
30+ """Model NFR distribution with gamma distribution"""
31+ smoothed = smooth (self .fragmentsizes .get (), 19 , window = "gaussian" , sd = 3 , mode = 'same' )
32+ def gamma_pdf (x ,* p ): #defines the PDF
33+ k = p [0 ]
34+ theta = p [1 ]
35+ a = p [ 2 ]
36+ return a * x ** ( k - 1 ) * np .exp (- x / theta ) / ( theta ** k * gamma ( k ) )
37+ x = np .arange ( boundaries [ 0 ], boundaries [ 1 ])
38+ p0 = (3 , 25 , 1 )
39+ coeff , var_matrix = optimize .curve_fit (gamma_pdf ,x , self .fragmentsizes .get (boundaries [ 0 ], boundaries [ 1 ] ),
3940 p0 = p0 )
40- nfr = np .concatenate ((self .fragmentsizes .get (self .lower ,boundary ), exp_pdf (np .array (range (boundary ,self .upper )),* coeff )))
41+ nfr = np .concatenate ((self .fragmentsizes .get (self .lower ,boundaries [ 1 ] ), gamma_pdf (np .array (range (boundaries [ 1 ] ,self .upper )),* coeff )))
4142 nfr [nfr == 0 ] = min (nfr [nfr != 0 ])* 0.01
4243 self .nfr_fit = FragmentSizes (self .lower ,self .upper , vals = nfr )
43- nuc = np .concatenate ((np .zeros (boundary - self .lower ),
44- self .fragmentsizes .get (boundary ,self .upper ) -
45- self .nfr_fit .get (boundary ,self .upper )))
44+ nuc = np .concatenate ((np .zeros (boundaries [ 1 ] - self .lower ),
45+ self .fragmentsizes .get (boundaries [ 1 ] ,self .upper ) -
46+ self .nfr_fit .get (boundaries [ 1 ] ,self .upper )))
4647 nuc [nuc <= 0 ]= min (min (nfr )* 0.1 ,min (nuc [nuc > 0 ])* 0.001 )
4748 self .nuc_fit = FragmentSizes (self .lower , self .upper , vals = nuc )
4849 def plotFits (self ,filename = None ):
0 commit comments