changeset 4:64f86e6e8604 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_genome_dbkeys_all_fasta commit 8652f36a3a3838dca989426961561e81432acf4f
author iuc
date Tue, 04 Apr 2017 17:12:57 -0400
parents b7d514d57e02
children 16b5d4a3b313
files data_manager/data_manager_fetch_genome_all_fasta_dbkeys.py data_manager/data_manager_fetch_genome_all_fasta_dbkeys.xml test-data/test.tar test-data/test.tar.gz test-data/test.zip
diffstat 5 files changed, 256 insertions(+), 210 deletions(-) [+]
line wrap: on
line diff
--- a/data_manager/data_manager_fetch_genome_all_fasta_dbkeys.py	Thu Jan 19 11:47:19 2017 -0500
+++ b/data_manager/data_manager_fetch_genome_all_fasta_dbkeys.py	Tue Apr 04 17:12:57 2017 -0400
@@ -1,35 +1,36 @@
 #!/usr/bin/env python
-#Dan Blankenberg
+# Dan Blankenberg
 
-import sys
+import bz2
+import gzip
+import optparse
 import os
-import tempfile
 import shutil
-import optparse
-from ftplib import FTP
+import sys
 import tarfile
+import tempfile
 import zipfile
-import gzip
-import bz2
+from ftplib import FTP
+from json import dumps, loads
+
 try:
     # For Python 3.0 and later
-    from urllib.request import urlopen
     from io import BytesIO as StringIO
     from io import UnsupportedOperation
+    from urllib.request import urlopen
 except ImportError:
-    # Fall back to Python 2's urllib2
+    # Fall back to Python 2 imports
+    from StringIO import StringIO
     from urllib2 import urlopen
-    from StringIO import StringIO
+
     UnsupportedOperation = AttributeError
-from json import loads, dumps
+
+CHUNK_SIZE = 2 ** 20  # 1mb
 
 
-CHUNK_SIZE = 2**20  # 1mb
-
-
-def cleanup_before_exit( tmp_dir ):
-    if tmp_dir and os.path.exists( tmp_dir ):
-        shutil.rmtree( tmp_dir )
+def cleanup_before_exit(tmp_dir):
+    if tmp_dir and os.path.exists(tmp_dir):
+        shutil.rmtree(tmp_dir)
 
 
 def stop_err(msg):
@@ -37,20 +38,20 @@
     sys.exit(1)
 
 
-def get_dbkey_dbname_id_name( params, dbkey_description=None ):
+def get_dbkey_dbname_id_name(params, dbkey_description=None):
     dbkey = params['param_dict']['dbkey_source']['dbkey']
-    #TODO: ensure sequence_id is unique and does not already appear in location file
+    # TODO: ensure sequence_id is unique and does not already appear in location file
     sequence_id = params['param_dict']['sequence_id']
     if not sequence_id:
-        sequence_id = dbkey #uuid.uuid4() generate and use an uuid instead?
-    
+        sequence_id = dbkey  # uuid.uuid4() generate and use an uuid instead?
+
     if params['param_dict']['dbkey_source']['dbkey_source_selector'] == 'new':
         dbkey_name = params['param_dict']['dbkey_source']['dbkey_name']
         if not dbkey_name:
             dbkey_name = dbkey
     else:
         dbkey_name = None
-    
+
     sequence_name = params['param_dict']['sequence_name']
     if not sequence_name:
         sequence_name = dbkey_description
@@ -59,146 +60,146 @@
     return dbkey, dbkey_name, sequence_id, sequence_name
 
 
-def _get_files_in_ftp_path( ftp, path ):
+def _get_files_in_ftp_path(ftp, path):
     path_contents = []
-    ftp.retrlines( 'MLSD %s' % ( path ), path_contents.append )
-    return [ line.split( ';' )[ -1 ].lstrip() for line in path_contents ]
+    ftp.retrlines('MLSD %s' % (path), path_contents.append)
+    return [line.split(';')[-1].lstrip() for line in path_contents]
 
 
-def _get_stream_readers_for_tar( fh, tmp_dir ):
-    fasta_tar = tarfile.open( fileobj=fh, mode='r:*' )
+def _get_stream_readers_for_tar(fh, tmp_dir):
+    fasta_tar = tarfile.open(fileobj=fh, mode='r:*')
     return [x for x in [fasta_tar.extractfile(member) for member in fasta_tar.getmembers()] if x]
 
 
-def _get_stream_readers_for_zip( fh, tmp_dir ):
+def _get_stream_readers_for_zip(fh, tmp_dir):
     """
     Unpacks all archived files in a zip file.
     Individual files will be concatenated (in _stream_fasta_to_file)
     """
