RT = line['RT']
            if len(line['ans'])>1:
                ans = line['ans'][1:]
            if cond.id[0] == 'M':
                cond_nums = (onset, RT, ans)
            if cond.id[0] == 'L':
                cond_nums = (onset, RT)
            cond.onsets.append(cond_nums)


    last_run = run_name

for subrun in subruns:
    if 'L' in subrun.id: lm = 'lrn'
    if 'M' in subrun.id: lm = 'mem'
    outpath = uf.buildtree(root, [lm, subrun.subid, subrun.runid])
    out = open(os.path.join(outpath, 'conds.txt'), 'w')
    out.write('\n'.join([cond.id for cond in subrun.conds]))
    out.close()
    for cond in subrun.conds:
        cond.onsets = sorted(cond.onsets, key=lambda tup:float(tup[0]))
        cond_out = open(os.path.join(outpath, cond.id + '.txt'), 'w')
        cond_out.write('\n'.join([str(onset[0]) for onset in cond.onsets]))
        cond_out.close()
        RT_out = open(os.path.join(outpath, cond.id + '_RT.txt'), 'w')
        RT_out.write('\n'.join([str(onset[1]) for onset in cond.onsets]))
        RT_out.close()
        if cond.id[0] == 'M':
            ans_out = open(os.path.join(outpath, cond.id + '_ans.txt'), 'w')
            ans_out.write('\n'.join([str(onset[2]) for onset in cond.onsets]))
from __future__ import with_statement
import os, csv, shutil,tarfile, uf, dcm_ops
dest_root = '/ifs/scratch/pimri/soccog/test_working'
dst_path_lst = ['7648', 'run1L4']
uf.buildtree(dest_root, dst_path_lst)
uf.copytree('/ifs/scratch/pimri/soccog/old/SocCog_Raw_Data_By_Exam_Number/3310/E3310_e161637/s167370_1904_s6', '/ifs/scratch/pimri/soccog/test_working/7648/run1L4')
t = tarfile.open(os.path.join('/ifs/scratch/pimri/soccog/test_working/7648/run1L4','MRDC_files.tar.gz'), 'r')
t.extractall('/ifs/scratch/pimri/soccog/test_working/7648/run1L4')
for f in os.listdir('/ifs/scratch/pimri/soccog/test_working/7648/run1L4'):
   if 'MRDC' in f and 'gz' not in f:
       old = os.path.join('/ifs/scratch/pimri/soccog/test_working/7648/run1L4', f)
       new = os.path.join('/ifs/scratch/pimri/soccog/test_working/7648/run1L4', f + '.dcm')
       os.rename(old, new)
qsub_cnv_out = dcm_ops.cnv_dcm('/ifs/scratch/pimri/soccog/test_working/7648/run1L4', '7648_run1L4', '/ifs/scratch/pimri/soccog/scripts/mastersort/scripts_dir/cnv')
#qsub_cln_out = dcm_ops.cnv_dcm('/ifs/scratch/pimri/soccog/test_working/7648/run1L4', '7648_run1L4', '/ifs/scratch/pimri/soccog/scripts/mastersort/scripts_dir/cln')
    ser_dir = ser_dir.replace(' ', '')
    ser_id = subj_id + '_' + ser_name
    src_path = os.path.join(src_root, exam, ex_dir, ser_dir)
    notes = line['Notes']
    if 'anat' in ser_name:
        if 'best' in notes:
            dst_path_lst = [subj_id, 'anat']
        else:
            dst_path_lst = [subj_id, 'spare_anat', ser_name]

    if 'run' in ser_name:
        dst_path_lst = [subj_id, 'func', ser_name]

    ###COPY RAW DIRECTORY TO NEW DIR, SORTED BY SUBJECT###

    dst_path = uf.buildtree(dst_root, dst_path_lst)
    uf.copytree(src_path, dst_path)

    logline['CopiedRaw'] = dst_path

    ###UNTAR FILES###

    t = tarfile.open(os.path.join(dst_path,'MRDC_files.tar.gz'), 'r')
    t.extractall(dst_path)

    #cmd = 'tar -xvzf MRDC_files.tar.gz'
    #res = subprocess.check_output(cmd,cwd=dst_path,shell=True)

    files = [f for f in os.listdir(dst_path) if f[0] == 'i']
    logline['Extracted'] = 'Extracted_' + str(len(files)) + '_files_' + os.path.join(dst_path, files[0])