# Path: pmx/forcefield2.py
# class Topology( TopolBase ):
# 
# #    def __init__(self, filename, topfile = None, assign_types = True, cpp_path = [os.environ.get('GMXDATA')+'/top'], cpp_defs = [], version = 'old', ff = 'amber' ):
#     def __init__(self, filename, topfile = None, assign_types = True, cpp_path = [os.environ.get('GMXLIB')], cpp_defs = [], version = 'old', ff = 'amber', ffpath=None ):
#         TopolBase.__init__(self, filename, version)
# 	bItp = False
#         if not topfile:
#             topfile = filename
# 	    bItp = True
#         if assign_types:
#             l = cpp_parse_file(topfile, cpp_defs = cpp_defs, cpp_path = cpp_path, itp = bItp, ffpath = ffpath)
#             l = kickOutComments(l,'#')
#             l = kickOutComments(l,';')
#             self.BondedParams = BondedParser( l )
#             self.NBParams = NBParser( l, version, ff = ff )
#             self.assign_fftypes()
# 
# 
#     def set_molecule(self, molname, n):
#         mol_exists = False
#         for i, mol in enumerate(self.molecules):
#             if mol[0] == molname:
#                 self.molecules[i][1] = n
#                 mol_exists = True
#         if not mol_exists:
#             self.molecules.append([molname,n])
#             
#     def del_molecule(self, molname):
#         if not hasattr(molname,"append"):
#             molname = [molname]
#         new = []
#         for m in self.molecules:
#             if m[0] not in molname:
#                 new.append(m)
#         self.molecules = new
# 
# 
#     def assign_fftypes(self):
#         for atom in self.atoms:
#             atom.type = self.NBParams.atomtypes[atom.atomtype]['bond_type']
#             if atom.atomtypeB is not None:
#                 atom.typeB = self.NBParams.atomtypes[atom.atomtypeB]['bond_type']
#             else:
#                 atom.typeB = atom.type
#             
#     def make_bond_params(self):
#         for i, (at1,at2,func) in enumerate(self.bonds):
#             param = self.BondedParams.get_bond_param(at1.type,at2.type)
#             if param is None:
#                 print 'Error! No bonded parameters found! (%s-%s)' % \
#                       (at1.type, at2.type)
#                 sys.exit(1)
#             self.bonds[i].append(param[1:])
# 
#     def make_angle_params(self):
#         for i, (at1, at2, at3, func) in enumerate(self.angles):
#             param = self.BondedParams.get_angle_param(at1.type, at2.type, at3.type)
#             if param is None:
#                 print 'Error! No angle parameters found! (%s-%s-%s)' % \
#                       (at1.type, at2.type, at3.type)
#                 sys.exit(1)
#             self.angles[i].append(param[1:])
#             
#     def make_dihedral_params(self):
#         for i, d in enumerate(self.dihedrals):
#             if d[5]!='': # we have a prefefined dihedral
#                 continue
#             else:
#                 at1, at2, at3, at4, func, dih = d
#                 param = self.BondedParams.get_dihedral_param(at1.type, at2.type, \
#                                                              at3.type, at4.type, \
#                                                              func)
#                 if param is None:
#                     print 'Error! No dihedral parameters found! (%s-%s-%s-%s)' % \
#                           (at1.type, at2.type, at3.type, at4.type)
#                     print func, dih
#                     sys.exit(1)
#                 del self.dihedrals[i][-1]
#                 self.dihedrals[i].append(param[1:])


import sys, os, copy
from pmx import *
from pmx.forcefield2 import Topology
from pmx.mutdb import read_mtp_entry
from pmx.parser import kickOutComments
                        program_desc = help_text,
                        check_for_existing_files = False )

    
    do_scale_mass = cmdl['-scale_mass']
    top_file = cmdl['-p']
    out_file = cmdl['-o']
    log_file = cmdl['-log']
    bDNA = cmdl['-dna']
    bRNA = cmdl['-rna']
    if bDNA:
        mtp_file = os.path.join( get_ff_path(cmdl['-ff']), 'mutres_dna.mtp')
    elif bRNA:
        mtp_file = os.path.join( get_ff_path(cmdl['-ff']), 'mutres_rna.mtp')
    else:
        mtp_file = os.path.join( get_ff_path(cmdl['-ff']), 'mutres.mtp')
    ffbonded_file = os.path.join( get_ff_path(cmdl['-ff']), 'ffbonded.itp')
    do_split = cmdl['-split']
    if cmdl.opt['-itp'].is_set:
        input_itp = cmdl['-itp']
    else:
        input_itp = None
    if input_itp and out_file.split('.')[-1] != 'itp':
        out_file = change_outfile_format(out_file, 'itp')
        print 'log_> Setting outfile name to %s' % out_file

    if input_itp:
#        print 'log_> Reading input files "%s" and "%s"' % (top_file, input_itp)
#        topol = Topology( input_itp,  topfile = top_file, version = 'new', ff = cmdl['-ff'] )
        print 'log_> Reading input .itp file "%s""' % (input_itp)

