import os.path
import json
import numpy as np
from pprint import pprint
from sys import argv

#args : SUBJECT_DIR, ${overlapFlag}, JSONTABLE filename, number_ROIS
subject_dir = argv[1] 
overlapName = argv[2] 
jsonFile = argv[3] 
nb_ROIS = argv[4]

DIR_Surfaces = subject_dir + '/OutputSurfaces' + overlapName + '/labelSurfaces/'

#Open Json file and parse 
with open(jsonFile) as data_file:    
    data = json.load(data_file)

#Create file for seedList
seedPath = subject_dir + '/seeds.txt'
seedList = open(seedPath, 'w')

#Put all MatrixRow to -1 
for seed in data:
  seed['MatrixRow']=-1

seedID = 0 
#For each file in DIR
for i in range(int(nb_ROIS)):
    numberStr = str(i+1)
    file = DIR_Surfaces + numberStr + ".asc"
    val = os.path.isfile(file)
    if (val == True):
      #Write in seedList Path 
      seedList.write(file + "\n")
      seedID = seedID + 1
      #Update JSON file : 'MatrixRow'
      for j in data:
        if(j['AAL_ID'] == i+1):
          j['MatrixRow'] = seedID
     
seedList.close()

#Update JSON file 
with open(jsonFile, 'w') as txtfile:
    json.dump(data, txtfile, indent = 2)

@@ -2,21 +2,16 @@
 mport os
-.path
 %0Aimport 
@@ -251,16 +251,29 @@
 faces = 
+os.path.join(
 subject_
@@ -275,21 +275,19 @@
 ject_dir
- +
+,
  '
-/
 OutputSu
@@ -307,21 +307,19 @@
 rlapName
- +
+,
  '
-/
 labelSur
@@ -327,10 +327,10 @@
 aces
-/
 '
+)
 %0A%0A#O
@@ -623,16 +623,18 @@
  in DIR%0A
+# 
 for i in
@@ -655,20 +655,22 @@
 ROIS)):%0A
+#
     
+ 
 numberSt
@@ -682,16 +682,18 @@
 tr(i+1)%0A
+# 
     file
@@ -729,16 +729,18 @@
  %22.asc%22%0A
+# 
     val 
@@ -762,16 +762,18 @@
 e(file)%0A
+# 
     if (
@@ -786,22 +786,24 @@
  True):%0A
+#
       
+ 
 #Write i
@@ -819,16 +819,18 @@
 t Path %0A
+# 
       se
@@ -855,16 +855,18 @@
 + %22%5Cn%22)%0A
+# 
       se
@@ -921,22 +921,16 @@
 rixRow'%0A
-      
 for j in
@@ -942,71 +942,187 @@
 :%0A  
-      if(j%5B'
+filename = os.path.join(DIR_Surfaces, str(j%5B%22
 AAL_ID
-'%5D == i+1):%0A          j%5B'MatrixRow'%5D = seedID
+%22%5D) + %22.asc%22)%0A  if(os.path.isfile(file)):%0A    j%5B'MatrixRow'%5D = seedID%0A    seedID = seedID + 1%0A  %09seedList.write(filename + %22%5Cn%22)
 %0A   

Use AAL_ID to construct filename and set MatrixRow