comparison TrackHub.py @ 31:d8049deb0c97 draft

planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit faeedda55e23f1197bc454d3db2d52af29d786e8-dirty
author yating-l
date Fri, 17 Mar 2017 12:28:32 -0400
parents a4a54b925c73
children 046c5bfc0413
comparison
equal deleted inserted replaced
30:daf6a1122200 31:d8049deb0c97
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import os 3 import os
4 import trackObject 4 import subprocess
5 import shutil
5 import utils 6 import utils
6 import subprocess
7 import string
8 import shutil
9 import tempfile
10 7
11 #TODO: package JBrowse file conversion .pl files
12 8
13 class TrackHub: 9 class TrackHub:
14 def __init__(self, inputFiles, reference, outputDirect, tool_dir, genome, extra_files_path): 10 def __init__(self, inputFiles, reference, outputDirect, tool_dir, genome, extra_files_path):
15 self.input_files = inputFiles.tracks 11 self.input_files = inputFiles.tracks
16 self.outfile = outputDirect 12 self.outfile = outputDirect
19 self.reference = reference 15 self.reference = reference
20 self.tool_dir = tool_dir 16 self.tool_dir = tool_dir
21 self.raw = os.path.join(self.out_path, 'raw') 17 self.raw = os.path.join(self.out_path, 'raw')
22 self.json = os.path.join(self.out_path, 'json') 18 self.json = os.path.join(self.out_path, 'json')
23 try: 19 try:
24 if not self.out_path:
25 raise ValueError('empty output path\n')
26 if not os.path.exists(self.out_path):
27 raise ValueError('the output folder has not been created')
28 if os.path.exists(self.json): 20 if os.path.exists(self.json):
29 shutil.rmtree(self.json) 21 shutil.rmtree(self.json)
30 os.makedirs(self.json) 22 os.makedirs(self.json)
31 except OSError as e: 23 except OSError as e:
32 print "Cannot create json folder error({0}): {1}".format(e.errno, e.strerror) 24 print "Cannot create json folder error({0}): {1}".format(e.errno, e.strerror)
37 self.prepareRefseq() 29 self.prepareRefseq()
38 for input_file in self.input_files: 30 for input_file in self.input_files:
39 self.addTrack(input_file) 31 self.addTrack(input_file)
40 self.indexName() 32 self.indexName()
41 self.makeArchive() 33 self.makeArchive()
42 #shutil.rmtree(self.out_path)
43 self.outHtml() 34 self.outHtml()
44 print "Success!\n" 35 print "Success!\n"
45 36
46 def prepareRefseq(self): 37 def prepareRefseq(self):
47 try: 38 try:
57 self.createTrackList() 48 self.createTrackList()
58 json_file = os.path.join(self.json, "trackList.json") 49 json_file = os.path.join(self.json, "trackList.json")
59 bam_track = dict() 50 bam_track = dict()
60 bam_track['type'] = 'JBrowse/View/Track/Alignments2' 51 bam_track['type'] = 'JBrowse/View/Track/Alignments2'
61 bam_track['storeClass'] = 'JBrowse/Store/SeqFeature/BAM' 52 bam_track['storeClass'] = 'JBrowse/Store/SeqFeature/BAM'
62 bam_track['label'] = track['fileName'] 53 bam_track['label'] = track['label']
63 bam_track['urlTemplate'] = os.path.join('../raw', track['fileName']) 54 bam_track['urlTemplate'] = os.path.join('../raw', track['fileName'])
64 bam_track['baiUrlTemplate'] = os.path.join('../raw', track['index']) 55 bam_track['baiUrlTemplate'] = os.path.join('../raw', track['index'])
65 utils.add_tracks_to_json(json_file, bam_track, 'add_tracks') 56 utils.add_tracks_to_json(json_file, bam_track, 'add_tracks')
66 print "add bam track\n" 57 print "add bam track\n"
67 elif track['dataType'] == 'bigwig': 58 elif track['dataType'] == 'bigwig':
68 self.createTrackList() 59 self.createTrackList()
69 json_file = os.path.join(self.json, "trackList.json") 60 json_file = os.path.join(self.json, "trackList.json")
70 bigwig_track = dict() 61 bigwig_track = dict()
71 bigwig_track['label'] = track['fileName'] 62 bigwig_track['label'] = track['label']
72 bigwig_track['urlTemplate'] = os.path.join('../raw', track['fileName']) 63 bigwig_track['urlTemplate'] = os.path.join('../raw', track['fileName'])
73 bigwig_track['type'] = 'JBrowse/View/Track/Wiggle/XYPlot' 64 bigwig_track['type'] = 'JBrowse/View/Track/Wiggle/XYPlot'
74 bigwig_track['storeClass'] = 'JBrowse/Store/SeqFeature/BigWig' 65 bigwig_track['storeClass'] = 'JBrowse/Store/SeqFeature/BigWig'
75 utils.add_tracks_to_json(json_file, bigwig_track, 'add_tracks') 66 utils.add_tracks_to_json(json_file, bigwig_track, 'add_tracks')
76 else: 67 else:
77 gff3_file = os.path.join(self.raw, track['fileName']) 68 gff3_file = os.path.join(self.raw, track['fileName'])
78 label = track['fileName'] 69 label = track['label']
79 if track['dataType'] == 'bedSpliceJunctions' or track['dataType'] == 'gtf': 70 if track['dataType'] == 'bedSpliceJunctions' or track['dataType'] == 'gtf':
80 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--config', '{"glyph": "JBrowse/View/FeatureGlyph/Segments"}', '--out', self.json]) 71 p = subprocess.Popen(['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': 72 elif track['dataType'] == 'gff3_transcript':
82 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--config', '{"transcriptType": "transcript"}', '--out', self.json]) 73 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--config', '{"transcriptType": "transcript"}', '--out', self.json])
83 elif track['dataType'] == 'gff3_mrna': 74 elif track['dataType'] == 'gff3_mrna':
96 shutil.rmtree(self.out_path) 87 shutil.rmtree(self.out_path)
97 88
98 #TODO: this will list all zip files in the filedir and sub-dirs. worked in Galaxy but all list zip files in test-data when 89 #TODO: this will list all zip files in the filedir and sub-dirs. worked in Galaxy but all list zip files in test-data when
99 #run it locally. May need modify 90 #run it locally. May need modify
100 def outHtml(self): 91 def outHtml(self):
101 #htmloutput = tempfile.NamedTemporaryFile(self.outfile, suffix = '.html', bufsize=0, delete=False)
102 with open(self.outfile, 'w') as htmlfile: 92 with open(self.outfile, 'w') as htmlfile:
103 htmlstr = 'The JBrowse Hub is created: <br>' 93 htmlstr = 'The JBrowse Hub is created: <br>'
104 zipfiles = '<li><a href = "%s">Download</a></li>' 94 zipfiles = '<li><a href = "%s">Download</a></li>'
105 filedir_abs = os.path.abspath(self.outfile) 95 filedir_abs = os.path.abspath(self.outfile)
106 filedir = os.path.dirname(filedir_abs) 96 filedir = os.path.dirname(filedir_abs)
110 if file.endswith('.zip'): 100 if file.endswith('.zip'):
111 relative_directory = os.path.relpath(root, filedir) 101 relative_directory = os.path.relpath(root, filedir)
112 relative_file_path = os.path.join(relative_directory, file) 102 relative_file_path = os.path.join(relative_directory, file)
113 htmlstr += zipfiles % relative_file_path 103 htmlstr += zipfiles % relative_file_path
114 104
115 #htmlstr = htmlstr % zipfile
116 htmlfile.write(htmlstr) 105 htmlfile.write(htmlstr)
117 106
118 def createTrackList(self): 107 def createTrackList(self):
119 trackList = os.path.join(self.json, "trackList.json") 108 trackList = os.path.join(self.json, "trackList.json")
120 if not os.path.exists(trackList): 109 if not os.path.exists(trackList):
121 os.mknod(trackList) 110 os.mknod(trackList)
122 #open(trackList,'w').close() 111
123
124 112
125 113
126 114
127 115
128 116