[importers] Add importer for MindBoogle
authorVincent Michel <vincent.michel@logilab.fr>
Fri, 14 Dec 2012 17:09:57 +0100
changeset 19 da04d356b60d
parent 18 e74cc1d215ce
child 20 7d4b684cfbf8
[importers] Add importer for MindBoogle
importers/helpers.py
importers/localizer.py
importers/mindboogle.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/importers/helpers.py	Fri Dec 14 17:09:57 2012 +0100
@@ -0,0 +1,98 @@
+# copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr -- mailto:contact@logilab.fr
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import pickle
+
+import nibabel as nb
+
+
+def get_image_info(image_path, get_tr=True):
+    """ Return the image info readed from the image using Nibabel """
+    img = nb.load(image_path)
+    data = {}
+    data['voxel_res_x'] = int(img.get_header()['pixdim'][1])
+    data['voxel_res_y'] = int(img.get_header()['pixdim'][2])
+    data['voxel_res_z'] = int(img.get_header()['pixdim'][3])
+    data['shape_x'] = int(img.get_shape()[0])
+    data['shape_y'] = int(img.get_shape()[1])
+    data['shape_z'] = int(img.get_shape()[2])
+    data['shape_t'] = int(img.get_shape()[3]) if len(img.get_shape()) == 4 else None
+    data['affine'] = pickle.dumps(img.get_affine().tolist())
+    desc = str(img.get_header()['descrip'])
+
+    try:
+        if get_tr:
+            tr, te = re.findall(
+                'TR=(.*)ms.*TE=(.*)ms', desc)[0]
+            data['tr'] = float(tr)
+            data['te'] = float(te)
+    except Exception, e:
+        data['tr'] = None
+        data['te'] = None
+
+    return data
+
+
+def import_genes(genetics_dir):
+    ref_chr_path = os.path.join(genetics_dir, 'chromosomes.json')
+    ref_gene_path = os.path.join(
+        genetics_dir, 'sprintBack', 'hg18.refGene.meta')
+    chromosomes = json.load(open(ref_chr_path))
+    ref_gene = []
+    for row in csv.reader(open(ref_gene_path), delimiter='\t'):
+        gene = {}
+        gene['name'] = unicode(row[0])
+        gene['gene_id'] = unicode(row[0])
+        gene['uri'] = None
+        gene['start_position'] = int(row[1])
+        gene['stop_position'] = int(row[2])
+        gene['chromosome'] = row[3].split('_')[0]
+        ref_gene.append(gene)
+    return ref_gene
+
+
+def import_chromosomes(genetics_dir):
+    ref_chr_path = os.path.join(genetics_dir, 'chromosomes.json')
+    chromosomes = json.load(open(ref_chr_path))
+    chrs = []
+    for chr_id in chromosomes:
+        chr = {}
+        chr['name'] = u'chr%s' % chromosomes[chr_id].upper()
+        chr['identifier'] = unicode(chr['name'])
+
+        chrs.append(chr)
+    return chrs
+
+
+def import_snps(genetics_dir):
+    ref_chr_path = os.path.join(genetics_dir, 'chromosomes.json')
+    ref_snp_path = os.path.join(
+        genetics_dir, 'sprintBack', 'bysubj', 'bru3506.bim')
+    chromosomes = json.load(open(ref_chr_path))
+    snps = []
+    for row in csv.reader(open(ref_snp_path), delimiter='\t'):
+        snp = {}
+        if row[0] == '0':
+            continue
+
+        chr_id = chromosomes[row[0]]
+
+        snp['rs_id'] = unicode(row[1])
+        snp['position'] = int(row[3])
+        snp['chromosome'] = u'chr%s' % chr_id.upper()
+
+        snps.append(snp)
+    return snps
--- a/importers/localizer.py	Thu Dec 13 14:32:06 2012 +0100
+++ b/importers/localizer.py	Fri Dec 14 17:09:57 2012 +0100
@@ -84,6 +84,7 @@
 
 def import_study(data_dir=None):
     data = {}
+    data['data_filepath'] = data_dir
     data['name'] = u'localizer'
     data['description'] = u'localizer db'
 
@@ -430,7 +431,6 @@
 
 
 if __name__ == '__main__':
-    from cubicweb.dataimport import NoHookRQLObjectStore as Store
     from cubicweb.dataimport import SQLGenObjectStore as Store
 
     root_dir = '/opt/localizer/localizer/subjects'
@@ -464,7 +464,7 @@
     # Study
     # ---------------------------------------------------------------------
 
-    study = import_study()
+    study = import_study(data_dir=root_dir)
     study = store.create_entity('Study', **study)
 
     # ---------------------------------------------------------------------
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/importers/mindboogle.py	Fri Dec 14 17:09:57 2012 +0100
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+# copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr -- mailto:contact@logilab.fr
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import sys
+import os.path as osp
+import glob
+
+import nibabel as nb
+
+from cubicweb.dataimport import SQLGenObjectStore as Store
+
+from cubes.brainomics.importers.helpers import get_image_info
+
+
+if __name__ == '__main__':
+    base_path = osp.abspath(sys.argv[4])
+    store = Store(session)
+    study = store.create_entity('Study', data_filepath=unicode(base_path), name = u'mindboggle',
+                                description=u'''The Mindboggle-101 dataset includes manually labeled anatomical regions for 101 healthy subjects. The manually edited cortical labels follow sulcus landmarks according to the Desikan-Killiany-Tourville (DKT) protocol. See http://www.frontiersin.org/Brain_Imaging_Methods/10.3389/fnins.2012.00171/full “101 labeled brain images and a consistent human cortical labeling protocol”
+Arno Klein, Jason Tourville. Frontiers in Brain Imaging Methods. 6:171. DOI: 10.3389/fnins.2012.00171
+    and http://mindboggle.info/data.html''')
+    count_subject = 0
+    for subject_path in glob.iglob(osp.join(base_path,'*/*')):
+        subject_id = subject_path.split('/')[-1]
+        print '--->', subject_id
+        subject = store.create_entity('Subject', identifier=unicode(subject_id),
+                                      gender=u'unknown', handedness=u'unknown')
+        store.relate(subject.eid, 'related_studies', study.eid)
+        assessment = store.create_entity('Assessment',identifier=u'label',
+                                         protocol=u'mindboggle-label')
+        store.relate(subject.eid, 'concerned_by', assessment.eid)
+        for image in glob.iglob(osp.join(base_path, subject_path, '*.nii.gz')):
+            image_id = image.split('/')[-1].split('.')[0]
+            anat = {'identifier': u'%s_%s' % (subject_id, image_id),
+                    'label': u'labels' if 'labels' in image_id else u'anatomy',
+                    'format': u'nii.gz',
+                    'type': u'normalized T1',
+                    'filepath': unicode(osp.relpath(image)),
+                    'valid': True, 'completed': True, 'description': 'Image from Mindboggle'}
+            mri_data = store.create_entity('MRIData', sequence=u'T1')
+            # MRI data
+            mri_data = {'sequence': u'T1'}
+            mri_data.update(get_image_info(image))
+            mri_data = store.create_entity('MRIData', **mri_data)
+            anat['has_data'] = mri_data.eid
+            anat = store.create_entity('Scan', **anat)
+            store.relate(anat.eid, 'concerns', subject.eid, 'Scan')
+            store.relate(assessment.eid, 'generates', anat.eid)
+        count_subject += 1
+    # Flush
+    store.flush()
+    store.commit()
+    print '-------> %s subjects imported' % count_subject