Carries out Freesurfer's reconall on T1 nifti file
    
    WARNING: Reconall takes very long to run!!

    http://nipy.sourceforge.net/nipype/users/examples/smri_freesurfer.html

    Parameters
    ----------
    subjfile: nifti file
        Path to subject's T1 nifti file
    
    subjID: string
        optional name for subject's output folder

    subjdir: string

        The directory to where segmentation results should be saved. Defaults
        to same directory as subjfile.

    runreconall: boolean
        If set to true, runs reconall, otherwise just converts assorted mgz
        files to nii

def reconall(subjfile, subjID=None, subjdir=None, runreconall=True): 
    """
    Carries out Freesurfer's reconall on T1 nifti file
    
    WARNING: Reconall takes very long to run!!

    http://nipy.sourceforge.net/nipype/users/examples/smri_freesurfer.html

    Parameters
    ----------
    subjfile: nifti file
        Path to subject's T1 nifti file
    
    subjID: string
        optional name for subject's output folder

    subjdir: string

        The directory to where segmentation results should be saved. Defaults
        to same directory as subjfile.

    runreconall: boolean
        If set to true, runs reconall, otherwise just converts assorted mgz
        files to nii 
    """  
    T1dir = os.path.dirname(subjfile)
    filename = os.path.basename(subjfile)

    # subject ID
    if subjID==None:
        m=re.search('(\w+?)_*_', subjfile)
        subjID=m.group(0) + 'seg'        

    # Tell freesurfer what subjects directory to use
    if subjdir==None:
        subjdir=T1dir
    fs.FSCommand.set_default_subjects_dir(subjdir)
    segdir=subjdir+'/'+subjID+'/'
    print('saving to ' + subjdir)

    # check if file exists
    if os.path.isfile(subjfile):
        print('running recon-all on ' + filename)
    else:
        raise ValueError("File: %s does not exist!"%filename)

    # check if nifti format
    ext=filename.split('.')[1].lower()
    if ext != "nii":
        raise ValueError("File: %s is not a nifti file!"%filename)


    wf = pe.Workflow(name="segment")
    wf.base_dir = T1dir

    if runreconall:
        # run recon-all
        reconall = pe.Node(interface=fs.ReconAll(), name='reconall')
        reconall.inputs.subject_id = subjID 
        reconall.inputs.directive = 'all'
        reconall.inputs.subjects_dir = subjdir
        reconall.inputs.T1_files = subjfile
    
        wf.add_nodes([reconall])
        result = wf.run()

    # convert mgz to nii
    wf2 = pe.Workflow(name="convertmgz")
    wf2.base_dir = T1dir

    convertmgz = pe.Node(interface=fs.MRIConvert(), name='convertmgz')
    convertmgz.inputs.in_file = segdir+'mri/aseg.auto.mgz'
    convertmgz.inputs.out_orientation='LPS'
    convertmgz.inputs.resample_type= 'nearest'
    convertmgz.inputs.reslice_like= subjfile
    convertmgz.inputs.out_file=segdir+subjID+'_aseg.nii.gz'

    wf2.add_nodes([convertmgz])
    result2 = wf2.run()
    if runreconall:
        return (result, result2)
    else:
        return (result2)