Finish new RDF based ddf construction.
[htsworkflow.git] / extra / ucsc_encode_submission / ucsc_gather.py
index 4cea711b96bafa3233361c3cbe44310f572a6bff..3877379c1de838e3762f4b6ccedd2c76cd0d25f4 100755 (executable)
@@ -4,24 +4,36 @@ import fnmatch
 from glob import glob
 import json
 import logging
-from optparse import OptionParser
+import netrc
+from optparse import OptionParser, OptionGroup
 import os
 from pprint import pprint, pformat
 import shlex
 from StringIO import StringIO
-import time
+import stat
+from subprocess import Popen, PIPE
 import sys
+import time
 import types
 import urllib
 import urllib2
 import urlparse
 
 from htsworkflow.util import api
-from htsworkflow.pipelines.sequences import \
-    create_sequence_table, \
-    scan_for_sequences
-from htsworkflow.pipelines import qseq2fastq
-from htsworkflow.pipelines import srf2fastq
+from htsworkflow.util.rdfhelp import \
+     dafTermOntology, \
+     fromTypedNode, \
+     get_model, \
+     get_serializer, \
+     load_into_model, \
+     submissionOntology 
+from htsworkflow.submission.daf import \
+     DAFMapper, \
+     MetadataLookupException, \
+     get_submission_uri
+from htsworkflow.submission.condorfastq import CondorFastqExtract
+
+logger = logging.getLogger('ucsc_gather')
 
 def main(cmdline=None):
     parser = make_parser()
@@ -34,10 +46,20 @@ def main(cmdline=None):
     else:
         logging.basicConfig(level = logging.WARNING )        
     
-    apidata = {'apiid': opts.apiid, 'apikey': opts.apikey }
+    apidata = api.make_auth_from_opts(opts, parser)
+
+    model = get_model(opts.load_model)
+    mapper = DAFMapper(opts.name, opts.daf,  model)
+    submission_uri = get_submission_uri(opts.name)
+
+    if opts.library_url is not None:
+        mapper.library_url = opts.library_url
+        
+    if opts.load_rdf is not None:
+        load_into_model(model, 'turtle', opts.load_rdf, submission_uri)
 
-    if opts.host is None or opts.apiid is None or opts.apikey is None:
-        parser.error("Please specify host url, apiid, apikey")
+    if opts.make_ddf and opts.daf is None:
+        parser.error("Please specify your daf when making ddf files")
 
     if len(args) == 0:
         parser.error("I need at least one library submission-dir input file")
@@ -49,76 +71,67 @@ def main(cmdline=None):
     if opts.make_tree_from is not None:
         make_tree_from(opts.make_tree_from, library_result_map)
             
-    if opts.daf is not None:
+    if opts.link_daf:
         link_daf(opts.daf, library_result_map)
 
     if opts.fastq:
-        build_fastqs(opts.host, 
-                     apidata, 
-                     opts.sequence, 
-                     library_result_map,
-                     force=opts.force)
+        extractor = CondorFastqExtract(opts.host, apidata, opts.sequence,
+                                       force=opts.force)
+        extractor.build_fastqs(library_result_map)
 
-    if opts.ini:
-        make_submission_ini(opts.host, apidata, library_result_map)
+    if opts.scan_submission:
+        scan_submission_dirs(mapper, library_result_map)
 
-    if opts.makeddf:
-        make_all_ddfs(library_result_map, opts.daf, force=opts.force)
+    if opts.make_ddf:
+        make_all_ddfs(mapper, library_result_map, opts.daf, force=opts.force)
 
+    if opts.print_rdf:
+        writer = get_serializer()
+        print writer.serialize_model_to_string(model)
 
+        
 def make_parser():
-    # Load defaults from the config files
-    config = SafeConfigParser()
-    config.read([os.path.expanduser('~/.htsworkflow.ini'), '/etc/htsworkflow.ini'])
-    
-    sequence_archive = None
-    apiid = None
-    apikey = None
-    apihost = None
-    SECTION = 'sequence_archive'
-    if config.has_section(SECTION):
-        sequence_archive = config.get(SECTION, 'sequence_archive',sequence_archive)
-        sequence_archive = os.path.expanduser(sequence_archive)
-        apiid = config.get(SECTION, 'apiid', apiid)
-        apikey = config.get(SECTION, 'apikey', apikey)
-        apihost = config.get(SECTION, 'host', apihost)
-
     parser = OptionParser()
 
