2章3節 補遺 Bioformatsをコマンドラインから使う (書きかけ、三浦)

連載で紹介したLOCI Bio-Formatsはさまざまな画像ファイル形式を開くのに大変便利な機能を有しており、たいへん便利なツールである。元々はOME(Open Microscopy Environment)の一部として始まったプロジェクトである。

顕微鏡会社が配布するソフトは往々にして独自の画像フォーマットを使用するため、これらの様々なフォーマットの間での互換性に問題があり、さらにいえば、自分で解析しようとした時にそもそも画像を開くことができない、という、まさにどうしようもない問題が横たわっている。こうしたファイル互換性の問題に対して、かつては顕微鏡会社にファイルを一般的なフォーマットでエクスポートする機能の追加を要請するか、ファイルの仕様の書類を送ってもらって自分でファイルを開くためのプログラムを書く、より悲惨なことにファイルの仕様が社外秘になっている場合などは、自分でファイルのヘッダーをhex viewerなどを使って読んでハッキングするかしかなかったのであるが、OMEという複数の研究室にまたがる共同作業により、こうした努力がまとめあげられていった。このBioFormatライブラリの使用が一般的になるにつれ、顕微鏡やカメラの企業もBioFormatsのグループにファイルの仕様をすすんで提供するようになりつつある。

こうした活動のもうひとつの側面はよりユニバーサルな顕微鏡画像フォーマットの開発にある。どうせならばバラバラのフォーマットをすべてまとめあげて一つのフォーマットとして扱えないか?ということで、汎用性のあるフォーマットが考案されたのである。このフォーマットはOME-TIFFと呼ばれ、広く使われている画像のTIFF形式に、標準化したメタデータを添えたものである。このメタデータが鍵である。というのも、顕微鏡で取得した画像には、解像度、励起光周波数、時間間隔、Z方向の間隔、といったさまざまなメタデータが含まれている。このメタデータの形式が顕微鏡の会社ごとにもっとも異なっている点であり、ある会社ではAのようによぶパラメータが、別の会社ではCと呼ばれている、というような一貫性のない状況にあるため、これらの情報を標準化することで、初めて統一して扱うことができるようになる。OME-TIFFのメタデータはXML形式で記述されており、プログラミングでも扱いやすい形式になっている。

一方、注意したいのは、こうしたOME-TIFFの形式の開発に平行して、HDF5とよばれる形式も一般的になりつつあることである。HDF形式では、これまでメタデータと呼ばれていた部分と、画像そのものの部分をデータとして平等に扱っている形式である。目下、OME-TIFFとHDF5の間に互換性は少ないが、管見ながらいずれはHDF5に収斂していくのではないかとおもわれる。

BioFormatsをImageJマクロから使う

(準備中)

BioFormatsをJythonから使う

(準備中)

BioFormatsのコマンドライン諸機能の概要

さて、この"BioFormats"ツールは、コマンドラインからスタンドアローンでも使うことが可能であり、とくにサーバーサイドで画像の形式を変換するのに便利である。シェルスクリプトでの使用に適している。

以下、その使い方を紹介するつもりである。BioFormatsは画像形式の変換だけではなく、さまざまな機能を有している。まずそれらの諸機能を簡単に説明し、次にセットアップの説明、最後に画像に関する情報を得る方法、形式の変換の仕方を説明する。

showinf

画像に関する情報を取得し、コンソールに出力する。また、画像を表示する。

ijview

ImageJで画像を表示する。

bfconvert

画像のファイル形式を変換する。実際にコマンドラインからよく使われるのはこの機能と、showinfである。

formatlist

BioFormatsがサポートしているファイル形式のリストをHTML、テキストあるいはXMLの形式で出力する。

xmlindent

XMLを清書するための機能。インデントを揃える、など。シンタックスエラーがあっても作動する。

xmlvalid

OMEのメタデータ形式(OME-XML)が正しいかどうかチェックする機能。

tiffcomment

TIFF形式のファイルの最初のIFDエントリを出力する。OME-TIFF形式のファイルのメタデータをチェックするのに便利。

BioFormatsをコマンドラインから使用するためのセットアップ

LOCI BioFormatsのコマンドラインツールをダウンロードする。

解凍して、実行形式のファイル群を通常おいている場所に移動する。たとえば~/binフォルダ以下など。

