diff bamparse.py @ 2:8ea06787c08a draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/bamparse commit 968c9ab925ed768027ff8012d0ff6410fc24f079
author artbio
date Tue, 09 Oct 2018 17:14:57 -0400
parents ae9ea0488850
children
line wrap: on
line diff
--- a/bamparse.py	Sun Oct 15 19:14:29 2017 -0400
+++ b/bamparse.py	Tue Oct 09 17:14:57 2018 -0400
@@ -9,10 +9,6 @@
     the_parser = argparse.ArgumentParser()
     the_parser.add_argument('--output', nargs='+', action='store', type=str,
                             help='Count tables')
-    the_parser.add_argument('--polarity',
-                            choices=["sense", "antisense", "both"],
-                            help="forward, reverse or both forward an\
-                                reverse reads are counted")
     the_parser.add_argument('--alignments', nargs='+',
                             help="bam alignments files")
     the_parser.add_argument('--labels', nargs='+', help="Alignments labels")
@@ -23,37 +19,17 @@
     return args
 
 
-def get_counts(bamfile, polarity="both"):
+def get_counts(bamfile):
     """
     Takes an AlignmentFile object and returns a dictionary of counts for sense,
-    antisense, or both sense and antisense reads aligning to the bam references
+    antisense, or both sense and antisense bam alignments to the references,
+    depending on the pre-treatment performed by sambamba in the xml wrapper
     """
-    def filter_sense_read(read):
-        if read.is_reverse:
-            return 0
-        else:
-            return 1
-
-    def filter_antisense_read(read):
-        if read.is_reverse:
-            return 1
-        else:
-            return 0
-
     counts = defaultdict(int)
     for ref_name in bamfile.references:
         counts[ref_name] = 0
-    if polarity == "both":
-        for ref_name in bamfile.references:
-            counts[ref_name] = bamfile.count(reference=ref_name)
-    if polarity == "sense":
-        for ref_name in bamfile.references:
-            for read in bamfile.fetch(ref_name):
-                counts[ref_name] += filter_sense_read(read)
-    if polarity == "antisense":
-        for ref_name in bamfile.references:
-            for read in bamfile.fetch(ref_name):
-                counts[ref_name] += filter_antisense_read(read)
+    for ref_name in bamfile.references:
+        counts[ref_name] = bamfile.count(reference=ref_name)
     return counts
 
 
@@ -80,14 +56,14 @@
             out.close()
 
 
-def main(alignments, labels, polarity, output, number):
+def main(alignments, labels, output, number):
     diclist = []
     for file in alignments:
         bam_object = pysam.AlignmentFile(file, 'rb')
-        diclist.append(get_counts(bam_object, polarity=polarity))
+        diclist.append(get_counts(bam_object))
     writetable(diclist, labels, output, number)
 
 
 if __name__ == "__main__":
     args = Parser()
-    main(args.alignments, args.labels, args.polarity, args.output, args.number)
+    main(args.alignments, args.labels, args.output, args.number)