annotate get_flanks.py @ 2:4b14d0e3a837

Added tool images.
author devteam <devteam@galaxyproject.org>
date Mon, 14 Apr 2014 09:18:30 -0400
parents a72f0decd7b3
children 4cd116158aef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
2 #Done by: Guru
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
3
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
4 """
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
5 Get Flanking regions.
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
6
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
7 usage: %prog input out_file size direction region
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
8 -l, --cols=N,N,N,N: Columns for chrom, start, end, strand in file
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
9 -o, --off=N: Offset
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
10 """
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
11
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
12 import sys, re, os
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
13 from bx.cookbook import doc_optparse
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
14 from galaxy.tools.util.galaxyops import *
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
15
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
16 def stop_err( msg ):
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
17 sys.stderr.write( msg )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
18 sys.exit()
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
19
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
20 def main():
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
21 try:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
22 if int( sys.argv[3] ) < 0:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
23 raise Exception
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
24 except:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
25 stop_err( "Length of flanking region(s) must be a non-negative integer." )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
26
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
27 # Parsing Command Line here
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
28 options, args = doc_optparse.parse( __doc__ )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
29 try:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
30 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
31 inp_file, out_file, size, direction, region = args
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
32 if strand_col_1 <= 0:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
33 strand = "+" #if strand is not defined, default it to +
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
34 except:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
35 stop_err( "Metadata issue, correct the metadata attributes by clicking on the pencil icon in the history item." )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
36 try:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
37 offset = int(options.off)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
38 size = int(size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
39 except:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
40 stop_err( "Invalid offset or length entered. Try again by entering valid integer values." )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
41
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
42 fo = open(out_file,'w')
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
43
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
44 skipped_lines = 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
45 first_invalid_line = 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
46 invalid_line = None
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
47 elems = []
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
48 j=0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
49 for i, line in enumerate( file( inp_file ) ):
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
50 line = line.strip()
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
51 if line and (not line.startswith( '#' )) and line != '':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
52 j+=1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
53 try:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
54 elems = line.split('\t')
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
55 #if the start and/or end columns are not numbers, skip that line.
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
56 assert int(elems[start_col_1])
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
57 assert int(elems[end_col_1])
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
58 if strand_col_1 != -1:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
59 strand = elems[strand_col_1]
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
60 #if the stand value is not + or -, skip that line.
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
61 assert strand in ['+', '-']
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
62 if direction == 'Upstream':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
63 if strand == '+':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
64 if region == 'end':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
65 elems[end_col_1] = str(int(elems[end_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
66 elems[start_col_1] = str( int(elems[end_col_1]) - size )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
67 else:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
68 elems[end_col_1] = str(int(elems[start_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
69 elems[start_col_1] = str( int(elems[end_col_1]) - size )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
70 elif strand == '-':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
71 if region == 'end':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
72 elems[start_col_1] = str(int(elems[start_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
73 elems[end_col_1] = str(int(elems[start_col_1]) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
74 else:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
75 elems[start_col_1] = str(int(elems[end_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
76 elems[end_col_1] = str(int(elems[start_col_1]) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
77 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
78 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
79
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
80 elif direction == 'Downstream':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
81 if strand == '-':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
82 if region == 'start':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
83 elems[end_col_1] = str(int(elems[end_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
84 elems[start_col_1] = str( int(elems[end_col_1]) - size )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
85 else:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
86 elems[end_col_1] = str(int(elems[start_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
87 elems[start_col_1] = str( int(elems[end_col_1]) - size )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
88 elif strand == '+':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
89 if region == 'start':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
90 elems[start_col_1] = str(int(elems[start_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
91 elems[end_col_1] = str(int(elems[start_col_1]) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
92 else:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
93 elems[start_col_1] = str(int(elems[end_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
94 elems[end_col_1] = str(int(elems[start_col_1]) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
95 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
96 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
97
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
98 elif direction == 'Both':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
99 if strand == '-':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
100 if region == 'start':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
101 start = str(int(elems[end_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
102 end1 = str(int(start) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
103 end2 = str(int(start) - size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
104 elems[start_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
105 elems[end_col_1]=end1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
106 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
107 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
108 elems[start_col_1]=end2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
109 elems[end_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
110 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
111 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
112 elif region == 'end':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
113 start = str(int(elems[start_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
114 end1 = str(int(start) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
115 end2 = str(int(start) - size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
116 elems[start_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
117 elems[end_col_1]=end1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
118 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
119 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
120 elems[start_col_1]=end2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
121 elems[end_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
122 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
123 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
124 else:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
125 start1 = str(int(elems[end_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
126 end1 = str(int(start1) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
127 start2 = str(int(elems[start_col_1]) - offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
128 end2 = str(int(start2) - size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
129 elems[start_col_1]=start1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
130 elems[end_col_1]=end1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
131 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
132 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
133 elems[start_col_1]=end2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
134 elems[end_col_1]=start2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
135 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
136 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
137 elif strand == '+':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
138 if region == 'start':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
139 start = str(int(elems[start_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
140 end1 = str(int(start) - size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
141 end2 = str(int(start) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
142 elems[start_col_1]=end1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
143 elems[end_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
144 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
145 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
146 elems[start_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
147 elems[end_col_1]=end2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
148 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
149 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
150 elif region == 'end':
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
151 start = str(int(elems[end_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
152 end1 = str(int(start) - size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
153 end2 = str(int(start) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
154 elems[start_col_1]=end1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
155 elems[end_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
156 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
157 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
158 elems[start_col_1]=start
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
159 elems[end_col_1]=end2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
160 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
161 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
162 else:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
163 start1 = str(int(elems[start_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
164 end1 = str(int(start1) - size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
165 start2 = str(int(elems[end_col_1]) + offset)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
166 end2 = str(int(start2) + size)
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
167 elems[start_col_1]=end1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
168 elems[end_col_1]=start1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
169 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
170 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
171 elems[start_col_1]=start2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
172 elems[end_col_1]=end2
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
173 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
174 fo.write( "%s\n" % '\t'.join( elems ) )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
175 except:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
176 skipped_lines += 1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
177 if not invalid_line:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
178 first_invalid_line = i + 1
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
179 invalid_line = line
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
180 fo.close()
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
181
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
182 if skipped_lines == j:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
183 stop_err( "Data issue: click the pencil icon in the history item to correct the metadata attributes." )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
184 if skipped_lines > 0:
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
185 print 'Skipped %d invalid lines starting with #%dL "%s"' % ( skipped_lines, first_invalid_line, invalid_line )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
186 print 'Location: %s, Region: %s, Flank-length: %d, Offset: %d ' %( direction, region, size, offset )
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
187
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
188 if __name__ == "__main__":
a72f0decd7b3 Imported from capsule None
devteam
parents:
diff changeset
189 main()