大鼠DTI数据分析流程

大鼠DTI数据分析流程

20180728
  • 首先把数据分好类,我需要分析b0、fa和md文件,因此就提前分好类各自放在各自的文件夹内。

    image.png
    image.png
最好是每个文件夹内的文件命名有规律,像下图一样:

image.png
image.png
这样方便后续的分析,省时省力。
  • 第二步,找到一个标准点的大鼠,然后把其他所有的大鼠配准到这只大鼠上,得到对应的.mat文件,这一步因为文件命名好了,因此写了一个简单的python脚本来重复执行操作,代码如下:
# 20180717 By galory 
#This code is processing rat's DTI data 
import os
path = "/brain/zhanjunzhang/Desktop/galory/time1secondprocess/b0process/"
files = os.listdir(path)
# s= []
#cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
for file in files:
    file1 = str(file)
    three_str = str(file1[0]+file1[1]+file1[2])
    if file != '104b0.nii.gz':
        # cmd
        print (file)
        os.system("flirt -in %s -ref 104b0.nii.gz -dof 12 -out %sto104 -omat %s/%sto104.mat" %(file1,three_str,path,three_str))
#        os.system('ls')
# cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
# cmd = flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat
这里需要注意的地方是,当你在windows下写python的时候,绝大可能你的配置环境是python3.x,但因为要在服务器运行,因此要看服务器的python版本号,我这里是python2.7.11,因此需要将代码稍微修改修改。
  • 下一步就是把上一步配准好的文件平均一下得到一个平均的模板average.nii.gz
    因为这一步的fslmaths 命令需要输入的参数很多,因此写了个循环把需要的循环都输出,然后复制,代码如下:
# 20180717 By galory  Mail 996377370a@gmail.com
#This code is processing rat's DTI data 
import os
path = "/brain/zhanjunzhang/Desktop/galory/time1secondprocess/b0process/"
files = os.listdir(path)
# s= []
#cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
count = 0
for file in files:
    if len(file) == 15:
        print "-add %s" %(file),
        count +=1
print(count)
image.png
image.png
  • 下一步就可以执行fslmaths img1 -add img2 -add img3 -add ...... -add imgN -div N命令了
    image.png
    image.png
  • 下一步,把所有的b0像配准到上一步得到的average.nii.gz上,得到相应的.mat文件,用于下一步fa的配准(原理是基于每只大鼠的b0与fa具有相同的结构信息)
    这一步的代码如下:
# 20180729 By galory 
#This code is processing rat's DTI data 
import os
path = "/brain/zhanjunzhang/Desktop/galory/time1secondprocess/b0process/"
files = os.listdir(path)
# s= []
#cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
for file in files:
    file1 = str(file)
    three_str = str(file1[0]+file1[1]+file1[2])
    if file != '104b0.nii.gz':
        # cmd
        print (file)
        os.system("flirt -in %s -ref 104b0.nii.gz -dof 12 -out %sto104 -omat %s/%sto104.mat" %(file1,three_str,path,three_str))
#        os.system('ls')
# cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
# cmd = flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat

  • 这一步,就是利用上一步得到的.mat文件把fa配准到average.nii.gz
    代码如下:
# 20180720 By galory  Mail 996377370a@gmail.com
#This code is processing rat's DTI data 
#This is for registering FA.nii to average.nii.

import os
from os import walk

path_average = "/brain/zhanjunzhang/Desktop/galory/time1secondprocess/faprocess/fatoaverage/"
# path = "/brain/zhanjunzhang/Desktop/galory/trial/fatoaverage/"
path = "/brain/zhanjunzhang/Desktop/galory/time1secondprocess/faprocess/fatoaverage/fa/"
files = os.listdir(path)


# path1 = "/brain/zhanjunzhang/Desktop/galory/trial/fatoaveragemat/"
path1 = "/brain/zhanjunzhang/Desktop/galory/time1secondprocess/faprocess/fatoaverage/mat/"
filesmat = os.listdir(path1)

# s= []
#cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
f = []
def filenew1(filesmat1):
    for file in filesmat1:
        filenew = str(file)
        
        four_strmat = str(filenew[0]+filenew[1]+filenew[2]+filenew[3])
        f.extend(four_strmat)

    return four_strmat
matfile = filenew1(filesmat)
#print(f)
print('------')

# first,convert the list to string
fstr = "".join(f)
# print (fstr.split('t',-1))
str0 = fstr.split('t',-1)
# delete the last element
del str0[-1]
#print(str0)
str0_sort = sorted(str0)
print(str0_sort)



f1 = []
def filenames(files1):
    for file in files1:
        file1 = str(file)
        
        four_str = str(file1[0]+file1[1]+file1[2]+file1[3])
        f1.extend(four_str)

    return four_str
file_name = filenames(files)
#print(f1)
# first,convert the list to string
f1str = "".join(f1)
# print (f1str.split('f',-1))
str1 = f1str.split('f',-1)
del str1[-1]
#print(str1)
str1_sort = sorted(str1)
print(str1_sort)
# test ,success!  
# print(str1[0])


# sort the two strings



# now to compare str1 and str2
i = 0
while i < 68:
    first =  int(str0_sort[i])
    second = int(str1_sort[i])
    print('Hello,Galory.You have a good smile!')
    os.system("flirt -in %s/%sfa.nii.gz -ref %s/average.nii.gz -out %s/%sfatoaverage -applyxfm -init %s/%stoaverage.mat" %(path,str0_sort[i],path_average,path,str0_sort[i],path1,str0_sort[i]))
    i = i + 1
    
