annotate overlapping_reads.py @ 14:5150d641d515 draft default tip

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit a54b915a3a245a71ca01ec9de2b5426cfb88504c
author artbio
date Thu, 06 Apr 2023 23:00:54 +0000
parents 124f404b0fe7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
1 import argparse
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
2 from collections import defaultdict
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
3
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
4 import pysam
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
5
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
6
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
7 def Parser():
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
8 the_parser = argparse.ArgumentParser()
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
9 the_parser.add_argument(
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
10 '--input', action="store", type=str, help="bam alignment file")
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
11 the_parser.add_argument(
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
12 '--minquery', type=int,
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
13 help="Minimum readsize of query reads (nt) - must be an integer")
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
14 the_parser.add_argument(
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
15 '--maxquery', type=int,
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
16 help="Maximum readsize of query reads (nt) - must be an integer")
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
17 the_parser.add_argument(
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
18 '--mintarget', type=int,
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
19 help="Minimum readsize of target reads (nt) - must be an integer")
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
20 the_parser.add_argument(
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
21 '--maxtarget', type=int,
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
22 help="Maximum readsize of target reads (nt) - must be an integer")
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
23 the_parser.add_argument(
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
24 '--overlap', type=int,
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
25 help="Overlap analyzed (nt) - must be an integer")
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
26 the_parser.add_argument(
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
27 '--output', action="store", type=str,
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
28 help="Pairable sequences")
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
29 args = the_parser.parse_args()
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
30 return args
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
31
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
32
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
33 class Map:
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
34
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
35 def __init__(self, bam_file, output, minquery=23, maxquery=29,
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
36 mintarget=23, maxtarget=29, overlap=10):
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
37 self.bam_object = pysam.AlignmentFile(bam_file, 'rb')
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
38 self.output = output
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
39 self.query_range = range(minquery, maxquery + 1)
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
40 self.target_range = range(mintarget, maxtarget + 1)
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
41 self.overlap = overlap
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
42 self.chromosomes = dict(zip(self.bam_object.references,
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
43 self.bam_object.lengths))
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
44 self.alignement_dic = self.index_alignments(self.bam_object)
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
45 self.all_query_positions = self.query_positions(self.bam_object,
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
46 overlap=self.overlap)
3
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
47 self.readdic = self.make_readdic(self.bam_object)
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
48 self.pairing()
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
49
3
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
50 def make_readdic(self, bam_object):
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
51 readdic = defaultdict(int)
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
52 for read in bam_object.fetch():
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
53 readdic[read.query_sequence] += 1
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
54 return readdic
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
55
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
56 def index_alignments(self, bam_object):
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
57 '''
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
58 dic[(chrom, pos, polarity)]: [readseq1, readseq2, ...]
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
59 the list value is further converted in set
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
60 '''
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
61 dic = defaultdict(list)
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
62 for chrom in self.chromosomes:
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
63 for read in bam_object.fetch(chrom):
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
64 if read.is_reverse:
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
65 coord = read.reference_end-1
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
66 pol = 'R'
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
67 else:
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
68 coord = read.reference_start
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
69 pol = 'F'
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
70 dic[(chrom, coord, pol)].append(read.query_sequence)
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
71 for key in dic:
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
72 dic[key] = set(dic[key])
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
73 return dic
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
74
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
75 def query_positions(self, bam_object, overlap):
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
76 all_query_positions = defaultdict(list)
11
8d3ca9652a5b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 8691f2575cacd71c971338658198a1324e5f9370"
artbio
parents: 7
diff changeset
77 for genomicKey in list(self.alignement_dic):
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
78 chrom, coord, pol = genomicKey
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
79 if pol == 'F' and len(self.alignement_dic[(chrom,
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
80 coord+overlap-1,
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
81 'R')]) > 0:
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
82 all_query_positions[chrom].append(coord)
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
83 for chrom in all_query_positions:
3
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
84 all_query_positions[chrom] = sorted(
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
85 list(set(all_query_positions[chrom])))
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
86 return all_query_positions
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
87
6
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
88 def countpairs(self, uppers, lowers):
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
89 query_range = self.query_range
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
90 target_range = self.target_range
7
07771982ef9b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 7276b6b73aef7af4058ad2c1e34c4557e9cccbe0
artbio
parents: 6
diff changeset
91 uppers = [seq for seq in uppers if (len(seq) in query_range or len(seq)
07771982ef9b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 7276b6b73aef7af4058ad2c1e34c4557e9cccbe0
artbio
parents: 6
diff changeset
92 in target_range)]
6
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
93 uppers_expanded = []
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
94 for seq in uppers:
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
95 expand = [seq for i in range(self.readdic[seq])]
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
96 uppers_expanded.extend(expand)
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
97 uppers = uppers_expanded
7
07771982ef9b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 7276b6b73aef7af4058ad2c1e34c4557e9cccbe0
artbio
parents: 6
diff changeset
98 lowers = [seq for seq in lowers if (len(seq) in query_range or len(seq)
07771982ef9b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 7276b6b73aef7af4058ad2c1e34c4557e9cccbe0
artbio
parents: 6
diff changeset
99 in target_range)]
6
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
100 lowers_expanded = []
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
101 for seq in lowers:
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
102 expand = [seq for i in range(self.readdic[seq])]
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
103 lowers_expanded.extend(expand)
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
104 lowers = lowers_expanded
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
105 paired = []
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
106 for upread in uppers:
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
107 for downread in lowers:
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
108 if (len(upread) in query_range and len(downread) in
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
109 target_range) or (len(upread) in target_range and
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
110 len(downread) in query_range):
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
111 paired.append(upread)
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
112 lowers.remove(downread)
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
113 break
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
114 return len(paired)
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
115
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
116 def pairing(self):
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
117 F = open(self.output, 'w')
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
118 query_range = self.query_range
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
119 target_range = self.target_range
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
120 overlap = self.overlap
3
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
121 stringresult = []
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
122 header_template = '>%s|coord=%s|strand %s|size=%s|nreads=%s\n%s\n'
6
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
123 total_pairs = 0
3
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
124 for chrom in sorted(self.chromosomes):
6
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
125 number_pairs = 0
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
126 for pos in self.all_query_positions[chrom]:
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
127 stringbuffer = []
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
128 uppers = self.alignement_dic[chrom, pos, 'F']
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
129 lowers = self.alignement_dic[chrom, pos+overlap-1, 'R']
6
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
130 number_pairs += self.countpairs(uppers, lowers)
4da23f009c9e planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6c727f4b2288c9b2517b28addf1eed6409d682a4
artbio
parents: 5
diff changeset
131 total_pairs += number_pairs
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
132 if uppers and lowers:
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
133 for upread in uppers:
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
134 for downread in lowers:
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
135 if (len(upread) in query_range and len(downread) in
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
136 target_range) or (len(upread) in target_range
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
137 and len(downread) in
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
138 query_range):
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
139 stringbuffer.append(
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
140 header_template %
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
141 (chrom, pos+1, '+', len(upread),
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
142 self.readdic[upread], upread))
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
143 stringbuffer.append(
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
144 header_template %
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
145 (chrom, pos+overlap-len(downread)+1, '-',
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
146 len(downread), self.readdic[downread],
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
147 self.revcomp(downread)))
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
148 stringresult.extend(sorted(set(stringbuffer)))
3
4d9682bd3a6b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 96ed5824190aff281cc3aa47dc60fc66aac41db3
artbio
parents: 1
diff changeset
149 F.write(''.join(stringresult))
1
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
150
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
151 def revcomp(self, sequence):
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
152 antidict = {"A": "T", "T": "A", "G": "C", "C": "G", "N": "N"}
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
153 revseq = sequence[::-1]
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
154 return "".join([antidict[i] for i in revseq])
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
155
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
156
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
157 if __name__ == "__main__":
6f1378738798 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 6133bb114c76a795fa12a4a11edb1a8b80fd104d
artbio
parents:
diff changeset
158 args = Parser()
5
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
159 mapobj = Map(args.input, args.output, args.minquery, args.maxquery,
a7fd04208764 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_signatures commit 24d44a9b7ec9db4dce3d839b597eea2b1be34adb
artbio
parents: 4
diff changeset
160 args.mintarget, args.maxtarget, args.overlap)