Mercurial > repos > rmarenco > hubarchivecreator
comparison BigBed.py @ 24:fcc1021bd496 draft
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 93e2e2fb59f99677425104a80c17f665fa7b2b4a-dirty
author | yating-l |
---|---|
date | Wed, 31 May 2017 11:35:16 -0400 |
parents | |
children | df42241d3731 |
comparison
equal
deleted
inserted
replaced
23:2677f1899aa8 | 24:fcc1021bd496 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import os | |
4 import shutil | |
5 from subprocess import Popen, PIPE | |
6 import re | |
7 | |
8 # Internal dependencies | |
9 from Datatype import Datatype | |
10 | |
11 class BigBed(Datatype): | |
12 """ Configurations for creating the bigBed evidence track """ | |
13 | |
14 def __init__(self, input_bigbed_path, data_bigbed): | |
15 super(BigBed, self).__init__() | |
16 | |
17 self.track = None | |
18 | |
19 self.input_bigbed_path = input_bigbed_path | |
20 self.name_bigbed = data_bigbed["name"] | |
21 self.priority = data_bigbed["order_index"] | |
22 self.track_color = data_bigbed["track_color"] | |
23 self.group_name = data_bigbed["group_name"] | |
24 | |
25 track_name = "".join((self.name_bigbed, ".bigbed")) | |
26 if data_bigbed["long_label"]: | |
27 self.long_label = data_bigbed["long_label"] | |
28 else: | |
29 self.long_label = self.name_bigbed | |
30 | |
31 bigbed_file_path = os.path.join(self.myTrackFolderPath, track_name) | |
32 | |
33 track_type = self.determine_track_type(input_bigbed_path) | |
34 | |
35 shutil.copy(self.input_bigbed_path, bigbed_file_path) | |
36 | |
37 # Create the Track Object | |
38 self.createTrack(file_path=track_name, | |
39 track_name=track_name, | |
40 long_label=self.long_label, | |
41 track_type=track_type, | |
42 visibility='hide', | |
43 priority=self.priority, | |
44 track_file=bigbed_file_path, | |
45 track_color=self.track_color, | |
46 group_name=self.group_name) | |
47 | |
48 print "- BigBed %s created" % self.name_bigbed | |
49 | |
50 | |
51 def determine_track_type(self, bb_file): | |
52 """ | |
53 Determine the number of standard and extra fields using bigBedSummary | |
54 | |
55 Implementation of reading from stdout is based on a Stackoverflow post: | |
56 http://stackoverflow.com/questions/2715847/python-read-streaming-input-from-subprocess-communicate | |
57 | |
58 :param bb_file: path to a bigBed file | |
59 | |
60 :returns: the bigBed track type | |
61 """ | |
62 | |
63 cmd_ph = Popen(["bigBedSummary", "-fields", bb_file, "stdout"], | |
64 stdout=PIPE, bufsize=1) | |
65 | |
66 pattern = r"(\d+) bed definition fields, (\d+) total fields" | |
67 | |
68 with cmd_ph.stdout: | |
69 for line in iter(cmd_ph.stdout.readline, b''): | |
70 match = re.match(pattern, line) | |
71 | |
72 if match: | |
73 extra_mark = "." if match.group(1) == match.group(2) else "+" | |
74 bed_type = "bigBed %s %s" % (match.group(1), extra_mark) | |
75 break | |
76 | |
77 cmd_ph.wait() | |
78 | |
79 return bed_type |