-    fasta_zip = zipfile.ZipFile( fh, 'r' )
+    fasta_zip = zipfile.ZipFile(fh, 'r')
     rval = []
     for member in fasta_zip.namelist():
-        fasta_zip.extract( member, tmp_dir )
-        rval.append( open( os.path.join( tmp_dir, member ), 'rb' ) )
+        fasta_zip.extract(member, tmp_dir)
+        rval.append(open(os.path.join(tmp_dir, member), 'rb'))
     return rval
 
 
-def _get_stream_readers_for_gzip( fh, tmp_dir ):
-    return [ gzip.GzipFile( fileobj=fh, mode='rb') ]
+def _get_stream_readers_for_gzip(fh, tmp_dir):
+    return [gzip.GzipFile(fileobj=fh, mode='rb')]
 
 
-def _get_stream_readers_for_bz2( fh, tmp_dir ):
-    return [ bz2.BZ2File( fh.name, 'rb') ]
+def _get_stream_readers_for_bz2(fh, tmp_dir):
+    return [bz2.BZ2File(fh.name, 'rb')]
 
 
-def sort_fasta( fasta_filename, sort_method, params ):
+def sort_fasta(fasta_filename, sort_method, params):
     if sort_method is None:
         return
-    assert sort_method in SORTING_METHODS, ValueError( "%s is not a valid sorting option." % sort_method )
-    return SORTING_METHODS[ sort_method ]( fasta_filename, params )
+    assert sort_method in SORTING_METHODS, ValueError("%s is not a valid sorting option." % sort_method)
+    return SORTING_METHODS[sort_method](fasta_filename, params)
 
 
-def _move_and_index_fasta_for_sorting( fasta_filename ):
+def _move_and_index_fasta_for_sorting(fasta_filename):
     unsorted_filename = tempfile.NamedTemporaryFile().name
-    shutil.move( fasta_filename, unsorted_filename )
+    shutil.move(fasta_filename, unsorted_filename)
     fasta_offsets = {}
-    unsorted_fh = open( unsorted_filename )
+    unsorted_fh = open(unsorted_filename)
     while True:
         offset = unsorted_fh.tell()
         line = unsorted_fh.readline()
         if not line:
             break
-        if line.startswith( ">" ):
-            line = line.split( None, 1 )[0][1:]
-            fasta_offsets[ line ] = offset
+        if line.startswith(">"):
+            line = line.split(None, 1)[0][1:]
+            fasta_offsets[line] = offset
     unsorted_fh.close()
-    current_order = map( lambda x: x[1], sorted( map( lambda x: ( x[1], x[0] ), fasta_offsets.items() ) ) )
-    return ( unsorted_filename, fasta_offsets, current_order )
+    current_order = [_[1] for _ in sorted((_[1], _[0]) for _ in fasta_offsets.items())]
+    return (unsorted_filename, fasta_offsets, current_order)
 
 
-def _write_sorted_fasta( sorted_names, fasta_offsets, sorted_fasta_filename, unsorted_fasta_filename ):
-    unsorted_fh = open( unsorted_fasta_filename )
-    sorted_fh = open( sorted_fasta_filename, 'wb+' )
-    
+def _write_sorted_fasta(sorted_names, fasta_offsets, sorted_fasta_filename, unsorted_fasta_filename):
+    unsorted_fh = open(unsorted_fasta_filename)
+    sorted_fh = open(sorted_fasta_filename, 'wb+')
+
     for name in sorted_names:
-        offset = fasta_offsets[ name ]
-        unsorted_fh.seek( offset )
-        sorted_fh.write( unsorted_fh.readline() )
+        offset = fasta_offsets[name]
+        unsorted_fh.seek(offset)
+        sorted_fh.write(unsorted_fh.readline())
         while True:
             line = unsorted_fh.readline()
-            if not line or line.startswith( ">" ):
+            if not line or line.startswith(">"):
                 break
-            sorted_fh.write( line )
+            sorted_fh.write(line)
     unsorted_fh.close()
     sorted_fh.close()
 
 
-def _sort_fasta_as_is( fasta_filename, params ):
+def _sort_fasta_as_is(fasta_filename, params):
     return
 
-def _sort_fasta_lexicographical( fasta_filename, params ):
-    ( unsorted_filename, fasta_offsets, current_order ) = _move_and_index_fasta_for_sorting( fasta_filename )
-    sorted_names = sorted( fasta_offsets.keys() )
+
+def _sort_fasta_lexicographical(fasta_filename, params):
+    (unsorted_filename, fasta_offsets, current_order) = _move_and_index_fasta_for_sorting(fasta_filename)
+    sorted_names = sorted(fasta_offsets.keys())
     if sorted_names == current_order:
-        shutil.move( unsorted_filename, fasta_filename )
+        shutil.move(unsorted_filename, fasta_filename)
     else:
-        _write_sorted_fasta( sorted_names, fasta_offsets, fasta_filename, unsorted_filename )    
+        _write_sorted_fasta(sorted_names, fasta_offsets, fasta_filename, unsorted_filename)
 
 
-def _sort_fasta_gatk( fasta_filename, params ):
-    #This method was added by reviewer request.
-    ( unsorted_filename, fasta_offsets, current_order ) = _move_and_index_fasta_for_sorting( fasta_filename )
-    sorted_names = map( str, range( 1, 23 ) ) + [ 'X', 'Y' ]
-    #detect if we have chrN, or just N
+def _sort_fasta_gatk(fasta_filename, params):
+    (unsorted_filename, fasta_offsets, current_order) = _move_and_index_fasta_for_sorting(fasta_filename)
+    sorted_names = list(map(str, range(1, 23))) + ['X', 'Y']
+    # detect if we have chrN, or just N
     has_chr = False
     for chrom in sorted_names:
         if "chr%s" % chrom in current_order:
             has_chr = True
             break
-    
+
     if has_chr:
-        sorted_names = map( lambda x: "chr%s" % x, sorted_names)
-        sorted_names.insert( 0, "chrM" )
+        sorted_names = ["chr%s" % x for x in sorted_names]
+        sorted_names.insert(0, "chrM")
     else:
-        sorted_names.insert( 0, "MT" )
-    sorted_names.extend( map( lambda x: "%s_random" % x, sorted_names ) )
-    
+        sorted_names.insert(0, "MT")
+    sorted_names.extend("%s_random" % x for x in sorted_names)
+
     existing_sorted_names = []
     for name in sorted_names:
         if name in current_order:
-            existing_sorted_names.append( name )
+            existing_sorted_names.append(name)
     for name in current_order:
-        #TODO: confirm that non-canonical names do not need to be sorted specially
+        # TODO: confirm that non-canonical names do not need to be sorted specially
         if name not in existing_sorted_names:
-            existing_sorted_names.append( name )
-    
+            existing_sorted_names.append(name)
+
     if existing_sorted_names == current_order:
-        shutil.move( unsorted_filename, fasta_filename )
+        shutil.move(unsorted_filename, fasta_filename)
     else:
-        _write_sorted_fasta( existing_sorted_names, fasta_offsets, fasta_filename, unsorted_filename )
+        _write_sorted_fasta(existing_sorted_names, fasta_offsets, fasta_filename, unsorted_filename)
 
 
-def _sort_fasta_custom( fasta_filename, params ):
-    ( unsorted_filename, fasta_offsets, current_order ) = _move_and_index_fasta_for_sorting( fasta_filename )
+def _sort_fasta_custom(fasta_filename, params):
+    (unsorted_filename, fasta_offsets, current_order) = _move_and_index_fasta_for_sorting(fasta_filename)
     sorted_names = []
     for id_repeat in params['param_dict']['sorting']['sequence_identifiers']:
-        sorted_names.append( id_repeat[ 'identifier' ] )
+        sorted_names.append(id_repeat['identifier'])
     handle_not_listed = params['param_dict']['sorting']['handle_not_listed_selector']
-    if handle_not_listed.startswith( 'keep' ):
+    if handle_not_listed.startswith('keep'):
         add_list = []
         for name in current_order:
             if name not in sorted_names:
-                add_list.append( name )
+                add_list.append(name)
         if add_list:
             if handle_not_listed == 'keep_append':
-                sorted_names.extend( add_list )
+                sorted_names.extend(add_list)
             else:
-                add_list.extend( sorted_names )
+                add_list.extend(sorted_names)
                 sorted_names = add_list
     if sorted_names == current_order:
-        shutil.move( unsorted_filename, fasta_filename )
+        shutil.move(unsorted_filename, fasta_filename)
     else:
-        _write_sorted_fasta( sorted_names, fasta_offsets, fasta_filename, unsorted_filename )
+        _write_sorted_fasta(sorted_names, fasta_offsets, fasta_filename, unsorted_filename)
 
 
 def _download_file(start, fh):
@@ -223,17 +224,17 @@
     start_of_file = fh.read(CHUNK_SIZE)
     try:
         fh.seek(0)
