Mercurial > repos > yating-l > hubarchivecreator
comparison Datatype.py @ 0:f493979f1408 draft default tip
planemo upload for repository https://github.com/Yating-L/hubarchivecreator-test commit 48b59e91e2dcc2e97735ee35d587960cbfbce932-dirty
author | yating-l |
---|---|
date | Wed, 21 Dec 2016 12:13:04 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f493979f1408 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf8 -*- | |
3 | |
4 """ | |
5 Super Class of the managed datatype | |
6 """ | |
7 | |
8 import os | |
9 import tempfile | |
10 | |
11 from util import subtools | |
12 from Track import Track | |
13 from TrackDb import TrackDb | |
14 | |
15 | |
16 class Datatype(object): | |
17 | |
18 twoBitFile = None | |
19 | |
20 input_fasta_file = None | |
21 extra_files_path = None | |
22 tool_directory = None | |
23 | |
24 mySpecieFolderPath = None | |
25 myTrackFolderPath = None | |
26 | |
27 twoBitFile = None | |
28 chromSizesFile = None | |
29 | |
30 track = None | |
31 | |
32 def __init__(self): | |
33 | |
34 not_init_message = "The {0} is not initialized." \ | |
35 "Did you use pre_init static method first?" | |
36 if Datatype.input_fasta_file is None: | |
37 raise TypeError(not_init_message.format('reference genome')) | |
38 if Datatype.extra_files_path is None: | |
39 raise TypeError(not_init_message.format('track Hub path')) | |
40 if Datatype.tool_directory is None: | |
41 raise TypeError(not_init_message.format('tool directory')) | |
42 | |
43 @staticmethod | |
44 def pre_init(reference_genome, two_bit_path, chrom_sizes_file, | |
45 extra_files_path, tool_directory, specie_folder, tracks_folder): | |
46 Datatype.extra_files_path = extra_files_path | |
47 Datatype.tool_directory = tool_directory | |
48 | |
49 # TODO: All this should be in TrackHub and not in Datatype | |
50 Datatype.mySpecieFolderPath = specie_folder | |
51 Datatype.myTrackFolderPath = tracks_folder | |
52 | |
53 Datatype.input_fasta_file = reference_genome | |
54 | |
55 # 2bit file creation from input fasta | |
56 Datatype.twoBitFile = two_bit_path | |
57 Datatype.chromSizesFile = chrom_sizes_file | |
58 | |
59 @staticmethod | |
60 def get_largest_scaffold_name(self): | |
61 # We can get the biggest scaffold here, with chromSizesFile | |
62 with open(Datatype.chromSizesFile.name, 'r') as chrom_sizes: | |
63 # TODO: Check if exists | |
64 return chrom_sizes.readline().split()[0] | |
65 | |
66 # TODO: Rename for PEP8 | |
67 def getShortName( self, name_to_shortify ): | |
68 # Slice to get from Long label the short label | |
69 short_label_slice = slice(0, 15) | |
70 | |
71 return name_to_shortify[short_label_slice] | |
72 | |
73 # TODO: Better handle parameters, use heritance mecanism | |
74 # TODO: Use default parameters for some, like visibility | |
75 def createTrack(self, | |
76 file_path=None, | |
77 track_name=None, long_label=None, thick_draw_item='off', | |
78 short_label=None, track_type=None, visibility=None, priority=None, | |
79 track_file=None, track_color='#000000', group_name="Default"): | |
80 | |
81 # TODO: Remove the hardcoded "tracks" by the value used as variable from myTrackFolderPath | |
82 data_url = "tracks/%s" % file_path | |
83 | |
84 if not short_label: | |
85 short_label = self.getShortName(long_label) | |
86 | |
87 # Replace '_' by ' ', to invert the sanitization mecanism | |
88 # TODO: Find a better way to manage the sanitization of file path | |
89 long_label = long_label.replace("_", " ") | |
90 short_label = short_label.replace("_", " ") | |
91 | |
92 #TODO: Check if rgb or hexa | |
93 # Convert hexa to rgb array | |
94 hexa_without_sharp = track_color.lstrip('#') | |
95 rgb_array = [int(hexa_without_sharp[i:i+2], 16) for i in (0, 2, 4)] | |
96 rgb_ucsc = ','.join(map(str, rgb_array)) | |
97 | |
98 track_db = TrackDb( | |
99 trackName=track_name, | |
100 longLabel=long_label, | |
101 shortLabel=short_label, | |
102 trackDataURL=data_url, | |
103 trackType=track_type, | |
104 visibility=visibility, | |
105 thickDrawItem=thick_draw_item, | |
106 priority=priority, | |
107 track_color=rgb_ucsc, | |
108 group_name=group_name | |
109 ) | |
110 | |
111 # Return the Bam Track Object | |
112 self.track = Track( | |
113 trackFile=track_file, | |
114 trackDb=track_db, | |
115 ) |