Mercurial > repos > artbio > small_rna_maps
annotate small_rna_maps.py @ 4:a6b9a081064b draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit b58cb36616bf351278e57ec1949e9ebf3c3cdff1
| author | artbio | 
|---|---|
| date | Fri, 25 Aug 2017 12:22:03 -0400 | 
| parents | ed8b0142538d | 
| children | 12c14642e6ac | 
| rev | line source | 
|---|---|
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 1 import argparse | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 2 from collections import defaultdict | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 3 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 4 import numpy | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 5 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 6 import pysam | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 7 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 8 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 9 def Parser(): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 10 the_parser = argparse.ArgumentParser() | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 11 the_parser.add_argument('--inputs', dest='inputs', required=True, | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 12 nargs='+', help='list of input BAM files') | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 13 the_parser.add_argument('--sample_names', dest='sample_names', | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 14 required=True, nargs='+', | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 15 help='list of sample names') | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 16 the_parser.add_argument('--outputs', nargs='+', action='store', | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 17 help='list of two output paths (only two)') | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 18 the_parser.add_argument('-M', '--plot_methods', nargs='+', action='store', | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 19 help='list of 2 plot methods (only two) among:\ | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 20 Counts, Max, Mean, Median, Coverage and Size') | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 21 args = the_parser.parse_args() | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 22 return args | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 23 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 24 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 25 class Map: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 26 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 27 def __init__(self, bam_file, sample): | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 28 self.sample_name = sample | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 29 self.bam_object = pysam.AlignmentFile(bam_file, 'rb') | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 30 self.chromosomes = dict(zip(self.bam_object.references, | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 31 self.bam_object.lengths)) | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 32 self.map_dict = self.create_map(self.bam_object) | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 33 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 34 def create_map(self, bam_object): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 35 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 36 Returns a map_dictionary {(chromosome,read_position,polarity): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 37 [read_length, ...]} | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 38 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 39 map_dictionary = defaultdict(list) | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 40 # get empty value for start and end of each chromosome | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 41 for chrom in self.chromosomes: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 42 map_dictionary[(chrom, 1, 'F')] = [] | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 43 map_dictionary[(chrom, self.chromosomes[chrom], 'F')] = [] | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 44 for chrom in self.chromosomes: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 45 for read in bam_object.fetch(chrom): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 46 positions = read.positions # a list of covered positions | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 47 if read.is_reverse: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 48 map_dictionary[(chrom, positions[-1]+1, | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 49 'R')].append(read.query_alignment_length) | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 50 else: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 51 map_dictionary[(chrom, positions[0]+1, | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 52 'F')].append(read.query_alignment_length) | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 53 return map_dictionary | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 54 | 
| 3 
ed8b0142538d
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit 7b2ceb05489c27ddb769c38fdec56274108a6fa1
 artbio parents: 
