select  s.sample_name || '.' || sp.sequence_prep_id 
         from    sample s 
                 inner join sequence_prep sp 
                 on s.sample_id = sp.sample_id
         where   s.study_id = {0}
                 and sp.run_prefix = '{1}'
         """.format(study_id, run_prefix[:-1])
         sample_and_prep = data_access.dynamicMetadataSelect(sql).fetchone()[0]
         input_str = '-i {0} --sample_id {1}'.format(filenames[0], sample_and_prep)
     except Exception, e:
         error = 'Failed to obtain sample and sequence prep info for study_id {0} and run_prefix {1}\n'.format(study_id, run_prefix)
         error += 'SQL was: \n {0} \n'.format(sql)
         error += 'Original exception was: \n {0}'.format(str(e))
         raise Exception(error)
 else:
     input_str=get_split_libraries_fastq_params_and_file_types(filenames, mapping_fp)
 
 # create split_libaries folder
 split_library_output=join(output_dir,'split_libraries')
 create_dir(split_library_output)
 
 # get params string
 try:
     params_str = get_params_str(params['split_libraries_fastq'])
 except KeyError:
     params_str = ''
 
 # Build the split libraries command
 split_libraries_cmd = '%s %s/split_libraries_fastq.py -o %s -m %s %s %s' % \
  (python_exe_fp, script_dir, split_library_output, mapping_input_fp_copy,
   input_str,params_str)
def run_process_illumina_through_split_lib(study_id,run_prefix,input_fp,
    mapping_fp, output_dir, 
    command_handler, params, qiime_config,
    write_to_all_fasta=False,
    status_update_callback=print_to_stdout):
    """ NOTE: Parts of this function are a directly copied from the
        run_qiime_data_preparation function from the workflow.py library file 
        in QIIME.
    
        The steps performed by this function are:
          1) De-multiplex sequences. (split_libraries_fastq.py)
    
    """

    # Prepare some variables for the later steps
    filenames=input_fp.split(',')
    commands = []
    create_dir(output_dir)
    python_exe_fp = qiime_config['python_exe_fp']
    script_dir = get_qiime_scripts_dir()
    logger = WorkflowLogger(generate_log_fp(output_dir),
                            params=params,
                            qiime_config=qiime_config)
    
    # copy the mapping file
    copied_mapping=split(mapping_fp)[-1]
    mapping_input_fp_copy=join(output_dir, copied_mapping)
    copy_mapping_cmd='cp %s %s' % (mapping_fp,mapping_input_fp_copy)
    commands.append([('CopyMapping', copy_mapping_cmd)])

    # sort the filenames
    filenames.sort()
    
    # determine which file is seq-file and which is barcode-file and associate
    # to mapping file
    input_str=get_split_libraries_fastq_params_and_file_types(filenames,
                                                              mapping_fp)
    
    # create split_libaries folder
    split_library_output=join(output_dir,'split_libraries')
    create_dir(split_library_output)
    
    # get params string
    try:
        params_str = get_params_str(params['split_libraries_fastq'])
    except KeyError:
        params_str = ''
    
    # Build the split libraries command
    split_libraries_cmd = '%s %s/split_libraries_fastq.py -o %s -m %s %s %s' % \
     (python_exe_fp, script_dir, split_library_output, mapping_input_fp_copy,
      input_str,params_str)
    
    commands.append([('SplitLibraries', split_libraries_cmd)])
    
    # define the generate files
    input_fp=join(split_library_output,'seqs.fna')
    
    # create per sample fastq files
    fastq_output=join(split_library_output,'per_sample_fastq')
    create_dir(fastq_output)
    
    """
    # not used for the one-off
    try:
        params_str = get_params_str(params['convert_fastaqual_fastq'])
    except KeyError:
        params_str = ''
    """
    
    # build the per-sample fastq command
    input_qual_fp=join(split_library_output,'seqs.qual')
    create_fastq_cmd = '%s %s/git/qiime_web_app/python_code/scripts/make_per_sample_fastq.py -i %s -q %s -o %s' % \
    (python_exe_fp, environ['HOME'], input_fp, input_qual_fp, fastq_output)
    
    """
    # TURN ON when convert_fastaqual_fastq can handle Illumina qual file
    create_fastq_cmd = '%s %s/convert_fastaqual_fastq.py -f %s -q %s -o %s %s'%\
     (python_exe_fp, script_dir, input_fp, input_qual_fp,
      fastq_output, params_str)
    """
    commands.append([('Create FASTQ', create_fastq_cmd)])
    
    # Call the command handler on the list of commands
    command_handler(commands,status_update_callback,logger=logger)

    # Return the fasta file paths
    return filenames