Fix typo in srf command
[htsworkflow.git] / scripts / makebed
index 2f1289f2ebed3ffb66b702c9ff29ed39cda1d217..577b86891f098ab5c7cfeb6e2b4139814f1731a5 100755 (executable)
@@ -3,42 +3,8 @@ import optparse
 import sys
 import os
 
-def make_bed(instream, outstream, name, description, chromosome_prefix='chr'):
-  """
-  read an eland result file from instream and write a bedfile to outstream
-  """
-  # indexes into fields in eland_result.txt file
-  SEQ = 1
-  CHR = 6
-  START = 7
-  SENSE = 8
-  # map eland_result.txt sense 
-  sense_map = { 'F': '+', 'R': '-'}
-  sense_color = { 'F': '0,0,255', 'R': '255,255,0' }
-  # provide default track names
-  if name is None: name = "track"
-  if description is None: description = "eland result file"
-  bed_header = 'track name="%s" description="%s" visibility=4 itemRgb="ON"'
-  bed_header += os.linesep
-  outstream.write(bed_header % (name, description))
-
-  for line in instream:
-    fields = line.split()
-    # we need more than the CHR field, and it needs to match a chromosome
-    if len(fields) <= CHR or fields[CHR][:3] != chromosome_prefix:
-      continue
-    start = fields[START]
-    stop = int(start) + len(fields[SEQ])
-    chromosome, extension = fields[CHR].split('.')
-    assert extension == "fa"
-    outstream.write('%s %s %d read 0 %s - - %s%s' % (
-      chromosome,
-      start,
-      stop,
-      sense_map[fields[SENSE]], 
-      sense_color[fields[SENSE]],
-      os.linesep  
-    ))
+from htsworkflow.util.opener import autoopen
+from htsworkflow.util.makebed import make_bed_from_eland_stream, make_bed_from_multi_eland_stream, make_description
 
 def make_parser():
   parser = optparse.OptionParser()
@@ -55,9 +21,34 @@ def make_parser():
   parser.add_option('--chromosome', dest='prefix',
                     help='Set the chromosome prefix name. defaults to "chr"',
                     default='chr')
+  parser.add_option("--database", dest='database',
+                    help="specify location of fctracker database",
+                    default=None)
+  parser.add_option("--flowcell", dest='flowcell',
+                    help="compute name and description from database using flowcell id",
+                    default=None)
+  parser.add_option("--lane", dest='lane',
+                    help='specify which lane to use when retrieving description from database',
+                    default=None)
+
+  multi = optparse.OptionGroup(parser, 'Multi-read ELAND support')
+
+  multi.add_option('-m', '--multi', action='store_true',
+                    help='Enable parsing multi-read eland files',
+                    default=False)
+  multi.add_option('--reads', type='int',
+                   help='limit reporting multi reads to this many reads'
+                        '(most usefully --reads=1 will turn a multi-read '
+                        'file into a single read file)',
+                   default=255)
+  parser.add_option_group(multi)
+
   return parser
 
 def main(command_line=None):
+  instream = None
+  outstream = None
+
   if command_line is None:
     command_line = sys.argv[1:]
 
@@ -68,23 +59,53 @@ def main(command_line=None):
     parser.error("Need eland input file name")
     return 1
 
-  if options.outname is None:
-    options.outname = os.path.splitext(options.inname)[0]+'.bed'
-    print >>sys.stderr, "defaulting to outputname", options.outname
-
-  if os.path.exists(options.inname):
-    instream = open(options.inname, 'r')
+  if options.inname == '-':
+    instream = sys.stdin
+  elif os.path.exists(options.inname):
+    instream = autoopen(options.inname, 'r')
   else:
     parser.error('%s was not found' % (options.inname))
     return 1
 
-  if os.path.exists(options.outname):
-      parser.error("not overwriting %s" % (options.outname))
-      return 1
+  # figure out name for output file
+  if options.outname is None:
+      # if outname wasn't defined, and we're reading from stdout
+      if instream is sys.stdin:
+          # write to stdout
+          outstream = sys.stdout
+      else:
+          # if there's a name write to name.bed
+          options.outname = os.path.splitext(options.inname)[0]+'.bed'
+          print >>sys.stderr, "defaulting to outputname", options.outname
+  elif options.outname == '-':
+      outstream = sys.stdout
+
+  if outstream is None:
+      if os.path.exists(options.outname):
+          parser.error("not overwriting %s" % (options.outname))
+          return 1
+      else:
+          outstream = open(options.outname, 'w')
+
+  if options.flowcell is not None and options.lane is not None:
+    # get our name/description out of the database
+    name, description = make_description(
+                           options.database, options.flowcell, options.lane
+                        )
   else:
-    outstream = open(options.outname, 'w')
+    name = options.name
+    description = options.description
+
+  if options.multi:
+    make_bed_from_multi_eland_stream(instream, outstream, 
+                                     name, description, 
+                                     options.prefix,
+                                     options.reads)
 
-  make_bed(instream, outstream, options.name, options.description, options.prefix)
+  else:
+    make_bed_from_eland_stream(instream, outstream, 
+                               name, description, 
+                               options.prefix)
   return 0
 
 if __name__ == "__main__":