なおこのフォルダはPATH変数に含まれている必要がある。OSXの場合であれば~/.profileあるいは~/.bash_profileなどに記載されている必要がある。たとえば~/bin/以下であれば

export PATH=$PATH:~/bin 

など。

まず次のようなコマンドでセットアップがうまくいったかどうかチェックする。

bfconvert --help 

詳細なヘルプがコンソールに出力されるはずである。

コマンドラインで画像のメタデータを読み出す

サンプル画像をまずダウンロードする。[EMBL > Samples > save centrosomes.lsm]によってローカルに保存できる。これはZeissのlsm形式のデータである。次のコマンドを実行してみよう。

 showinf centrosomes.lsm 

初回はJava自体が立ち上がるのに少々時間がかかるかもしれない。コンソールに、下に貼り付けるような画像ファイルのメタデータが出力されることがわかるだろう。撮影条件など、細かい情報が記載されていることがわかる。時系列のデータなどであれば、たとえば実際の撮影時間を確認し、微妙な差を検討したりすることも可能である。

こうした情報とともに、画像自体がデスクトップ上に表示される。あまりよいインターフェースではないので、画像を見る必要がないならばオプションを加え

showinf -nopix centrosomes.lsm 

とすればよい。他にもさまざまなオプションがある。

showinf -help 

で確認するとさまざまな機能があることがわかる。

To test read a file in any format, run:   showinf file [-nopix] [-nocore] [-nometa] [-thumbs] [-minmax]     [-merge] [-nogroup] [-stitch] [-separate] [-expand] [-omexml]     [-normalize] [-fast] [-debug] [-range start end] [-series num]     [-swap inputOrder] [-shuffle outputOrder] [-map id] [-preload]     [-crop x,y,w,h] [-autoscale] [-novalid] [-omexml-only] [-no-sas]     [-format Format]      -version: print the library version and exit         file: the image file to read       -nopix: read metadata only, not pixels      -nocore: do not output core metadata      -nometa: do not parse format-specific metadata table    -nofilter: do not filter metadata fields      -thumbs: read thumbnails instead of normal pixels      -minmax: compute min/max statistics       -merge: combine separate channels into RGB image     -nogroup: force multi-file datasets to be read as individual files      -stitch: stitch files with similar names    -separate: split RGB image into separate channels      -expand: expand indexed color to RGB      -omexml: populate OME-XML metadata   -normalize: normalize floating point images*        -fast: paint RGB images as quickly as possible*       -debug: turn on debugging output       -range: specify range of planes to read (inclusive)      -series: specify which image series to read        -swap: override the default input dimension order     -shuffle: override the default output dimension order         -map: specify file on disk to which name should be mapped     -preload: pre-read entire file into a buffer; significantly               reduces the time required to read the images, but               requires more memory        -crop: crop images before displaying; argument is 'x,y,w,h'   -autoscale: used in combination with '-fast' to automatically adjust               brightness and contrast     -novalid: do not perform validation of OME-XML -omexml-only: only output the generated OME-XML      -no-sas: do not output OME-XML StructuredAnnotation elements      -format: read file with a particular reader (e.g., ZeissZVI) 

画像centrosomes.lsmのメタデータ

