def initialize_system(m1, molecule_type, basis, numranges, reslow, numcont):

    resid = m1.resid()
    first_last_resid = [resid[0], resid[-1]]

    print 'first_last_resid = ', first_last_resid

    print 'resid = ', resid

    txtOutput = None

    if(molecule_type == 'protein'):
        basis_filter = 'name[i] == "' + basis + '"'
        error, basis_mask = m1.get_subset_mask(basis_filter)
        check_error(error)
        basis_m1 = sasmol.SasMol(1)
        error = m1.copy_molecule_using_mask(basis_m1, basis_mask, 0)
        check_error(error)
        basis_resname = basis_m1.resname()
        basis_resid = basis_m1.resid()

        print 'reslow = ', reslow
        print 'numcont = ', numcont
        print 'numranges = ', numranges
        print 'basis_resname = ', basis_resname
        print 'basis_resid = ', basis_resid

        respsi = []
        resphi = []
        dihedral_energy.protein_initialization(
            respsi, resphi, basis_resid, basis_resname, numranges, reslow, numcont, first_last_resid, txtOutput)
        dihedral_parameters = [respsi, resphi]
    elif(molecule_type == 'rna'):
        #rna_filter = 'name[i] == "P"'
        #rna_filter = 'name[i] == "O5\'"'
        rna_filter = 'name[i] == "' + basis + '"'
        error, basis_mask = m1.get_subset_mask(rna_filter)
        basis_m1 = sasmol.SasMol(1)
        error = m1.copy_molecule_using_mask(basis_m1, basis_mask, 0)
        basis_resname = basis_m1.resname()
        basis_resid = basis_m1.resid()

        print 'sum(basis_mask) rna = ', sum(basis_mask)
        print 'len(resid) rna = ', len(resid)
        resalpha = []
        resbeta = []
        resgamma = []
        resdelta = []
        resepsilon = []
        reseta = []
        dihedral_energy.rna_initialization(resalpha, resbeta, resgamma, resdelta, resepsilon,
                                           reseta, basis_resid, basis_resname, numranges, reslow, numcont, first_last_resid, txtOutput)
        dihedral_parameters = [
            resalpha, resbeta, resgamma, resdelta, resepsilon, reseta]

    print 'dihedral parameters = ', dihedral_parameters

    flexible_residues = dihedral_monte_carlo.get_flexible_residues(
        numranges, reslow, numcont)

    print 'molecule_type = ', molecule_type

    residue_rotation_indices, residue_rotation_mask = dihedral_monte_carlo.get_rotation_indices(
        m1, molecule_type, flexible_residues, txtOutput)

    step_parameters = step.Setup()

    return dihedral_parameters, flexible_residues, residue_rotation_indices, residue_rotation_mask, step_parameters, first_last_resid
def initialize_system(m1, molecule_type, basis, numranges, reslow, numcont):

    resid = m1.resid()
    first_last_resid = [resid[0], resid[-1]]

    print 'first_last_resid = ', first_last_resid

    print 'resid = ', resid

    txtOutput = None

    if (molecule_type == 'protein'):
        basis_filter = 'name[i] == "' + basis + '"'
        error, basis_mask = m1.get_subset_mask(basis_filter)
        check_error(error)
        basis_m1 = sasmol.SasMol(1)
        error = m1.copy_molecule_using_mask(basis_m1, basis_mask, 0)
        check_error(error)
        basis_resname = basis_m1.resname()
        basis_resid = basis_m1.resid()

        print 'reslow = ', reslow
        print 'numcont = ', numcont
        print 'numranges = ', numranges
        print 'basis_resname = ', basis_resname
        print 'basis_resid = ', basis_resid

        respsi = []
        resphi = []
        dihedral_energy.protein_initialization(respsi, resphi, basis_resid,
                                               basis_resname, numranges,
                                               reslow, numcont,
                                               first_last_resid, txtOutput)
        dihedral_parameters = [respsi, resphi]
    elif (molecule_type == 'rna'):
        #rna_filter = 'name[i] == "P"'
        #rna_filter = 'name[i] == "O5\'"'
        rna_filter = 'name[i] == "' + basis + '"'
        error, basis_mask = m1.get_subset_mask(rna_filter)
        basis_m1 = sasmol.SasMol(1)
        error = m1.copy_molecule_using_mask(basis_m1, basis_mask, 0)
        basis_resname = basis_m1.resname()
        basis_resid = basis_m1.resid()

        print 'sum(basis_mask) rna = ', sum(basis_mask)
        print 'len(resid) rna = ', len(resid)
        resalpha = []
        resbeta = []
        resgamma = []
        resdelta = []
        resepsilon = []
        reseta = []
        dihedral_energy.rna_initialization(resalpha, resbeta, resgamma,
                                           resdelta, resepsilon, reseta,
                                           basis_resid, basis_resname,
                                           numranges, reslow, numcont,
                                           first_last_resid, txtOutput)
        dihedral_parameters = [
            resalpha, resbeta, resgamma, resdelta, resepsilon, reseta
        ]

    print 'dihedral parameters = ', dihedral_parameters

    flexible_residues = dihedral_monte_carlo.get_flexible_residues(
        numranges, reslow, numcont)

    print 'molecule_type = ', molecule_type

    residue_rotation_indices, residue_rotation_mask = dihedral_monte_carlo.get_rotation_indices(
        m1, molecule_type, flexible_residues, txtOutput)

    step_parameters = step.Setup()

    return dihedral_parameters, flexible_residues, residue_rotation_indices, residue_rotation_mask, step_parameters, first_last_resid
