Script for registration of a time series using elastix

@bogovicj
I have a time series of an embryonic sample with N time points (tp) and I want to time-register them in series using elastix.

Basically, I want to register tp1 (fixed) with tp2 (moving) and use this output as the fixed image for tp3 and so on.

Can you help with a script, which can loop through time points that can be used with elastix?

Thanks a lot

1 Like

Hi @sundar !

Hope this will get you started.
If you make a file called list.txt like this:

tp1.nrrd
tp2.nrrd
tp3.nrrd
tp4.nrrd

Then running the script below like this ./loopOverFileLines.sh list.txt will produce the output:

register tp2.nrrd to tp1.nrrd
register tp3.nrrd to tp2.nrrd
register tp4.nrrd to tp3.nrrd

I’ve left some work for you to do, because from our talk offline, I think you can fill
in what’s missing. But please ask questions if something is unclear.

John

loopOverFileLines.sh

#!/bin/bash

# the input to this script should be a text file, with the path to a file on each line, in order

# Make sure there's exactly one input
if [[ "$#" -ne 1 ]]; then
    echo "Must provide a file" >&2 
    exit 1
fi

list="$1" 

i="0"
current=""
previous=""
for t in `cat $list`
do
    previous="$current"
    current="$t"

    # do nothing when $i equals zero, since we work on pairs.
    if (( $i > 0 )); 
    then
        # In here, the "$current" variable stores whatever is on line (i) of the input
        # the "$previous" variable stores whatever is on line (i-1) of the input

        echo "register $current to $previous"

        # PUT 
        # YOUR 
        # COMMANDS 
        # HERE

    fi
    ((i++))
done

2 Likes

@bogovicj

Thanks for the script. I have added some changes as you suggested. It does not work though. It says syntax error (unexpected end of file, line 50). I have certainly made some mistakes with the syntaxes in the code, as I find it quite hard to code with bash scripting language.

Also, is line 39 (which has mv command) written the right way? If I am registering tp2 (moving) to tp1 (fixed), the result.0.nrrd file should be renamed and overwrite the existing tp2.nrrd so that this can be used for the next iteration, while registering tp3.

#!/bin/bash

# the input to this script should be a text file, with the path to a file on each line, in order

# Make sure there's exactly one input
if [[ "$#" -ne 1 ]]; then
    echo "Must provide a file" >&2 # >&2 sends log to stderror file
    exit 1
fi

list="$1" # $1 takes the first argument while calling this function

i="0"
current=""
previous=""
for t in `cat $list`
do
    previous="$current"
    current="$t"

    # do nothing when $i equals zero, since we work on pairs.
    if (( $i > 0 ));
    then
        # In here, the "$current" variable stores whatever is on line (i) of the input
        # the "$previous" variable stores whatever is on line (i-1) of the input

        echo "register $current to $previous"
        
        source ../setVariables.sh
        rigid_params="Parameters_Rigid.txt"
        nThreads="2"

        elastix -f $previous \
            -m $current \
            -out . \
            -threads $nThreads \
            -p $rigid_params
        
        mv result.0.nrrd tp${i}.nrrd
    
    fi
    ((i++))
done#!/bin/sh

#  Run_time_series.sh
#  
#
# 
#  

Hi @sundar,

What text editor did you use to edit the script. If you use something “fancy”, it may add formatting and/or extra stuff that you don’t want. Some people I know like Atom, for example.

That’s my guess for the cause of the unexpected end of file.

Your mv command looks right to me.

If you register 2>1, 3>2, 4>3 etc, you should go with something like

elastix -f $previous \
            -m $current \
            -out . \
            -threads $nThreads \
            -p $rigid_params
        
mv result.0.nrrd tp${i}.nrrd
previous="tp${i}.nrrd"

and forget about the way I set the value of the variable previous.

John

1 Like

Great! The registration of successive time points works :slight_smile:
I use xcode for editing and i figured that the following ending was the problem, which I removed.
#!/bin/sh

Thanks for ‘atom’ suggestion. Just downloaded it.

Thanks for all the help. Really appreciate it.
Best regards
Sundar

1 Like