Repository: nipy/nipype

Issue:
Empty output files list if filename contains metacharacters in dcm2nii
### Summary
If the dcm2nii output file names contain regex metacharacters (eg: [] or *)  nipype dcm2nii interface is not able to parse the output filename properly and will return empty output files. 

### Actual behavior
```
from nipype.interfaces.dcm2nii import Dcm2niix
converter = Dcm2niix()
converter.inputs.source_dir = 'testdicomdir'
converter.inputs.anon_bids = True
converter.inputs.bids_format = True
converter.inputs.compress = 'y'
converter.inputs.compression = 6
converter.inputs.out_filename = '%d'
output = converter.run()
>>> Convert 109 DICOM as /opt/work/[DICOMEXAMPLE_-_Frame_1_of_6] 
Compress: "/usr/bin/pigz" -b 960 -n -f -6 "/opt/work/[DICOMEXAMPLE_-_Frame_1_of_6].nii"

output.outputs 
>>> bids = <undefined>
>>> bvals = <undefined>
>>> bvecs = <undefined>
>>> converted_files = <undefined>
```
### Expected behavior
```
output.outputs 
>>> bids = <undefined>
>>> bvals = <undefined>
>>> bvecs = <undefined>
>>> converted_files = <undefined>
bids = ['/opt/work/[DICOMEXAMPLE_-_Frame_1_of_6].json']
bvals = <undefined>
bvecs = <undefined>
converted_files = ['/opt/work/[DICOMEXAMPLE_-_Frame_1_of_6].nii.gz']
```
### How to replicate the behavior
Set the output filename template to a name that contains metacharacters

### Platform details:

```
nipype.get_info()
{'pkg_path': '/usr/local/lib/python3.6/dist-packages/nipype', 'commit_source': 'installation', 'commit_hash': '071523a50', 'nipype_version': '1.5.1', 'sys_version': '3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]', 'sys_executable': '/usr/bin/python3', 'sys_platform': 'linux', 'numpy_version': '1.18.5', 'scipy_version': '1.5.4', 'networkx_version': '2.5.1', 'nibabel_version': '3.1.1', 'traits_version': '6.2.0'}

```

### Execution environment

- Container: neurodebian:bionic
- Python version: 3.6.9



Discussion:

Resolving patch:
diff --git a/nipype/interfaces/dcm2nii.py b/nipype/interfaces/dcm2nii.py
--- a/nipype/interfaces/dcm2nii.py
+++ b/nipype/interfaces/dcm2nii.py
@@ -4,6 +4,7 @@
 import re
 from copy import deepcopy
 import itertools as it
+import glob
 from glob import iglob
 
 from ..utils.filemanip import split_filename
@@ -494,4 +495,6 @@ def _list_outputs(self):
 
 # https://stackoverflow.com/a/4829130
 def search_files(prefix, outtypes):
-    return it.chain.from_iterable(iglob(prefix + outtype) for outtype in outtypes)
+    return it.chain.from_iterable(
+        iglob(glob.escape(prefix + outtype)) for outtype in outtypes
+    )


Test patch:
diff --git a/nipype/interfaces/tests/test_dcm2nii.py b/nipype/interfaces/tests/test_dcm2nii.py
new file mode 100644
--- /dev/null
+++ b/nipype/interfaces/tests/test_dcm2nii.py
@@ -0,0 +1,21 @@
+import pytest
+
+
+from nipype.interfaces import dcm2nii
+
+
+@pytest.mark.parametrize(
+    "fname, extension",
+    [
+        ("output_1", ".txt"),
+        ("output_w_[]_meta_1", ".json"),
+        ("output_w_**^$?_meta_2", ".txt"),
+    ],
+)
+def test_search_files(tmp_path, fname, extension):
+    tmp_fname = fname + extension
+    test_file = tmp_path / tmp_fname
+    test_file.touch()
+    actual_files_list = dcm2nii.search_files(str(tmp_path / fname), [extension])
+    for f in actual_files_list:
+        assert str(test_file) == f