Convert a value into CWL formatted JSON, handling files and complex things.

def _to_cwl(val, input_files):
    """Convert a value into CWL formatted JSON, handling files and complex things.
    """
    if isinstance(val, six.string_types):
        if _file_and_exists(val, input_files):
            val = {"class": "File", "path": val}
            secondary = []
            for idx in [".bai", ".tbi", ".gbi", ".fai", ".crai", ".db"]:
                idx_file = val["path"] + idx
                if _file_and_exists(idx_file, input_files):
                    secondary.append({"class": "File", "path": idx_file})
            for idx in [".dict"]:
                idx_file = os.path.splitext(val["path"])[0] + idx
                if _file_and_exists(idx_file, input_files):
                    secondary.append({"class": "File", "path": idx_file})
            cur_dir, cur_file = os.path.split(val["path"])
            # Handle relative paths
            if not cur_dir:
                cur_dir = os.getcwd()
            if cur_file.endswith(cwlutils.DIR_TARGETS):
                if os.path.exists(cur_dir):
                    for fname in os.listdir(cur_dir):
                        if fname != cur_file and not os.path.isdir(os.path.join(cur_dir, fname))\
                                and fname != 'sbg.worker.log':
                            secondary.append({"class": "File", "path": os.path.join(cur_dir, fname)})
                else:
                    for f in input_files:
                        if f.startswith(cur_dir) and f != cur_file and not os.path.isdir(f):
                            secondary.append({"class": "File", "path": f})
            if secondary:
                val["secondaryFiles"] = _remove_duplicate_files(secondary)
    elif isinstance(val, (list, tuple)):
        val = [_to_cwl(x, input_files) for x in val]
    elif isinstance(val, dict):
        # File representation with secondary files
        if "base" in val and "secondary" in val:
            out = {"class": "File", "path": val["base"]}
            secondary = [{"class": "File", "path": x} for x in val["secondary"] if not os.path.isdir(x)]
            if secondary:
                out["secondaryFiles"] = _remove_duplicate_files(secondary)
            val = out
        else:
            val = json.dumps(val, sort_keys=True, separators=(',', ':'))
    return val