2diff
changeset | 55 def compute_readcount(self, map_dictionary, out): | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 56 ''' | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 57 takes a map_dictionary as input and writes | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 58 a readmap_dictionary {(chromosome,read_position,polarity): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 59 number_of_reads} | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 60 in an open file handler out | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 61 ''' | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 62 readmap_dictionary = dict() | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 63 for key in map_dictionary: | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 64 readmap_dictionary[key] = len(map_dictionary[key]) | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 65 self.write_table(readmap_dictionary, out) | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 66 | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 67 def compute_max(self, map_dictionary, out): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 68 ''' | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 69 takes a map_dictionary as input and writes | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 70 a max_dictionary {(chromosome,read_position,polarity): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 71 max_of_number_of_read_at_any_position} | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 72 Not clear this function is still required | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 73 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 74 merge_keylist = [(i[0], 0) for i in map_dictionary.keys()] | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 75 max_dictionary = dict(merge_keylist) | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 76 for key in map_dictionary: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 77 if len(map_dictionary[key]) > max_dictionary[key[0]]: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 78 max_dictionary[key[0]] = len(map_dictionary[key]) | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 79 self.write_table(max_dictionary, out) | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 80 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 81 def compute_mean(self, map_dictionary, out): | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 82 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 83 takes a map_dictionary as input and returns | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 84 a mean_dictionary {(chromosome,read_position,polarity): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 85 mean_value_of_reads} | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 86 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 87 mean_dictionary = dict() | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 88 for key in map_dictionary: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 89 if len(map_dictionary[key]) == 0: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 90 mean_dictionary[key] = 0 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 91 else: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 92 mean_dictionary[key] = round(numpy.mean(map_dictionary[key]), | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 93 1) | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 94 self.write_table(mean_dictionary, out) | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 95 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 96 def compute_median(self, map_dictionary, out): | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 97 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 98 takes a map_dictionary as input and returns | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 99 a mean_dictionary {(chromosome,read_position,polarity): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 100 mean_value_of_reads} | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 101 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 102 median_dictionary = dict() | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 103 for key in map_dictionary: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 104 if len(map_dictionary[key]) == 0: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 105 median_dictionary[key] = 0 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 106 else: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 107 median_dictionary[key] = numpy.median(map_dictionary[key]) | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 108 self.write_table(median_dictionary, out) | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 109 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 110 def compute_coverage(self, map_dictionary, out, quality=10): | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 111 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 112 takes a map_dictionary as input and returns | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 113 a coverage_dictionary {(chromosome,read_position,polarity): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 114 coverage} | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 115 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 116 coverage_dictionary = dict() | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 117 for chrom in self.chromosomes: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 118 coverage_dictionary[(chrom, 1, 'F')] = 0 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 119 coverage_dictionary[(chrom, self.chromosomes[chrom], 'F')] = 0 | 
| 4 
a6b9a081064b
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit b58cb36616bf351278e57ec1949e9ebf3c3cdff1
 artbio parents: 
3diff
changeset | 120 for read in self.bam_object.fetch(chrom): | 
| 
a6b9a081064b
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit b58cb36616bf351278e57ec1949e9ebf3c3cdff1
 artbio parents: 
3diff
changeset | 121 positions = read.positions # a list of covered positions | 
| 
a6b9a081064b
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit b58cb36616bf351278e57ec1949e9ebf3c3cdff1
 artbio parents: 
3diff
changeset | 122 for pos in positions: | 
| 
a6b9a081064b
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit b58cb36616bf351278e57ec1949e9ebf3c3cdff1
 artbio parents: 
3diff
changeset | 123 if not map_dictionary[(chrom, pos+1, 'F')]: | 
| 
a6b9a081064b
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit b58cb36616bf351278e57ec1949e9ebf3c3cdff1
 artbio parents: 
3diff
changeset | 124 map_dictionary[(chrom, pos+1, 'F')] = [] | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 125 for key in map_dictionary: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 126 coverage = self.bam_object.count_coverage( | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 127 reference=key[0], | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 128 start=key[1]-1, | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 129 end=key[1], | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 130 quality_threshold=quality) | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 131 """ Add the 4 coverage values """ | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 132 coverage = [sum(x) for x in zip(*coverage)] | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 133 coverage_dictionary[key] = coverage[0] | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 134 self.write_table(coverage_dictionary, out) | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 135 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 136 def compute_size(self, map_dictionary, out): | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 137 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 138 Takes a map_dictionary and returns a dictionary of sizes: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 139 {chrom: {polarity: {size: nbre of reads}}} | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 140 ''' | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 141 size_dictionary = defaultdict(lambda: defaultdict( | 
| 1 
40972a8dfab9
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit ad60e6655aabe30246043d95f14646b2527c9255
 artbio parents: 
0diff
changeset | 142 lambda: defaultdict(int))) | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 143 # to track empty chromosomes | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 144 for chrom in self.chromosomes: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 145 if self.bam_object.count(chrom) == 0: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 146 size_dictionary[chrom]['F'][10] = 0 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 147 for key in map_dictionary: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 148 for size in map_dictionary[key]: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 149 size_dictionary[key[0]][key[2]][size] += 1 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 150 self.write_size_table(size_dictionary, out) | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 151 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 152 def write_table(self, mapdict, out): | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 153 ''' | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 154 Generic writer | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 155 Dataset, Chromosome, Chrom_length, Coordinate, Polarity, | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 156 <some mapped value> | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 157 out is an *open* file handler | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 158 ''' | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 159 for key in sorted(mapdict): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 160 line = [self.sample_name, key[0], self.chromosomes[key[0]], | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 161 key[1], key[2], mapdict[key]] | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 162 line = [str(i) for i in line] | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 163 out.write('\t'.join(line) + '\n') | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 164 | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 165 def write_size_table(self, sizedic, out): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 166 ''' | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 167 Generic writer of summary values | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 168 Dataset, Chromosome, Chrom_length, <some category>, <some value> | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 169 out is an *open* file handler | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 170 ''' | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 171 for chrom in sorted(sizedic): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 172 sizes = sizedic[chrom]['F'].keys() | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 173 sizes.extend(sizedic[chrom]['R'].keys()) | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 174 for polarity in sorted(sizedic[chrom]): | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 175 for size in range(min(sizes), max(sizes)+1): | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 176 try: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 177 line = [self.sample_name, chrom, polarity, size, | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 178 sizedic[chrom][polarity][size]] | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 179 except KeyError: | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 180 line = [self.sample_name, chrom, polarity, size, 0] | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 181 line = [str(i) for i in line] | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 182 out.write('\t'.join(line) + '\n') | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 183 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 184 | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 185 def main(inputs, samples, methods, outputs): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 186 for method, output in zip(methods, outputs): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 187 F = open(output, 'w') | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 188 if method == 'Size': | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 189 header = ["Dataset", "Chromosome", "Polarity", method, "Count"] | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 190 else: | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 191 header = ["Dataset", "Chromosome", "Chrom_length", "Coordinate", | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 192 "Polarity", method] | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 193 F.write('\t'.join(header) + '\n') | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 194 for input, sample in zip(inputs, samples): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 195 mapobj = Map(input, sample) | 
| 3 
ed8b0142538d
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit 7b2ceb05489c27ddb769c38fdec56274108a6fa1
 artbio parents: 
2diff
changeset | 196 token = {"Counts": mapobj.compute_readcount, | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 197 "Max": mapobj.compute_max, | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 198 "Mean": mapobj.compute_mean, | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 199 "Median": mapobj.compute_median, | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 200 "Coverage": mapobj.compute_coverage, | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 201 "Size": mapobj.compute_size} | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 202 token[method](mapobj.map_dict, F) | 
| 0 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 203 F.close() | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 204 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 205 | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 206 if __name__ == "__main__": | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 207 args = Parser() | 
| 
6d48150495e3
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit d4d8106d66b65679a1a685ab94bfcf99cdb7b959
 artbio parents: diff
changeset | 208 # if identical sample names | 
| 2 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 209 if len(set(args.sample_names)) != len(args.sample_names): | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 210 args.sample_names = [name + '_' + str(i) for | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 211 i, name in enumerate(args.sample_names)] | 
| 
507383cce5a8
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
 artbio parents: 
1diff
changeset | 212 main(args.inputs, args.sample_names, args.plot_methods, args.outputs) | 