print("Done!")
注意,以上代码是基于特定的文件夹存放结构,如下图:average.nii.gz、mat文件夹以及fa文件夹都在同一个目录下,各自的位置对应于以上代码中的path
image.png
image.png
  • fa配准完成
  • md的配准与fa配准过程完全一样,只需要修改下md文件所在的路径就可以。还有一个特别注意的是最后一步的代码里因为过程中分隔字符在fa分析中使用'f',而在md分析中要换为'm';以及数量不一样,第一个时间点数量有69个,因此循环69次;第二个时间点64个,循环64次。

附上第二个时间点的代码,基本没什么变化。
# 20180718 By galory  Mail 996377370a@gmail.com
#This code is processing rat's DTI data 
#This is registering all b0 file to average file obtaining from the second step
import os
path = "/brain/zhanjunzhang/Desktop/galory/time2process/b0/"
files = os.listdir(path)
# s= []
#cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
for file in files:
    file1 = str(file)
    three_str = str(file1[0]+file1[1]+file1[2])
    if (file != 'average.nii.gz')and (file != 'linux_process3.py'):
        # cmd
        print (file)
        os.system("flirt -in %s -ref average.nii.gz -dof 12 -out %s/%stoaverage -omat %s/%stoaverage.mat" %(file,path,three_str,path,three_str))
#     os.system('ls')
# cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
# cmd = flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat

# 20180720 By galory  Mail 996377370a@gmail.com
#This code is processing rat's DTI data 
#This is for registering FA.nii to average.nii.

import os
from os import walk

path_average = "/brain/zhanjunzhang/Desktop/galory/time2process/md/step2/"
# path = "/brain/zhanjunzhang/Desktop/galory/trial/fatoaverage/"
path = "/brain/zhanjunzhang/Desktop/galory/time2process/md/step2/md/"
files = os.listdir(path)


# path1 = "/brain/zhanjunzhang/Desktop/galory/trial/fatoaveragemat/"
path1 = "/brain/zhanjunzhang/Desktop/galory/time2process/md/step2/mat/"
filesmat = os.listdir(path1)

# s= []
#cmd = os.system("flirt -in 101b0.gz -ref 104b0.gz -dof 12 -out 101to104 -omat 101to104.mat")
f = []
def filenew1(filesmat1):
    for file in filesmat1:
        filenew = str(file)
        
        four_strmat = str(filenew[0]+filenew[1]+filenew[2]+filenew[3])
        f.extend(four_strmat)

    return four_strmat
matfile = filenew1(filesmat)
#print(f)
print('------')

# first,convert the list to string
fstr = "".join(f)
# print (fstr.split('t',-1))
str0 = fstr.split('t',-1)
# delete the last element
del str0[-1]
#print(str0)
str0_sort = sorted(str0)
print(str0_sort)



f1 = []
def filenames(files1):
    for file in files1:
        file1 = str(file)
        
        four_str = str(file1[0]+file1[1]+file1[2]+file1[3])
        f1.extend(four_str)

    return four_str
file_name = filenames(files)
#print(f1)
# first,convert the list to string
f1str = "".join(f1)
# print (f1str.split('f',-1))
str1 = f1str.split('m',-1)
del str1[-1]
#print(str1)
str1_sort = sorted(str1)
print(str1_sort)
# test ,success!  
# print(str1[0])
# sort the two strings
# now to compare str1 and str2
i = 0
while i < 64:
    first =  int(str0_sort[i])
    second = int(str1_sort[i])
    print('Hello,Galory.You have a good smile!')
    os.system("flirt -in %s/%smd.nii.gz -ref %s/average.nii.gz -out %s/%smdtoaverage -applyxfm -init %s/%stoaverage.mat" %(path,str0_sort[i],path_average,path,str0_sort[i],path1,str0_sort[i]))
    i = i + 1
    
print("Done!")

20180729
修正(上文已修改):第一步把所有的b0像配准到一只标准一点的大鼠,比如配到104b0.nii.gz上,然后用fslmaths命令得到average.nii.gz,这样就是为了得到average.nii.gz。第二步应该是把所有的b0像配到average.nii.gz上得到一个.mat文件,这个.mat文件在最后一步fa配准到average时用得到。原理就是每个大鼠的b0跟fa具有同样的结构信息,因此可以用这个矩阵来得到想要的fa.
第一个时间点和第二个时间点的简单步骤如下:
第一个时间点:

    1. flirt -in 101b0.nii.gz -ref 104b0.nii.gz -dof 12 -out 101to104 -omat 101to104.mat
    2. fslmaths img1 -add img2 -add img3 -add ...... -add imgN -div N average

    3. flirt -in 101b0.nii.gz -ref average.nii.gz -dof 12 -out 101toaverage -omat 101toaverage.mat
    4. flirt -in 101fa.nii.gz  -ref  average.nii.gz  -out 101tofa.nii.gz -applyxfm -init 101toaverage.mat

第二个时间点:

    1. flirt -in 101b0.nii.gz -ref average.nii.gz -dof 12 -out 101toaverage -omat 101toaverage.mat
    2. flirt -in 101fa.nii.gz  -ref  average.nii.gz  -out 101tofa.nii.gz -applyxfm -init 101toaverage.mat 

  • 此外,除了写脚本,也可以把全部命令都放到同一个txt文本中,然后依次执行。

评论

此博客中的热门博文

如何下载Internet Archive中借阅类图书【详细指南】

2021年网站更新,再出发

心理学书籍推荐