def md_code_from_string(command, index=False):
    """
    Given a command line string, identify/guess the relevant MD code.
    Returns a string, or None if it cannot identify a likely code.

    If index is not none, then the index of the identificed keyword is
    also ruturned with the code name as a (codename, index) tuple.
    """
    code = None
    words = command.split()
    leftmost = len(words)
    codes = {'gmx': 'GROMACS', 'gmx_d': 'GROMACS', 'sander': 'AMBER',
        'pmemd': 'AMBER', 'pmemd.MPI': 'AMBER', 'namd2': 'NAMD', 'namd': 'NAMD'
        }
    for key in codes:
        for word in words:
            if key in word:
                if words.index(word) < leftmost:
                    leftmost = words.index(word)
                    code = codes[key]
    if not index:
        return code
    else:
        return code, leftmost