-    except UnsupportedOperation:  # This is if fh has been created by urlopen
+    except UnsupportedOperation:  # This happens if fh has been created by urlopen
         fh = _download_file(start_of_file, fh)
-    for k,v in magic_dict.items():
-        if start_of_file.startswith(k):
-            return v(fh, tmp_dir)
     try:  # Check if file is tar file
         if tarfile.open(fileobj=StringIO(start_of_file)):
             return _get_stream_readers_for_tar(fh, tmp_dir)
     except tarfile.ReadError:
         pass
-    return fh
+    for k, v in magic_dict.items():
+        if start_of_file.startswith(k):
+            return v(fh, tmp_dir)
+    return [fh]
 
 
 def _get_ucsc_download_address(params, dbkey):
@@ -266,82 +267,126 @@
 
     raise Exception('Unable to determine filename for UCSC Genome for %s: %s' % (ucsc_dbkey, path_contents))
 
-def add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params):
-    for data_table_name, data_table_entry in _stream_fasta_to_file( fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params ):
-        if data_table_entry:
-            _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name )
+
+def add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params, **kwds):
+    fasta_filename = _stream_fasta_to_file(fasta_stream=fasta_readers,
+                                           target_directory=target_directory,
+                                           sequence_id=sequence_id)
+    sort_fasta(fasta_filename, params['param_dict']['sorting']['sort_selector'], params)
+    if dbkey_name:
+        add_dbkey_to_table(data_manager_dict=data_manager_dict,
+                           target_directory=target_directory,
+                           dbkey=dbkey,
+                           dbkey_name=dbkey_name,
+                           fasta_filename=fasta_filename)
+    _add_data_table_entry(data_manager_dict,
+                          data_table_entry=dict(value=sequence_id, dbkey=dbkey, name=sequence_name, path=os.path.basename(fasta_filename)),
+                          data_table_name='all_fasta')
+    return fasta_filename
 
 
-def download_from_ucsc( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
-    url = _get_ucsc_download_address(params, dbkey)
-    fasta_readers = get_stream_reader(urlopen(url), tmp_dir)
-    add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params)
+def add_dbkey_to_table(data_manager_dict, target_directory, dbkey, dbkey_name, fasta_filename):
+    # do len calc here
+    len_base_name = "%s.len" % (dbkey)
+    compute_fasta_length(fasta_filename, os.path.join(target_directory, len_base_name), keep_first_word=True)
+    dbkey_dict = dict(value=dbkey, name=dbkey_name, len_path=len_base_name)
+    _add_data_table_entry(data_manager_dict,
+                          data_table_entry=dbkey_dict,
+                          data_table_name='__dbkeys__')
 
 
-def download_from_ncbi( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
-    NCBI_DOWNLOAD_URL = 'http://togows.dbcls.jp/entry/ncbi-nucleotide/%s.fasta' #FIXME: taken from dave's genome manager...why some japan site?
+def download_from_ucsc(params, dbkey, tmp_dir, **kwds):
+    url = _get_ucsc_download_address(params, dbkey)
+    return get_stream_reader(urlopen(url), tmp_dir)
+
+
+def download_from_ncbi(params, tmp_dir, **kwds):
+    NCBI_DOWNLOAD_URL = 'http://togows.dbcls.jp/entry/ncbi-nucleotide/%s.fasta'  # FIXME: taken from dave's genome manager...why some japan site?
     requested_identifier = params['param_dict']['reference_source']['requested_identifier']
     url = NCBI_DOWNLOAD_URL % requested_identifier
-    fasta_readers = get_stream_reader(urlopen(url), tmp_dir)
-    add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params)
-
-
-def download_from_url( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
-    urls = filter( bool, map( lambda x: x.strip(), params['param_dict']['reference_source']['user_url'].split( '\n' ) ) )
-    fasta_readers = [ get_stream_reader(urlopen( url ), tmp_dir) for url in urls ]
-    add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id,sequence_name, params)
+    return get_stream_reader(urlopen(url), tmp_dir)
 
 
-def download_from_history( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
-    #TODO: allow multiple FASTA input files
+def download_from_url(params, tmp_dir, **kwds):
+    """
+    Download a file from a URL and return a list of filehandles from which to read the data.
+
+    >>> url = 'https://github.com/mvdbeek/tools-devteam/raw/data_manager/data_managers/data_manager_fetch_genome_dbkeys_all_fasta/test-data/test.tar'
+    >>> params = {'param_dict': {'reference_source': {'user_url': url}}}
+    >>> tmp_dir = tempfile.mkdtemp()
+    >>> fh = download_from_url(params=params, tmp_dir=tmp_dir)[0][0]
+    >>> assert fh.readline().startswith('>FBtr0304171')
+    >>> url = 'https://github.com/mvdbeek/tools-devteam/raw/data_manager/data_managers/data_manager_fetch_genome_dbkeys_all_fasta/test-data/test.tar.bz2'
+    >>> params = {'param_dict': {'reference_source': {'user_url': url}}}
+    >>> fh = download_from_url(params=params, tmp_dir=tmp_dir)[0][0]
+    >>> assert fh.readline().startswith('>FBtr0304171')
+    >>> url = 'https://github.com/mvdbeek/tools-devteam/raw/data_manager/data_managers/data_manager_fetch_genome_dbkeys_all_fasta/test-data/test.tar.gz'
+    >>> params = {'param_dict': {'reference_source': {'user_url': url}}}
+    >>> fh = download_from_url(params=params, tmp_dir=tmp_dir)[0][0]
+    >>> assert fh.readline().startswith('>FBtr0304171')
+    >>> url = 'https://github.com/mvdbeek/tools-devteam/raw/data_manager/data_managers/data_manager_fetch_genome_dbkeys_all_fasta/test-data/test.zip'
+    >>> params = {'param_dict': {'reference_source': {'user_url': url}}}
+    >>> fh = download_from_url(params=params, tmp_dir=tmp_dir)[0][0]
+    >>> assert fh.readline().startswith('>FBtr0304171')
+    >>> url = 'https://raw.githubusercontent.com/galaxyproject/tools-devteam/master/data_managers/data_manager_fetch_genome_dbkeys_all_fasta/test-data/phiX174.fasta'
+    >>> params = {'param_dict': {'reference_source': {'user_url': url}}}
+    >>> fh = download_from_url(params=params, tmp_dir=tmp_dir)[0][0]
+    >>> assert fh.readline().startswith('>phiX174')
+    """
+    urls = filter(bool, [x.strip() for x in params['param_dict']['reference_source']['user_url'].split('\n')])
+    return [get_stream_reader(urlopen(url), tmp_dir) for url in urls]
+
+
+def download_from_history(params, tmp_dir, **kwds):
+    # TODO: allow multiple FASTA input files
     input_filename = params['param_dict']['reference_source']['input_fasta']
-    if isinstance( input_filename, list ):
-        fasta_readers = [ get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename ]
+    if isinstance(input_filename, list):
+        fasta_readers = [get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename]
     else:
         fasta_readers = get_stream_reader(open(input_filename), tmp_dir)
-    add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params)
+    return fasta_readers
 
 
-def copy_from_directory( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
+def copy_from_directory(data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir):
     input_filename = params['param_dict']['reference_source']['fasta_filename']
     create_symlink = params['param_dict']['reference_source']['create_symlink'] == 'create_symlink'
     if create_symlink:
-        data_table_entries = _create_symlink( input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name )
+        data_table_entries = _create_symlink(input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name)
     else:
-        if isinstance( input_filename, list ):
-            fasta_readers = [ get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename ]
+        if isinstance(input_filename, list):
+            fasta_readers = [get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename]
         else:
             fasta_readers = get_stream_reader(open(input_filename), tmp_dir)
-        data_table_entries = _stream_fasta_to_file( fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params )
+        return fasta_readers
     for data_table_name, data_table_entry in data_table_entries:
         if data_table_entry:
-            _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name )
+            _add_data_table_entry(data_manager_dict, data_table_entry, data_table_name)
 
 
-def _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name ):
-    data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
-    data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get( 'all_fasta', [] )
-    data_manager_dict['data_tables'][data_table_name].append( data_table_entry )
+def _add_data_table_entry(data_manager_dict, data_table_entry, data_table_name):
+    data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {})
+    data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get('all_fasta', [])
+    data_manager_dict['data_tables'][data_table_name].append(data_table_entry)
     return data_manager_dict
 
 
