{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Adding library niak to the search path.\n", "\n", "Adding library psom to the search path.\n", "\n" ] } ], "source": [ "cd\n", "build_path niak psom\n", "cd /home/pbellec/git/niak_tutorials/glm_connectome" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# GLM connectome pipeline - intra-run\n", "This tutorial shows how to use the NIAK GLM connectome pipeline to compare two groups of subjects, 10 patients diagnosed with schizophrenia vs 10 healthy controls. We will illustrate how to include an intra-run contrast, i.e. manipulate the fMRI time series of each subject prior to generate connectivity measures. See the [documentation](http://niak.simexp-lab.org/pipe_glm_connectome.html) of the pipeline for a more detailed description of the options. There is also inline documentation you can access by typing `help niak_pipeline_glm_connectome`. Download the tutorial as a notebook [here](https://raw.githubusercontent.com/SIMEXP/niak_tutorials/master/glm_connectome/niak_tutorial_glm_connectome_intra_run.ipynb) and a matlab script [here](https://raw.githubusercontent.com/SIMEXP/niak_tutorials/master/connectome/niak_tutorial_glm_connectome_intra_run.m). To run this tutorial, we recommend to use [jupyter](http://jupyter.org/) from a niak docker container, as described in the [NIAK installation page](http://niak.simexp-lab.org/niak_installation.html). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Preparing the files\n", "### Functional MRI \n", "First download a small preprocessed fMRI dataset. " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2017-02-01 12:08:21-- https://www.nitrc.org/frs/download.php/9401/cobre_lightweight20.zip\n", "Resolving www.nitrc.org (www.nitrc.org)... 132.239.16.23\n", "Connecting to www.nitrc.org (www.nitrc.org)|132.239.16.23|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 89902898 (86M) [application/force-download]\n", "Saving to: ‘/home/pbellec/git/niak_tutorials/glm_connectome/cobre_lightweight20/cobre_lightweight20.zip’\n", "\n", "cobre_lightweight20 100%[===================>] 85.74M 2.29MB/s in 44s \n", "\n", "2017-02-01 12:09:05 (1.97 MB/s) - ‘/home/pbellec/git/niak_tutorials/glm_connectome/cobre_lightweight20/cobre_lightweight20.zip’ saved [89902898/89902898]\n", "\n", "Deleting file '/home/pbellec/git/niak_tutorials/glm_connectome/cobre_lightweight20/cobre_lightweight20.zip' \n", "ans = 0\n" ] } ], "source": [ "clear\n", "path_data = [pwd filesep];\n", "[status,msg,data_fmri] = niak_wget('cobre_lightweight20_nii');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When starting from fMRI data preprocessed with NIAK, it is possible to use `niak_grab_fmri_preprocess` on the output folder to collect the file names, as described in the [pipeline documentation](http://niak.simexp-lab.org/pipe_connectome.html). In this case, we explicitely list all the files" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "file_pheno = [data_fmri.path filesep 'phenotypic_data.tsv.gz'];\n", "tab = niak_read_csv_cell(file_pheno);\n", "list_subject = tab(2:end,1);\n", "files_in = struct;\n", "for ss = 1:length(list_subject)\n", " files_in.fmri.(list_subject{ss}).sess1.rest = [data_fmri.path filesep 'fmri_' list_subject{ss} '.nii.gz'];\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Brain parcellation\n", "The second input of the pipeline is a set of brain parcels. We will just download the so-called Cambridge functional template." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2017-02-01 12:16:23-- http://files.figshare.com/1861821/template_cambridge_basc_multiscale_mnc_sym.zip\n", "Resolving files.figshare.com (files.figshare.com)... 54.194.135.36, 52.211.238.180\n", "Connecting to files.figshare.com (files.figshare.com)|54.194.135.36|:80... connected.\n", "HTTP request sent, awaiting response... 301 Moved Permanently\n", "Location: https://ndownloader.figshare.com/files/1861821 [following]\n", "--2017-02-01 12:16:23-- https://ndownloader.figshare.com/files/1861821\n", "Resolving ndownloader.figshare.com (ndownloader.figshare.com)... 52.209.81.33, 52.49.140.44\n", "Connecting to ndownloader.figshare.com (ndownloader.figshare.com)|52.209.81.33|:443... connected.\n", "HTTP request sent, awaiting response... 302 Found\n", "Location: https://s3-eu-west-1.amazonaws.com/pfigshare-u-files/1861821/template_cambridge_basc_multiscale_mnc_sym.zip [following]\n", "--2017-02-01 12:16:24-- https://s3-eu-west-1.amazonaws.com/pfigshare-u-files/1861821/template_cambridge_basc_multiscale_mnc_sym.zip\n", "Resolving s3-eu-west-1.amazonaws.com (s3-eu-west-1.amazonaws.com)... 54.231.131.139\n", "Connecting to s3-eu-west-1.amazonaws.com (s3-eu-west-1.amazonaws.com)|54.231.131.139|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 211786 (207K) [binary/octet-stream]\n", "Saving to: ‘/home/pbellec/git/niak_tutorials/glm_connectome/template_cambridge_basc_multiscale_mnc_sym/template_cambridge_basc_multiscale_mnc_sym.zip’\n", "\n", "template_cambridge_ 100%[===================>] 206.82K 347KB/s in 0.6s \n", "\n", "2017-02-01 12:16:25 (347 KB/s) - ‘/home/pbellec/git/niak_tutorials/glm_connectome/template_cambridge_basc_multiscale_mnc_sym/template_cambridge_basc_multiscale_mnc_sym.zip’ saved [211786/211786]\n", "\n", "Deleting file '/home/pbellec/git/niak_tutorials/glm_connectome/template_cambridge_basc_multiscale_mnc_sym/template_cambridge_basc_multiscale_mnc_sym.zip' \n", "ans = 0\n" ] } ], "source": [ "[status,msg,data_template] = niak_wget('cambridge_template_mnc1');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to pick the parcellation into 36 clusters." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "files_in.network = [data_template.path filesep 'template_cambridge_basc_multiscale_sym_scale007.mnc.gz'];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Group model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first load the phenotypic data from the COBRE sample. This command will not work if you haven't downloaded the data yet as part of the rmap tutorial. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "path_cobre = [pwd filesep 'cobre_lightweight20'];\n", "file_pheno = [path_cobre filesep 'phenotypic_data.tsv.gz'];\n", "tab = niak_read_csv_cell(file_pheno);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we convert the values into a series of numerical covariates, that we save in a separate .csv file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "list_subject = tab(2:end,1);\n", "patient = strcmp(tab(2:end,5),'Patient');\n", "age = str2double(tab(2:end,2)); \n", "FD = str2double(tab(2:end,9));\n", "opt_csv.labels_x = list_subject; % Labels for the rows\n", "opt_csv.labels_y = { 'age' , 'patient' , 'fd' };\n", "niak_write_csv('model_patient.csv', [age patient FD] , opt_csv);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We specify to the pipeline where to find the model:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "files_in.model.group = [pwd filesep 'model_patient.csv'];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Connectivity maps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are going to get a list of the connectivity maps associated with each subject for one network, say the DMN. Labels for each network have been specified when running the `connectome` pipeline. We simply grab the outputs of the connectome pipeline. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "path_connectome = [pwd filesep 'connectome'];\n", "files_conn = niak_grab_connectome(path_connectome);\n", "files_in.data = files_conn.rmap;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Brain mask\n", "We specify the mask of brain networks to the pipeline, so that it can use it to mask the grey matter. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "files_in.mask = files_conn.network_rois;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Set up the options of the pipeline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First specify where to save the outputs, and how many networks to use: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%% General\n", "opt.folder_out = [pwd filesep 'subtype']; " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then specify which covariates to use as confounds **before** the generation of subtypes. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% a list of variable names to be regressed out\n", "% If unspecified or left empty, no confounds are regressed\n", "opt.stack.regress_conf = {'fd'}; " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The options for the subtypes themselves:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%% Subtyping\n", "opt.subtype.nb_subtype = 2; % the number of subtypes to extract\n", "opt.subtype.sub_map_type = 'mean'; % the model for the subtype maps (options are 'mean' or 'median')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we add an association test between subtypes and the patient label:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% scalar number for the level of acceptable false-discovery rate (FDR) for the t-maps\n", "opt.association.patient.fdr = 0.05; \n", "% turn on/off normalization of covariates in model (true: apply / false: don't apply)\n", "opt.association.patient.normalize_x = false; \n", "% turn on/off normalization of all data (true: apply / false: don't apply)\n", "opt.association.patient.normalize_y = false; \n", "% turn on/off adding a constant covariate to the model\n", "opt.association.patient.flag_intercept = true; \n", "% To test a main effect of a variable\n", "opt.association.patient.contrast.patient = 1; % scalar number for the weight of the variable in the contrast\n", "opt.association.patient.contrast.fd = 0; % scalar number for the weight of the variable in the contrast\n", "opt.association.patient.contrast.age = 0; % scalar number for the weight of the variable in the contrast\n", "% type of data for visulization (options are 'continuous' or 'categorical')\n", "opt.association.patient.type_visu = 'continuous'; " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to add a single chi-square test on the relationship between subtypes and a categorical variable:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% string name of the column in files_in.model on which the contigency table will be based\n", "opt.chi2 = 'patient'; " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Run the pipeline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "opt.flag_test = false; % Put this flag to true to just generate the pipeline without running it.\n", "pipeline = niak_pipeline_subtype(files_in,opt);" ] } ], "metadata": { "kernelspec": { "display_name": "Octave", "language": "octave", "name": "octave" }, "language_info": { "file_extension": ".m", "help_links": [ { "text": "GNU Octave", "url": "https://www.gnu.org/software/octave/support.html" }, { "text": "Octave Kernel", "url": "https://github.com/Calysto/octave_kernel" }, { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-octave", "name": "octave", "version": "4.0.2" } }, "nbformat": 4, "nbformat_minor": 0 }