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