-def _stream_fasta_to_file( fasta_stream, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params, close_stream=True ):
+def _stream_fasta_to_file(fasta_stream, target_directory, sequence_id, close_stream=True):
     fasta_base_filename = "%s.fa" % sequence_id
-    fasta_filename = os.path.join( target_directory, fasta_base_filename )
-    with open( fasta_filename, 'wb+' ) as fasta_writer:
+    fasta_filename = os.path.join(target_directory, fasta_base_filename)
+    with open(fasta_filename, 'wb+') as fasta_writer:
 
-        if isinstance( fasta_stream, list ) and len( fasta_stream ) == 1:
+        if isinstance(fasta_stream, list) and len(fasta_stream) == 1:
             fasta_stream = fasta_stream[0]
 
-        if isinstance( fasta_stream, list ):
+        if isinstance(fasta_stream, list):
             last_char = None
             for fh in fasta_stream:
-                if last_char not in [ None, '\n', '\r', b'\n', b'\r' ]:
-                    fasta_writer.write( b'\n' )
+                if last_char not in [None, '\n', '\r', b'\n', b'\r']:
+                    fasta_writer.write(b'\n')
                 while True:
-                    data = fh.read( CHUNK_SIZE )
+                    data = fh.read(CHUNK_SIZE)
                     if data:
