diff --git a/lib/galaxy/datatypes/text.py b/lib/galaxy/datatypes/text.py index edae572402a..7a99d6f2ea5 100644 --- a/lib/galaxy/datatypes/text.py +++ b/lib/galaxy/datatypes/text.py @@ -17,62 +17,6 @@ from galaxy.util import nice_size, string_as_bool log = logging.getLogger(__name__) -class Biom(Text): - file_ext = "biom" - - def set_peek(self, dataset, is_multi_byte=False): - if not dataset.dataset.purged: - dataset.peek = get_file_peek(dataset.file_name, - is_multi_byte=is_multi_byte) - dataset.blurb = "Biological Observation Matrix" - else: - dataset.peek = 'file does not exist' - dataset.blurb = 'file purged from disc' - - def display_peek(self, dataset): - try: - return dataset.peek - except: - return "BIOM file (%s)" % (nice_size(dataset.get_size())) - - -class Biom1(Biom): - edam_format = "format_3464" - file_ext = "biom1" - - def set_peek(self, dataset, is_multi_byte=False): - super(Biom1, self).set_peek(dataset, is_multi_byte) - if not dataset.dataset.purged: - dataset.blurb = "Biological Observation Matrix v1" - - def sniff(self, filename): - return self._looks_like_biom(filename) - - def _looks_like_biom(self, filepath, check_limit_size=104857600): - """ - @param filepath: [str] The path to the evaluated file. - @param check_limit_size: [int] The maximum size of the checked file (in - bytes). - @note: If the size is superior than this number the format cannot be - validated. - """ - is_biom = False - try: - if os.path.getsize(filepath) < check_limit_size: - biom = json.load(open(filepath, "r")) - is_biom = True - biom_expected_fields = ["id", "format", "format_url", "type", - "generated_by", "date", "rows", - "columns", "matrix_type", - "matrix_element_type", "shape", "data"] - for expected_field in biom_expected_fields: - if not expected_field in biom: - is_biom = False - except: - is_biom = False - return is_biom - - class Json( Text ): edam_format = "format_3464" file_ext = "json" @@ -178,6 +122,46 @@ class Ipynb( Json ): pass +class Biom1(Json): + file_ext = "biom1" + + def set_peek(self, dataset, is_multi_byte=False): + super(Biom1, self).set_peek(dataset, is_multi_byte) + if not dataset.dataset.purged: + dataset.blurb = "Biological Observation Matrix v1" + + def sniff(self, filename): + is_biom = False + if self._looks_like_json( filename ): + is_biom = self._looks_like_biom(filename) + return is_biom + + def _looks_like_biom(self, filepath, load_size=50000): + """ + @param filepath: [str] The path to the evaluated file. + @param load_size: [int] The size of the file block load in RAM (in + bytes). + """ + is_biom = False + segment_size = int(load_size / 2) + try: + with open(filepath, "r") as fh: + prev_str = "" + segment_str = fh.read(segment_size) + if segment_str.strip().startswith('{'): + while segment_str and not is_biom: + current_str = prev_str + segment_str + if '"format"' in current_str: + current_str = re.sub(r'\s', '', current_str) + if '"format":"BiologicalObservationMatrix' in current_str: + is_biom = True + prev_str = segment_str + segment_str = fh.read(segment_size) + except: + pass + return is_biom + + class Obo( Text ): """ OBO file format description