+    model = OptionGroup(parser, 'model')
+    model.add_option('--name', help="Set submission name")
+    model.add_option('--load-model', default=None,
+      help="Load model database")
+    model.add_option('--load-rdf', default=None,
+      help="load rdf statements into model")
+    model.add_option('--print-rdf', action="store_true", default=False,
+      help="print ending model state")
+    parser.add_option_group(model)
     # commands
-    parser.add_option('--make-tree-from',
+    commands = OptionGroup(parser, 'commands')
+    commands.add_option('--make-tree-from',
                       help="create directories & link data files",
                       default=None)
-    parser.add_option('--fastq', help="generate scripts for making fastq files",
-                      default=False, action="store_true")
-
-    parser.add_option('--ini', help="generate submission ini file", default=False,
-                      action="store_true")
-
-    parser.add_option('--makeddf', help='make the ddfs', default=False,
+    commands.add_option('--fastq', default=False, action="store_true",
+                        help="generate scripts for making fastq files")
+    commands.add_option('--scan-submission', default=False, action="store_true",
+                      help="Import metadata for submission into our model")
+    commands.add_option('--link-daf', default=False, action="store_true",
+                        help="link daf into submission directories")
+    commands.add_option('--make-ddf', help='make the ddfs', default=False,
                       action="store_true")
+    parser.add_option_group(commands)
     
-    parser.add_option('--daf', default=None, help='specify daf name')
     parser.add_option('--force', default=False, action="store_true",
                       help="Force regenerating fastqs")
-
-    # configuration options
-    parser.add_option('--apiid', default=apiid, help="Specify API ID")
-    parser.add_option('--apikey', default=apikey, help="Specify API KEY")
-    parser.add_option('--host',  default=apihost,
-                      help="specify HTSWorkflow host",)
-    parser.add_option('--sequence', default=sequence_archive,
-                      help="sequence repository")
-
+    parser.add_option('--daf', default=None, help='specify daf name')
+    parser.add_option('--library-url', default=None,
+                      help="specify an alternate source for library information")
     # debugging
     parser.add_option('--verbose', default=False, action="store_true",
                       help='verbose logging')
     parser.add_option('--debug', default=False, action="store_true",
                       help='debug logging')
 
+    api.add_auth_options(parser)
+    
     return parser
 
-
 def make_tree_from(source_path, library_result_map):
     """Create a tree using data files from source path.
     """
@@ -126,7 +139,7 @@ def make_tree_from(source_path, library_result_map):
         if not os.path.exists(lib_path):
             logging.info("Making dir {0}".format(lib_path))
             os.mkdir(lib_path)
-        source_lib_dir = os.path.join(source_path, lib_path)
+        source_lib_dir = os.path.abspath(os.path.join(source_path, lib_path))
         if os.path.exists(source_lib_dir):
             pass
         for filename in os.listdir(source_lib_dir):
@@ -139,130 +152,6 @@ def make_tree_from(source_path, library_result_map):
                 logging.info(
                     'LINK {0} to {1}'.format(source_pathname, target_pathname))
     
-def build_fastqs(host, apidata, sequences_path, library_result_map, 
-                 force=False ):
-    """
-    Generate condor scripts to build any needed fastq files
-    
-    Args:
-      host (str): root of the htsworkflow api server
-      apidata (dict): id & key to post to the server
-      sequences_path (str): root of the directory tree to scan for files
-      library_result_map (list):  [(library_id, destination directory), ...]
-    """
-    qseq_condor_header = """
-Universe=vanilla
-executable=%(exe)s
-error=log/qseq2fastq.err.$(process).log
-output=log/qseq2fastq.out.$(process).log
-log=log/qseq2fastq.log
-
-""" % {'exe': sys.executable }
-    qseq_condor_entries = []
-    srf_condor_header = """
-Universe=vanilla
-executable=%(exe)s
-output=log/srf_pair_fastq.out.$(process).log
-error=log/srf_pair_fastq.err.$(process).log
-log=log/srf_pair_fastq.log
-environment="PYTHONPATH=/home/diane/lib/python2.6/site-packages:/home/diane/proj/solexa/gaworkflow PATH=/woldlab/rattus/lvol0/mus/home/diane/bin:/usr/bin:/bin"
-
-""" % {'exe': sys.executable }
-    srf_condor_entries = []
-    lib_db = find_archive_sequence_files(host, 
-                                         apidata, 
-                                         sequences_path, 
-                                         library_result_map)
-
-    needed_targets = find_missing_targets(library_result_map, lib_db, force)
-
-    for target_pathname, available_sources in needed_targets.items():
-        logging.debug(' target : %s' % (target_pathname,))
-        logging.debug(' candidate sources: %s' % (available_sources,))
-        if available_sources.has_key('qseq'):
-            source = available_sources['qseq']
-            qseq_condor_entries.append(
-                condor_qseq_to_fastq(source.path, 
-                                     target_pathname, 
-                                     source.flowcell,
-                                     force=force)
-            )
-        elif available_sources.has_key('srf'):
-            source = available_sources['srf']
-            mid = getattr(source, 'mid_point', None)
-            srf_condor_entries.append(
-                condor_srf_to_fastq(source.path, 
-                                    target_pathname,
-                                    source.paired,
-                                    source.flowcell,
-                                    mid,
-                                    force=force)
-            )
-        else:
-            print " need file", target_pathname
-
-    if len(srf_condor_entries) > 0:
-        make_submit_script('srf.fastq.condor', 
-                           srf_condor_header,
-                           srf_condor_entries)
-
-    if len(qseq_condor_entries) > 0:
-        make_submit_script('qseq.fastq.condor', 
-                           qseq_condor_header,
-                           qseq_condor_entries)
-
-
-def find_missing_targets(library_result_map, lib_db, force=False):
-    """
-    Check if the sequence file exists.
-    This requires computing what the sequence name is and checking
-    to see if it can be found in the sequence location.
-
-    Adds seq.paired flag to sequences listed in lib_db[*]['lanes']
-    """
-    fastq_paired_template = '%(lib_id)s_%(flowcell)s_c%(cycle)s_l%(lane)s_r%(read)s.fastq'
-    fastq_single_template = '%(lib_id)s_%(flowcell)s_c%(cycle)s_l%(lane)s.fastq'
-    # find what targets we're missing
-    needed_targets = {}
-    for lib_id, result_dir in library_result_map:
-        lib = lib_db[lib_id]
-        lane_dict = make_lane_dict(lib_db, lib_id)
-        
-        for lane_key, sequences in lib['lanes'].items():
-            for seq in sequences:
-                seq.paired = lane_dict[seq.flowcell]['paired_end']
-                lane_status = lane_dict[seq.flowcell]['status']
-
-                if seq.paired and seq.read is None:
-                    seq.read = 1
-                filename_attributes = { 
-                    'flowcell': seq.flowcell,
-                    'lib_id': lib_id,
-                    'lane': seq.lane,
-                    'read': seq.read,
-                    'cycle': seq.cycle
-                    }
-                # skip bad runs
-                if lane_status == 'Failed':
-                    continue
-                if seq.flowcell == '30DY0AAXX':
-                    # 30DY0 only ran for 151 bases instead of 152
-                    # it is actually 76 1st read, 75 2nd read
-                    seq.mid_point = 76
-
-                # end filters
-                if seq.paired:
-                    target_name = fastq_paired_template % filename_attributes
-                else:
-                    target_name = fastq_single_template % filename_attributes
-
-                target_pathname = os.path.join(result_dir, target_name)
-                if force or not os.path.exists(target_pathname):
-                    t = needed_targets.setdefault(target_pathname, {})
-                    t[seq.filetype] = seq
-
-    return needed_targets
-
 
 def link_daf(daf_path, library_result_map):
     if not os.path.exists(daf_path):
@@ -271,85 +160,32 @@ def link_daf(daf_path, library_result_map):
     base_daf = os.path.basename(daf_path)
     
     for lib_id, result_dir in library_result_map:
+        if not os.path.exists(result_dir):
+            raise RuntimeError("Couldn't find target directory %s" %(result_dir,))
         submission_daf = os.path.join(result_dir, base_daf)
         if not os.path.exists(submission_daf):
+            if not os.path.exists(daf_path):
+                raise RuntimeError("Couldn't find daf: %s" %(daf_path,))
             os.link(daf_path, submission_daf)
 
 
-def make_submission_ini(host, apidata, library_result_map, paired=True):
-    #attributes = get_filename_attribute_map(paired)
-    view_map = NameToViewMap(host, apidata)
-    
-    candidate_fastq_src = {}
-
+def scan_submission_dirs(view_map, library_result_map):
+    """Look through our submission directories and collect needed information
+    """
     for lib_id, result_dir in library_result_map:
-        order_by = ['order_by=files', 'view', 'replicate', 'cell', 
-                    'readType', 'mapAlgorithm', 'insertLength' ]
-        inifile =  ['[config]']
-        inifile += [" ".join(order_by)]
-        inifile += ['']
-        line_counter = 1
-        result_ini = os.path.join(result_dir, result_dir+'.ini')
-
-        # write other lines
-        submission_files = os.listdir(result_dir)
-        fastqs = {}
-        fastq_attributes = {}
-        for f in submission_files:
-            attributes = view_map.find_attributes(f, lib_id)
-            if attributes is None:
-                raise ValueError("Unrecognized file: %s" % (f,))
-            
-            ext = attributes["extension"]
-            if attributes['view'] is None:                   
-                continue               
-            elif attributes.get("type", None) == 'fastq':
-                fastqs.setdefault(ext, set()).add(f)
-                fastq_attributes[ext] = attributes
-            else:
-                inifile.extend(
-                    make_submission_section(line_counter,
-                                            [f],
-                                            attributes
-                                            )
-                    )
-                inifile += ['']
-                line_counter += 1
-                # add in fastqs on a single line.
-
-        for extension, fastq_files in fastqs.items():
-            inifile.extend(
-                make_submission_section(line_counter, 
-                                        fastq_files,
-                                        fastq_attributes[extension])
-            )
-            inifile += ['']
-            line_counter += 1
-            
-        f = open(result_ini,'w')
-        f.write(os.linesep.join(inifile))
-
+        logging.info("Importing %s from %s" % (lib_id, result_dir))
+        try:
+            view_map.import_submission_dir(result_dir, lib_id)
+        except MetadataLookupException, e:
+            logging.error("Skipping %s: %s" % (lib_id, str(e)))
         
-def make_lane_dict(lib_db, lib_id):
-    """
-    Convert the lane_set in a lib_db to a dictionary
-    indexed by flowcell ID
-    """
-    result = []
-    for lane in lib_db[lib_id]['lane_set']:
-        result.append((lane['flowcell'], lane))
-    return dict(result)
-
-
-def make_all_ddfs(library_result_map, daf_name, make_condor=True, force=False):
+def make_all_ddfs(view_map, library_result_map, daf_name, make_condor=True, force=False):
     dag_fragment = []
     for lib_id, result_dir in library_result_map:
-        ininame = result_dir+'.ini'
-        inipathname = os.path.join(result_dir, ininame)
-        if os.path.exists(inipathname):
-            dag_fragment.extend(
-                make_ddf(ininame, daf_name, True, make_condor, result_dir)
-            )
+        submissionNode = view_map.get_submission_node(result_dir)
+        dag_fragment.extend(
+            make_ddf(view_map, submissionNode, daf_name, make_condor, result_dir)
+        )
 
     if make_condor and len(dag_fragment) > 0:
         dag_filename = 'submission.dagman'
@@ -362,70 +198,77 @@ def make_all_ddfs(library_result_map, daf_name, make_condor=True, force=False):
             f.close()
             
 
-def make_ddf(ininame,  daf_name, guess_ddf=False, make_condor=False, outdir=None):
+def make_ddf(view_map, submissionNode, daf_name, make_condor=False, outdir=None):
     """
     Make ddf files, and bonus condor file
     """
     dag_fragments = []
-    curdir = os.getcwd()
+
+    name = fromTypedNode(view_map.model.get_target(submissionNode, submissionOntology['name']))
+    if name is None:
+        logging.error("Need name for %s" % (str(submissionNode)))
+        return []
+
+    ddf_name = name + '.ddf'
     if outdir is not None:
-        os.chdir(outdir)
-    output = sys.stdout
-    ddf_name = None
-    if guess_ddf:
-        ddf_name = make_ddf_name(ininame)
-        print ddf_name
-        output = open(ddf_name,'w')
+        outfile = os.path.join(outdir, ddf_name)
+        output = open(outfile,'w')
+    else:
+        output = sys.stdout
+    
+    # filename goes first
+    variables = ['filename']
+    variables.extend(view_map.get_daf_variables())
+    output.write('\t'.join(variables))
+    output.write(os.linesep)
+    
+    nameTerm = dafTermOntology['name']
 
-    file_list = read_ddf_ini(ininame, output)
+    submission_views = view_map.model.get_targets(submissionNode, submissionOntology['has_view'])
+    file_list = []
+    for viewNode in submission_views:
+        record = []
+        for variable_name in variables:
+            varNode = dafTermOntology[variable_name]
+            values = list(view_map.model.get_targets(viewNode, varNode))
+            
+            if variable_name == 'view':
+                nameNode = view_map.model.get_target(values[0], nameTerm)
+                values = [fromTypedNode(nameNode)]
+            else:
+                values = [ fromTypedNode(v) for v in values ]
+                if variable_name == 'filename':
+                    file_list.extend(values)
+                              
+            if len(values) == 0:
+                attribute = "#None#"
+            elif len(values) == 1:
+                attribute = values[0]
+            else:
+                attribute = ",".join(values)
+            record.append(attribute)
+        output.write('\t'.join(record))
+        output.write(os.linesep)
+            
+    logging.info(
+        "Examined {0}, found files: {1}".format(
+            str(submissionNode), ", ".join(file_list)))
 
     file_list.append(daf_name)
-    if ddf_name is not None:
-        file_list.append(ddf_name)
-
+    file_list.append(ddf_name)
+    
     if make_condor:
-        archive_condor = make_condor_archive_script(ininame, file_list)
-        upload_condor = make_condor_upload_script(ininame)
+        print name, file_list
+        archive_condor = make_condor_archive_script(name, file_list, outdir)
+        upload_condor = make_condor_upload_script(name, outdir)
         
         dag_fragments.extend( 
-            make_dag_fragment(ininame, archive_condor, upload_condor)
+            make_dag_fragment(name, archive_condor, upload_condor)
         ) 
         
-    os.chdir(curdir)
-    
     return dag_fragments
 
 
-def read_ddf_ini(filename, output=sys.stdout):
-    """
-    Read a ini file and dump out a tab delmited text file
-    """
-    file_list = []
-    config = SafeConfigParser()
-    config.read(filename)
-
-    order_by = shlex.split(config.get("config", "order_by"))
-
-    output.write("\t".join(order_by))
-    output.write(os.linesep)
-    sections = config.sections()
-    sections.sort()
-    for section in sections:
-        if section == "config":
-            # skip the config block
-            continue
-        values = []
-        for key in order_by:
-            v = config.get(section, key)
-            values.append(v)
-            if key == 'files':
-                file_list.extend(parse_filelist(v))
-                
-        output.write("\t".join(values))
-        output.write(os.linesep)
-    return file_list
-
-
 def read_library_result_map(filename):
     """
     Read a file that maps library id to result directory.
@@ -445,7 +288,7 @@ def read_library_result_map(filename):
     return results
 
 
-def make_condor_archive_script(ininame, files):
+def make_condor_archive_script(name, files, outdir=None):
     script = """Universe = vanilla
 
 Executable = /bin/tar
@@ -460,27 +303,30 @@ request_memory = 20
 
 queue 
 """
+    if outdir is None:
+        outdir = os.getcwd()
     for f in files:
-        if not os.path.exists(f):
+        pathname = os.path.join(outdir, f)
+        if not os.path.exists(pathname):
             raise RuntimeError("Missing %s" % (f,))
 
-    context = {'archivename': make_submission_name(ininame),
+    context = {'archivename': make_submission_name(name),
                'filelist': " ".join(files),
                'initialdir': os.getcwd(),
                'user': os.getlogin()}
 
-    condor_script = make_condor_name(ininame, 'archive')
+    condor_script = os.path.join(outdir, make_condor_name(name, 'archive'))
     condor_stream = open(condor_script,'w')
     condor_stream.write(script % context)
     condor_stream.close()
     return condor_script
 
 
-def make_condor_upload_script(ininame):
+def make_condor_upload_script(name, outdir=None):
     script = """Universe = vanilla
 
 Executable = /usr/bin/lftp
-arguments = -c put ../%(archivename)s -o ftp://detrout@encodeftp.cse.ucsc.edu/%(archivename)s
+arguments = -c put ../%(archivename)s -o ftp://%(ftpuser)s:%(ftppassword)s@%(ftphost)s/%(archivename)s
 
 Error = upload.err.$(Process).log
 Output = upload.out.$(Process).log
@@ -489,14 +335,27 @@ initialdir = %(initialdir)s
 
 queue 
 """
-    context = {'archivename': make_submission_name(ininame),
-               'initialdir': os.getcwd(),
-               'user': os.getlogin()}
-
-    condor_script = make_condor_name(ininame, 'upload')
+    if outdir is None:
+        outdir = os.getcwd()
+        
+    auth = netrc.netrc(os.path.expanduser("~diane/.netrc"))
+    
+    encodeftp = 'encodeftp.cse.ucsc.edu'
+    ftpuser = auth.hosts[encodeftp][0]
+    ftppassword = auth.hosts[encodeftp][2]
+    context = {'archivename': make_submission_name(name),
+               'initialdir': outdir,
+               'user': os.getlogin(),
+               'ftpuser': ftpuser,
+               'ftppassword': ftppassword,
+               'ftphost': encodeftp}
+
+    condor_script = os.path.join(outdir, make_condor_name(name, 'upload'))
     condor_stream = open(condor_script,'w')
     condor_stream.write(script % context)
     condor_stream.close()
+    os.chmod(condor_script, stat.S_IREAD|stat.S_IWRITE)
+
     return condor_script
 
 
@@ -523,264 +382,6 @@ def get_library_info(host, apidata, library_id):
     return contents
 
 
-def condor_srf_to_fastq(srf_file, target_pathname, paired, flowcell=None,
-                        mid=None, force=False):
-    py = srf2fastq.__file__
-    args = [ py, srf_file, ]
-    if paired:
-        args.extend(['--left', target_pathname])
-        # this is ugly. I did it because I was pregenerating the target
-        # names before I tried to figure out what sources could generate
-        # those targets, and everything up to this point had been
-        # one-to-one. So I couldn't figure out how to pair the 
-        # target names. 
-        # With this at least the command will run correctly.
-        # however if we rename the default targets, this'll break
-        # also I think it'll generate it twice.
-        args.extend(['--right', 
-                     target_pathname.replace('_r1.fastq', '_r2.fastq')])
-    else:
-        args.extend(['--single', target_pathname ])
-    if flowcell is not None:
-        args.extend(['--flowcell', flowcell])
-
-    if mid is not None:
-        args.extend(['-m', str(mid)])
-
-    if force:
-        args.extend(['--force'])
-
-    script = """
-arguments="%s"
-queue
-""" % (" ".join(args),)
-    
-    return  script 
-
-
-def condor_qseq_to_fastq(qseq_file, target_pathname, flowcell=None, force=False):
-    py = qseq2fastq.__file__
-    args = [py, '-i', qseq_file, '-o', target_pathname ]
-    if flowcell is not None:
-        args.extend(['-f', flowcell])
-    script = """
-arguments="%s"
-queue
-""" % (" ".join(args))
-
-    return script 
-
-def find_archive_sequence_files(host, apidata, sequences_path, 
-                                library_result_map):
-    """
-    Find all the archive sequence files possibly associated with our results.
-
-    """
-    logging.debug("Searching for sequence files in: %s" %(sequences_path,))
-
-    lib_db = {}
-    seq_dirs = set()
-    #seq_dirs = set(os.path.join(sequences_path, 'srfs'))
-    candidate_lanes = {}
-    for lib_id, result_dir in library_result_map:
-        lib_info = get_library_info(host, apidata, lib_id)
-        lib_info['lanes'] = {}
-        lib_db[lib_id] = lib_info
-
-        for lane in lib_info['lane_set']:
-            lane_key = (lane['flowcell'], lane['lane_number'])
-            candidate_lanes[lane_key] = lib_id
-            seq_dirs.add(os.path.join(sequences_path, 
-                                         'flowcells', 
-                                         lane['flowcell']))
-    logging.debug("Seq_dirs = %s" %(unicode(seq_dirs)))
-    candidate_seq_list = scan_for_sequences(seq_dirs)
-
-    # at this point we have too many sequences as scan_for_sequences
-    # returns all the sequences in a flowcell directory
-    # so lets filter out the extras
-    
-    for seq in candidate_seq_list:
-        lane_key = (seq.flowcell, seq.lane)
-        lib_id = candidate_lanes.get(lane_key, None)
-        if lib_id is not None:
-            lib_info = lib_db[lib_id]
-            lib_info['lanes'].setdefault(lane_key, set()).add(seq)
-    
-    return lib_db
-
-
-class NameToViewMap(object):
-    """Determine view attributes for a given submission file name
-    """
-    def __init__(self, root_url, apidata):
-        self.root_url = root_url
-        self.apidata = apidata
-        
-        self.lib_cache = {}
-        self.lib_paired = {}
-        # ma is "map algorithm"
-        ma = 'TH1014'
-
-        self.patterns = [
-            ('*.bai',                   None),
-            ('*.splices.bam',           'Splices'),
-            ('*.bam',                   self._guess_bam_view),
-            ('junctions.bed',           'Junctions'),
-            ('*.jnct',                  'Junctions'),
-            ('*.plus.bigwig',           'PlusSignal'),
-            ('*.minus.bigwig',          'MinusSignal'),
-            ('*.bigwig',                'Signal'),
-            ('*.tar.bz2',               None),
-            ('*.condor',                None),
-            ('*.daf',                   None),
-            ('*.ddf',                   None),
-            ('*.?ufflinks-0.9.0?genes.expr',       'GeneDeNovo'),
-            ('*.?ufflinks-0.9.0?transcripts.expr', 'TranscriptDeNovo'),
-            ('*.?ufflinks-0.9.0?transcripts.gtf',  'GeneModel'),
-            ('*.GENCODE-v3c?genes.expr',       'GeneGencV3c'),
-            ('*.GENCODE-v3c?transcripts.expr', 'TranscriptGencV3c'),
-            ('*.GENCODE-v4?genes.expr',       'GeneGencV4'),
-            ('*.GENCODE-v4?transcripts.expr', 'TranscriptGencV4'),
-            ('*.GENCODE-v4?transcript.expr', 'TranscriptGencV4'),
-            ('*_1.75mers.fastq',              'FastqRd1'),
-            ('*_2.75mers.fastq',              'FastqRd2'),
-            ('*_r1.fastq',              'FastqRd1'),
-            ('*_r2.fastq',              'FastqRd2'),
-            ('*.fastq',                 'Fastq'),
-            ('*.gtf',                   'GeneModel'),
-            ('*.ini',                   None),
-            ('*.log',                   None),
-            ('paired-end-distribution*', 'InsLength'),
-            ('*.stats.txt',              'InsLength'),
-            ('*.srf',                   None),
-            ('*.wig',                   None),
-            ('*.zip',                   None),
-            ]
-
-        self.views = {
-            None: {"MapAlgorithm": "NA"},
-            "Paired": {"MapAlgorithm": ma},
-            "Aligns": {"MapAlgorithm": ma},
-            "Single": {"MapAlgorithm": ma},
-            "Splices": {"MapAlgorithm": ma},
-            "Junctions": {"MapAlgorithm": ma},
-            "PlusSignal": {"MapAlgorithm": ma},
-            "MinusSignal": {"MapAlgorithm": ma},
-            "Signal": {"MapAlgorithm": ma},
-            "GeneModel": {"MapAlgorithm": ma},
-            "GeneDeNovo": {"MapAlgorithm": ma},
-            "TranscriptDeNovo": {"MapAlgorithm": ma},
-            "GeneGencV3c": {"MapAlgorithm": ma},
-            "TranscriptGencV3c": {"MapAlgorithm": ma},
-            "GeneGencV4": {"MapAlgorithm": ma},
-            "TranscriptGencV4": {"MapAlgorithm": ma},
-            "FastqRd1": {"MapAlgorithm": "NA", "type": "fastq"},
-            "FastqRd2": {"MapAlgorithm": "NA", "type": "fastq"},
-            "Fastq": {"MapAlgorithm": "NA", "type": "fastq" },
-            "GeneModel": {"MapAlgorithm": ma},
-            "InsLength": {"MapAlgorithm": ma},
-            }
-        # view name is one of the attributes
-        for v in self.views.keys():
-            self.views[v]['view'] = v
-            
-    def find_attributes(self, pathname, lib_id):
-        """Looking for the best extension
-        The 'best' is the longest match
-        
-        :Args:
-        filename (str): the filename whose extention we are about to examine
-        """
-        path, filename = os.path.splitext(pathname)
-        if not self.lib_cache.has_key(lib_id):
-            self.lib_cache[lib_id] = get_library_info(self.root_url,
-                                                      self.apidata, lib_id)
-
-        lib_info = self.lib_cache[lib_id]
-        if lib_info['cell_line'].lower() == 'unknown':
-            logging.warn("Library %s missing cell_line" % (lib_id,))
-        attributes = {
-            'cell': lib_info['cell_line'],
-            'replicate': lib_info['replicate'],
-            }
-        is_paired = self._is_paired(lib_id, lib_info)
-        
-        if is_paired:
-            attributes.update(self.get_paired_attributes(lib_info))
-        else:
-            attributes.update(self.get_single_attributes(lib_info))
-            
-        for pattern, view in self.patterns:
-            if fnmatch.fnmatch(pathname, pattern):
-                if callable(view):
-                    view = view(is_paired=is_paired)
-                    
-                attributes.update(self.views[view])
-                attributes["extension"] = pattern
-                return attributes
-
-
-    def _guess_bam_view(self, is_paired=True):
-        """Guess a view name based on library attributes
-        """
-        if is_paired:
-            return "Paired"
-        else:
-            return "Aligns"
-
-
-    def _is_paired(self, lib_id, lib_info):
-        """Determine if a library is paired end"""
-        if len(lib_info["lane_set"]) == 0:
-            return False
-
-        if not self.lib_paired.has_key(lib_id):
-            is_paired = 0
-            isnot_paired = 0
-            failed = 0
-            # check to see if all the flowcells are the same.
-            # otherwise we might need to do something complicated
-            for flowcell in lib_info["lane_set"]:
-                # yes there's also a status code, but this comparison 
-                # is easier to read
-                if flowcell["status"].lower() == "failed":
-                    # ignore failed flowcell
-                    failed += 1
-                    pass
-                elif flowcell["paired_end"]:
-                    is_paired += 1
-                else:
-                    isnot_paired += 1
-                    
-            logging.debug("Library %s: %d paired, %d single, %d failed" % \
-                     (lib_info["library_id"], is_paired, isnot_paired, failed))
-
-            if is_paired > isnot_paired:
-                self.lib_paired[lib_id] = True
-            elif is_paired < isnot_paired:
-                self.lib_paired[lib_id] = False
-            else:
-                raise RuntimeError("Equal number of paired & unpaired lanes."\
-                                   "Can't guess library paired status")
-            
-        return self.lib_paired[lib_id]
-
-    def get_paired_attributes(self, lib_info):
-        if lib_info['insert_size'] is None:
-            errmsg = "Library %s is missing insert_size, assuming 200"
-            logging.warn(errmsg % (lib_info["library_id"],))
-            insert_size = 200
-        else:
-            insert_size = lib_info['insert_size']
-        return {'insertLength': insert_size,
-                'readType': '2x75'}
-
-    def get_single_attributes(self, lib_info):
-        return {'insertLength':'ilNA',
-                'readType': '1x75D'
-                }
-
 def make_submission_section(line_counter, files, attributes):
     """
     Create a section in the submission ini file
@@ -818,32 +419,6 @@ def make_condor_name(pathname, run_type=None):
     return ".".join(elements)
 
 
-def make_submit_script(target, header, body_list):
-    """
-    write out a text file
-
-    this was intended for condor submit scripts
-
-    Args:
-      target (str or stream): 
-        if target is a string, we will open and close the file
-        if target is a stream, the caller is responsible.
-
-      header (str);
-        header to write at the beginning of the file
-      body_list (list of strs):
-        a list of blocks to add to the file.
-    """
-    if type(target) in types.StringTypes:
-        f = open(target,"w")
-    else:
-        f = target
-    f.write(header)
-    for entry in body_list:
-        f.write(entry)
-    if type(target) in types.StringTypes:
-        f.close()
-
 def parse_filelist(file_string):
     return file_string.split(",")
 
@@ -856,6 +431,46 @@ def validate_filelist(files):
         if not os.path.exists(f):
             raise RuntimeError("%s does not exist" % (f,))
 
+def make_md5sum(filename):
+    """Quickly find the md5sum of a file
+    """
+    md5_cache = os.path.join(filename+".md5")
+    print md5_cache
+    if os.path.exists(md5_cache):
+        logging.debug("Found md5sum in {0}".format(md5_cache))
+        stream = open(md5_cache,'r')
+        lines = stream.readlines()
+        md5sum = parse_md5sum_line(lines, filename)
+    else:
+        md5sum = make_md5sum_unix(filename, md5_cache)
+    return md5sum
+    
+def make_md5sum_unix(filename, md5_cache):
+    cmd = ["md5sum", filename]
+    logging.debug("Running {0}".format(" ".join(cmd)))
+    p = Popen(cmd, stdout=PIPE)
+    stdin, stdout = p.communicate()
+    retcode = p.wait()
+    logging.debug("Finished {0} retcode {1}".format(" ".join(cmd), retcode))
+    if retcode != 0:
+        logging.error("Trouble with md5sum for {0}".format(filename))
+        return None
+    lines = stdin.split(os.linesep)
+    md5sum = parse_md5sum_line(lines, filename)
+    if md5sum is not None:
+        logging.debug("Caching sum in {0}".format(md5_cache))
+        stream = open(md5_cache, "w")
+        stream.write(stdin)
+        stream.close()
+    return md5sum
+
+def parse_md5sum_line(lines, filename):
+    md5sum, md5sum_filename = lines[0].split()
+    if md5sum_filename != filename:
+        errmsg = "MD5sum and I disagre about filename. {0} != {1}"
+        logging.error(errmsg.format(filename, md5sum_filename))
+        return None
+    return md5sum
 
 if __name__ == "__main__":
     main()