-                        fasta_writer.write( data )
+                        fasta_writer.write(data)
                         last_char = data[-1]
                     else:
                         break
@@ -349,45 +394,34 @@
                     fh.close()
         else:
             while True:
-                data = fasta_stream.read( CHUNK_SIZE )
+                data = fasta_stream.read(CHUNK_SIZE)
                 if data:
-                    fasta_writer.write( data )
+                    fasta_writer.write(data)
                 else:
                     break
             if close_stream:
                 fasta_stream.close()
-
-    sort_fasta( fasta_filename, params['param_dict']['sorting']['sort_selector'], params )
-    
-    dbkey_dict = None
-    if dbkey_name:
-        #do len calc here
-        len_base_name = "%s.len" % ( dbkey )
-        compute_fasta_length( fasta_filename, os.path.join( target_directory, len_base_name ), keep_first_word=True )
-        dbkey_dict = dict( value=dbkey, name=dbkey_name, len_path=len_base_name )
-    
-    return [ ( '__dbkeys__', dbkey_dict ), ( 'all_fasta', dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_filename ) ) ]
+    return fasta_filename
 
 
-def compute_fasta_length( fasta_file, out_file, keep_first_word=False ):
-
+def compute_fasta_length(fasta_file, out_file, keep_first_word=False):
     infile = fasta_file
-    out = open( out_file, 'w')
+    out = open(out_file, 'w')
 
     fasta_title = ''
     seq_len = 0
 
     first_entry = True
 
-    for line in open( infile ):
+    for line in open(infile):
         line = line.strip()
-        if not line or line.startswith( '#' ):
+        if not line or line.startswith('#'):
             continue
         if line[0] == '>':
-            if first_entry == False:
+            if not first_entry:
                 if keep_first_word:
                     fasta_title = fasta_title.split()[0]
-                out.write( "%s\t%d\n" % ( fasta_title[ 1: ], seq_len ) )
+                out.write("%s\t%d\n" % (fasta_title[1:], seq_len))
             else:
                 first_entry = False
             fasta_title = line
@@ -398,57 +432,75 @@
     # last fasta-entry
     if keep_first_word:
         fasta_title = fasta_title.split()[0]
-    out.write( "%s\t%d\n" % ( fasta_title[ 1: ], seq_len ) )
+    out.write("%s\t%d\n" % (fasta_title[1:], seq_len))
     out.close()
 
 
-def _create_symlink( input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name ):
+def _create_symlink(input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name):
     fasta_base_filename = "%s.fa" % sequence_id
-    fasta_filename = os.path.join( target_directory, fasta_base_filename )
-    os.symlink( input_filename, fasta_filename )
-    
+    fasta_filename = os.path.join(target_directory, fasta_base_filename)
+    os.symlink(input_filename, fasta_filename)
+
     dbkey_dict = None
     if dbkey_name:
-        #do len calc here
-        len_base_name = "%s.len" % ( dbkey )
-        compute_fasta_length( fasta_filename, os.path.join( target_directory, len_base_name ), keep_first_word=True )
-        dbkey_dict = dict( value=dbkey, name=dbkey_name, len_path=len_base_name )
-    
-    return [ ( '__dbkeys__', dbkey_dict ), ( 'all_fasta', dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_filename ) ) ]
+        # do len calc here
+        len_base_name = "%s.len" % (dbkey)
+        compute_fasta_length(fasta_filename, os.path.join(target_directory, len_base_name), keep_first_word=True)
+        dbkey_dict = dict(value=dbkey, name=dbkey_name, len_path=len_base_name)
+
+    return [('__dbkeys__', dbkey_dict), ('all_fasta', dict(value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_filename))]
 
 
-REFERENCE_SOURCE_TO_DOWNLOAD = dict( ucsc=download_from_ucsc, ncbi=download_from_ncbi, url=download_from_url, history=download_from_history, directory=copy_from_directory )
-
-SORTING_METHODS = dict( as_is=_sort_fasta_as_is, lexicographical=_sort_fasta_lexicographical, gatk=_sort_fasta_gatk, custom=_sort_fasta_custom )
+REFERENCE_SOURCE_TO_DOWNLOAD = dict(ucsc=download_from_ucsc, ncbi=download_from_ncbi, url=download_from_url, history=download_from_history,
+                                    directory=copy_from_directory)
+SORTING_METHODS = dict(as_is=_sort_fasta_as_is, lexicographical=_sort_fasta_lexicographical, gatk=_sort_fasta_gatk, custom=_sort_fasta_custom)
 
 
 def main():
