We're Hiring!

Concatenate multi-channel nd2 files with bfconvert

Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats
Please note:
Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats

If you are having trouble with image files, there is information about reporting bugs in the Bio-Formats documentation. Please send us the data and let us know what version of Bio-Formats you are using. For issues with your code, please provide a link to a public repository, ideally GitHub.

Concatenate multi-channel nd2 files with bfconvert

Postby mattek » Wed Nov 16, 2016 10:44 pm

Hi,
I have trouble concatenating nd2 files with BF command-line tool, bfconvert (version 5.2.2). The two files I want to concatenate are time-lapse stacks with two channels.

Let's say I have:
file01.nd2 (T=10, C=2, Z=1)
file02.nd2 (T=20, C=2, Z=1)

I tried the following.
(1) Straightforward stitching:
bfconvert -bigtiff -stitch file01.nd2 file_cat.tiff

However, I obtain a stack with T=10, C=2, Z=2, instead of T=30, C=2, Z=1.

(2) Splitting both files into single-frame 2-channel tiffs with:
bfconvert -bigtiff -padded file01.nd2 file_T%t.tiff

Applying the same to file02.nd2 and shifting time index by 10 (number of frames in the 1st file), I have a series of tiffs:
file_T{00..29}

However, stitching yields a very strange concatenation with T=30, Z=2, where T=1..15 contains one channel and T=16-30 the other one.

(3) Finally I split both files into individual tiffs:
bfconvert -bigtiff -padded file01.nd2 file_C%c_T1%t.tiff
bfconvert -bigtiff -padded file02.nd2 file_C%c_T2%t.tiff

Then I use ImageMagick's convert to concatenate time series into a single TIFF separately for every channel. The trouble is that I cannot bring these two stacks into a single one because apparently libtiff, and thus IM, doesn't handle multi-channel images. BF's bfconvert doesn't help here either.

Is there any command-line tool/way to handle this?
mattek
 
Posts: 4
Joined: Wed Nov 16, 2016 5:18 pm

Re: Concatenate multi-channel nd2 files with bfconvert

Postby szleo » Thu Nov 17, 2016 4:29 pm

Hi,

TL;DR: try the following (detailed explanation and caveats follow)

Code: Select all
bfconvert -bigtiff -padded file02.nd2 concat_C%c_T%t.tiff
bfconvert -bigtiff -padded file01.nd2 concat_C%c_T2%t.tiff
bfconvert -stitch 'concat_C<0-1>_T<0-29>.tiff' concat.ome.tiff


Detailed explanation

we reproduced your use case using artificially generated files of the same dimensions as you described:

Code: Select all
bfconvert "test&sizeT=10&sizeC=2.fake" file01.ome.tiff
bfconvert "test&sizeT=20&sizeC=2.fake" file02.ome.tiff
bfconvert -stitch file01.ome.tiff concat.ome.tiff


The resulting concat.ome.tiff image is a T=10 x C=2 x Z=2 image.

There are two underlying issues here:
- the first issue is associated with the guessing of the dimension alongside which images should be stitched. As this is based on the filename and there is no specific information, the code tries a best guess which defaults to Z.
- a second issue is related to the scope of the stitching functionality used in bfconvert. This option uses FileStitcher, which assumes all images have the same dimensions.

A potential workaround for the first issue might be to rename the files to something like file_t1.nd2 and file_t2.nd2. In this way, FileStitcher will see the "t" prefix and try to stitch along the T dimension. Due to the second issue, however, the T size of the concatenated image will be equal to that of the first file.

The best way to go about this (other than writing a custom Java program, ofc) is to split into individual files as you did in your third attempt, then use a file pattern to stitch all planes back together:

Code: Select all
bfconvert -bigtiff -padded file02.nd2 concat_C%c_T%t.tiff
bfconvert -bigtiff -padded file01.nd2 concat_C%c_T2%t.tiff
bfconvert -stitch 'concat_C<0-1>_T<0-29>.tiff' concat.ome.tiff


Caveats

In the general case, you'll have to rename individual files before the final stitch to get a continuous numeric sequence. The above works only because one of the files has exactly 10 time points.

Best,

Simone
User avatar
szleo
 
Posts: 2
Joined: Wed Sep 02, 2015 10:42 am

Re: Concatenate multi-channel nd2 files with bfconvert

Postby mattek » Mon Nov 21, 2016 4:31 pm

Thank you! This works well. I guess the piece I was missing was "Grouping files using a pattern".

I actually avoid renaming files to get a continuous sequence by using tiffcp command to merge individual single-channel tiffs into separate stacks with time sequences for each channel.

Then I use "bfconvert -stitch" with a pattern for C as you suggested.


Perhaps useful for future reference, I start with following nd2 files:
Code: Select all
Point0001_Seq0001.nd2 (T=5, C=2, Z=1)
Point0001_Seq0002.nd2 (T=20, C=2, Z=1)
Point0001_Seq0003.nd2 (T=15, C=2, Z=1)

Point0002_Seq0001.nd2 (T=5, C=2, Z=1)
Point0002_Seq0002.nd2 (T=20, C=2, Z=1)
Point0002_Seq0003.nd2 (T=15, C=2, Z=1)

etc., where 'Point' identifies FOV and 'Seq' corresponds to consecutive time-courses for a particular FOV.

Then, for all nd2's:
Code: Select all
bfconvert -bigtiff -padded Point0001_Seq0001.nd2 Point0001_C%c_T1%t.tiff
bfconvert -bigtiff -padded Point0001_Seq0002.nd2 Point0001_C%c_T2%t.tiff
bfconvert -bigtiff -padded Point0001_Seq0003.nd2 Point0001_C%c_T3%t.tiff

etc.

Note that nd2's are converted to
Code: Select all
*_T1[0-4].tiff
*_T2[00-19].tiff
*_T3[00-14].tiff


The sequence after T is ordered but not consecutive. However, it's not a problem to merge them with tiffcp:
Code: Select all
tiffcp  Point0001_C0_T*.tiff Point0001_C0.tiff
tiffcp  Point0001_C1_T*.tiff Point0001_C1.tiff


which produces tiff stacks with T=40, C=1, Z=1 for each FOV and channels {0,1}.

Then I stitch:
Code: Select all
bfconvert -bigtiff -stitch 'Point0001_C<0-1>.tiff' PointAll.tiff


which produces a single hyperstack with T=40, C=2, Z='number of FOV's.
mattek
 
Posts: 4
Joined: Wed Nov 16, 2016 5:18 pm


Return to User Discussion [Legacy]

Who is online

Users browsing this forum: No registered users and 1 guest

cron