test_that("read_biom", {
  
  skip_on_cran()
  
  f <- expect_silent(write_biom(hmp5, tempfile(), 'json'))
  g <- expect_silent(write_biom(min5, tempfile(), 'json'))
  expect_silent(read_biom(src = f))
  expect_silent(read_biom(src = g))
  unlink(c(f, g))
  
  f <- expect_silent(write_biom(hmp5, tempfile(fileext = '.gz'), 'tab'))
  g <- expect_silent(write_biom(min5, tempfile(fileext = '.gz'), 'tab'))
  expect_silent(read_biom(src = f))
  expect_silent(read_biom(src = g))
  unlink(c(f, g))
  
  expect_error(read_biom_hdf5(tempfile()))
  expect_error(parse_json_metadata(list(columns = list(list(metadata = c('a' = 1, 'a' = 1))))))
  
  json <- list(matrix_type = 'dense', rows = list(list(id='t1')), columns = list(list(id='s1')), data = list(1))
  expect_silent(parse_json_counts(json))
  expect_error(parse_json_counts(list(matrix_type = 'invalid')))
  expect_error(parse_json_counts(list(matrix_type = 'dense', data = list())))
  
  
  json <- list(generated_by = 'MicrobiomeDB', rows = list(list(id='Archaea;Euryarchaeota', metadata = list())))
  expect_silent(parse_json_taxonomy(json))
  json <- list(rows = list(list(id='Unc01pdq', metadata = list(list("Bacteria", "__Fusobacteriota")))))
  expect_silent(parse_json_taxonomy(json))
  expect_silent(parse_json_taxonomy(list(rows = list(list(id='t1')))))
  json <- list(rows = list(list(metadata=list(taxonomy='t1')), list(metadata=list(taxonomy='t2;t3'))))
  expect_silent(parse_json_taxonomy(json))
  
  expect_null(parse_json_sequences(list(rows = list(list(metadata=list())))))
  
  
  skip_if_not_installed('h5lite')
  
  # https://github.com/biocore/biom-format/blob/master/examples/rich_sparse_otu_table_hdf5.biom
  x <- expect_silent(read_biom(test_path('inputs/biom.hdf5')))
  expect_identical(x$samples, paste0('Sample', 1:6))
  expect_identical(x$otus,    paste0('GG_OTU_', 1:5))
  expect_identical(x$ranks,   c('.otu', 'Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Species'))
  expect_identical(x$fields,  c('.sample', 'BODY_SITE', 'BarcodeSequence', 'Description', 'LinkerPrimerSequence'))
  
  f <- expect_silent(write_biom(hmp5, tempfile(), 'hdf5'))
  g <- expect_silent(write_biom(min5, tempfile(), 'hdf5'))
  expect_silent(read_biom(src = f))
  expect_silent(read_biom(src = g))
  unlink(c(f, g))
})
