Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

########################################################################### 

# # 

# This file is part of Counter RNAseq Window (craw) package. # 

# # 

# Authors: Bertrand Neron # 

# Copyright (c) 2017-2019 Institut Pasteur (Paris). # 

# see COPYRIGHT file for details. # 

# # 

# craw is free software: you can redistribute it and/or modify # 

# it under the terms of the GNU General Public License as published by # 

# the Free Software Foundation, either version 3 of the License, or # 

# (at your option) any later version. # 

# # 

# craw 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 General Public License for more details. # 

# # 

# You should have received a copy of the GNU General Public License # 

# along with craw (see COPYING file). # 

# If not, see <http://www.gnu.org/licenses/>. # 

# # 

########################################################################### 

 

import logging 

import numpy as np 

import scipy.interpolate 

 

from pysam import AlignmentFile 

 

from .wig import Genome 

 

_log = logging.getLogger(__name__) 

 

 

def sum_coverage_maker(input_data, qual_thr=None): 

""" 

This function return a new function :func:`get_sum_coverage` 

 

:param input_data: the input either a samfile (see pysam library) or a genome build from a wig file (see wig module) 

:type input_data: :class:`craw.wig.Genome` or :class:`pysam.AlignmentFile` object 

:param qual_thr: The quality threshold 

if input data come from wig this parameter is not used, 

:type qual_thr: int 

:return: :func:`get_sum_coverage`, a function which compute the sum of coverage for a gene on each strand 

between position [start, stop[ This function take 3 parameters: 

 

- **annot_entry**: an entry of the annotation file. 

- **start**: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

- **stop**: The position to stop to compute the coverage (coordinates are 0-based, stop position is excluded). 

 

and 

 

- **return**: a tuple with 2 tuple of float or int representing the coverage on strand forward and reverse. 

 

:rtype: function 

""" 

get_raw_coverage = get_raw_coverage_function(input_data) 

 

 

def get_sum_coverage(annot_entry, start, stop): 

if start < 0: 

# if start is negative 

# when start is compute from large window and reads map at the beginning of the reference 

# pysam crash see issue #10 

# and numpy return empty silce 

# So we consider that negative positions (which doe not really exists) have coverage of 0 

start = 0 

 

covs = get_raw_coverage(input_data, annot_entry, start, stop, qual_thr) 

return tuple([(sum(cov),) for cov in covs]) 

 

return get_sum_coverage 

 

 

def resized_coverage_maker(input_data, new_size, qual_thr=None): 

""" 

 

:param input_data: the input either a samfile (see pysam library) or a genome build from a wig file (see wig module) 

:type input_data: :class:`craw.wig.Genome` or :class:`pysam.AlignmentFile` object 

:param new_size: the number of values in the coverage vector. 

:type new_size: postive int 

:param qual_thr: The quality threshold 

if input data come from wig this parameter is not used, 

:type qual_thr: int 

:return: a function :func:`get_resized_coverage`, a function which compute the coverage for a gene on each strand 

between position [start, stop[ This function take 3 parameters: 

the coverage values are generate by linear interpolation from raw values between [start, stop[ 

using the scipy. 

see https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.interpolate.interp1d.html 

 

- **annot_entry**: an entry of the annotation file. 

- **start**: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

- **stop**: The position to stop to compute the coverage (coordinates are 0-based, stop position is excluded). 

 

and 

 

- **return**: a tuple with 2 tuple of float or int representing the coverage on strand forward and reverse. 

 

:rtype: function 

""" 

get_raw_coverage = get_raw_coverage_function(input_data) 

 

def get_resized_coverage(annot_entry, start, stop): 

""" 

 

:param annot_entry: an entry of the annotation file. 

:type annot_entry: :class:`annotation.Entry` object. 

:param start: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

:type start: int 

:param stop: The position to stop to compute the coverage (coordinates are 0-based, stop position is excluded). 

:type stop: int 

:return: a new serie with new_size length 

:rtype: list of float instance. 

""" 

if start < 0: 

# if start is negative 

# when start is compute from large window and reads map at the beginning of the reference 

# pysam crash see issue #10 

# and numpy return empty silce 

# So we ommit negative value generate new values using real positions 

start = 0 

raw_forward_cov, raw_reverse_cov = get_raw_coverage(input_data, annot_entry, start, stop, qual_thr) 

coverages = [] 

serie_len = len(raw_forward_cov) 

