Exemple #1
0
def gen_itp(args):

    # Import of Itp and FF files
    force_field = load_library(args.name, args.lib, args.inpath)

    # Generate the MetaMolecule
    if args.seq:
        monomers = split_seq_string(args.seq)
        meta_molecule = MetaMolecule.from_monomer_seq_linear(
            monomers=monomers, force_field=force_field, mol_name=args.name)
    #ToDo
    # fix too broad except
    elif args.seq_file:
        extension = args.seq_file.suffix.casefold()[1:]
        try:
            parser = getattr(MetaMolecule, 'from_{}'.format(extension))
            meta_molecule = parser(json_file=args.seq_file,
                                   force_field=force_field,
                                   mol_name=args.name)
        except AttributeError:
            raise IOError(
                "Cannot parse file with extension {}.".format(extension))

    # Do transformationa and apply link
    meta_molecule = MapToMolecule().run_molecule(meta_molecule)
    meta_molecule = ApplyLinks().run_molecule(meta_molecule)

    with open(args.outpath, 'w') as outpath:
        vermouth.gmx.itp.write_molecule_itp(meta_molecule.molecule,
                                            outpath,
                                            moltype=args.name,
                                            header=["polyply-itp"])
    DeferredFileWriter().write()
def write_map(filename, cg_mol):
    aa_nodes = {}
    aa_to_cg = defaultdict(list)
    for cg_idx in cg_mol:
        bead = cg_mol.nodes[cg_idx]
        aa_graph = bead['graph']
        for aa_idx in aa_graph:
            atom = aa_graph.nodes[aa_idx]
            aa_nodes[aa_idx] = atom
            aa_to_cg[aa_idx].append(cg_idx)
    with open(filename, 'w') as file_out:
        molname = cg_mol.meta['moltype']
        file_out.write('[ molecule ]\n')
        file_out.write(molname + '\n')
        file_out.write('[ martini ]\n')
        file_out.write(' '.join(cg_mol.nodes[idx]['atomname']
                                for idx in cg_mol) + '\n')
        file_out.write('[ mapping ]\n')
        file_out.write('<FORCE FIELD NAMES>\n')
        file_out.write('[ atoms ]\n')
        for aa_idx, atom in sorted(aa_nodes.items(),
                                   key=lambda i: i[1].get('atomid', i[0])):
            cg_idxs = aa_to_cg[aa_idx]
            file_out.write('{} {} {}\n'.format(
                atom.get('atomid', aa_idx), atom['atomname'],
                ' '.join(cg_mol.nodes[cg_idx]['atomname']
                         for cg_idx in cg_idxs)))
def write_ndx(filename, cg_mol, stepsize=10):
    with open(filename, 'w') as file_out:
        for bead_idx in cg_mol:
            node = cg_mol.nodes[bead_idx]
            at_idxs = list(node.get('graph', []))
            file_out.write('[ {} ]\n'.format(node['atomname']))
            for idx in range(0, len(at_idxs), stepsize):
                idxs = (at_idx + 1 for at_idx in at_idxs[idx:idx + stepsize])
                file_out.write(' '.join(map(str, idxs)) + '\n')
            file_out.write('\n')
def write_itp(path, cg_mol):
    with open(path, 'w') as out:
        write_molecule_itp(cg_mol, out)
def write_pdb(path, cg_mol):
    system = System()
    system.add_molecule(cg_mol)
    with open(path, 'w') as out:
        out.write(write_pdb_string(system))