a4a414b27d57608d89cc5dca4b9b93f629c2fb62
[htsworkflow.git] / scripts / makebed
1 #!/usr/bin/python
2 import optparse
3 import sys
4 import os
5
6 from gaworkflow.util.makebed import make_bed_from_eland_stream, make_bed_from_multi_eland_stream, make_description
7
8 def make_parser():
9   parser = optparse.OptionParser()
10   parser.add_option('-e', '--eland', dest='inname',
11                     help='specify input eland filename')
12   parser.add_option('-b', '--bed', dest='outname',
13                     help='specify output befilename')
14   parser.add_option('-n', '--name', dest='name',
15                     help='specify the track (short) name.',
16                     default=None)
17   parser.add_option('-d', '--description', dest='description',
18                     help='specify the track description',
19                     default=None)
20   parser.add_option('--chromosome', dest='prefix',
21                     help='Set the chromosome prefix name. defaults to "chr"',
22                     default='chr')
23   parser.add_option("--database", dest='database',
24                     help="specify location of fctracker database",
25                     default=None)
26   parser.add_option("--flowcell", dest='flowcell',
27                     help="compute name and description from database using flowcell id",
28                     default=None)
29   parser.add_option("--lane", dest='lane',
30                     help='specify which lane to use when retrieving description from database',
31                     default=None)
32
33   multi = optparse.OptionGroup(parser, 'Multi-read ELAND support')
34
35   multi.add_option('-m', '--multi', action='store_true',
36                     help='Enable parsing multi-read eland files',
37                     default=False)
38   multi.add_option('--reads', type='int',
39                    help='limit reporting multi reads to this many reads'
40                         '(most usefully --reads=1 will turn a multi-read '
41                         'file into a single read file)',
42                    default=255)
43   parser.add_option_group(multi)
44
45   return parser
46
47 def main(command_line=None):
48   if command_line is None:
49     command_line = sys.argv[1:]
50
51   parser = make_parser()
52   (options, args) = parser.parse_args(command_line)
53
54   if options.inname is None:
55     parser.error("Need eland input file name")
56     return 1
57
58   if options.inname == '-':
59     instream = sys.stdin
60   elif os.path.exists(options.inname):
61     instream = open(options.inname, 'r')
62   else:
63     parser.error('%s was not found' % (options.inname))
64     return 1
65
66   if options.outname is None:
67       # if outname wasn't defined, and we're reading from stdout
68       if instream is sys.stdin:
69           # write to stdout
70           outstream = sys.stdout
71       else:
72           # if there's a name write to name.bde
73           options.outname = os.path.splitext(options.inname)[0]+'.bed'
74           print >>sys.stderr, "defaulting to outputname", options.outname
75   elif options.outname == '-':
76       outstream = sys.stdout
77   elif os.path.exists(options.outname):
78       parser.error("not overwriting %s" % (options.outname))
79       return 1
80   else:
81     outstream = open(options.outname, 'w')
82
83   if options.flowcell is not None and options.lane is not None:
84     # get our name/description out of the database
85     name, description = make_description(
86                            options.database, options.flowcell, options.lane
87                         )
88   else:
89     name = options.name
90     description = options.description
91
92   if options.multi:
93     make_bed_from_multi_eland_stream(instream, outstream, 
94                                      name, description, 
95                                      options.prefix,
96                                      options.reads)
97
98   else:
99     make_bed_from_eland_stream(instream, outstream, 
100                                name, description, 
101                                options.prefix)
102   return 0
103
104 if __name__ == "__main__":
105   sys.exit(main(sys.argv[1:]))
106