for serie in raw_forward_cov, raw_reverse_cov: 

f = scipy.interpolate.interp1d(range(serie_len), serie) 

coverages.append(f(np.linspace(0, serie_len - 1, num=new_size, endpoint=True)).tolist()) 

return tuple(coverages) 

 

return get_resized_coverage 

 

 

def padded_coverage_maker(input_data, max_left, max_right, qual_thr=None): 

""" 

 

:param input_data: the input either a samfile (see pysam library) or a genome build from a wig file (see wig module) 

:type input_data: :class:`wig.Genome` or :class:`pysam.AlignmentFile` object 

:param max_left: The highest number of base before the reference position to take in account. 

:type max_left: int 

:param max_right: The highest number of base after the reference position to take in account. 

:type max_right: int 

:param qual_thr: The quality threshold 

if input data come from wig this parameter is not used, 

:type qual_thr: int 

:return: a function :func:`get_padded_coverage`, a function which compute the coverage for a gene on each strand 

between position *[start, stop[* 

 

The coverage values are centered on the annot_entry.ref position, the matrix is padded by ``None`` value.:: 

 

[.......[ coverage ref.pos ] .....] 

[....[covergae ref.pos ] .....] 

[............[ cov ref.pos ]] 

 

This function take 3 parameters: 

 

- **annot_entry**: an entry of the annotation file. 

- **start**: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

- **stop**: The position to stop to compute the coverage (coordinates are 0-based, stop position is excluded). 

 

and 

 

- **return**: a tuple with 2 tuple of float or int representing the coverage on strand forward and reverse. 

:rtype: function 

""" 

get_raw_coverage = get_raw_coverage_function(input_data) 

 

def get_padded_coverage(annot_entry, start, stop): 

""" 

 

:param annot_entry: an entry of the annotation file. 

:type annot_entry: :class:`annotation.Entry` object. 

:param start: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

:type start: int 

:param stop: The position to stop to compute the coverage (coordinates are 0-based, stop position is excluded). 

:type stop: int 

:return: the coverage for forward and reverse strand padded with ``None``. 

:rtype: tuple of 2 list containing int or float (forward coverage, reverse coverage) 

""" 

real_start = start 

pad_neg_start = [] 

if start < 0: 

# if start is negative 

# when start is compute from large window and reads map at the beginning of the reference 

# pysam crash see issue #10 

# so we ask coverage from 0 and pad with None value for negative positions 

start = 0 

pad_neg_start = [None] * abs(real_start) 

 

raw_forward_cov, raw_reverse_cov = get_raw_coverage(input_data, annot_entry, start, stop, qual_thr) 

 

if annot_entry.strand == '+': 

# -1 because the ref must not be take in account in pad 

# start and stop are 0 based (see docstring) 

pad_left = [None] * (max_left - (annot_entry.ref - 1 - real_start)) 

# but stop is excluded in get_bam and included in annot_entry 

# so it (stop -1) - ( ref -1) => stop -1 

pad_right = [None] * (max_right - (stop - annot_entry.ref)) 

pad_left += pad_neg_start 

else: 

pad_left = [None] * (max_left - (stop - annot_entry.ref)) 

pad_right = [None] * (max_right - (annot_entry.ref - 1 - real_start)) 

pad_right += pad_neg_start 

forward_cov = pad_left + raw_forward_cov + pad_right 

reverse_cov = pad_left + raw_reverse_cov + pad_right 

return forward_cov, reverse_cov 

return get_padded_coverage 

 

 

def get_raw_coverage_function(input): 

""" 

 

:param input: the input either a samfile (see pysam library) or a genome build from a wig file (see wig module) 

:type input: :class:`wig.Genome` or :class:`pysam.calignmentfile.AlignmentFile` object 

:return: get_wig_coverage or get_bam_coverage according the type of input 

:rtype: function 

:raise RuntimeError: when input is not instance of :class:`pysam.calignmentfile.AlignmentFile` or :class:`wig.Genome` 

""" 

if isinstance(input, AlignmentFile): 

return get_raw_bam_coverage 

elif isinstance(input, Genome): 

return get_raw_wig_coverage 

else: 

raise RuntimeError("get_coverage support only 'wig.Genome' or " 

"'pysam.calignmentfile.AlignmentFile' as Input, not {}".format(input.__class__.__name__)) 

 

 

