Mercurial > repos > artbio > small_rna_maps
comparison small_rna_maps.py @ 25:07aa8f928d4b draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit a4aa17f675a9caaca0859fee210fae6ada74460f
author | artbio |
---|---|
date | Wed, 10 Apr 2019 06:38:33 -0400 |
parents | e75a10eba0a6 |
children | 376fae7c9f32 |
comparison
equal
deleted
inserted
replaced
24:e75a10eba0a6 | 25:07aa8f928d4b |
---|---|
288 | 288 |
289 def write_size_table(self, sizedic, out): | 289 def write_size_table(self, sizedic, out): |
290 ''' | 290 ''' |
291 Writer of a tabular file | 291 Writer of a tabular file |
292 Dataset, Chromosome, Chrom_length, <category (size)>, <some value> | 292 Dataset, Chromosome, Chrom_length, <category (size)>, <some value> |
293 from a dictionary of sizes: {chrom: {polarity: {size: nbre of reads}}} | |
293 out is an *open* file handler | 294 out is an *open* file handler |
294 ''' | 295 ''' |
295 for chrom in sorted(sizedic): | 296 for chrom in sorted(sizedic): |
296 sizes = sizedic[chrom]['F'].keys() | 297 sizes = sizedic[chrom]['F'].keys() |
297 sizes.extend(sizedic[chrom]['R'].keys()) | 298 sizes.extend(sizedic[chrom]['R'].keys()) |
299 strandness = defaultdict(int) | |
300 sizeness = defaultdict(int) | |
301 for polarity in sizedic[chrom]: | |
302 for size in range(min(sizes), max(sizes)+1): | |
303 try: | |
304 strandness[polarity] += sizedic[chrom][polarity][size] | |
305 except KeyError: | |
306 pass | |
307 sizeness[size] += sizedic[chrom][polarity][size] | |
308 Strandbias = strandness['F'] + strandness['R'] | |
309 if Strandbias: | |
310 Strandbias = strandness['F'] / float(Strandbias) | |
311 else: | |
312 Strandbias = 2 | |
313 Mean = numpy.mean(sizeness.values()) | |
314 StDev = numpy.std(sizeness.values()) | |
315 for size in sizeness: | |
316 if StDev: | |
317 sizeness[size] = (sizeness[size] - Mean) / StDev | |
318 else: | |
319 sizeness[size] = 0 | |
298 for polarity in sorted(sizedic[chrom]): | 320 for polarity in sorted(sizedic[chrom]): |
299 for size in range(min(sizes), max(sizes)+1): | 321 for size in range(min(sizes), max(sizes)+1): |
300 try: | 322 try: |
301 line = [self.sample_name, chrom, polarity, size, | 323 line = [self.sample_name, chrom, polarity, size, |
302 sizedic[chrom][polarity][size]] | 324 sizedic[chrom][polarity][size], |
325 Strandbias, sizeness[size]] | |
303 except KeyError: | 326 except KeyError: |
304 line = [self.sample_name, chrom, polarity, size, 0] | 327 line = [self.sample_name, chrom, polarity, size, 0, |
328 Strandbias, sizeness[size]] | |
305 line = [str(i) for i in line] | 329 line = [str(i) for i in line] |
306 out.write('\t'.join(line) + '\n') | 330 out.write('\t'.join(line) + '\n') |
307 | 331 |
308 def write_cluster_table(self, clustered_dic, out, bedpath): | 332 def write_cluster_table(self, clustered_dic, out, bedpath): |
309 ''' | 333 ''' |
353 def main(inputs, samples, methods, outputs, minsize, maxsize, cluster, | 377 def main(inputs, samples, methods, outputs, minsize, maxsize, cluster, |
354 nostrand, bedfile=None, bed_skipsize=0): | 378 nostrand, bedfile=None, bed_skipsize=0): |
355 for method, output in zip(methods, outputs): | 379 for method, output in zip(methods, outputs): |
356 out = open(output, 'w') | 380 out = open(output, 'w') |
357 if method == 'Size': | 381 if method == 'Size': |
358 header = ["Dataset", "Chromosome", "Polarity", method, "Counts"] | 382 header = ["Dataset", "Chromosome", "Polarity", method, "Counts", |
383 "Strandness", "z-score"] | |
359 elif cluster: | 384 elif cluster: |
360 header = ["Dataset", "Chromosome", "Chrom_length", "Coordinate", | 385 header = ["Dataset", "Chromosome", "Chrom_length", "Coordinate", |
361 "Polarity", method, "Start-End", "Cluster Size", | 386 "Polarity", method, "Start-End", "Cluster Size", |
362 "density"] | 387 "density"] |
363 else: | 388 else: |