def dihedralgenerate(variables, txtOutput):

    runname, dcdfile, path, pdbfile, trials, goback, temp, molecule_type, numranges, dtheta, reslow, numcont, lowres1, highres1, basis, cutoff, lowrg, highrg, zflag, zcutoff, cflag, confile, nonbondflag, nonbondscale, psffilepath, psffilename, parmfilepath, parmfilename, plotflag, directedmc, seed = unpack_variables(
        variables)

    alignflag = '1'
    saveflag = '0'

    pdbfilename = pdbfile

    if(runname[-1] == '/'):
        lin = len(runname)
        runname = runname[:lin - 1]

    direxist = os.path.exists(runname)
    if(direxist == 0):
        os.system('mkdir -p ' + runname)

    genpath = runname + '/generate'
    genpaths = genpath + '/'
    direxist = os.path.exists(genpath)
    if(direxist == 0):
        os.system('mkdir -p ' + genpath)

    if(path != ""):
        if path[-1] != "/":
            path += '/'

    cpfile = os.path.join(path, pdbfilename)
    cpst = 'cp ' + cpfile + ' ' + genpaths
    os.system(cpst)

    parmfilepath += '/'

    fileexist = os.path.exists('.last_sas')
    if(fileexist == 1):
        os.system('mv -f .last_sas .last_sas_bu')
    lastsasfile = open('./.last_sas', 'w')
    lastsasfile.write('run_name\t' + runname + '\n')
    lastsasfile.write('pdb_name\t' + pdbfile + '\n')
    lastsasfile.write('dcd_name\t' + dcdfile + '\n')

    kb = 1.380658E-23  # J/K
    beta = 1.0 / (temp * kb)

    m1 = sasmol.SasMol(0)
    m2 = sasmol.SasMol(1)

    m1.read_pdb(path + pdbfile, saspdbrx_topology=True)
    nf1 = m1.number_of_frames()
    m1.calccom(0)

    dcdoutfile = m1.open_dcd_write(genpaths + dcdfile)

    resid = m1.resid()
    first_last_resid = [resid[0], resid[-1]]

    print 'first_last_resid = ', first_last_resid

    cutoff_array = []
    vdw_factor = 0.3

    if(molecule_type == 'protein'):

        if(basis.lower() == 'all'):
            print 'setting up all atom overlap arrays'
            m1.set_average_vdw()
            npairs = m1.natoms() * (m1.natoms() - 1) / 2
            cutoff_array = numpy.zeros(npairs, numpy.float)
            pairs.pairs(m1.atom_vdw(), cutoff_array)
            basis_filter = 'not name[i] == None'

        elif(basis.lower() == 'backbone'):
            basis_filter = 'name[i] == "N" or name[i] == "CA" or name[i] == "C" '
        elif(basis.lower() == 'heavy'):
            basis_filter = 'not name[i][0] == "H" '
        else:
            string_basis = string.split(basis, ",")
            number_basis_types = len(string_basis)
            for i in xrange(number_basis_types):
                if(i == 0):
                    basis_filter = 'name[i] == "' + string_basis[i] + '" '
                else:
                    basis_filter = basis_filter + \
                        ' or name[i] == "' + string_basis[i] + '" '

        print 'basis_filter = ', basis_filter

        error, basis_mask = m1.get_subset_mask(basis_filter)

        basis_atom = "CA"
        setup_basis_filter = 'name[i] == "' + basis_atom + '"'
        error, setup_basis_mask = m1.get_subset_mask(setup_basis_filter)

        basis_m1 = sasmol.SasMol(1)
        error = m1.copy_molecule_using_mask(basis_m1, setup_basis_mask, 0)
        basis_resname = basis_m1.resname()
        basis_resid = basis_m1.resid()

        respsi = []
        resphi = []
        dihedral_energy.protein_initialization(
            respsi, resphi, basis_resid, basis_resname, numranges, reslow, numcont, first_last_resid, txtOutput)
        dihedral_parameters = [respsi, resphi]

    elif(molecule_type == 'rna'):

        #	ATOM N1   NN2    -0.13  !                            \
        #	ATOM C6   CN3     0.05  !        O1P    H5' H4'  O4'  \
        #	ATOM H6   HN3     0.17  !         |      |    \ /   \  \
        #	ATOM C5   CN3    -0.13  !        -P-O5'-C5'---C4'    C1'
        #	ATOM H5   HN3     0.07  !         |      |     \     / \
        #	ATOM C2   CN1     0.52  !        O2P    H5''   C3'--C2' H1'
        #	ATOM O2   ON1C   -0.49  !                     / \   / \
        #	ATOM N3   NN3    -0.66  !                  O3' H3' O2' H2''
        #	ATOM C4   CN2     0.65  !                   |       |
        #	ATOM N4   NN1    -0.75  !                          H2'

        if(basis.lower() == 'all'):
            print 'setting up all atom overlap arrays'
            m1.set_average_vdw()
            npairs = m1.natoms() * (m1.natoms() - 1) / 2
            cutoff_array = numpy.zeros(npairs, numpy.float)
            pairs.pairs(m1.atom_vdw(), cutoff_array)
            basis_filter = 'not name[i] == None'

        elif(basis.lower() == 'backbone'):
            basis_filter = 'name[i] == "P" or name[i] == "O5\'" or name[i] == "C5\'" or name[i] == "C4\'" or name[i] == "C3\'" or name[i] == "O3\'" '
        elif(basis.lower() == 'heavy'):
            basis_filter = 'not name[i][0] == "H" '
        else:
            string_basis = string.split(basis, ",")
            number_basis_types = len(string_basis)
            for i in xrange(number_basis_types):
                if(i == 0):
                    basis_filter = 'name[i] == "' + string_basis[i] + '" '
                else:
                    basis_filter = basis_filter + \
                        ' or name[i] == "' + string_basis[i] + '" '

        error, basis_mask = m1.get_subset_mask(basis_filter)

        basis_atom = "P"
        setup_basis_filter = 'name[i] == "' + basis_atom + '"'

        error, setup_basis_mask = m1.get_subset_mask(setup_basis_filter)
        basis_m1 = sasmol.SasMol(1)
        error = m1.copy_molecule_using_mask(basis_m1, setup_basis_mask, 0)
        basis_resname = basis_m1.resname()
        basis_resid = basis_m1.resid()

        resalpha = []
        resbeta = []
        resgamma = []
        resdelta = []
        resepsilon = []
        reseta = []
        dihedral_energy.rna_initialization(resalpha, resbeta, resgamma, resdelta, resepsilon,
                                           reseta, basis_resid, basis_resname, numranges, reslow, numcont, first_last_resid, txtOutput)
        dihedral_parameters = [
            resalpha, resbeta, resgamma, resdelta, resepsilon, reseta]
    else:
        print 'molecule_type = ', molecule_type
        print 'm1.moltype() = ', m1.moltype()
        message = 'rotation basis is not setup for molecule type = ' + \
            m1.moltype()[0]
        message += ' : stopping here'
        print_failure(message, txtOutput)
        return

    if(cflag == 1):

        filter_flag = 0
        error, constraint_basis1_array, constraint_basis2_array, distance_array, type_array = constraints.read_constraints(
            m1, confile, filter_flag)

        mask_a_array = []
        mask_b_array = []

        for i in xrange(len(distance_array)):
            print constraint_basis1_array[i]
            print constraint_basis2_array[i]
            print distance_array[i]
            print type_array[i]

            error, local_mask_a_array = m1.get_subset_mask(
                constraint_basis1_array[i])
            error, local_mask_b_array = m1.get_subset_mask(
                constraint_basis2_array[i])

            mask_a_array.append(local_mask_a_array)
            mask_b_array.append(local_mask_b_array)

            # testing code is below (to the end of this loop) ... remove at
            # some point

            test_filename1 = 'dum_a_' + str(i) + '.pdb'
            test_filename2 = 'dum_b_' + str(i) + '.pdb'
            new_beta = m1.beta()
            m1.setBeta('0.00')
            value = '1.00'
            error = m1.set_descriptor_using_mask(
                local_mask_a_array, beta, value)
            m1.write_pdb(test_filename1, 0, 'w')
            value = '0.00'
            error = m1.set_descriptor_using_mask(
                local_mask_a_array, beta, value)
            value = '1.00'
            error = m1.set_descriptor_using_mask(
                local_mask_b_array, beta, value)
            m1.write_pdb(test_filename2, 0, 'w')

            value = '0.00'
            error = m1.set_descriptor_using_mask(
                local_mask_b_array, beta, value)

    else:
        mask_a_array = []
        mask_b_array = []
        distance_array = []
        type_array = []

    if(nonbondflag == 1):
        message = '> energy implementation is not implemented in this version '
        message += ' : stopping here'
        print_failure(message, txtOutput)
        return

    else:
        charge = []
        exclusionlist = []
        onefourlist = []
        vdwparam = []
        angleparam = []
        dihedralparam = []

        angleoutfile = open(genpaths + runname + '.angle_energies.txt', 'a')

    hrg = 0.0
    lowestrg = 1000.0
    accepted = 0
    over = 0
    badrg = 0
    nsteps = 0
    arg = 0.0
    trg = 0.0
    badz = 0
    badc = 0

    pairdat = ['dum', 1, 1.0]
    all_rg_tally = []
    accepted_rg_tally = []

    if(plotflag == 1):
        graph = Gnuplot.Gnuplot(debug=1)
        graph.clear()
        graph('set title "Rg Results"')
        graph.xlabel('Structure Number')
        graph.ylabel('Rg (Angstrom^2)')

    failtally = 0
    acc = 0
    oofile = 'objects.txt'

    lineintxtOutput = ''.join(['=' for x in xrange(60)])
    ttxt = time.ctime()
    txtOutput.put("\n%s \n" % (lineintxtOutput))
    txtOutput.put("DATA FROM RUN: %s \n\n" % (ttxt))

    coor = m1.coor()

    frame = 0

    flexible_residues = get_flexible_residues(numranges, reslow, numcont)

    print 'molecule_type = ', molecule_type

    residue_rotation_indices, residue_rotation_mask = get_rotation_indices(
        m1, molecule_type, flexible_residues, txtOutput)
    name = m1.name()

    step_parameters = step.Setup()

    # get alignment sub molecule

    #align_filter = 'name[i] == "'+basis+'" and (resid[i] >= '+str(lowres1)+' and resid[i] <= '+str(highres1)+')'
    align_filter = 'name[i] == "' + basis_atom + \
        '" and (resid[i] >= ' + str(lowres1) + \
        ' and resid[i] <= ' + str(highres1) + ')'
    error, align_mask = m1.get_subset_mask(align_filter)

    sub_m1 = sasmol.SasMol(2)
    error = m1.copy_molecule_using_mask(sub_m1, align_mask, 0)
    com_sub_m1 = sub_m1.calccom(0)
    sub_m1.center(0)
    coor_sub_m1 = sub_m1.coor()[0]

    sub_m2 = sasmol.SasMol(4)
    error = m1.copy_molecule_using_mask(sub_m2, align_mask, 0)

    # MAIN LOOP

    if(seed[0] == 1):
        from numpy.random import RandomState
        seed_object = RandomState(seed[1])
    else:
        seed_object = -1

    minx = []
    miny = []
    minz = []
    maxx = []
    maxy = []
    maxz = []

    if(directedmc > 0):
        rg_difference_list = []
        directed_rg_list = []
        accepted_rg_list = []
        rg_list_length = 10  # hardwired

    for i in range(trials):
        print '.',
        sys.stdout.flush()
        # print i ; sys.stdout.flush()

        vdi, vdf, indices, this_mask = step_parameters.chooser(
            coor, m1, pairdat, dtheta, numranges, reslow, numcont, dihedral_parameters, beta, residue_rotation_indices, residue_rotation_mask, nonbondflag, first_last_resid, molecule_type, seed_object)

        an = pairdat[0]
        q0 = pairdat[1]
        th = pairdat[2]
        nsteps += 1
        re = [0, 0, 0, 0.0, 0.0, lowestrg, hrg, 0, 0, []]

        this_first_coor_x = m1.coor()[0, 0, 0]
        this_first_coor_y = m1.coor()[0, 0, 1]
        this_first_coor_z = m1.coor()[0, 0, 2]
        this_final_coor_x = m1.coor()[0, -1, 0]
        this_final_coor_y = m1.coor()[0, -1, 1]
        this_final_coor_z = m1.coor()[0, -1, 2]

        if(this_first_coor_x == this_final_coor_x and this_first_coor_y == this_final_coor_y and this_first_coor_z == this_final_coor_z):
            print 'this_first_coor_x = ', this_first_coor_x
            print 'this_first_coor_y = ', this_first_coor_y
            print 'this_first_coor_z = ', this_first_coor_z
            print 'this_final_coor_x = ', this_final_coor_x
            print 'this_final_coor_y = ', this_final_coor_y
            print 'this_final_coor_z = ', this_final_coor_z

            if(i > 0):
                print 'previous_first_coor = ', previous_first_coor
                print 'previous_final_coor = ', previous_final_coor

            print 'failtally = ', failtally
            print 'goback = ', goback

            print '>>> STOPPING NOW'

            sys.stdout.flush()

            sys.exit()

        else:
            previous_first_coor = m1.coor()[0, 0, :]
            previous_final_coor = m1.coor()[0, -1, :]

        newafile = dihedral_rotate.rotate(coor, m1, q0, th, an, cutoff, lowrg, highrg, re, accepted, zflag, zcutoff, cflag, dcdoutfile, indices, this_mask, basis_mask,
                                          sub_m2, align_mask, coor_sub_m1, com_sub_m1, mask_a_array, mask_b_array, distance_array, type_array, first_last_resid, molecule_type, cutoff_array, vdw_factor)