def get_raw_wig_coverage(genome, annot_entry, start, stop, qual_thr=None): 

""" 

:param genome: The genome which store all coverages. 

:type genome: :class:`craw.wig.Genome` object 

:param annot_entry: an entry of the annotation file 

:type annot_entry: :class:`annotation.Entry` object 

:param start: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

:type start: int 

:param stop: The position to stop to compute the coverage (coordinates are 0-based, stop position is excluded). 

:type stop: int 

:param qual_thr: this parameter is not used, It's here to have the same api as get_bam_coverage. 

:type qual_thr: None 

:return: the coverage (all bases) 

:rtype: tuple of 2 list containing int or float 

""" 

chromosome = genome[annot_entry.chromosome] 

forward_cov, reverse_cov = chromosome[start:stop] 

if annot_entry.strand == '-': 

forward_cov.reverse() 

reverse_cov.reverse() 

return forward_cov, reverse_cov 

 

 

def get_raw_bam_coverage(sam_file, annot_entry, start, stop, qual_thr=15): 

""" 

Compute the coverage for a region position by position on each strand 

 

:param sam_file: the samfile openend with pysam 

:type sam_file: :class:`pysam.AlignmentFile` object. 

:param annot_entry: an entry of the annotation file 

:type annot_entry: :class:`annotation.Entry` object 

:param start: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

:type start: positive int 

:param stop: The position to start to compute the coverage (coordinates are 0-based, stop position is excluded). 

:type stop: positive int 

:param qual_thr: The quality threshold 

:type qual_thr: int 

:return: the coverage (all bases) 

:rtype: tuple of 2 list containing int 

""" 

def on_forward(al_seg): 

""" 

:param al_seg: a pysam aligned segment (the object used by pysam to represent an aligned read) 

:type al_seg: :class:`pysam.AlignedSegment` 

:return: True if read is mapped to forward strand 

:rtype: boolean 

""" 

return not al_seg.is_reverse 

 

def on_reverse(al_seg): 

""" 

:param al_seg: a pysam aligned segment (the object used by pysam to represent an aligned read) 

:type al_seg: :class:`pysam.AlignedSegment` 

:return: True if read is mapped to reverse strand. 

:rtype: boolean 

""" 

return al_seg.is_reverse 

 

def coverage_one_strand(sam_file, chromosome, start, stop, qual, strand): 

""" 

Compute the coverage for each position between start and stop on the chromosome on the strand. 

 

:param sam_file: the sam alignment to use 

:type sam_file: a :class:`pysam.AlignmentFile` object 

:param chromosome: the name of the chromosome 

:type chromosome: basestring 

:param start: The position to start to compute the coverage(coordinates are 0-based, start position is included). 

:type start: int 

:param stop:The position to start to compute the coverage (coordinates are 0-based, stop position is excluded). 

:type stop: int 

:param qual: The quality threshold. 

:type qual: int 

:param strand: the strand on which the read match 

:type strand: string 

:return: the coverage on forward then on reverse strand. 

The coverage is the sum of all kind bases mapped for each position 

:rtype: tuple of 2 list containing int 

""" 

call_back = on_forward if strand == '+' else on_reverse 

 

try: 

coverage = sam_file.count_coverage(chromosome, 

start=start, 

stop=stop, 

quality_threshold=qual, 

read_callback=call_back) 

except SystemError as err: 

import sys 

_log.critical("ERROR when call count_coverage with following arguments\n" 

"reference= {chromosome} \n" 

"start={start}\n" 

"end={stop}\n" 

"quality_threshold={qual}\n" 

"read_callback={call_back}".format(chromosome=chromosome, 

start=start, 

stop=stop, 

qual=qual, 

call_back=call_back) 

) 

raise err 

 

coverage = [array.tolist() for array in coverage] 

window_cov = [] 

for cov_A, cov_T, cov_C, cov_G in zip(*coverage): 

window_cov.append(cov_A + cov_T + cov_C + cov_G) 

return window_cov 

 

forward_cov = coverage_one_strand(sam_file, 

annot_entry.chromosome, 

start, 

stop, 

qual_thr, 

'+' 

) 

reverse_cov = coverage_one_strand(sam_file, 

annot_entry.chromosome, 

start, 

stop, 

qual_thr, 

'-' 

) 

if annot_entry.strand == '-': 

forward_cov.reverse() 

reverse_cov.reverse() 

return forward_cov, reverse_cov