-    #Parse Command Line
     parser = optparse.OptionParser()
-    parser.add_option( '-d', '--dbkey_description', dest='dbkey_description', action='store', type="string", default=None, help='dbkey_description' )
+    parser.add_option('-d', '--dbkey_description', dest='dbkey_description', action='store', type="string", default=None, help='dbkey_description')
     (options, args) = parser.parse_args()
-    
+
     filename = args[0]
-    
-    params = loads( open( filename ).read() )
-    target_directory = params[ 'output_data' ][0]['extra_files_path']
-    os.mkdir( target_directory )
+
+    params = loads(open(filename).read())
+    target_directory = params['output_data'][0]['extra_files_path']
+    os.mkdir(target_directory)
     data_manager_dict = {}
-    
-    dbkey, dbkey_name, sequence_id, sequence_name = get_dbkey_dbname_id_name( params, dbkey_description=options.dbkey_description ) 
-    
-    if dbkey in [ None, '', '?' ]:
-        raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) )
+
+    dbkey, dbkey_name, sequence_id, sequence_name = get_dbkey_dbname_id_name(params, dbkey_description=options.dbkey_description)
+
+    if dbkey in [None, '', '?']:
+        raise Exception('"%s" is not a valid dbkey. You must specify a valid dbkey.' % (dbkey))
 
     # Create a tmp_dir, in case a zip file needs to be uncompressed
     tmp_dir = tempfile.mkdtemp()
-    #Fetch the FASTA
+    # Fetch the FASTA
     try:
-        REFERENCE_SOURCE_TO_DOWNLOAD[ params['param_dict']['reference_source']['reference_source_selector'] ]( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir )
+        reference_source = params['param_dict']['reference_source']['reference_source_selector']
+        fasta_readers = REFERENCE_SOURCE_TO_DOWNLOAD[reference_source](data_manager_dict=data_manager_dict,
+                                                                       params=params,
+                                                                       target_directory=target_directory,
+                                                                       dbkey=dbkey,
+                                                                       dbkey_name=dbkey_name,
+                                                                       sequence_id=sequence_id,
+                                                                       sequence_name=sequence_name,
+                                                                       tmp_dir=tmp_dir)
+        if fasta_readers:
+            add_fasta_to_table(data_manager_dict=data_manager_dict,
+                               fasta_readers=fasta_readers,
+                               target_directory=target_directory,
+                               dbkey=dbkey,
+                               dbkey_name=dbkey_name,
+                               sequence_id=sequence_id,
+                               sequence_name=sequence_name,
+                               params=params)
+
     finally:
         cleanup_before_exit(tmp_dir)
-    #save info to json file
-    open( filename, 'wb' ).write( dumps( data_manager_dict ).encode() )
-        
+    # save info to json file
+    open(filename, 'wb').write(dumps(data_manager_dict).encode())
+
+
 if __name__ == "__main__":
     main()
--- a/data_manager/data_manager_fetch_genome_all_fasta_dbkeys.xml	Thu Jan 19 11:47:19 2017 -0500
+++ b/data_manager/data_manager_fetch_genome_all_fasta_dbkeys.xml	Tue Apr 04 17:12:57 2017 -0400
@@ -1,11 +1,12 @@
 <tool id="data_manager_fetch_genome_all_fasta_dbkey" name="Create DBKey and Reference Genome" version="0.0.2" tool_type="manage_data">
     <description>fetching</description>