# Path: pmx/forcefield2.py
# class Topology( TopolBase ):
# 
# #    def __init__(self, filename, topfile = None, assign_types = True, cpp_path = [os.environ.get('GMXDATA')+'/top'], cpp_defs = [], version = 'old', ff = 'amber' ):
#     def __init__(self, filename, topfile = None, assign_types = True, cpp_path = [os.environ.get('GMXLIB')], cpp_defs = [], version = 'old', ff = 'amber', ffpath=None ):
#         TopolBase.__init__(self, filename, version)
# 	bItp = False
#         if not topfile:
#             topfile = filename
# 	    bItp = True
#         if assign_types:
#             l = cpp_parse_file(topfile, cpp_defs = cpp_defs, cpp_path = cpp_path, itp = bItp, ffpath = ffpath)
#             l = kickOutComments(l,'#')
#             l = kickOutComments(l,';')
#             self.BondedParams = BondedParser( l )
#             self.NBParams = NBParser( l, version, ff = ff )
#             self.assign_fftypes()
# 
# 
#     def set_molecule(self, molname, n):
#         mol_exists = False
#         for i, mol in enumerate(self.molecules):
#             if mol[0] == molname:
#                 self.molecules[i][1] = n
#                 mol_exists = True
#         if not mol_exists:
#             self.molecules.append([molname,n])
#             
#     def del_molecule(self, molname):
#         if not hasattr(molname,"append"):
#             molname = [molname]
#         new = []
#         for m in self.molecules:
#             if m[0] not in molname:
#                 new.append(m)
#         self.molecules = new
# 
# 
#     def assign_fftypes(self):
#         for atom in self.atoms:
#             atom.type = self.NBParams.atomtypes[atom.atomtype]['bond_type']
#             if atom.atomtypeB is not None:
#                 atom.typeB = self.NBParams.atomtypes[atom.atomtypeB]['bond_type']
#             else:
#                 atom.typeB = atom.type
#             
#     def make_bond_params(self):
#         for i, (at1,at2,func) in enumerate(self.bonds):
#             param = self.BondedParams.get_bond_param(at1.type,at2.type)
#             if param is None:
#                 print 'Error! No bonded parameters found! (%s-%s)' % \
#                       (at1.type, at2.type)
#                 sys.exit(1)
#             self.bonds[i].append(param[1:])
# 
#     def make_angle_params(self):
#         for i, (at1, at2, at3, func) in enumerate(self.angles):
#             param = self.BondedParams.get_angle_param(at1.type, at2.type, at3.type)
#             if param is None:
#                 print 'Error! No angle parameters found! (%s-%s-%s)' % \
#                       (at1.type, at2.type, at3.type)
#                 sys.exit(1)
#             self.angles[i].append(param[1:])
#             
#     def make_dihedral_params(self):
#         for i, d in enumerate(self.dihedrals):
#             if d[5]!='': # we have a prefefined dihedral
#                 continue
#             else:
#                 at1, at2, at3, at4, func, dih = d
#                 param = self.BondedParams.get_dihedral_param(at1.type, at2.type, \
#                                                              at3.type, at4.type, \
#                                                              func)
#                 if param is None:
#                     print 'Error! No dihedral parameters found! (%s-%s-%s-%s)' % \
#                           (at1.type, at2.type, at3.type, at4.type)
#                     print func, dih
#                     sys.exit(1)
#                 del self.dihedrals[i][-1]
#                 self.dihedrals[i].append(param[1:])


# Path: scripts/generate_hybrid_topology.py
import sys, os, copy
from pmx import *
from pmx.forcefield2 import Topology
from pmx.mutdb import read_mtp_entry
from pmx.parser import kickOutComments

                        program_desc = help_text,
                        check_for_existing_files = False )

    
    do_scale_mass = cmdl['-scale_mass']
    top_file = cmdl['-p']
    out_file = cmdl['-o']
    log_file = cmdl['-log']
    bDNA = cmdl['-dna']
    bRNA = cmdl['-rna']
    if bDNA:
        mtp_file = os.path.join( get_ff_path(cmdl['-ff']), 'mutres_dna.mtp')
    elif bRNA:
        mtp_file = os.path.join( get_ff_path(cmdl['-ff']), 'mutres_rna.mtp')
    else:
        mtp_file = os.path.join( get_ff_path(cmdl['-ff']), 'mutres.mtp')
    ffbonded_file = os.path.join( get_ff_path(cmdl['-ff']), 'ffbonded.itp')
    do_split = cmdl['-split']
    if cmdl.opt['-itp'].is_set:
        input_itp = cmdl['-itp']
    else:
        input_itp = None
    if input_itp and out_file.split('.')[-1] != 'itp':
        out_file = change_outfile_format(out_file, 'itp')
        print 'log_> Setting outfile name to %s' % out_file

    if input_itp:
#        print 'log_> Reading input files "%s" and "%s"' % (top_file, input_itp)
#        topol = Topology( input_itp,  topfile = top_file, version = 'new', ff = cmdl['-ff'] )
        print 'log_> Reading input .itp file "%s""' % (input_itp)

        topol = Topology( input_itp,  topfile = None, version = 'new', ff = cmdl['-ff'], ffpath=get_ff_path(cmdl['-ff']) )