Mercurial > repos > yating-l > jbrowse_hub
comparison TrackHub.py @ 0:e4f3f2ed4fa5 draft
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
author | yating-l |
---|---|
date | Fri, 10 Mar 2017 13:48:19 -0500 |
parents | |
children | e7c80e9b70ae |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e4f3f2ed4fa5 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import os | |
4 import trackObject | |
5 import utils | |
6 import subprocess | |
7 import string | |
8 import shutil | |
9 from mako.lookup import TemplateLookup | |
10 | |
11 class TrackHub: | |
12 def __init__(self, inputFiles, reference, outputDirect, tool_dir): | |
13 self.input_files = inputFiles | |
14 self.out_path = outputDirect | |
15 self.reference = reference | |
16 self.tool_dir = tool_dir | |
17 try: | |
18 if not self.out_path: | |
19 raise ValueError('empty output path\n') | |
20 if os.path.exists(self.out_path): | |
21 shutil.rmtree(self.out_path) | |
22 os.mkdir(self.out_path) | |
23 self.json = os.path.join(self.out_path, 'json') | |
24 if os.path.exists(self.json): | |
25 shutil.rmtree(self.json) | |
26 os.mkdir(self.json) | |
27 self.raw = os.path.join(self.out_path, 'raw') | |
28 if os.path.exists(self.raw): | |
29 shutil.rmtree(self.raw) | |
30 shutil.move('raw', self.out_path) | |
31 except OSError as e: | |
32 print "Cannot create json folder error({0}): {1}".format(e.errno, e.strerror) | |
33 else: | |
34 print "Create jbrowse folder {}".format(self.out_path) | |
35 | |
36 def createHub(self): | |
37 self.prepareRefseq() | |
38 for input_file in self.input_files: | |
39 self.addTrack(input_file) | |
40 self.indexName() | |
41 self.makeArchive() | |
42 shutil.rmtree(self.out_path) | |
43 print "Success!\n" | |
44 | |
45 def prepareRefseq(self): | |
46 try: | |
47 p = subprocess.Popen([os.path.join(self.tool_dir, 'prepare-refseqs.pl'), '--fasta', self.reference, '--out', self.json]) | |
48 # Wait for process to terminate. | |
49 p.communicate() | |
50 except OSError as e: | |
51 print "Cannot prepare reference error({0}): {1}".format(e.errno, e.strerror) | |
52 | |
53 def addTrack(self, track): | |
54 if track.dataType == 'bam': | |
55 json_file = os.path.join(self.json, "trackList.json") | |
56 bam_track = dict() | |
57 bam_track['type'] = 'JBrowse/View/Track/Alignments2' | |
58 bam_track['label'] = track.fileName | |
59 bam_track['urlTemplate'] = os.path.join('../raw', track.fileName) | |
60 utils.add_tracks_to_json(json_file, bam_track, 'add_tracks') | |
61 print "add bam track\n" | |
62 elif track.dataType == 'bigwig': | |
63 json_file = os.path.join(self.json, "trackList.json") | |
64 bigwig_track = dict() | |
65 bigwig_track['label'] = 'rnaseq' | |
66 bigwig_track['key'] = 'RNA-Seq Coverage' | |
67 bigwig_track['urlTemplate'] = os.path.join('../raw', track.fileName) | |
68 bigwig_track['type'] = 'JBrowse/View/Track/Wiggle/XYPlot' | |
69 bigwig_track['variance_band'] = True | |
70 bigwig_track['style'] = dict() | |
71 bigwig_track['style']['pos_color'] = '#FFA600' | |
72 bigwig_track['style']['neg_color'] = '#005EFF' | |
73 bigwig_track['style']['clip_marker_color'] = 'red' | |
74 bigwig_track['style']['height'] = 100 | |
75 utils.add_tracks_to_json(json_file, bigwig_track, 'add_tracks') | |
76 else: | |
77 gff3_file = os.path.join(self.raw, track.fileName) | |
78 label = track.fileName | |
79 if track.dataType == 'bedSpliceJunctions' or track.dataType == 'gtf': | |
80 p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--config', '{"glyph": "JBrowse/View/FeatureGlyph/Segments"}', '--out', self.json]) | |
81 elif track.dataType == 'gff3-transcript': | |
82 p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--config', '{"transcriptType": "transcript"}', '--out', self.json]) | |
83 elif track.dataType == 'gff3': | |
84 p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--out', self.json]) | |
85 else: | |
86 p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--out', self.json]) | |
87 p.communicate() | |
88 | |
89 def indexName(self): | |
90 p = subprocess.Popen([os.path.join(self.tool_dir, 'generate-names.pl'), '-v', '--out', self.json]) | |
91 p.communicate() | |
92 print "finished name index \n" | |
93 | |
94 def makeArchive(self): | |
95 filename = os.path.basename(self.out_path) | |
96 shutil.make_archive(filename, 'tar', self.out_path) | |
97 | |
98 | |
99 | |
100 | |
101 |