function crop_dconn(main_dconn_file, roi_func_file, output_dconn_filename, direction, hemi)

% Crops an ROI-specific dconn from the main dconn file.
%
% Use:
%   crop_dconn('some_dir/sub_dir/your_data.dconn.nii', 'your_roi.func.nii','output_filename.dconn.nii', 'seed', 'R');
%
% Obligatory inputs:
%   main_dconn_file - full path+name of the dconn file (string)
%   roi_func_file   - full path+name of the mask to be used (string)
%   output_dconn_file - full path+name of the output file (string)
%   direction       - seed or target (crop this: 'seed', 'target') (string)
%   hemi            - define which hemisphere to work on 'L' or 'R' (string)
%
% Version history:
% 2018-09-16    suhas      added functionality seed/target crop, output_dconn_file_name
% 2017-06-06    suhas      created
%
% Copyright
% Suhas Vijayakumar / Rogier B. Mars
% Donders Institute & University of Oxford, 2018
%--------------------------------------------------------------------------

%==================================================
% Check for OS and add path to wb_command
%==================================================

if ismac
    path_workbench  = '/Applications/workbench/bin_macosx64/';
else
    error('Please edit to add path to workbench manually.')
end

% Parse file name of ROI-func-file and use the same for the output
fprintf('Output file will be saved as - \n NAME: %s \n PATH: %s \n', output_dconn_filename, pwd);

%==================================================
% Do the work
%==================================================
% Crop using wb_command -cifti-restrict-dense-map
switch upper(direction)
    
    case 'SEED'
        
        switch upper(hemi)
            
            case 'L'
                system([path_workbench 'wb_command -cifti-restrict-dense-map ' main_dconn_file ...
                    ' COLUMN ' output_dconn_filename ' -left-roi ' roi_func_file]);
                
            case 'R'
                system([path_workbench 'wb_command -cifti-restrict-dense-map ' main_dconn_file ...
                    ' COLUMN ' output_dconn_filename ' -right-roi ' roi_func_file]);
                
            otherwise
                error('Please provide a valid hemisphere - "L" for left, "R" for right');
                
        end
        
    case 'TARGET'
        
        switch upper(hemi)
            
            case 'L'
                system([path_workbench 'wb_command -cifti-restrict-dense-map ' main_dconn_file ...
                    ' ROW ' output_dconn_filename ' -left-roi ' roi_func_file]);
                
            case 'R'
                system([path_workbench 'wb_command -cifti-restrict-dense-map ' main_dconn_file ...
                    ' ROW ' output_dconn_filename ' -right-roi ' roi_func_file]);
                
            otherwise
                error('Please provide a valid hemisphere - "L" for left, "R" for right');
                
        end
        
        fprintf('Fin. \n');
        
end

end