diff small_rna_maps.py @ 18:2c95c899d0a4 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit 06c6feefeaaa66f7dec1f6143a2c5aaf91836320
author artbio
date Thu, 22 Nov 2018 03:07:41 -0500
parents b28dcd4051e8
children f33afecac67a
line wrap: on
line diff
--- a/small_rna_maps.py	Thu Nov 15 12:29:57 2018 -0500
+++ b/small_rna_maps.py	Thu Nov 22 03:07:41 2018 -0500
@@ -19,6 +19,14 @@
     the_parser.add_argument('--sample_names', dest='sample_names',
                             required=True, nargs='+',
                             help='list of sample names')
+    the_parser.add_argument('--bed', dest='bed', required=False,
+                            help='Name of bed output must be specified\
+                            if --cluster option used')
+    the_parser.add_argument('--bed_skipcluster', dest='bed_skipcluster',
+                            required=False, type=int, default=0,
+                            help='Skip clusters of size equal or less than\
+                            specified integer in the bed output. \
+                            Default = 1')
     the_parser.add_argument('--outputs', nargs='+', action='store',
                             help='list of two output paths (only two)')
     the_parser.add_argument('-M', '--plot_methods', nargs='+', action='store',
@@ -269,27 +277,40 @@
                     line = [str(i) for i in line]
                     out.write('\t'.join(line) + '\n')
 
-    def write_cluster_table(self, clustered_dic, out):
+    def write_cluster_table(self, clustered_dic, out, bedpath, skip):
         '''
         Writer of a tabular file
         Dataset, Chromosome, Chrom_length, Coordinate, Polarity,
         <some mapped value>
         out is an *open* file handler
+        bed is an a file handler internal to the function
         '''
+        bed = open(bedpath, 'w')
         for key in sorted(clustered_dic):
             start = clustered_dic[key][1][0]
             end = clustered_dic[key][1][1]
             size = end - start + 1
+            if self.nostrand:
+                polarity = '.'
+            elif key[2] == 'F':
+                polarity = '+'
+            else:
+                polarity = '-'
             density = float(clustered_dic[key][0]) / size
             line = [self.sample_name, key[0], self.chromosomes[key[0]],
                     key[1], key[2], clustered_dic[key][0],
                     str(start) + "-" + str(end), str(size), str(density)]
             line = [str(i) for i in line]
+            if size > skip:
+                bedline = [key[0], str(start-1), str(end), 'cluster', '.',
+                           polarity]
+                bed.write('\t'.join(bedline) + '\n')
             out.write('\t'.join(line) + '\n')
+        bed.close()
 
 
 def main(inputs, samples, methods, outputs, minsize, maxsize, cluster,
-         nostrand):
+         nostrand, bedfile=None, bed_skipcluster=0):
     for method, output in zip(methods, outputs):
         out = open(output, 'w')
         if method == 'Size':
@@ -312,10 +333,10 @@
                      "Size": mapobj.compute_size,
                      "cluster": mapobj.write_cluster_table}
             if cluster:
-                token["cluster"](mapobj.map_dict, out)
+                token["cluster"](mapobj.map_dict, out, bedfile,
+                                 bed_skipcluster)
             else:
                 token[method](mapobj.map_dict, out)
-            #   mapobj.compute_coverage(mapobj.map_dict, out)
         out.close()
 
 
@@ -326,4 +347,5 @@
         args.sample_names = [name + '_' + str(i) for
                              i, name in enumerate(args.sample_names)]
     main(args.inputs, args.sample_names, args.plot_methods, args.outputs,
-         args.minsize, args.maxsize, args.cluster, args.nostrand)
+         args.minsize, args.maxsize, args.cluster, args.nostrand, args.bed,
+         args.bed_skipcluster)