Recon-all gives an error with trying to re-run existing anatomical data

Summary of what happened:

Using recon-all to create structural maps of around 14 subjects. I want to try it on one subject initially.

Command used (and if a helper script was used, a link to the helper script or the command generated):

# Function to run recon-all for a single subject
run_recon_all() {
    subject_id=$1
    anat_file=$2

    echo "Running recon-all for $subject_id"
    recon-all -s "${subject_id}" -i "${anat_file}" -all
    if [ $? -ne 0 ]; then
        echo "ERROR: recon-all failed for $subject_id"
        return 1
    fi
    echo "Completed recon-all for $subject_id"
}

# Loop through all subject directories
for subject_dir in /data/mgh-meditation/mihir/converted/sub-*/; do
    # Extract subject ID from the directory path
    subject_id=$(basename ${subject_dir})

    # Find the T1w file (more flexible search)
    anat_file=$(find "${subject_dir}" -type f -name "*T1w.nii*" | head -n 1)

    if [ -n "${anat_file}" ]; then
        echo "Processing ${subject_id} with file: ${anat_file}"
        run_recon_all "${subject_id}" "${anat_file}"
    else
        echo "ERROR: Anatomical file not found for ${subject_id}"
        echo "Searched in: ${subject_dir}"
        echo "Expected a file matching pattern: *T1w.nii*"
    fi
done

Version:

freesurfer/7.4.1

Environment (Docker, Singularity / Apptainer, custom installation):

NoMachine, Freesurfer

Data formatted according to a validatable standard? Please provide the output of the validator:

PASTE VALIDATOR OUTPUT HERE

Relevant log outputs (up to 20 lines):

Processing sub-003 with file: /data/mgh-meditation/mihir/converted/sub-003/anat/sub-003_ses-01_T1w.nii
Running recon-all for sub-003
ERROR: You are trying to re-run an existing subject with (possibly)
 new input data (-i). If this is truly new input data, you should delete
 the subject folder and re-run, or specify a different subject name.
 If you are just continuing an analysis of an existing subject, then
 omit all -i flags.
ERROR: recon-all failed for sub-003

PS: The stderr output (if any) follows:

/PHShome/mw1264/.lsbatch/1727197362.875014.shell: line 14: export: `=': not a valid identifier
/PHShome/mw1264/.lsbatch/1727197362.875014.shell: line 14: export: `/data/mgh-meditation/mihir/postpreproc_scripts/license.txt': not a valid identifier

I get an additional error messages with the License. I have the license.txt path exported as a export FS_LICENSE variable.

Any help related to scripting out a recon-all command would be appreciated!


Hi @mwalvekar,

The error message described what you need to do. If you are resuming an analysis (e.g., it crashed halfway through) then either remove the -i argument or delete any previous recon-all outputs and have it run from scratch. If going by the removing -i argument, you may also have to delete the IsRunning file in the scripts folder of the recon-all output directory.

For the license file exporting, it doesn’t look like you shared the code that led to that error, so I cannot say why it failed.

I recommend trying ChatGPT, which generated a modified script: https://chatgpt.com/share/66f58624-ca4c-8003-a365-29fdba10fa65

# Function to run recon-all for a single subject
run_recon_all() {
    subject_id=$1
    anat_file=$2
    subject_dir_path="$SUBJECTS_DIR/${subject_id}"

    echo "Running recon-all for $subject_id"

    if [ -d "${subject_dir_path}" ]; then
        echo "Subject ${subject_id} already has recon-all outputs, running without -i flag"
        recon-all -s "${subject_id}" -all
    else
        echo "No previous recon-all outputs found, running with -i flag"
        recon-all -s "${subject_id}" -i "${anat_file}" -all
    fi

    if [ $? -ne 0 ]; then
        echo "ERROR: recon-all failed for $subject_id"
        return 1
    fi

    echo "Completed recon-all for $subject_id"
}

# Loop through all subject directories
for subject_dir in /data/mgh-meditation/mihir/converted/sub-*/; do
    # Extract subject ID from the directory path
    subject_id=$(basename ${subject_dir})

    # Find the T1w file (more flexible search)
    anat_file=$(find "${subject_dir}" -type f -name "*T1w.nii*" | head -n 1)

    if [ -n "${anat_file}" ]; then
        echo "Processing ${subject_id} with file: ${anat_file}"
        run_recon_all "${subject_id}" "${anat_file}"
    else
        echo "ERROR: Anatomical file not found for ${subject_id}"
        echo "Searched in: ${subject_dir}"
        echo "Expected a file matching pattern: *T1w.nii*"
    fi
done

Best,
Steven

Hi @Steven ,

Thanks for responding. Yea I deleted the previous made folders in the output directory. I was under the assumptions that no folder would be made unless the script executed successfully,

Here is first part of the script where I define the license file.

# Load FreeSurfer module (adjust as needed for your system)
module load freesurfer/7.4.1

#set license file path
export FS_LICENSE = /data/mgh-meditation/mihir/postpreproc_scripts/license.txt

# Set up FreeSurfer environment
export SUBJECTS_DIR=/data/mgh-meditation/mihir/freesurfer_output

# Create SUBJECTS_DIR if it doesn't exist
if [ ! -d "$SUBJECTS_DIR" ]; then
  :wq
  mkdir -p "$SUBJECTS_DIR"
    if [ $? -ne 0 ]; then
        echo "ERROR: Failed to create SUBJECTS_DIR: $SUBJECTS_DIR"
        exit 1
    fi
    echo "Created SUBJECTS_DIR: $SUBJECTS_DIR"
fi

# Ensure SUBJECTS_DIR is writable
if [ ! -w "$SUBJECTS_DIR" ]; then
    echo "ERROR: SUBJECTS_DIR is not writable: $SUBJECTS_DIR"
    exit 1
fi

You have extra spaces around the equal sign in the export statement. And freesurfer outputs are not contingent upon successful execution. Outputs are stored in the final directory as they are made.

Best,
Steven

Hi @Steven,

I see what the problem is now, thanks for the help!

Mihir