forked from rordenlab/MRIcroGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsl_calls.pas
executable file
·273 lines (241 loc) · 7.42 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
unit fsl_calls;
{$IFDEF FPC}{$mode objfpc}{$H+} {$ENDIF}
interface
uses
{$IFNDEF UNIX} SimdUtils, FileUtil, windows,{$ENDIF}
LResources, Dialogs, Classes, SysUtils,Process;
//procedure FSLcmd (lCmd: string);
function FSLbet (lFilename: string; lFrac: single; lOutname: string = ''): string;
function FSLmcflirt (lFilename4D: string): string;
function FSLmean(lFilename4D: string): string;
function FSLflirt (lFilenames: tstrings): string;
function GetFSLdir: string;
var gFSLbase: string;
implementation
uses dcm_load, strutils;
function ChangeFilePrefix(lInName,lPrefix: string): string;
begin
result := extractfilepath(lInName)+lPrefix+extractfilename(lInName);
end;
function ChangeFileExtX( lFilename: string; lExt: string): string;
begin
result := ChangeFileExt(ExtractFileName(lFilename),'');
if upcase(ExtractFileExt(result))= '.NII' then
result := ChangeFileExt(result,''); //img.nii.gz -> img
result := ExtractFilePath(lFilename) + result + lExt;
end;
procedure msg(S: string);
begin
showmessage(S);
end;
{$IFNDEF UNIX}
procedure riteln(S: string);
begin
//showmessage(S);
end;
function GetFSLdir: string;
begin
result := '';
end;
function FindDefaultExecutablePathX(const Executable: string): string;
begin
result := FindDefaultExecutablePath(Executable);
if fileexists(result) then exit;
result := FindDefaultExecutablePath(ExtractFilePath (paramstr(0)) +Executable);
if fileexists(result) then exit;
result := ResourceDir + pathdelim + Executable;
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 + [poNoConsole,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; lOutname: string = ''): 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 := lOutname;
if lOutname = '' then
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
writeln(S);
end;
(*function RunCommandX(exe,args: string; out outputstring: string): boolean;
//e.g. RunCommandX('/bin/zsh',' -l -c "which afni"', s)
begin
outputstring := '';
if not FileExists(exe) then exit(false);
result := RunCommand(exe+' '+args, outputstring);
end;*)
function GetFSLdir: string;
//const FSLBase = '/usr/local/fsl';
label 123;
begin
result := gFSLBASE;
if (length(result) > 0) and (DirectoryExists(result)) then exit;
{$IFDEF UNIX}
result := GetEnvironmentVariable('FSLDIR');
if DirectoryExists(result) then goto 123;
result := GetEnvironmentVariable('FSL_DIR');
//if DirectoryExists(result) then goto 123;
//RunCommandX('/bin/zsh','-l -c "which fsl"', result);
//if DirectoryExists(result) then goto 123;
//RunCommandX('/bin/bash', '-l -c "which fsl"', result);
{$ENDIF}
123:
if not DirectoryExists(result) then begin
result := '';
{$IFDEF UNIX}
writeln('Unable to find FSLDIR');
{$ENDIF}
end else begin
{$IFDEF UNIX}
writeln('Found FSLDIR: '+result);
{$ENDIF}
end;
gFSLBASE := result;
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
if not (directoryexists(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];
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; lOutname: string = ''): string;
var
lCmd: string;
begin
result := lOutname;
if lOutname = '' then
result := ChangeFilePrefix(lFilename,'b');
{$IFDEF Darwin}
if PosEx('.app', result) > 0 then begin
result := extractfilename(result);
result := HomeDir(false) + result;
end;
{$ENDIF}
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.