Checking file format [Zeiss Laser-Scanning Microscopy] Initializing reader Reading LSM metadata for series #0 Unknown LaserMedium value '' will be stored as "Other" Unknown LaserType value 'DPSS 561-10' will be stored as "Other" Initialization took 0.184s  Reading core metadata Filename = centrosomes.lsm Series count = 1 Series #0 :     Image count = 24     RGB = false (1)     Interleaved = false     Indexed = true (false color)     Width = 512     Height = 512     SizeZ = 12     SizeT = 1     SizeC = 2     Thumbnail size = 128 x 128     Endianness = intel (little)     Dimension order = XYCZT (uncertain)     Pixel type = uint16     Valid bits per pixel = 12     Metadata complete = false     Thumbnail series = false     -----     Plane #0 <=> Z 0, C 0, T 0     Plane #10 <=> Z 5, C 0, T 0     Plane #11 <=> Z 5, C 1, T 0     Plane #12 <=> Z 6, C 0, T 0     Plane #13 <=> Z 6, C 1, T 0     Plane #14 <=> Z 7, C 0, T 0     Plane #23 <=> Z 11, C 1, T 0   Reading pixel data (0-23)     ************ no LUT for plane #0 ************     ************ no LUT for plane #1 ************     ************ no LUT for plane #2 ************     ************ no LUT for plane #3 ************     ************ no LUT for plane #4 ************     ************ no LUT for plane #5 ************     ************ no LUT for plane #6 ************     ************ no LUT for plane #7 ************     ************ no LUT for plane #8 ************     ************ no LUT for plane #9 ************     ************ no LUT for plane #10 ************     ************ no LUT for plane #11 ************     ************ no LUT for plane #12 ************     ************ no LUT for plane #13 ************     ************ no LUT for plane #14 ************     ************ no LUT for plane #15 ************     ************ no LUT for plane #16 ************     ************ no LUT for plane #17 ************     ************ no LUT for plane #18 ************     ************ no LUT for plane #19 ************     ************ no LUT for plane #20 ************     ************ no LUT for plane #21 ************     ************ no LUT for plane #22 ************     ************ no LUT for plane #23 ************     Read 24/24 planes (100%) [done] 0.674s elapsed (28.083334ms per plane)  Launching image viewer  Reading global metadata BeamSplitter #1 Acquire: true DataChannel #1 Acquire: true DataChannel #2 Acquire: true DetectionChannel #1 Acquire: true DetectionChannel #2 Acquire: true IlluminationChannel #1 Acquire: true IlluminationChannel #2 Acquire: true Laser #1 Acquire: true Laser #2 Acquire: true Recording #1 Acquire: true Track #1 Acquire: true Track #2 Acquire: true X position for position #1: 0.0 Y position for position #1: 0.0 Z position for position #1: 0.0  Reading metadata BeamSplitter #1 Filter: NoneLSM BeamSplitter #1 Filter Set: FW1 BeamSplitter #1 Name: FW1 ChannelName0: Ch1-T1 DataChannel #1 Acquire: 1 DataChannel #1 Bits Per Sample: 12 DataChannel #1 Color: 16711680 DataChannel #1 Name: Ch1 DataChannel #1 Ratio Const. 1: 0.0 DataChannel #1 Ratio Const. 2: 0.0 DataChannel #1 Ratio Const. 3: 1.0 DataChannel #1 Ratio Const. 4: 0.0 DataChannel #1 Ratio Const. 5: 0.0 DataChannel #1 Ratio Const. 6: 0.0 DataChannel #1 Ratio Track 1: 0 DataChannel #1 Ratio Track 2: 0 DataChannel #1 Ratio Type: 0 DataChannel #1 Sample Type: 2 DataChannel #2 Acquire: 1 DataChannel #2 Bits Per Sample: 12 DataChannel #2 Color: 255 DataChannel #2 Name: Ch2 DataChannel #2 Ratio Const. 1: 0.0 DataChannel #2 Ratio Const. 2: 0.0 DataChannel #2 Ratio Const. 3: 1.0 DataChannel #2 Ratio Const. 4: 0.0 DataChannel #2 Ratio Const. 5: 0.0 DataChannel #2 Ratio Const. 6: 0.0 DataChannel #2 Ratio Track 1: 0 DataChannel #2 Ratio Track 2: 0 DataChannel #2 Ratio Type: 0 DataChannel #2 Sample Type: 2 DataType: varying data types DataType2: original scan data DetectionChannel #1 Acquire: 1 DetectionChannel #1 Amplifier Gain: 1.0 DetectionChannel #1 Amplifier Gain B/C 1: 0.0 DetectionChannel #1 Amplifier Gain B/C 2: 0.0 DetectionChannel #1 Amplifier Name: Amplifier1 DetectionChannel #1 Amplifier Offset: 0.0 DetectionChannel #1 Amplifier Offset B/C 1: 0.0 DetectionChannel #1 Amplifier Offset B/C 2: 0.0 DetectionChannel #1 Detection Channel Name: Ch1 DetectionChannel #1 Detector Gain: 650.0 DetectionChannel #1 Detector Gain B/C 1: 0.0 DetectionChannel #1 Detector Gain B/C 2: 0.0 DetectionChannel #1 Detector Name: Pmt1 DetectionChannel #1 Dye Name: Hoechst 33342 (buffer) DetectionChannel #1 Filter Set Name: EF1 DetectionChannel #1 Integrator Name: Integrator1 DetectionChannel #1 Pinhole Diameter: 110.2758 DetectionChannel #1 Pinhole Name: PH1 DetectionChannel #1 SPI Wavelength End: 518.32 DetectionChannel #1 SPI Wavelength Start: 410.00000000000006 DetectionChannel #1 Spectral Scan Channels: 32 DetectionChannel #2 Acquire: 1 DetectionChannel #2 Amplifier Gain: 1.0 DetectionChannel #2 Amplifier Gain B/C 1: 0.0 DetectionChannel #2 Amplifier Gain B/C 2: 0.0 DetectionChannel #2 Amplifier Name: Amplifier1 DetectionChannel #2 Amplifier Offset: 0.0 DetectionChannel #2 Amplifier Offset B/C 1: 0.0 DetectionChannel #2 Amplifier Offset B/C 2: 0.0 DetectionChannel #2 Detection Channel Name: Ch2 DetectionChannel #2 Detector Gain: 800.0 DetectionChannel #2 Detector Gain B/C 1: 0.0 DetectionChannel #2 Detector Gain B/C 2: 0.0 DetectionChannel #2 Detector Name: Pmt2 DetectionChannel #2 Dye Name: Alexa Fluor 647 DetectionChannel #2 Filter Set Name: EF2 DetectionChannel #2 Integrator Name: Integrator2 DetectionChannel #2 Pinhole Diameter: 110.2758 DetectionChannel #2 Pinhole Name: PH2 DetectionChannel #2 SPI Wavelength End: 755.0000000000001 DetectionChannel #2 SPI Wavelength Start: 655.5100000000001 DetectionChannel #2 Spectral Scan Channels: 32 DimensionChannels: 2 DimensionM: 1 DimensionP: 1 DimensionX: 512 DimensionY: 512 DimensionZ: 12 DisplayAspectTime: 1.0 DisplayAspectX: 1.0 DisplayAspectY: 1.0 DisplayAspectZ: 1.0 IlluminationChannel #1 Acquire: 1 IlluminationChannel #1 Name: 405 IlluminationChannel #1 Power: 0.2 IlluminationChannel #1 Power B/C 1: 0.0 IlluminationChannel #1 Power B/C 2: 0.0 IlluminationChannel #1 Wavelength: 405.00000000000006 IlluminationChannel #2 Acquire: 1 IlluminationChannel #2 Name: 633 IlluminationChannel #2 Power: 1.5 IlluminationChannel #2 Power B/C 1: 0.0 IlluminationChannel #2 Power B/C 2: 0.0 IlluminationChannel #2 Wavelength: 633.0 Laser #1 Acquire: 1 Laser #1 Name: DPSS 561-10 Laser #1 Power: 20.0 Laser #2 Acquire: 1 Laser #2 Name: HeNe633 Laser #2 Power: 5.0 MagicNumber: 67127628 OriginX: 0.0 OriginY: 0.0 OriginZ: 0.0 Recording #1 Camera Binning: 1 Recording #1 Camera Frame Height: 1030 Recording #1 Camera Frame Width: 1300 Recording #1 Camera Offset X: 0.0 Recording #1 Camera Offset Y: 0.0 Recording #1 Camera Supersampling: 0 Recording #1 Images Height: 512 Recording #1 Images Width: 512 Recording #1 Interpolation Y: 1 Recording #1 Line Spacing: 0.26357645109963146 Recording #1 Lines Per Plane: 512 Recording #1 Linescan XY Size: 512 Recording #1 Name: Image2 Recording #1 Number of Channels: 2 Recording #1 Number of Planes: 12 Recording #1 Number of Stacks: 10 Recording #1 Objective: Plan-Apochromat 63x/1.4 Oil DIC M27 Recording #1 Original Scan Data: 1 Recording #1 Plane Spacing: 0.75 Recording #1 Planes Per Volume: 12 Recording #1 Position B/C Contrast 1: 0.0 Recording #1 Position B/C Contrast 2: 0.0 Recording #1 Precession: 0.0 Recording #1 Rotation: 0.0 Recording #1 Sample 0Time: 41806.5649537037 Recording #1 Sample 0X: 0.0 Recording #1 Sample 0Y: 0.0 Recording #1 Sample 0Z: 8.249999999999998 Recording #1 Sample Spacing: 0.26357645109963146 Recording #1 Samples Per Line: 512 Recording #1 Scan Direction: 0 Recording #1 Scan Mode: Stack Recording #1 Special Scan Mode: FocusStep Recording #1 Start Scan Event: 0 Recording #1 Start Scan Time: 0.0 Recording #1 Stop Scan Event: 0 Recording #1 Stop Scan Time: 0.0 Recording #1 Time Series: 0 Recording #1 Use B/C Correction: 0 Recording #1 Use ROIs: 0 Recording #1 Use Reduced Memory ROIs: 0 Recording #1 User: LSM User Recording #1 Zoom X: 1.0 Recording #1 Zoom Y: 1.0 Recording #1 Zoom Z: 1.0 ScanType: x-y-z scan SpectralScan: no spectral scan StructureSize: 464 ThumbnailX: 128 ThumbnailY: 128 TimeInterval: 0.0 TimeStamp0: 2763.3897953333335 ToolbarFlags: 0 Track #1 Acquire: 1 Track #1 Bleach After Scan Number: 0 Track #1 Bleach Count: 0 Track #1 Bleach Scan Number: 0 Track #1 Is Bleach Track: 0 Track #1 Is Ratio Track: 0 Track #1 Multiplex Order: 1 Track #1 Multiplex Type: 1 Track #1 Name: DNA Track #1 Pixel Time: 1.2727272727270875 Track #1 SPI Center Wavelength: 551.4881260000003 Track #1 Sampling Method: 1 Track #1 Sampling Mode: 1 Track #1 Sampling Number: 4 Track #2 Acquire: 1 Track #2 Bleach After Scan Number: 0 Track #2 Bleach Count: 0 Track #2 Bleach Scan Number: 0 Track #2 Is Bleach Track: 0 Track #2 Is Ratio Track: 0 Track #2 Multiplex Order: 2 Track #2 Multiplex Type: 1 Track #2 Name: g-tubulin Track #2 Pixel Time: 1.2727272727270875 Track #2 SPI Center Wavelength: 551.4881260000003 Track #2 Sampling Method: 1 Track #2 Sampling Mode: 1 Track #2 Sampling Number: 4 VoxelSizeX: 0.26357645109963146 VoxelSizeY: 0.26357645109963146 VoxelSizeZ: 0.75 