-    <command><![CDATA[
-       python "$__tool_directory__"/data_manager_fetch_genome_all_fasta_dbkeys.py "${out_file}"
+    <command detect_errors="exit_code"><![CDATA[
+       python '$__tool_directory__/data_manager_fetch_genome_all_fasta_dbkeys.py'
+       '${out_file}'
        #if str( $dbkey_source.dbkey_source_selector ) == 'existing':
-       --dbkey_description ${ dbkey_source.dbkey.get_display_text() }
+           --dbkey_description '${ dbkey_source.dbkey.get_display_text() }'
        #else
-       --dbkey_description "${ dbkey_source.dbkey_name or $dbkey_source.dbkey }"
+           --dbkey_description '${ dbkey_source.dbkey_name or $dbkey_source.dbkey }'
        #end if
     ]]></command>
     <inputs>
@@ -18,12 +19,12 @@
                 <param name="dbkey" type="genomebuild" label="DBKEY to assign to data" />
             </when>
             <when value="new">
-                <param type="text" name="dbkey" value="" label="dbkey" optional="False" />
-                <param type="text" name="dbkey_name" value="" label="Display name for dbkey" />
+                <param name="dbkey" type="text" value="" optional="false" label="dbkey" />
+                <param name="dbkey_name" type="text" value="" label="Display name for dbkey" />
             </when>
         </conditional>
-        <param type="text" name="sequence_name" value="" label="Name of sequence" />
-        <param type="text" name="sequence_id" value="" label="ID for sequence" />
+        <param name="sequence_name" type="text" value="" label="Name of sequence" />
+        <param name="sequence_id" type="text" value="" label="ID for sequence" />
         <conditional name="reference_source">
             <param name="reference_source_selector" type="select" label="Choose the source for the reference genome">
                 <option value="ucsc" selected="True">UCSC</option>
@@ -33,20 +34,20 @@
                 <option value="directory">Directory on Server</option>
             </param>
             <when value="ucsc">
-                <param type="text" name="requested_dbkey" value="" label="UCSC's DBKEY for source FASTA" optional="False" />
+                <param name="requested_dbkey" type="text" value="" optional="false" label="UCSC's DBKEY for source FASTA" />
             </when>
             <when value="ncbi">
-                <param type="text" name="requested_identifier" value="" label="NCBI identifier/accession" help="Identifiers (e.g 667699573) or accessions (e.g AC020606.7) may be used" optional="False" />
+                <param name="requested_identifier" type="text" value="" optional="false" label="NCBI identifier/accession" help="Identifiers (e.g 667699573) or accessions (e.g AC020606.7) may be used" />
             </when>
             <when value="url">
-                <param type="text" area="True" name="user_url" value="http://" label="URLs" optional="False" />
+                <param name="user_url" type="text" area="True" value="http://" optional="false" label="URLs" />
             </when>
             <when value="history">
-                <param name="input_fasta" type="data" format="fasta" label="FASTA File" multiple="False" optional="False" />
+                <param name="input_fasta" type="data" format="fasta" label="FASTA file" />
             </when>
             <when value="directory">
-                <param type="text" name="fasta_filename" value="" label="Full path to FASTA File on disk" optional="False" />
-                <param type="boolean" name="create_symlink" truevalue="create_symlink" falsevalue="copy_file" label="Create symlink to original data instead of copying" checked="False" />
+                <param name="fasta_filename" type="text" value="" optional="false" label="Full path to FASTA file on disk" />
+                <param name="create_symlink" type="boolean" truevalue="create_symlink" falsevalue="copy_file" label="Create symlink to original data instead of copying" />
             </when>
         </conditional>
         <conditional name="sorting">
@@ -56,15 +57,12 @@
                 <option value="gatk">GATK</option>
                 <option value="custom">Custom</option>
             </param>
-            <when value="as_is">
-            </when>
-            <when value="lexicographical">
-            </when>
-            <when value="gatk">
-            </when>
+            <when value="as_is" />
+            <when value="lexicographical" />
+            <when value="gatk" />
             <when value="custom">
                 <repeat name="sequence_identifiers" title="Sequence Identifiers" min="1" default="1">
-                    <param type="text" name="identifier" value="" label="Sequence Identifier" optional="False" />
+                    <param name="identifier" type="text" value="" optional="false" label="Sequence Identifier" />
                 </repeat>
                 <param name="handle_not_listed_selector" type="select" label="How to handle non-specified Identifiers">
                     <option value="discard" selected="True">Discard</option>
@@ -82,7 +80,6 @@
         <test>
             <param name="dbkey" value="anoGam1"/>
             <param name="sequence_name" value=""/>
-            <param name="sequence_desc" value=""/>
             <param name="sequence_id" value=""/>
             <param name="reference_source_selector" value="history"/>
             <param name="input_fasta" value="phiX174.fasta"/>
@@ -97,11 +94,8 @@
 
 ------
 
-
-
 .. class:: infomark
 
 **Notice:** If you leave name, description, or id blank, it will be generated automatically.
-
     </help>
 </tool>
Binary file test-data/test.tar has changed
Binary file test-data/test.tar.gz has changed
Binary file test-data/test.zip has changed