How to use MDAnalysis - 10 common examples

To help you get started, we’ve selected a few MDAnalysis 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 maxscheurer / pycontact / mdanalysis.py View on Github external
def parseParameterFileString(string):
        # read charmm parameter/topology file to determine AtomTypes and their AtomHBondType
        spl = string.split("!")
        name = spl[0].split()[2]
        comment = spl[1][1:]
        try:
            htype = spl[2]
        except:
            htype = "none"
        tp = AtomType(name, comment, AtomHBondType.mapping[htype])
        return tp
github maxscheurer / pycontact / mdanalysis.py View on Github external
def makeKeyArraysFromKey(self,key):
        keystring1, keystring2 = key.split("-")
        mapping = AccumulationMapIndex.mapping
        maximal = len(mapping)
        key1 = []
        for i in range(0, maximal):
            current = mapping[i]
            if current not in keystring1:
                key1.append("none")
                continue
            if i == (maximal - 1):
                key1.append(keystring1[keystring1.index(current) + len(current):])
                break
            nextCurrent = mapping[i + 1]
            if nextCurrent not in keystring1:
                nxt = ""
                for k in mapping[i + 1:]:
                    if k in keystring1[keystring1.index(current) + len(current):]:
                        nxt = k
github maxscheurer / pycontact / mdanalysis.py View on Github external
def human_readable_title(self):
        total = []
        for key in [self.key1, self.key2]:
            titleDict = {}
            counter = 0
            for item in key:
                titleDict[AccumulationMapIndex.mapping[counter]] = (item if item != "none" else "")
                counter += 1
            residueString = "%s%s" % (titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resname]],
                                      str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resid]]))
            atomIndexString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.index],
                                          str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]])) if
                               titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]] != "" else "")
            atomNameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.name],
                                         str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]])) if
                              titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]] != "" else "")
            segnameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.segid],
                                        str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]])) if
                             titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]] != "" else "")
            list = [residueString, atomIndexString, atomNameString, segnameString]
            finishedList = []
            for string in list:
                if string != "":
                    finishedList.append(string)
            finishedString = " , ".join(finishedList)
            total.append(finishedString)
        return " - ".join(total)
github maxscheurer / pycontact / mdanalysis.py View on Github external
def human_readable_title(self):
        total = []
        for key in [self.key1, self.key2]:
            titleDict = {}
            counter = 0
            for item in key:
                titleDict[AccumulationMapIndex.mapping[counter]] = (item if item != "none" else "")
                counter += 1
            residueString = "%s%s" % (titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resname]],
                                      str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resid]]))
            atomIndexString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.index],
                                          str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]])) if
                               titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]] != "" else "")
            atomNameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.name],
                                         str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]])) if
                              titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]] != "" else "")
            segnameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.segid],
                                        str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]])) if
                             titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]] != "" else "")
            list = [residueString, atomIndexString, atomNameString, segnameString]
            finishedList = []
            for string in list:
                if string != "":
                    finishedList.append(string)
            finishedString = " , ".join(finishedList)
            total.append(finishedString)
        return " - ".join(total)
github maxscheurer / pycontact / mdanalysis.py View on Github external
def makeKeyArraysFromMaps(self,map1, map2, contact):
        idx1 = contact.idx1
        idx2 = contact.idx2
        counter = 0
        keys1 = []
        for val in map1:
            if val == 1:
                if counter == AccumulationMapIndex.index:
                    keys1.append(idx1)
                elif counter == AccumulationMapIndex.atype:
                    keys1.append(self.type_array[idx1])
                elif counter == AccumulationMapIndex.name:
                    keys1.append(self.name_array[idx1])
                elif counter == AccumulationMapIndex.resid:
                    keys1.append(self.resid_array[idx1])
                elif counter == AccumulationMapIndex.resname:
                    keys1.append(self.resname_array[idx1])
                elif counter == AccumulationMapIndex.segid:
                    keys1.append(self.segids[idx1])
            else:
                keys1.append("none")
            counter += 1
        counter = 0
        keys2 = []
        for val in map2:
            if val == 1:
                if counter == AccumulationMapIndex.index:
                    keys2.append(idx2)
                elif counter == AccumulationMapIndex.atype:
                    keys2.append(self.type_array[idx2])
                elif counter == AccumulationMapIndex.name:
                    keys2.append(self.name_array[idx2])
github maxscheurer / pycontact / mdanalysis.py View on Github external
elif counter == AccumulationMapIndex.segid:
                    keys1.append(self.segids[idx1])
            else:
                keys1.append("none")
            counter += 1
        counter = 0
        keys2 = []
        for val in map2:
            if val == 1:
                if counter == AccumulationMapIndex.index:
                    keys2.append(idx2)
                elif counter == AccumulationMapIndex.atype:
                    keys2.append(self.type_array[idx2])
                elif counter == AccumulationMapIndex.name:
                    keys2.append(self.name_array[idx2])
                elif counter == AccumulationMapIndex.resid:
                    keys2.append(self.resid_array[idx2])
                elif counter == AccumulationMapIndex.resname:
                    keys2.append(self.resname_array[idx2])
                elif counter == AccumulationMapIndex.segid:
                    keys2.append(self.segids[idx2])
            else:
                keys2.append("none")
            counter += 1
        return [keys1, keys2]
