view util/santitizer.py @ 18:79b23a8afb15 draft

planemo upload for repository https://github.com/goeckslab/jbrowse-archive-creator.git commit 398c2fd8d1cb38decddf8cd58574cf39b1b52eaa
author yating-l
date Fri, 20 Jul 2018 12:25:36 -0400
parents 237707a6b74d
children
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf8 -*-

"""
This class handles the subprocess calls of the different tools used
in HubArchiveCreator
"""

import logging
import os
import subprocess
import sys
import string
import tempfile

    
def prefixTrackName(filename):       
    """
    santitize trackName. Because track name must begin with a letter and
    contain only the following chars: [a-zA-Z0-9_].
    See the "track" Common settings at:
    https://genome.ucsc.edu/goldenpath/help/trackDb/trackDbHub.html#bigPsl_-_Pairwise_Alignments
    skip the santitization for cytoBandIdeo track
    """
    if filename == 'cytoBandIdeo':
        return filename
    valid_chars = "_%s%s" % (string.ascii_letters, string.digits)
    sanitize_name = ''.join([c if c in valid_chars else '_' for c in filename])
    sanitize_name = "gonramp_" + sanitize_name
    return sanitize_name

def sanitize_name_input(string_to_sanitize):
    """
    Sanitize the string passed in parameter by replacing '/' and ' ' by '_'

    :param string_to_sanitize:
    :return :

    :Example:

    >>> sanitize_name_input('this/is an//example')
    this_is_an__example
    """
    return string_to_sanitize \
            .replace("/", "_") \
            .replace(" ", "_")

def sanitize_name_inputs(inputs_data):
    """
    Sanitize value of the keys "name" of the dictionary passed in parameter.

    Because sometimes output from Galaxy, or even just file name, from user inputs, have spaces.
    Also, it can contain '/' character and could break the use of os.path function.

    :param inputs_data: dict[string, dict[string, string]]
    """
    for key in inputs_data:
        inputs_data[key]["name"] = sanitize_name_input(inputs_data[key]["name"])

def sanitize_group_name(group_name):
    return group_name.lower().replace(' ', '_')

def sanitize_name(input_name):
    """
    Galaxy will name all the files and dirs as *.dat, 
    the function can replace '.' to '_' for the dirs
    """
    validChars = "_-%s%s" % (string.ascii_letters, string.digits)
    sanitized_name = ''.join([c if c in validChars else '_' for c in input_name])
    return "gonramp_" + sanitized_name