#		print 'back from dihedral_rotate'; sys.stdout.flush()

        accepted = accepted + re[0]
        over = over + re[1]
        badrg = badrg + re[2]
        rg_value = re[3]
        trg = trg + re[3]
        arg = arg + re[4]
        lowestrg = re[5]
        hrg = re[6]
        badz = badz + re[7]
        badc = badc + re[8]
        if(len(re[9]) > 0):
            minmax = re[9]

            minx.append(minmax[0][0])
            miny.append(minmax[0][1])
            minz.append(minmax[0][2])
            maxx.append(minmax[1][0])
            maxy.append(minmax[1][1])
            maxz.append(minmax[1][2])

        all_rg_tally.append([i, rg_value])

        if(re[0] == 1):
            accepted_rg_tally.append([i, accepted, rg_value])
            if(directedmc > 0):
                if(len(rg_difference_list) <= rg_list_length):
                    this_rg_difference = abs(rg_value - directedmc)
                    rg_difference_list.append(this_rg_difference)
                    directed_rg_list.append(rg_value)
                    accepted_rg_list.append(accepted)
                else:
                    this_rg_difference = abs(rg_value - directedmc)
                    evaluate_rg(rg_difference_list, directed_rg_list,
                                accepted_rg_list, this_rg_difference, rg_value, accepted)

        if(re[0] == 0):
            if(failtally == goback):
                failtally = 0
                if(accepted > 0):
                    if(seed[0] == 1):
                        ran_num = seed_object.rand()
                        dum = int(accepted * ran_num) - 1

                    elif(directedmc > 0):
                        local_rg_list_length = len(directed_rg_list)
                        ran_num = random.randrange(0, local_rg_list_length)
                        dum = accepted_rg_list[ran_num]

                    else:
                        dum = int(accepted * random.random()) - 1
                    if(dum == -1):
                        print '\nreloading coordinates from original starting structure'
                        m1.read_pdb(
                            path + pdbfile, fastread=True, saspdbrx_topology=True)
                        coor = m1.coor()
                        print 'accepted = ', accepted
                        print 'dum = ', dum
                    else:
                        print '\nreloading coordinates from a previously accepted structure'
                        # print 'accepted = ',accepted
                        #dum = int(accepted*1)-1
                        # print 'dum = ',dum
                        # print 'dum+1 = ',dum+1

                        m1.read_single_dcd_step(genpaths + dcdfile, dum + 1)
                        # m1.read_single_dcd_step(genpaths+dcdfile,dum)
                        coor = m1.coor()
                else:
                    print '\n>>>>>reloading coordinates from original starting structure'
                    m1.read_pdb(
                        path + pdbfile, fastread=True, saspdbrx_topology=True)
                    coor = m1.coor()
            else:
                failtally = failtally + 1

        if(((i + 1) % (float(trials) / 100.0) == 0 or (trials < 10))):
            fraction_done = (float(i + 1) / float(trials))
            progress_string = '\nCOMPLETED ' + \
                str(i + 1) + ' of ' + str(trials) + ' : ' + \
                str(fraction_done * 100.0) + ' % done'
            print('%s\n' % progress_string)
            print accepted, ' configurations accepted out of ', nsteps, (float(accepted) / nsteps) * 100.0, ' %\n\n'
            report_string = 'STATUS\t' + str(fraction_done)
            txtOutput.put(report_string)

        if(i > 9):
            if((i + 1) % (trials / 10) == 0 and accepted > 0 and i + 1 > 10):
                if(plotflag == 1):
                    graph.plot(Gnuplot.Data(all_rg_tally, using='1:2 w p ps 4', title='all Rg'), Gnuplot.Data(
                        accepted_rg_tally, using='1:3 w lp pt 5 ps 2', title='accepted'))
            fraction_done = (float(i + 1) / float(trials))
            report_string = 'STATUS\t' + str(fraction_done)
            txtOutput.put(report_string)

        elif(accepted > 0):
            if(plotflag == 1):
                graph.plot(Gnuplot.Data(all_rg_tally, using='1:2 w p ps 4', title='all Rg'), Gnuplot.Data(
                    accepted_rg_tally, using='1:3 w lp pt 5 ps 2', title='accepted'))

            fraction_done = (float(i + 1) / float(trials))
            report_string = 'STATUS\t' + str(fraction_done)
            txtOutput.put(report_string)

        # print 'finished loop for i = ',i,'\n' ; sys.stdout.flush()

    m1.close_dcd_write(dcdoutfile)

    rgplot = open(
        './' + runname + '/generate/' + dcdfile + '.all_rg_results_data.txt', 'a')
    rgplot.write('# structure number (structure 1 = 1; not 0), Rg (all)\n')
    for ii in range(len(all_rg_tally)):
        rgplot.write('%i\t%f\n' %
                     (all_rg_tally[ii][0] + 1, all_rg_tally[ii][1]))
    rgplot.close()

    rgplot = open(
        './' + runname + '/generate/' + dcdfile + '.accepted_rg_results_data.txt', 'a')
    rgplot.write(
        '# structure number (structure 1 = 1; not 0), Rg (accepted), trial number\n')
    for ii in range(len(accepted_rg_tally)):
        rgplot.write('%i\t%f\t%i\n' % (accepted_rg_tally[ii][
                     1] + 0, accepted_rg_tally[ii][2], accepted_rg_tally[ii][0] + 1))
    rgplot.close()

    outfile7 = open(genpaths + dcdfile + '.stats', 'a')
    outfile7.write('%s\t%f\t%s\t%f\n' %
                   ('lowest Rg = ', lowestrg, 'highest Rg = ', hrg))
    outfile7.write('%s\t%i\t%s\t%i\t%s\t%f%s\n' % ('accepted ', accepted,
                                                   ' out of ', nsteps, ' moves : ', (accepted / float(nsteps)) * 99.0, ' %'))
    outfile7.write('%s\t%i\t%s\t%i\t%s\t%f%s\n' % (
        'overlapped ', over, ' out of ', nsteps, ' moves : ', (over / float(nsteps)) * 100.0, ' %'))
    outfile7.write('%s\t%i\t%s\t%i\t%s\t%f%s\n' % (
        'bad rg2 ', badrg, ' out of ', nsteps, ' moves : ', (badrg / float(nsteps)) * 100.0, ' %'))
    if(zflag == 1):
        outfile7.write('%s\t%i\t%s\t%i\t%s\t%f%s\n' % (
            'bad zcut ', badz, ' out of ', nsteps, ' moves : ', (badz / float(nsteps)) * 100.0, ' %'))
    if(cflag == 1):
        outfile7.write('%s\t%i\t%s\t%i\t%s\t%f%s\n' % ('constraint filter rejected ',
                                                       badc, ' out of ', nsteps, ' moves : ', (badz / float(nsteps)) * 100.0, ' %'))
    if(accepted > 0):
        outfile7.write('%s\t%f\n' %
                       ('average accepted rg2 = ', arg / (accepted)))
    else:
        outfile7.write('%s\t%f\n' % ('average accepted rg2 = ', 0.0))
    outfile7.write('%s\t%f\n' %
                   ('average total rg2 of ensemble = ', trg / (nsteps)))

    lastsasfile.close()
    angleoutfile.close()

    if(accepted > 0):
        txtOutput.put("Average accepted rg2 = %lf\n" % (arg / (accepted)))
        txtOutput.put(
            "\nConfigurations and statistics saved in %s directory\n\n" % ('./' + genpaths))
    else:
        txtOutput.put(
            "\n NO ACCEPTED MOVES\n\n Statistics saved in %s directory\n\n" % (genpaths))

    txtOutput.put("lowest Rg = %lf\t highest Rg = %lf\n" % (lowestrg, hrg))
    txtOutput.put("accepted %d out of %d : %lf percent\n" %
                  (accepted, nsteps, (accepted / float(nsteps)) * 100.0))
    txtOutput.put("overlapped %d out of %d moves : %lf percent\n" %
                  (over, nsteps, (float(over) / float(nsteps)) * 100.0))
    txtOutput.put("bad rg2 %d out of %d moves : %lf percent\n\n\n" %
                  (badrg, nsteps, (float(badrg) / float(nsteps)) * 100.0))
    if(zflag == 1):
        txtOutput.put("bad zcut %d out of %d moves : %lf percent\n\n\n" % (
            badz, nsteps, (float(badz) / float(nsteps)) * 100.0))
    if(cflag == 1):
        txtOutput.put("constraint filter rejected %d out of %d moves : %lf percent\n\n\n" % (
            badc, nsteps, (float(badc) / float(nsteps)) * 100.0))

    if(len(minx) > 0 and len(miny) > 0 and len(minz) > 0 and len(maxx) > 0 and len(maxy) > 0 and len(maxz) > 0):
        min_x = numpy.min(minx)
        min_y = numpy.min(miny)
        min_z = numpy.min(minz)
        max_x = numpy.max(maxx)
        max_y = numpy.max(maxy)
        max_z = numpy.max(maxz)

        txtOutput.put("minimum x = %lf\t maximum x = %lf -> range: %lf Angstroms\n" %
                      (min_x, max_x, (max_x - min_x)))

        txtOutput.put("minimum y = %lf\t maximum y = %lf -> range: %lf Angstroms\n" %
                      (min_y, max_y, (max_y - min_y)))
        txtOutput.put("minimum z = %lf\t maximum z = %lf -> range: %lf Angstroms\n\n" %
                      (min_z, max_z, (max_z - min_z)))

        outfile7.write("\nminimum x = %lf\t maximum x = %lf -> range: %lf Angstroms\n" %
                       (min_x, max_x, (max_x - min_x)))

        outfile7.write("minimum y = %lf\t maximum y = %lf -> range: %lf Angstroms\n" %
                       (min_y, max_y, (max_y - min_y)))

        outfile7.write("minimum z = %lf\t maximum z = %lf -> range: %lf Angstroms\n\n" %
                       (min_z, max_z, (max_z - min_z)))

        outfile7.close()
    else:
        outfile7.close()

    txtOutput.put("\n%s \n" % (lineintxtOutput))
    time.sleep(1.0)

    if(plotflag == 1):
        wait('\n')

    print 'DIHEDRAL IS DONE'
    return()