Mercurial > repos > yating-l > jbrowsearchivecreator
comparison TrackHub.py @ 39:4a69515eed63 draft
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit 91271a6c0d39c923f0d460b2979247baa297286b-dirty
| author | yating-l |
|---|---|
| date | Fri, 13 Apr 2018 18:21:35 -0400 |
| parents | d17f629f5486 |
| children | b1553f2a4942 |
comparison
equal
deleted
inserted
replaced
| 38:d17f629f5486 | 39:4a69515eed63 |
|---|---|
| 15 from util import santitizer | 15 from util import santitizer |
| 16 | 16 |
| 17 | 17 |
| 18 class TrackHub: | 18 class TrackHub: |
| 19 def __init__(self, inputFastaFile, outputFile, extra_files_path, tool_directory, trackType): | 19 def __init__(self, inputFastaFile, outputFile, extra_files_path, tool_directory, trackType): |
| 20 | |
| 21 self.rootAssemblyHub = None | |
| 22 | 20 |
| 23 self.mySpecieFolderPath = None | 21 self.mySpecieFolderPath = None |
| 24 | 22 |
| 25 # Store intermediate files, will be removed if not in debug mode | 23 # Store intermediate files, will be removed if not in debug mode |
| 26 self.myTracksFolderPath = None | 24 self.myTracksFolderPath = None |
| 25 | |
| 26 # Store interval files and their tabix index | |
| 27 self.myFinalTracksFolderPath = None | |
| 27 | 28 |
| 28 # Store binary files: Bam, BigWig | 29 # Store binary files: Bam, BigWig |
| 29 self.myBinaryFolderPath = None | 30 self.myBinaryFolderPath = None |
| 30 | 31 |
| 31 self.tool_directory = tool_directory | 32 self.tool_directory = tool_directory |
| 36 self.outputFile = outputFile | 37 self.outputFile = outputFile |
| 37 self.chromSizesFile = None | 38 self.chromSizesFile = None |
| 38 | 39 |
| 39 | 40 |
| 40 # Set all the missing variables of this class, and create physically the folders/files | 41 # Set all the missing variables of this class, and create physically the folders/files |
| 41 self.rootAssemblyHub = self.__createAssemblyHub__(extra_files_path=extra_files_path) | 42 self.__createAssemblyHub__(extra_files_path=extra_files_path) |
| 42 # Init the Datatype | 43 # Init the Datatype |
| 43 Datatype.pre_init(self.reference_genome, self.chromSizesFile, | 44 Datatype.pre_init(self.reference_genome, self.chromSizesFile, |
| 44 self.extra_files_path, self.tool_directory, | 45 self.extra_files_path, self.tool_directory, |
| 45 self.mySpecieFolderPath, self.myTracksFolderPath, self.myFinalTracksFolderPath, self.myBinaryFolderPath, self.trackType) | 46 self.mySpecieFolderPath, self.myTracksFolderPath, self.myBinaryFolderPath, self.trackType) |
| 46 | 47 |
| 47 self._prepareRefseq() | 48 self._prepareRefseq() |
| 48 self.trackList = os.path.join(self.mySpecieFolderPath, "trackList.json") | 49 self.trackList = os.path.join(self.mySpecieFolderPath, "trackList.json") |
| 49 self._createTrackList() | 50 self._createTrackList() |
| 50 | 51 if Datatype.trackType == 'HTMLFeatures': |
| 51 self.myTrackStyle = TrackStyles(self.tool_directory, self.mySpecieFolderPath, self.trackList) | 52 self.myTrackStyle = TrackStyles(self.tool_directory, self.mySpecieFolderPath, self.trackList) |
| 52 #self.cssFolderPath = os.path.join(self.mySpecieFolderPath, 'css') | |
| 53 #self.cssFilePath = os.path.join(self.cssFolderPath, 'custom_track_styles.css') | |
| 54 self.logger = logging.getLogger(__name__) | 53 self.logger = logging.getLogger(__name__) |
| 55 | 54 |
| 56 | 55 |
| 57 | 56 |
| 58 def addTrack(self, trackDbObject): | 57 def addTrack(self, trackDbObject): |
| 59 if trackDbObject['dataType'].lower() == 'bam': | 58 if trackDbObject['dataType'].lower() == 'bam': |
| 60 #new_track = subprocess.Popen(['echo', trackDbObject['options']], stdout=subprocess.PIPE) | |
| 61 #subprocess.call(['add-track-json.pl', json_file], stdin=new_track.stdout) | |
| 62 subtools.add_track_json(self.trackList, trackDbObject['options']) | 59 subtools.add_track_json(self.trackList, trackDbObject['options']) |
| 63 #subtools.add_track_json(self.trackList, trackDbObject['track_json']) | |
| 64 elif trackDbObject['dataType'].lower() == 'bigwig': | 60 elif trackDbObject['dataType'].lower() == 'bigwig': |
| 65 subtools.add_track_json(self.trackList, trackDbObject['options']) | 61 subtools.add_track_json(self.trackList, trackDbObject['options']) |
| 66 else: | 62 else: |
| 67 if trackDbObject['trackType'] == 'HTMLFeatures': | 63 if trackDbObject['trackType'] == 'HTMLFeatures': |
| 68 self._customizeHTMLFeature(trackDbObject) | 64 self._customizeHTMLFeature(trackDbObject) |
| 69 subtools.flatfile_to_json(trackDbObject['trackDataURL'], trackDbObject['dataType'], trackDbObject['trackType'], trackDbObject['trackLabel'], self.mySpecieFolderPath, trackDbObject['options']) | 65 subtools.flatfile_to_json(trackDbObject['trackDataURL'], trackDbObject['dataType'], trackDbObject['trackType'], trackDbObject['trackLabel'], self.mySpecieFolderPath, trackDbObject['options']) |
| 70 # Use Tabix index tracks by default for CanvasFeatures | 66 # Use Tabix index tracks by default for CanvasFeatures |
| 71 # TODO: add support for HTMLFeatures | 67 # TODO: add support for HTMLFeatures |
| 72 else: | 68 else: |
| 73 subtools.generate_tabix_indexed_track(trackDbObject['trackDataURL'], trackDbObject['dataType'], self.myFinalTracksFolderPath) | 69 subtools.generate_tabix_indexed_track(trackDbObject['trackDataURL'], trackDbObject['dataType'], trackDbObject['track'], self.myFinalTracksFolderPath) |
| 74 subtools.add_track_json(self.trackList, trackDbObject['options']) | 70 subtools.add_track_json(self.trackList, trackDbObject['options']) |
| 75 | 71 |
| 76 def terminate(self, debug=False): | 72 def terminate(self, debug=False): |
| 77 """ Write html file """ | 73 """ Write html file """ |
| 78 self._indexName() | 74 self._indexName() |
| 79 if not debug: | 75 if not debug: |
| 80 self._removeRaw() | 76 self._removeRaw() |
| 81 #self._makeArchive() | |
| 82 self._outHtml() | 77 self._outHtml() |
| 83 print "Success!\n" | 78 print "Success!\n" |
| 84 | 79 |
| 85 | 80 |
| 86 def _customizeHTMLFeature(self, trackDbObject): | 81 def _customizeHTMLFeature(self, trackDbObject): |
| 105 if not os.path.exists(self.trackList): | 100 if not os.path.exists(self.trackList): |
| 106 os.mknod(self.trackList) | 101 os.mknod(self.trackList) |
| 107 | 102 |
| 108 def _prepareRefseq(self): | 103 def _prepareRefseq(self): |
| 109 subtools.prepare_refseqs(self.reference_genome.false_path, self.mySpecieFolderPath) | 104 subtools.prepare_refseqs(self.reference_genome.false_path, self.mySpecieFolderPath) |
| 110 #try: | |
| 111 #print os.path.join(self.tool_dir, 'prepare-refseqs.pl') + ", '--fasta', " + self.reference +", '--out', self.json])" | |
| 112 #subprocess.call(['prepare-refseqs.pl', '--fasta', self.reference_genome.false_path, '--out', self.mySpecieFolderPath]) | |
| 113 #except OSError as e: | |
| 114 #print "Cannot prepare reference error({0}): {1}".format(e.errno, e.strerror) | |
| 115 | 105 |
| 116 def _indexName(self): | 106 def _indexName(self): |
| 117 #subprocess.call(['generate-names.pl', '-v', '--out', self.mySpecieFolderPath]) | |
| 118 subtools.generate_names(self.mySpecieFolderPath) | 107 subtools.generate_names(self.mySpecieFolderPath) |
| 119 print "finished name index \n" | 108 print "finished name index \n" |
| 120 | 109 |
| 121 def _outHtml(self): | 110 def _outHtml(self): |
| 122 mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates')], | 111 mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates')], |
| 123 output_encoding='utf-8', encoding_errors='replace') | 112 output_encoding='utf-8', encoding_errors='replace') |
| 124 htmlTemplate = mylookup.get_template("display.txt") | 113 htmlTemplate = mylookup.get_template("display.txt") |
| 125 | 114 |
| 126 with open(self.outputFile, 'w') as htmlfile: | 115 with open(self.outputFile, 'w') as htmlfile: |
| 127 htmlMakoRendered = htmlTemplate.render( | 116 htmlMakoRendered = htmlTemplate.render( |
| 128 species_folder = os.path.relpath(self.mySpecieFolderPath, self.extra_files_path), | 117 jbrowse_hub_name = self.reference_genome.assembly_id, |
| 129 trackList = os.path.relpath(self.trackList, self.extra_files_path) | 118 trackList = os.path.relpath(self.trackList, self.extra_files_path) |
| 130 ) | 119 ) |
| 131 htmlfile.write(htmlMakoRendered) | 120 htmlfile.write(htmlMakoRendered) |
| 132 #with open(self.outputFile, 'w') as htmlfile: | |
| 133 # htmlstr = 'The new Organism "%s" is created on Apollo: <br>' % self.genome_name | |
| 134 # jbrowse_hub = '<li><a href = "%s" target="_blank">View JBrowse Hub on Apollo</a></li>' % host_name | |
| 135 # htmlstr += jbrowse_hub | |
| 136 # htmlfile.write(htmlstr) | |
| 137 | |
| 138 | |
| 139 | 121 |
| 140 def __createAssemblyHub__(self, extra_files_path): | 122 def __createAssemblyHub__(self, extra_files_path): |
| 141 # Get all necessaries infos first | 123 # Get all necessaries infos first |
| 142 # 2bit file creation from input fasta | 124 # 2bit file creation from input fasta |
| 143 | |
| 144 # baseNameFasta = os.path.basename(fasta_file_name) | |
| 145 # suffixTwoBit, extensionTwoBit = os.path.splitext(baseNameFasta) | |
| 146 # nameTwoBit = suffixTwoBit + '.2bit' | |
| 147 twoBitFile = tempfile.NamedTemporaryFile(bufsize=0) | 125 twoBitFile = tempfile.NamedTemporaryFile(bufsize=0) |
| 148 subtools.faToTwoBit(self.reference_genome.false_path, twoBitFile.name) | 126 subtools.faToTwoBit(self.reference_genome.false_path, twoBitFile.name) |
| 149 | 127 |
| 150 # Generate the twoBitInfo | 128 # Generate the twoBitInfo |
| 151 twoBitInfoFile = tempfile.NamedTemporaryFile(bufsize=0) | 129 twoBitInfoFile = tempfile.NamedTemporaryFile(bufsize=0) |
| 152 subtools.twoBitInfo(twoBitFile.name, twoBitInfoFile.name) | 130 subtools.twoBitInfo(twoBitFile.name, twoBitInfoFile.name) |
| 153 | 131 |
| 154 # Then we get the output to generate the chromSizes | 132 # Then we get the output to generate the chromSizes |
| 155 self.chromSizesFile = tempfile.NamedTemporaryFile(bufsize=0, suffix=".chrom.sizes") | 133 self.chromSizesFile = tempfile.NamedTemporaryFile(bufsize=0, suffix=".chrom.sizes") |
| 156 subtools.sortChromSizes(twoBitInfoFile.name, self.chromSizesFile.name) | 134 subtools.sortChromSizes(twoBitInfoFile.name, self.chromSizesFile.name) |
| 157 | 135 |
| 158 # We can get the biggest scaffold here, with chromSizesFile | 136 # We can get the biggest scaffold here, with chromSizesFile |
| 159 with open(self.chromSizesFile.name, 'r') as chrom_sizes: | 137 with open(self.chromSizesFile.name, 'r') as chrom_sizes: |
| 160 # TODO: Check if exists | 138 # TODO: Check if exists |
| 161 self.default_pos = chrom_sizes.readline().split()[0] | 139 self.default_pos = chrom_sizes.readline().split()[0] |
| 162 | 140 |
| 163 # TODO: Manage to put every fill Function in a file dedicated for reading reasons | 141 # Create the specie folder, use generic name "myHub", to support display JBrowse with Galaxy display application |
| 164 # Create the root directory | 142 mySpecieFolderPath = os.path.join(extra_files_path, "myHub") |
| 165 myHubPath = os.path.join(extra_files_path, "myHub") | |
| 166 if not os.path.exists(myHubPath): | |
| 167 os.makedirs(myHubPath) | |
| 168 | |
| 169 # Create the specie folder | |
| 170 mySpecieFolderPath = os.path.join(myHubPath, self.genome_name) | |
| 171 if not os.path.exists(mySpecieFolderPath): | 143 if not os.path.exists(mySpecieFolderPath): |
| 172 os.makedirs(mySpecieFolderPath) | 144 os.makedirs(mySpecieFolderPath) |
| 173 self.mySpecieFolderPath = mySpecieFolderPath | 145 self.mySpecieFolderPath = mySpecieFolderPath |
| 174 | 146 |
| 175 # Create the folder named 'raw' inside the specie folder to place raw files | 147 # Create the folder named 'raw' inside the specie folder to place raw files |
| 186 | 158 |
| 187 myBinaryFolderPath = os.path.join(mySpecieFolderPath, 'bbi') | 159 myBinaryFolderPath = os.path.join(mySpecieFolderPath, 'bbi') |
| 188 if not os.path.exists(myBinaryFolderPath): | 160 if not os.path.exists(myBinaryFolderPath): |
| 189 os.makedirs(myBinaryFolderPath) | 161 os.makedirs(myBinaryFolderPath) |
| 190 self.myBinaryFolderPath = myBinaryFolderPath | 162 self.myBinaryFolderPath = myBinaryFolderPath |
| 191 | |
| 192 return myHubPath |
