-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfsl_calls.pas
executable file
·212 lines (187 loc) · 5.66 KB
/
fsl_calls.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
unit fsl_calls;
{$IFDEF FPC}{$mode objfpc}{$H+} {$ENDIF}
{$Include isgui.inc}
interface
uses
{$IFDEF GUI}Forms,{$ENDIF}
{$IFDEF UNIX} Process, {$ENDIF}
{$IFDEF FPC} LResources,{$ENDIF}
Classes, SysUtils, define_types;
//procedure FSLcmd (lCmd: string);
function FSLbet (lFilename: string; lFrac: single): string;
function FSLmcflirt (lFilename4D: string): string;
function FSLmean(lFilename4D: string): string;
function FSLflirt (lFilenames: tstrings): string;
var gFSLbase: string;
implementation
{$IFNDEF UNIX}
uses Process, FileUtil;
function FindDefaultExecutablePathX(const Executable: string): string;
begin
result := FindDefaultExecutablePath(Executable);
if result = '' then
result := FindDefaultExecutablePath(ExtractFilePath (paramstr(0)) +Executable);
end;
procedure Riteln(S: string);
begin
//
end;
procedure FSLcmd (lCmd: string);
const
FSLOUTPUTTYPE = 'FSLOUTPUTTYPE=NIFTI_GZ';
var
AProcess: TProcess;
i: integer;
AStringList: TStringList;
//PATH,FSLDIR,lS,FULL,FSLDIRBIN: string;
begin
AProcess := TProcess.Create(nil);
AStringList := TStringList.Create;
AProcess.Environment.Add(FSLOUTPUTTYPE);
AProcess.CommandLine := lCmd;
AProcess.Options := AProcess.Options + [poWaitOnExit, poStderrToOutPut, poUsePipes];
riteln(AProcess.CommandLine);
{$IFDEF GUI}application.processmessages;{$ENDIF}
AProcess.Execute;
AStringList.LoadFromStream(AProcess.Output);
if AStringList.Count > 0 then
for i := 1 to AStringList.Count do
riteln(AStringList.Strings[i-1]);
AStringList.Free;
AProcess.Free;
end;
function FSLbet (lFilename: string; lFrac: single): string;
const
kExe = 'bet2.exe';
var
lCmd, lExe,OutName: string;
begin
lExe := FindDefaultExecutablePathX(kExe);
if lExe = '' then begin
msg('Unable to find executable '+kExe);
exit;
end;
result := ChangeFilePrefix(lFilename,'b');
result := ChangeFileExtX( result, '.nii.gz'); //e.g. .nii -> .nii.gz
lCmd := lExe+' "'+lFilename+'" "'+result +'" -f '+floattostr(lFrac);
FSLCmd (lCmd);
//msg('FSL is only available for Unix');
end;
function FSLmcflirt (lFilename4D: string): string;
begin
msg('FSL is only available for Unix');
end;
function FSLmean(lFilename4D: string): string;
begin
msg('FSL is only available for Unix');
result := '';
end;
function FSLflirt (lFilenames: tstrings): string;
begin
msg('FSL is only available for Unix');
result := '';
end;
{$ELSE}
procedure Riteln(S: string);
begin
//
end;
function GetFSLdir: string;
//const FSLBase = '/usr/local/fsl';
begin
result := gFSLBASE;
if (length(result)<1) or (not DirExists(result)) then begin
if direxists (GetEnvironmentVariable('FSLDIR')) then
result:=GetEnvironmentVariable('FSLDIR');
end;
end;
procedure FSLcmd (lCmd: string);
const
FSLOUTPUTTYPE = 'FSLOUTPUTTYPE=NIFTI_GZ';
var
AProcess: TProcess;
i: integer;
AStringList: TStringList;
PATH,FSLDIR,lS,FULL,FSLDIRBIN: string;
begin
AProcess := TProcess.Create(nil);
AStringList := TStringList.Create;
PATH:=GetEnvironmentVariable('PATH');
FSLDIR := GetFSLdir;
FSLDIRBIN := FSLDIR+'/bin' ;
if not (fileexists(FSLDIR)) then begin
msg('Please install FSL, unable to find '+FSLDIR);
exit;
end;
FULL := PATH+':'+FSLDIR+':'+FSLDIRBIN;
lS := 'FSLDIR='+FSLDIR;
AProcess.Environment.Add(lS);
lS := 'LD_LIBRARY_PATH='+FSLDIRBIN;
AProcess.Environment.Add(lS);
lS := 'PATH='+FULL;
AProcess.Environment.Add(lS);
lS := 'FSLCLUSTER_MAILOPTS="n"';
AProcess.Environment.Add(lS);
AProcess.Environment.Add(FSLOUTPUTTYPE);
AProcess.CommandLine := FSLDIRBIN+pathdelim+lCmd;
AProcess.Options := AProcess.Options + [poWaitOnExit, poStderrToOutPut, poUsePipes];
(*for i := 1 to AProcess.Environment.Count do
riteln(AProcess.Environment.Strings[i-1]);*)
riteln(AProcess.CommandLine);
{$IFDEF GUI}application.processmessages;{$ENDIF}
AProcess.Execute;
AStringList.LoadFromStream(AProcess.Output);
if AStringList.Count > 0 then
for i := 1 to AStringList.Count do
riteln(AStringList.Strings[i-1]);
AStringList.Free;
AProcess.Free;
end;
function FSLbet (lFilename: string; lFrac: single): string;
var
lCmd: string;
begin
result := ChangeFilePrefix(lFilename,'b');
lCmd := 'bet "'+lFilename+'" "'+result +'" -S -B -R -f '+floattostr(lFrac);
FSLCmd (lCmd);
end;
function FSLmean (lFilename4D: string): string;
var
lCmd: string;
begin
result := ChangeFilePrefix(lFilename4D,'w');
lCmd := 'fslmaths "'+lFilename4D+'" -Tmean "'+result +'"';
FSLCmd (lCmd);
end;
function FSLmcflirt (lFilename4D: string): string;
var
lCmd: string;
begin
result := ChangeFilePrefix(lFilename4D,'r');
lCmd := 'mcflirt -in "'+lFilename4D+'" -o "'+result +'" -cost mutualinfo';
FSLCmd (lCmd);
end;
function FSLflirt (lFilenames: tstrings): string;
var
lCmd,Ref,lFilename,Mat,Warped: string;
i: integer;
begin
lFilename := lFilenames[0];
Ref := GetFSLdir+'/data/standard/MNI152_T1_2mm_brain';
Mat := ChangeFileExtX(lFilename,'.mat');
result := ChangeFilePrefix(lFilename,'w');
lCmd := 'flirt -in "'+lFilename+'" -ref '+ref+' -out '+result+' -omat "'+Mat+ '" -bins 256 -cost mutualinfo -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12 -interp trilinear';
FSLCmd (lCmd);
if lFilenames.count <= 1 then
exit;
for i := 2 to (lFilenames.count) do begin
lFilename := lFilenames[i-1];
Warped := ChangeFilePrefix(lFilename,'w');
lCmd := 'flirt -in "'+lFilename+'" -ref "'+Ref+'" -applyxfm -init "'+Mat+'" -out "'+Warped+'"';
FSLCmd (lCmd);
end;
end;
{$ENDIF}
initialization
gFSLbase:= '/usr/local/fsl';
end.