annotate windowSplitter.py @ 1:477a0cfe6e6c

Corrected version string.
author devteam <devteam@galaxyproject.org>
date Thu, 10 Apr 2014 13:47:14 -0400
parents 4815516900d4
children 1011f028f09d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
2
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
3 """
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
4 Split into windows.
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
5
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
6 usage: %prog input size out_file
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
7 -l, --cols=N,N,N,N: Columns for chrom, start, end, strand in file
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
8 """
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
9
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
10 import sys
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
11
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
12 from galaxy import eggs
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
13 import pkg_resources
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
14 pkg_resources.require( "bx-python" )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
15 from bx.cookbook import doc_optparse
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
16 from galaxy.tools.util.galaxyops import *
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
17
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
18 def stop_err( msg ):
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
19 sys.stderr.write( msg )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
20 sys.exit()
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
21
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
22
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
23 def main():
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
24 # Parsing Command Line here
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
25 options, args = doc_optparse.parse( __doc__ )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
26
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
27 try:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
28 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
29 inp_file, winsize, out_file, makesliding, offset = args
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
30 winsize = int(winsize)
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
31 offset = int(offset)
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
32 makesliding = int(makesliding)
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
33 except:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
34 stop_err( "Data issue, click the pencil icon in the history item to correct the metadata attributes of the input dataset." )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
35
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
36 fo = open(out_file,'w')
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
37
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
38 skipped_lines = 0
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
39 first_invalid_line = 0
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
40 invalid_line = None
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
41 if offset == 0:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
42 makesliding = 0
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
43
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
44 for i, line in enumerate( file( inp_file ) ):
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
45 line = line.strip()
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
46 if line and line[0:1] != "#":
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
47 try:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
48 elems = line.split('\t')
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
49 start = int(elems[start_col_1])
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
50 end = int(elems[end_col_1])
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
51 if makesliding == 0:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
52 numwin = (end - start)/winsize
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
53 else:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
54 numwin = (end - start)/offset
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
55 if numwin > 0:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
56 for win in range(numwin):
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
57 elems_1 = elems
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
58 elems_1[start_col_1] = str(start)
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
59 elems_1[end_col_1] = str(start + winsize)
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
60 fo.write( "%s\n" % '\t'.join( elems_1 ) )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
61 if makesliding == 0:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
62 start = start + winsize
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
63 else:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
64 start = start + offset
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
65 if start+winsize > end:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
66 break
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
67 except:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
68 skipped_lines += 1
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
69 if not invalid_line:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
70 first_invalid_line = i + 1
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
71 invalid_line = line
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
72
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
73 fo.close()
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
74
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
75 if makesliding == 1:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
76 print 'Window size=%d, Sliding=Yes, Offset=%d' % ( winsize, offset )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
77 else:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
78 print 'Window size=%d, Sliding=No' % (winsize)
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
79 if skipped_lines > 0:
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
80 print 'Skipped %d invalid lines starting with #%d: "%s"' % ( skipped_lines, first_invalid_line, invalid_line )
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
81
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
82
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
83 if __name__ == "__main__":
4815516900d4 Imported from capsule None
devteam
parents:
diff changeset
84 main()