コマンドラインで画像形式を変換する

bfconvertのオプション

To convert a file between formats, run:   bfconvert [-debug] [-stitch] [-separate] [-merge] [-expand]     [-bigtiff] [-compression codec] [-series series] [-map id]     [-range start end] [-crop x,y,w,h] [-channel channel] [-z Z]     [-timepoint timepoint] [-nogroup] [-autoscale] [-version]     in_file out_file      -version: print the library version and exit       -debug: turn on debugging output      -stitch: stitch input files with similar names    -separate: split RGB images into separate channels       -merge: combine separate channels into RGB image      -expand: expand indexed color to RGB     -bigtiff: force BigTIFF files to be written -compression: specify the codec to use when saving images      -series: specify which image series to convert         -map: specify file on disk to which name should be mapped       -range: specify range of planes to convert (inclusive)     -nogroup: force multi-file datasets to be read as individual              files   -autoscale: automatically adjust brightness and contrast before               converting; this may mean that the original pixel               values are not preserved   -overwrite: always overwrite the output file, if it already exists -nooverwrite: never overwrite the output file, if it already exists        -crop: crop images before converting; argument is 'x,y,w,h'     -channel: only convert the specified channel (indexed from 0)           -z: only convert the specified Z section (indexed from 0)   -timepoint: only convert the specified timepoint (indexed from 0)  If any of the following patterns are present in out_file, they will be replaced with the indicated metadata value from the input file.     Pattern: Metadata value:    ---------------------------    %s       series index    %n       series name    %c       channel index    %w       channel name    %z       Z index    %t       T index    %A       acquisition timestamp  If any of these patterns are present, then the images to be saved will be split into multiple files.  For example, if the input file contains 5 Z sections and 3 timepoints, and out_file is    converted_Z%z_T%t.tiff  then 15 files will be created, with the names    converted_Z0_T0.tiff   converted_Z0_T1.tiff   converted_Z0_T2.tiff   converted_Z1_T0.tiff   ...   converted_Z4_T2.tiff  Each file would have a single image plane. 

フォーマットの変換

特定次元の抽出