annotate flanking_features.py @ 3:9f12b7e500f1 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit d7b1a60c0aecc46b7f625c3e32f882562b512fd9
author devteam
date Mon, 13 Jun 2022 16:22:18 +0000
parents a09d13b108fd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
2 # By: Guruprasad Ananda
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
3 """
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
4 Fetch closest up/downstream interval from features corresponding to every interval in primary
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
5
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
6 usage: %prog primary_file features_file out_file direction
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
7 -1, --cols1=N,N,N,N: Columns for start, end, strand in first file
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
8 -2, --cols2=N,N,N,N: Columns for start, end, strand in second file
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
9 -G, --gff1: input 1 is GFF format, meaning start and end coordinates are 1-based, closed interval
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
10 -H, --gff2: input 2 is GFF format, meaning start and end coordinates are 1-based, closed interval
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
11 """
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
12 from __future__ import print_function
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
13
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
14 import fileinput
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
15 import sys
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
16
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
17 from bx.cookbook import doc_optparse
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
18 from bx.intervals.io import Comment, GenomicInterval, Header, NiceReaderWrapper
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
19 from bx.intervals.operations import quicksect
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
20 from bx.tabular.io import ParseError
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
21 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
22
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
23 from utils.gff_util import convert_bed_coords_to_gff, GFFIntervalToBEDReaderWrapper
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
24
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
25 assert sys.version_info[:2] >= ( 2, 4 )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
26
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
27
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
28 def get_closest_feature(node, direction, threshold_up, threshold_down, report_func_up, report_func_down):
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
29 # direction=1 for +ve strand upstream and -ve strand downstream cases; and it is 0 for +ve strand downstream and -ve strand upstream cases
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
30 # threhold_Up is equal to the interval start for +ve strand, and interval end for -ve strand
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
31 # threhold_down is equal to the interval end for +ve strand, and interval start for -ve strand
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
32 if direction == 1:
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
33 if node.maxend <= threshold_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
34 if node.end == node.maxend:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
35 report_func_up(node)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
36 elif node.right and node.left:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
37 if node.right.maxend == node.maxend:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
38 get_closest_feature(node.right, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
39 elif node.left.maxend == node.maxend:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
40 get_closest_feature(node.left, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
41 elif node.right and node.right.maxend == node.maxend:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
42 get_closest_feature(node.right, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
43 elif node.left and node.left.maxend == node.maxend:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
44 get_closest_feature(node.left, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
45 elif node.minend <= threshold_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
46 if node.end <= threshold_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
47 report_func_up(node)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
48 if node.left and node.right:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
49 if node.right.minend <= threshold_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
50 get_closest_feature(node.right, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
51 if node.left.minend <= threshold_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
52 get_closest_feature(node.left, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
53 elif node.left:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
54 if node.left.minend <= threshold_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
55 get_closest_feature(node.left, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
56 elif node.right:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
57 if node.right.minend <= threshold_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
58 get_closest_feature(node.right, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
59 elif direction == 0:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
60 if node.start > threshold_down:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
61 report_func_down(node)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
62 if node.left:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
63 get_closest_feature(node.left, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
64 else:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
65 if node.right:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
66 get_closest_feature(node.right, direction, threshold_up, threshold_down, report_func_up, report_func_down)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
67
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
68
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
69 def proximal_region_finder(readers, region, comments=True):
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
70 """
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
71 Returns an iterator that yields elements of the form [ <original_interval>, <closest_feature> ].
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
72 Intervals are GenomicInterval objects.
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
73 """
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
74 primary = readers[0]
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
75 features = readers[1]
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
76 either = False
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
77 if region == 'Upstream':
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
78 up, down = True, False
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
79 elif region == 'Downstream':
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
80 up, down = False, True
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
81 else:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
82 up, down = True, True
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
83 if region == 'Either':
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
84 either = True
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
85
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
86 # Read features into memory:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
87 rightTree = quicksect.IntervalTree()
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
88 for item in features:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
89 if type( item ) is GenomicInterval:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
90 rightTree.insert( item, features.linenum, item )
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
91
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
92 for interval in primary:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
93 if type( interval ) is Header:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
94 yield interval
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
95 if type( interval ) is Comment and comments:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
96 yield interval
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
97 elif type( interval ) == GenomicInterval:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
98 chrom = interval.chrom
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
99 start = int(interval.start)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
100 end = int(interval.end)
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
101 strand = interval.strand
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
102 if chrom not in rightTree.chroms:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
103 continue
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
104 else:
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
105 root = rightTree.chroms[chrom] # root node for the chrom tree
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
106 result_up = []
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
107 result_down = []
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
108 if (strand == '+' and up) or (strand == '-' and down):
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
109 # upstream +ve strand and downstream -ve strand cases
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
110 get_closest_feature(root, 1, start, None, lambda node: result_up.append( node ), None)
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
111
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
112 if (strand == '+' and down) or (strand == '-' and up):
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
113 # downstream +ve strand and upstream -ve strand case
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
114 get_closest_feature(root, 0, None, end - 1, None, lambda node: result_down.append( node ))
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
115
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
116 if result_up:
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
117 if len(result_up) > 1: # The results_up list has a list of intervals upstream to the given interval.
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
118 ends = []
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
119 for n in result_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
120 ends.append(n.end)
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
121 res_ind = ends.index(max(ends)) # fetch the index of the closest interval i.e. the interval with the max end from the results_up list
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
122 else:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
123 res_ind = 0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
124 if not(either):
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
125 yield [ interval, result_up[res_ind].other ]
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
126
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
127 if result_down:
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
128 if not(either):
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
129 # The last element of result_down will be the closest element to the given interval
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
130 yield [ interval, result_down[-1].other ]
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
131
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
132 if either and (result_up or result_down):
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
133 iter_val = []
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
134 if result_up and result_down:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
135 if abs(start - int(result_up[res_ind].end)) <= abs(end - int(result_down[-1].start)):
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
136 iter_val = [ interval, result_up[res_ind].other ]
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
137 else:
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
138 # The last element of result_down will be the closest element to the given interval
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
139 iter_val = [ interval, result_down[-1].other ]
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
140 elif result_up:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
141 iter_val = [ interval, result_up[res_ind].other ]
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
142 elif result_down:
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
143 # The last element of result_down will be the closest element to the given interval
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
144 iter_val = [ interval, result_down[-1].other ]
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
145 yield iter_val
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
146
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
147
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
148 def main():
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
149 options, args = doc_optparse.parse( __doc__ )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
150 try:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
151 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
152 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
153 in1_gff_format = bool( options.gff1 )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
154 in2_gff_format = bool( options.gff2 )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
155 in_fname, in2_fname, out_fname, direction = args
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
156 except:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
157 doc_optparse.exception()
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
158
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
159 # Set readers to handle either GFF or default format.
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
160 if in1_gff_format:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
161 in1_reader_wrapper = GFFIntervalToBEDReaderWrapper
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
162 else:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
163 in1_reader_wrapper = NiceReaderWrapper
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
164 if in2_gff_format:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
165 in2_reader_wrapper = GFFIntervalToBEDReaderWrapper
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
166 else:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
167 in2_reader_wrapper = NiceReaderWrapper
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
168
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
169 g1 = in1_reader_wrapper( fileinput.FileInput( in_fname ),
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
170 chrom_col=chr_col_1,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
171 start_col=start_col_1,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
172 end_col=end_col_1,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
173 strand_col=strand_col_1,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
174 fix_strand=True )
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
175 g2 = in2_reader_wrapper( fileinput.FileInput( in2_fname ),
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
176 chrom_col=chr_col_2,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
177 start_col=start_col_2,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
178 end_col=end_col_2,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
179 strand_col=strand_col_2,
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
180 fix_strand=True )
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
181
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
182 # Find flanking features.
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
183 out_file = open( out_fname, "w" )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
184 try:
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
185 for result in proximal_region_finder([g1, g2], direction):
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
186 if type( result ) is list:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
187 line, closest_feature = result
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
188 # Need to join outputs differently depending on file types.
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
189 if in1_gff_format:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
190 # Output is GFF with added attribute 'closest feature.'
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
191
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
192 # Invervals are in BED coordinates; need to convert to GFF.
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
193 line = convert_bed_coords_to_gff( line )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
194 closest_feature = convert_bed_coords_to_gff( closest_feature )
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
195
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
196 # Replace double quotes with single quotes in closest feature's attributes.
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
197 out_file.write( "%s closest_feature \"%s\" \n" %
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
198 ( "\t".join( line.fields ),
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
199 "\t".join( closest_feature.fields ).replace( "\"", "\\\"" )
1
8307665c4b6c planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
200 ) )
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
201 else:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
202 # Output is BED + closest feature fields.
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
203 output_line_fields = []
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
204 output_line_fields.extend( line.fields )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
205 output_line_fields.extend( closest_feature.fields )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
206 out_file.write( "%s\n" % ( "\t".join( output_line_fields ) ) )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
207 else:
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
208 out_file.write( "%s\n" % result )
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
209 except ParseError as exc:
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
210 fail( "Invalid file format: %s" % str( exc ) )
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
211
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
212 print("Direction: %s" % (direction))
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
213 if g1.skipped > 0:
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
214 print(skipped( g1, filedesc=" of 1st dataset" ))
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
215 if g2.skipped > 0:
2
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
216 print(skipped( g2, filedesc=" of 2nd dataset" ))
a09d13b108fd planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/flanking_features commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 1
diff changeset
217
0
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
218
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
219 if __name__ == "__main__":
90100b587723 Imported from capsule None
devteam
parents:
diff changeset
220 main()