forked from aces/Loris-MRI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMincinfo_wrapper.pl
executable file
·322 lines (278 loc) · 9.86 KB
/
Mincinfo_wrapper.pl
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/perl -w
#---------------------------------------------------------------------------
#@COPYRIGHT :
# Copyright 1998, Alex P. Zijdenbos
# McConnell Brain Imaging Centre,
# Montreal Neurological Institute, McGill University.
# Permission to use, copy, modify, and distribute this
# software and its documentation for any purpose and without
# fee is hereby granted, provided that the above copyright
# notice appear in all copies. The author and McGill University
# make no representations about the suitability of this
# software for any purpose. It is provided "as is" without
# express or implied warranty.
#----------------------------------------------------------------------------
#$RCSfile: Mincinfo_wrapper.pl,v $
#$Revision: 1.1.1.1 $
#$Author: moi $
#$Date: 2006/05/31 11:33:39 $
#$State: Exp $
#---------------------------------------------------------------------------
use MNI::Startup;
use MNI::Spawn;
use MNI::FileUtilities qw(check_output_dirs check_files);
use Getopt::Tabular qw(GetOptions);
use strict;
# User-modifyable globals
my($PrintFile) = 0;
my($Tab) = 0;
my($InsertLine) = 0;
# Other globals
my($Usage, $Help);
my(@Files);
my(@MincInfoOpt);
my(@Variables);
&Initialize;
my($file);
foreach $file (@Files) {
my(@miOpt) = @MincInfoOpt;
if (@Variables) {
if ($file =~ /\.(gz|z|Z)$/) {
my $tempfile = &ReplaceDir($TmpDir, $`);
&Spawn("mincexpand -header_only $file $tempfile");
$file = $tempfile;
}
my($variable);
foreach $variable (@Variables) {
my($status, $output) = &Spawn(['mincinfo', '-varatts', $variable, $file]);
&Fatal() if $status;
my(@attributes) = split(/\s/, $output);
my($attribute);
foreach $attribute (@attributes) {
push(@miOpt, ('-attvalue', "${variable}:${attribute}"));
}
}
}
my($output);
&Spawn(['mincinfo', @miOpt, $file], stdout => \$output);
chop($output);
$output = "$file\n$output" if ($PrintFile);
$output =~ s/\n/\t/g if ($Tab);
$output .= "\n";
$output .= "\n" if ($InsertLine);
print $output;
}
# ------------------------------ MNI Header ----------------------------------
#@NAME : &CreateInfoText
#@INPUT : none
#@OUTPUT : none
#@RETURNS : nothing
#@DESCRIPTION: Sets the $Help, $Usage, $Version, and $LongVersion globals,
# and registers the first two with ParseArgs so that user gets
# useful error and help messages.
#@METHOD :
#@GLOBALS : $Help, $Usage, $Version, $LongVersion
#@CALLS :
#@CREATED : 95/08/25, Greg Ward (from code formerly in &ParseArgs)
#@MODIFIED :
#-----------------------------------------------------------------------------
sub CreateInfoText
{
$Usage = <<USAGE;
Usage: $ProgramName [options] <mincfile> [<mincfile> ...]
$ProgramName -help
USAGE
$Help = <<HELP;
$ProgramName is a wrapper around mincinfo, providing additional functionality and allowing for multiple files.
HELP
&Getopt::Tabular::SetHelp ($Help, $Usage);
}
# ------------------------------ MNI Header ----------------------------------
#@NAME : &SetupArgTables
#@INPUT : none
#@OUTPUT : none
#@RETURNS : References to the four option tables:
# @site_args
# @pref_args
# @protocol_args
# @other_args
#@DESCRIPTION: Defines the tables of command line (and config file)
# options that we pass to ParseArgs. There are four
# separate groups of options, because not all of them
# are valid in all places. See comments in the routine
# for details.
#@METHOD :
#@GLOBALS : makes references to many globals (almost all of 'em in fact)
# even though most of them won't have been defined when
# this is called
#@CALLS :
#@CREATED : 95/08/23, Greg Ward
#@MODIFIED :
#-----------------------------------------------------------------------------
sub SetupArgTables
{
my (@args) =
(
["Mincinfo_wrapper.pl options", "section"],
["-image_info", "call", undef, \&MincInfoOption,
"Print out the default information about the images"],
["-dimnames", "call", undef, \&MincInfoOption,
"Print the names of the dimensions in the file"],
["-varnames", "call", undef, \&MincInfoOption,
"Print the names of the variables in the file."],
["-dimlength", "call", 1, \&OneArgMincInfoOption,
"Print the length of the specified dimension."],
["-vartype", "call", 1, \&OneArgMincInfoOption,
"Print the type of the specified variable."],
["-vardims", "call", 1, \&OneArgMincInfoOption,
"Print the dimension names for the specified variable."],
["-varatts", "call", 1, \&OneArgMincInfoOption,
"Print the attribute names for the specified variable."],
["-varvalues", "call", 1, \&OneArgMincInfoOption,
"Print the values for the specified variable."],
["-atttype", "call", 1, \&OneArgMincInfoOption,
"Print the type of the specified attribute (variable:attribute)."],
["-attvalue", "call", 1, \&OneArgMincInfoOption,
"Print the value(s) of the specified attribute (variable:attribute)."],
["-error_string", "call", 1, \&OneArgMincInfoOption,
"Error to print on stdout (default = exit with error status)."],
["Higher-level options", "section"],
["-file", "boolean", 1, \$PrintFile,
"Print file name. This will always be printed first."],
["-date", "call", 1, \&HighLevelOption,
"Print acquisition date and time."],
["-time", "call", 1, \&HighLevelOption,
"Synonymous with -date."],
["-history", "call", 1, \&HighLevelOption,
"Print history."],
["-trte", "call", 1, \&HighLevelOption,
"Print TR and TE."],
["-fa", "call", 1, \&HighLevelOption,
"Print flip angle."],
["-dimensions", "call", 1, \&HighLevelOption,
"Print zyx dimensions."],
["-voxel_dimensions", "call", 1, \&HighLevelOption,
"Print zyx voxel dimensions."],
["-starts", "call", 1, \&HighLevelOption,
"Print zyx voxel start values."],
["-variable", "call", 1, \&HighLevelOption,
"Print all attribute values of the specified variable.", "<variable>"],
["Formatting options", "section"],
["-tab", "boolean", 1, \$Tab,
"Use tabs (\\t) rather than newlines (\\n) between option outputs."],
["-insert_line", "boolean", 1, \$InsertLine,
"Print an empty line between each processed file."],
);
(\@DefaultArgs, \@args);
}
# ------------------------------ MNI Header ----------------------------------
#@NAME : &Initialize
#@INPUT :
#@OUTPUT :
#@RETURNS :
#@DESCRIPTION: Sets global variables from configuration file, parses
# command line, parses protocol file for more global variables,
# finds required programs, and sets their options. Dies on
# any error.
#@METHOD :
#@GLOBALS : site-specific: $ModelDir, $Model, $Protocol
# preferences: $Verbose, $Execute, $Clobber, $Debug, $KeepTmp
# protocol (data-specific preprocessing): @Subsample, @Crop,
# $Objective, @Blurs, $Blur
# $ProtocolArgs
#@CALLS : &JobControl::SetOptions
# &JobControl::AddProgramOptions
# &SetupArgTables
# &ReadConfigFile
# &GetOptions
# &ReadProtocol (indirectly through ParseArgs)
#
#@CREATED :
#@MODIFIED : incessantly
#-----------------------------------------------------------------------------
sub Initialize
{
my (@all_args);
$, = ' '; # set output field separator
# Set defaults for the global variables.
$Verbose = 1;
$Execute = 1;
$Clobber = 0;
$Debug = 0;
$KeepTmp = 0;
&CreateInfoText;
my($defargs, $args) = &SetupArgTables;
@all_args = (@$defargs, @$args);
&GetOptions (\@all_args, \@ARGV, \@Files) || die "\n";
if (@Files <= 0)
{
warn $Usage;
die "Please specify one or more input MINC volumes\n";
}
RegisterPrograms(['mincinfo', 'mincexpand']) || die;
MNI::Spawn::SetOptions (strict => 2);
check_files(@Files) || die;
check_output_dirs($TmpDir) if $Execute;
self_announce if $Verbose;
}
sub MincInfoOption {
my ($option) = @_;
push(@MincInfoOpt, $option);
1;
}
sub OneArgMincInfoOption {
my ($option, $rest) = @_;
my ($value) = shift @$rest;
# if (!defined($value) || ($value =~ /^-/)) {
if (!defined($value)) {
&Fatal("$option requires an argument");
}
push(@MincInfoOpt, ($option, $value));
1;
}
sub HighLevelOption {
my ($option, $rest) = @_;
SWITCH: {
if ($option =~ /^-date$/) {
&OneArgMincInfoOption('-attvalue', ['study:start_time']);
last SWITCH;
}
if ($option =~ /^-history$/) {
&OneArgMincInfoOption('-attvalue', [':history']);
last SWITCH;
}
if ($option =~ /^-trte$/) {
&OneArgMincInfoOption('-attvalue', ['acquisition:repetition_time']);
&OneArgMincInfoOption('-attvalue', ['acquisition:echo_time']);
last SWITCH;
}
if ($option =~ /^-fa$/) {
&OneArgMincInfoOption('-attvalue', ['acquisition:flip_angle']);
last SWITCH;
}
if ($option =~ /^-dimensions$/) {
&OneArgMincInfoOption('-dimlength', ['zspace']);
&OneArgMincInfoOption('-dimlength', ['yspace']);
&OneArgMincInfoOption('-dimlength', ['xspace']);
last SWITCH;
}
if ($option =~ /^-voxel_dimensions$/) {
&OneArgMincInfoOption('-attvalue', ['zspace:step']);
&OneArgMincInfoOption('-attvalue', ['yspace:step']);
&OneArgMincInfoOption('-attvalue', ['xspace:step']);
last SWITCH;
}
if ($option =~ /^-starts$/) {
&OneArgMincInfoOption('-attvalue', ['zspace:start']);
&OneArgMincInfoOption('-attvalue', ['yspace:start']);
&OneArgMincInfoOption('-attvalue', ['xspace:start']);
last SWITCH;
}
if ($option =~ /^-variable$/) {
push(@Variables, shift @$rest);
last SWITCH;
}
return 0;
}
1;
}