view Datatype.py @ 13:25809f699cb3 draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 65ab931ef2b05a5acf06cbde3a746c94a0a0a4cb
author rmarenco
date Thu, 11 Aug 2016 19:02:29 -0400
parents d05236b15f81
children 3233451a3bd6
line wrap: on
line source

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

"""
Super Class of the managed datatype
"""

import os
import tempfile

from util import subtools
from Track import Track
from TrackDb import TrackDb


class Datatype(object):

    twoBitFile = None

    input_fasta_file = None
    extra_files_path = None
    tool_directory = None

    mySpecieFolderPath = None
    myTrackFolderPath = None

    twoBitFile = None
    chromSizesFile = None

    track = None

    def __init__(self):

        not_init_message = "The {0} is not initialized." \
                           "Did you use pre_init static method first?"
        if Datatype.input_fasta_file is None:
            raise TypeError(not_init_message.format('reference genome'))
        if Datatype.extra_files_path is None:
            raise TypeError(not_init_message.format('track Hub path'))
        if Datatype.tool_directory is None:
            raise TypeError(not_init_message.format('tool directory'))

    @staticmethod
    def pre_init(reference_genome, two_bit_path, chrom_sizes_file,
                 extra_files_path, tool_directory, specie_folder, tracks_folder):
        Datatype.extra_files_path = extra_files_path
        Datatype.tool_directory = tool_directory

        # TODO: All this should be in TrackHub and not in Datatype
        Datatype.mySpecieFolderPath = specie_folder
        Datatype.myTrackFolderPath = tracks_folder

        Datatype.input_fasta_file = reference_genome

        # 2bit file creation from input fasta
        Datatype.twoBitFile = two_bit_path
        Datatype.chromSizesFile = chrom_sizes_file

    @staticmethod
    def get_largest_scaffold_name(self):
        # We can get the biggest scaffold here, with chromSizesFile
        with open(Datatype.chromSizesFile.name, 'r') as chrom_sizes:
            # TODO: Check if exists
            return chrom_sizes.readline().split()[0]

    # TODO: Rename for PEP8
    def getShortName( self, name_to_shortify ):
        # Slice to get from Long label the short label
        short_label_slice = slice(0, 15)

        return name_to_shortify[short_label_slice]

    # TODO: Better handle parameters, use heritance mecanism
    # TODO: Use default parameters for some, like visibility
    def createTrack(self,
                    file_path=None,
                    track_name=None, long_label=None, thick_draw_item='off',
                    short_label=None, track_type=None, visibility=None, priority=None,
                    track_file=None):

        # TODO: Remove the hardcoded "tracks" by the value used as variable from myTrackFolderPath
        data_url = "tracks/%s" % file_path

        if not short_label:
            short_label = self.getShortName(long_label)

        # Replace '_' by ' ', to invert the sanitization mecanism
        # TODO: Find a better way to manage the sanitization of file path
        long_label = long_label.replace("_", " ")
        short_label = short_label.replace("_", " ")

        track_db = TrackDb(
                trackName=track_name,
                longLabel=long_label,
                shortLabel=short_label,
                trackDataURL=data_url,
                trackType=track_type,
                visibility=visibility,
                thickDrawItem=thick_draw_item,
                priority=priority,
        )

        # Return the Bam Track Object
        self.track = Track(
                trackFile=track_file,
                trackDb=track_db,
        )