github maxscheurer / pycontact / mdanalysis.py View on Github external
for key in [self.key1, self.key2]:
            titleDict = {}
            counter = 0
            for item in key:
                titleDict[AccumulationMapIndex.mapping[counter]] = (item if item != "none" else "")
                counter += 1
            residueString = "%s%s" % (titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resname]],
                                      str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resid]]))
            atomIndexString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.index],
                                          str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]])) if
                               titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]] != "" else "")
            atomNameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.name],
                                         str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]])) if
                              titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]] != "" else "")
            segnameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.segid],
                                        str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]])) if
                             titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]] != "" else "")
            list = [residueString, atomIndexString, atomNameString, segnameString]
            finishedList = []
            for string in list:
                if string != "":
                    finishedList.append(string)
            finishedString = " , ".join(finishedList)
            total.append(finishedString)
        return " - ".join(total)
github maxscheurer / pycontact / mdanalysis.py View on Github external
def makeKeyArraysFromMaps(self,map1, map2, contact):
        idx1 = contact.idx1
        idx2 = contact.idx2
        counter = 0
        keys1 = []
        for val in map1:
            if val == 1:
                if counter == AccumulationMapIndex.index:
                    keys1.append(idx1)
                elif counter == AccumulationMapIndex.atype:
                    keys1.append(self.type_array[idx1])
                elif counter == AccumulationMapIndex.name:
                    keys1.append(self.name_array[idx1])
                elif counter == AccumulationMapIndex.resid:
                    keys1.append(self.resid_array[idx1])
                elif counter == AccumulationMapIndex.resname:
                    keys1.append(self.resname_array[idx1])
                elif counter == AccumulationMapIndex.segid:
                    keys1.append(self.segids[idx1])
            else:
                keys1.append("none")
            counter += 1
        counter = 0
        keys2 = []
        for val in map2:
            if val == 1:
github maxscheurer / pycontact / mdanalysis.py View on Github external
def human_readable_title(self):
        total = []
        for key in [self.key1, self.key2]:
            titleDict = {}
            counter = 0
            for item in key:
                titleDict[AccumulationMapIndex.mapping[counter]] = (item if item != "none" else "")
                counter += 1
            residueString = "%s%s" % (titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resname]],
                                      str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.resid]]))
            atomIndexString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.index],
                                          str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]])) if
                               titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.index]] != "" else "")
            atomNameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.name],
                                         str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]])) if
                              titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.name]] != "" else "")
            segnameString = ("%s %s" % (AccumulationMapIndex.mapping[AccumulationMapIndex.segid],
                                        str(titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]])) if
                             titleDict[AccumulationMapIndex.mapping[AccumulationMapIndex.segid]] != "" else "")
            list = [residueString, atomIndexString, atomNameString, segnameString]
            finishedList = []
            for string in list:
                if string != "":
github maxscheurer / pycontact / mdanalysis.py View on Github external
# jump out of loop if hydrogen contacts are found, only contacts between heavy atoms are considered, hydrogen bonds can still be detected!
                if re.match("H(.*)", self.name_array[convindex1]) or re.match("H(.*)", self.name_array[convindex2]):
                    continue
                    # distance between atom1 and atom2
                distance = distarray[idx1, idx2]
                weight = self.weight_function(distance)
                # read AtomHBondType from heavyatoms list
                type1 = next((x.htype for x in heavyatoms if x.name == self.type_array[convindex1]), AtomHBondType.none)
                type2 = next((x.htype for x in heavyatoms if x.name == self.type_array[convindex2]), AtomHBondType.none)
                ## HydrogenBondAlgorithm
                # TODO: outsource to another file?
                # elongates the current loop a lot...
                hydrogenBonds = []
                if type1 != AtomHBondType.none and type2 != AtomHBondType.none:
                    if (type1 == AtomHBondType.both and type2 == AtomHBondType.both) or \
                            (type1 == AtomHBondType.acc and type2 == AtomHBondType.don) or \
                            (type1 == AtomHBondType.don and type2 == AtomHBondType.acc) or \
                            (type1 == AtomHBondType.both and type2 == AtomHBondType.acc) or \
                            (type1 == AtomHBondType.acc and type2 == AtomHBondType.both) or \
                            (type1 == AtomHBondType.don and type2 == AtomHBondType.both) or \
                            (type1 == AtomHBondType.both and type2 == AtomHBondType.don):
                        # print("hbond? %s - %s" % (type_array[convindex1], type_array[convindex2]))
                        # search for hatom, check numbering in bond!!!!!!!!!!
                        b1 = self.bonds[convindex1]
                        b2 = self.bonds[convindex2]
                        # search for hydrogen atoms bound to atom 1
                        bondcount1 = 0
                        hydrogenAtomsBoundToAtom1 = []
                        # old code, wrong!
                        # for b in b1.types():
                        #     hydrogen = next((x for x in b if x.startswith("H")), 0)
                        #     # print(b)