comparison small_rna_map.py @ 0:1ad5d040f85f draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_map commit fa452d860cf550c7524df59e77b36fd39e3e2a45
author artbio
date Mon, 10 Jul 2017 10:31:49 -0400
parents
children 2e0dc6032a98
comparison
equal deleted inserted replaced
-1:000000000000 0:1ad5d040f85f
1 import os
2 from os.path import basename
3 import pysam
4 import argparse
5 import numpy
6 import collections
7
8
9 def Parser():
10 the_parser = argparse.ArgumentParser()
11 the_parser.add_argument('--input', dest="input", required=True, nargs='+', help='input BAM files')
12 the_parser.add_argument('--output', action="store", type=str, help="output tabular file")
13 args = the_parser.parse_args()
14 return args
15
16
17 def Ref_length(bamfile):
18 """
19 bamfile.references: list of reference names, and bamfile.lengths is list of their lengths
20 """
21 ref_length={}
22 for e1, e2 in zip(bamfile.references, bamfile.lengths):
23 ref_length[e1]=e2
24 return ref_length
25
26
27 def Ref_coord_pol(bamfile):
28 ref_coord ={}
29 rnames =list(bamfile.references)
30 for read in bamfile:
31 if not read.is_unmapped:
32 polarity = "F" if (not read.is_reverse) else "R"
33 read_rname = read.reference_name
34 read_position = read.pos + 1
35 the_key = (read_rname, read_position,polarity)
36 read_length = read.qlen
37 try:
38 ref_coord[the_key][0] = ref_coord[the_key][0]+1
39 ref_coord[the_key][1].append(read_length)
40 except:
41 ref_coord[the_key] = [1,[read_length]]
42 if read_rname in rnames:
43 rnames.remove(read_rname)
44 for rname in rnames:
45 ref_coord[(rname, 0, "F")]=[0,[0]]
46 return collections.OrderedDict(sorted(ref_coord.items())) #dictionary {(read_rname,read_position,polarity):[nmbr_read,list_of_alignment_length]}
47
48
49 def calcul_stat(dictionary,ref):
50 dico = {}
51 for keys in dictionary:
52 key = keys[0] #reference name
53 value = dictionary[keys][0] # position
54 try:
55 dico[key].append(value)
56 except:
57 dico[key] = []
58 dico[key].append(value)
59 the_max = max(dico[ref])
60 return the_max
61
62
63 def filename(infile):
64 filename = basename(infile.filename)
65 the_name = filename.split('.')
66 return the_name[0]
67
68
69 def main(infile, output):
70 with open(output,"w") as outfile:
71 outfile.write("Dataset" + "\t" + "Chromosome"+ "\t" + "Chrom_length" + "\t" + "Coordinate" + "\t" + "Nbr_reads" + "\t" + "Polarity" + "\t" + "Max" + "\t" + "Mean" + "\t" + "Median" + "\n")
72 for bamfile in infile:
73 with pysam.AlignmentFile(bamfile,"rb") as bamfile:
74 ref_coord = Ref_coord_pol(bamfile)
75 ref_length = Ref_length(bamfile)
76 tabulation = "\t"
77 for read in ref_coord:
78 table = (str(filename(bamfile)), str(read[0]), str(ref_length[str(read[0])]), str(read[1]), str(ref_coord[read][0]), str(read[2]), str(calcul_stat(ref_coord,read[0])), str(round(numpy.mean(ref_coord[read][1]),1)), str(numpy.median(ref_coord[read][1])))
79 outfile.write(tabulation.join(table) + "\n")
80
81
82 if __name__ == "__main__":
83 args= Parser()
84 main(args.input, args.output)