How to use the moltemplate.genpoly_lt.InputError function in moltemplate

To help you get started, we’ve selected a few moltemplate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jewettaij / moltemplate / moltemplate / genpoly_lt.py View on Github external
def main():
    try:
        g_program_name = __file__.split('/')[-1]
        g_version_str = '0.1.0'
        g_date_str = '2019-12-13'
        sys.stderr.write(g_program_name + ' v' +
                         g_version_str + ' ' + g_date_str + '\n')
        argv = [arg for arg in sys.argv]
        genpoly = GenPoly()
        genpoly.ParseArgs(argv)
        # Any remain arguments?
        if len(argv) > 1:
            raise InputError('Error(' + g_program_name + '):\n' +
                             'Unrecogized command line argument \"' + argv[1] +
                             '\"\n\n' +
                             g_usage_msg)

        if genpoly.settings.infile_name != '':
            infile = open(genpoly.settings.infile_name, 'r')
        else:
            infile = sys.stdin
        outfile = sys.stdout

        # Read the coordinates
        genpoly.ReadCoords(infile)

        # Did the user ask us to read a custom sequence of monomer type names?
        if genpoly.settings.name_sequence_filename != '':
            # Note: This will fill the contents of genpoly.name_sequence_multi
github jewettaij / moltemplate / moltemplate / genpoly_lt.py View on Github external
raise InputError(
                    'Error: file ' + filename +
                    ' could not be opened for reading\n')

        coords = []

        lines = infile.readlines()
        for i in range(0, len(lines)):
            tokens = lines[i].strip().split()
            if (len(tokens) == 3):
                coords.append(list(map(float, tokens)))

        self.N = len(coords)
        if self.N < 2:
            err_msg = 'Error: Coordinate file must have at least 2 positions'
            raise InputError(err_msg+'.\n')
                

        # Did the caller ask us to split the polymer into multiple polymers?
        if len(self.settings.cuts) > 0:
            if (self.settings.cuts[-1] < self.N+1):
                self.settings.cuts.append(self.N + 1)
                self.settings.cuts.sort()
            i = 0
            for j in self.settings.cuts:
                self.coords_multi.append(coords[i:j])
                i = j
        else:
            self.coords_multi.append(coords)

        # Did the caller ask us to reverse the direction of any polymers?
        for i in range(0, len(self.settings.reverse_polymer_directions)):
github jewettaij / moltemplate / moltemplate / genpoly_lt.py View on Github external
if len(self.box_padding) == 1:
                    self.box_padding = self.box_padding * 3
                del(argv[i:i + 2])

            # elif ((argv[i][0] == '-') and (__name__ == '__main__')):
            #
            #    raise InputError('Error('+g_program_name+'):\n'+\
            #        'Unrecogized command line argument \"'+argv[i]+\
            #        '\"\n\n'+\
            #        __doc__)
            else:
                i += 1

        if ((len(self.reverse_polymer_directions) != 0) and 
            (len(self.reverse_polymer_directions) != len(self.cuts) + 1)):
            raise InputError('Error: The number of entries in the file you supplied to "-polymer-directions"\n'
                             '       does not equal the number of polymers (which is either 1, or 1 + the\n'
                             '       number of entries in the file you supplied to "-cuts", if applicable)\n')


        for b in range(0, len(self.bonds_type)):
            if len(self.bonds_type) > 1:
                self.bonds_name.append('genpoly' + str(b + 1) + '_')
            else:
                self.bonds_name.append('genpoly')
        for b in range(0, len(self.angles_type)):
            if len(self.angles_type) > 1:
                self.angles_name.append('genpoly' + str(b + 1) + '_')
            else:
                self.angles_name.append('genpoly')
        for b in range(0, len(self.dihedrals_type)):
            if len(self.dihedrals_type) > 1:
github jewettaij / moltemplate / moltemplate / genpoly_lt.py View on Github external
def ParseArgs(self, argv):
        i = 1
        while i < len(argv):
            #sys.stderr.write('argv['+str(i)+'] = \"'+argv[i]+'\"\n')

            if ((argv[i].lower() == '-in') or
                (argv[i].lower() == '-i')):
                if i + 1 >= len(argv):
                    raise InputError(
                        'Error: ' + argv[i] + ' flag should be followed by a file name.\n')
                self.infile_name = argv[i + 1]
                del(argv[i:i + 2])
            elif argv[i].lower() == '-bond':
                if i + 3 >= len(argv):
                    raise InputError(
                        'Error: ' + argv[i] + ' flag should be followed by 4 strings.\n')
                # self.bonds_name.append(argv[i+1])
                self.bonds_type.append(argv[i + 1])
                self.bonds_atoms.append((argv[i + 2],
                                         argv[i + 3]))
                self.bonds_index_offsets.append((0, 1))
                del(argv[i:i + 4])
            elif argv[i].lower() == '-angle':
                if i + 7 >= len(argv):
                    raise InputError(
                        'Error: ' + argv[i] + ' flag should be followed by 5 strings and 3 integers.\n')
                # self.angles_name.append(argv[i+1])
                self.angles_type.append(argv[i + 1])
                self.angles_atoms.append((argv[i + 2],
                                          argv[i + 3],
                                          argv[i + 4]))
github jewettaij / moltemplate / moltemplate / genpoly_lt.py View on Github external
if genpoly.settings.name_sequence_filename != '':
            # Note: This will fill the contents of genpoly.name_sequence_multi
            genpoly.ReadSequence(genpoly.settings.name_sequence_filename)

        else:
            # Otherwise just fill genpoly.name_sequence_multi with
            #  repeated copies of genpoly.settings.name_monomer
            #   (...using this ugly two-dimensional list-of-lists comprehension)
            genpoly.name_sequence_multi = [[genpoly.settings.name_monomer
                                            for j in
                                            range(0, len(genpoly.coords_multi[i]))]
                                           for i in range(0,len(genpoly.coords_multi))]

        # Now, check for polymer and sequence length inconsistencies:
        if (len(genpoly.coords_multi) != len(genpoly.name_sequence_multi)):
            raise InputError('Error(' +
                             g_program_name + '):\n' +
                             '      The coordinate file and sequence file have different lengths.\n')
        for i in range(0, len(genpoly.coords_multi)):
            if len(genpoly.name_sequence_multi[i]) > 0:
                if (len(genpoly.coords_multi[i]) !=
                    len(genpoly.name_sequence_multi[i])):
                    raise InputError('Error(' +
                                     g_program_name + '):\n' +
                                     '      The coordinate file and sequence file have different lengths.\n')



        # Convert all of this information to moltemplate (LT) format:
        genpoly.WriteLTFile(outfile)

        # Now close the input file
github jewettaij / moltemplate / moltemplate / genpoly_lt.py View on Github external
def ReadSequence(self, infile):
        """
        Read a sequence of monomer type names from a file.
        This function is similar to ReadCoords().
        """
        name_sequence = []

        filename = ''
        if isinstance(infile, str):
            filename = infile
            try:
                infile = open(filename, 'r')
            except IOError:
                raise InputError(
                    'Error: file ' + filename +
                    ' could not be opened for reading\n')

        for line_orig in infile:
            line = line_orig.strip()
            ic = line.find('#')
            if ic != -1:
                line = line[:ic]
            else:
                line = line.strip()
                if len(line) > 0:
                    name_sequence.append(line)

        N = len(name_sequence)

        # Did the caller ask us to split the polymer into multiple polymers?