Ejemplo n.º 1
0
def check_isomorphism(l):

    if simple:
        # open BGF
        myBGF1 = bgf.BgfFile(bgf_file1)
        myBGF2 = bgf.BgfFile(bgf_file2)

        # remove hydrogens only in carbon atoms
        remove_aNo1 = []
        remove_aNo2 = []
        for atom in myBGF1.a:
            if "C_" in atom.ffType:
                n_H = []
                for aNo in atom.CONECT:
                    if myBGF1.getAtom(aNo).is_hydrogen():
                        n_H.append(myBGF1.a2i[aNo])
                if len(n_H) != 3:
                    n_H.sort()
                    n_H.reverse()
                    myBGF1.delAtoms(n_H)
                    myBGF1.renumber()

        for atom in myBGF2.a:
            if "C_" in atom.ffType:
                n_H = []
                for aNo in atom.CONECT:
                    if myBGF2.getAtom(aNo).is_hydrogen():
                        n_H.append(myBGF2.a2i[aNo])
                if len(n_H) != 3:
                    n_H.sort()
                    n_H.reverse()
                    myBGF2.delAtoms(n_H)
                    myBGF2.renumber()

        # toss reduced BGFs to networkx
        pei1 = myBGF1
        pei2 = myBGF2

    else:
        # toss whole structures
        pei1 = bgf_file1
        pei2 = bgf_file2

    # convert connection into dictionary
    d_graph1 = bgftools.getConnectionDict(pei1)
    d_graph2 = bgftools.getConnectionDict(pei2)

    # convert dictionary into Graph
    G1 = nx.Graph(d_graph1)
    G2 = nx.Graph(d_graph2)

    # check isomorphism
    GM = isomorphism.GraphMatcher(G1, G2)
    result = GM.is_isomorphic()

    return bgf_file1, bgf_file2, result
Ejemplo n.º 2
0
def test_isomorphism(bgf_file1, bgf_file2, simple, silent=True):
    """
def test_isomorphism():

Function Parameters:
	bgf_file	A string of filename or BgfFile class.

	"""
    # initialize
    pei1 = 0
    pei2 = 0

    # open BGF
    if isinstance(bgf_file1, bgf.BgfFile):
        myBGF = bgf_file1
    else:
        if not silent: print("opening bgf file 1.. " + str(bgf_file1))
        myBGF = bgf.BgfFile(bgf_file1)

    if isinstance(bgf_file2, bgf.BgfFile):
        myBGF = bgf_file2
    else:
        if not silent: print("opening bgf file 2.. " + str(bgf_file2))
        myBGF = bgf.BgfFile(bgf_file2)

    if simple:
        # reduce hydrogens
        pei1 = bgftools.getBackbone(bgf_file1)
        pei2 = bgftools.getBackbone(bgf_file2)
    else:
        pei1 = bgf_file1
        pei2 = bgf_file2

    # convert connection into dictionary
    d_graph1 = bgftools.getConnectionDict(pei1)
    d_graph2 = bgftools.getConnectionDict(pei2)
    if not silent: print("structures are converted to graph.")

    G1 = nx.Graph(d_graph1)
    G2 = nx.Graph(d_graph2)
    if not silent: print("graphs are loaded.")

    # check isomorphism
    if not silent: print("now comparing two graphs..")
    GM = isomorphism.GraphMatcher(G1, G2)
    result = GM.is_isomorphic()

    if not silent:
        print(result)
    else:
        return result
Ejemplo n.º 3
0
def calculate_wiener_index(body):
    """
    Calculates Wiener index of the generated polymer.
    :BgfAtom body
    :returns int(index): Wiener index 

    Method from from PEI_calcWienerIndex_parallel.py
    """

    myBGF = copy.deepcopy(body)

    # remove hydrogens
    pei = bgftools.getBackbone(myBGF, 0)

    # convert connection into dictionary
    d_graph = bgftools.getConnectionDict(pei)

    # convert dictionary into Graph
    G = nx.Graph(d_graph)

    # calculate the all shortest length path
    d_dist = nx.all_pairs_shortest_path_length(G)

    # get Wiener Index
    index = 0
    for key in d_dist.iterkeys():
        for key2 in d_dist[key].iterkeys():
            index += d_dist[key][key2]
    index = index / 2.0

    return int(index)
Ejemplo n.º 4
0
def do_work(bgf_file, out_file, simple, silent=True):

    #initialize
    myPEI = bgf.BgfFile()
    n_H = []

    # open BGF
    myBGF = bgf.BgfFile(bgf_file)

    # remove hydrogens
    pei = bgftools.getBackbone(myBGF, 0)

    # convert connection into dictionary
    d_graph = bgftools.getConnectionDict(pei)

    # convert dictionary into Graph
    G = nx.Graph(d_graph)

    # calculate the all shortest length path
    #d_dist = nx.floyd_warshall(G)	# seems a problem
    d_dist = nx.all_pairs_shortest_path_length(G)

    # get Wiener Index
    index = 0
    for key in d_dist.iterkeys():
        for key2 in d_dist[key].iterkeys():
            index += d_dist[key][key2]
    index = index / 2.0

    return index
Ejemplo n.º 5
0
def test_isomorphism(bgf_file1, bgf_file2, simple, silent=True):
    """
def test_isomorphism():

Function Parameters:
	bgf_file	A string of filename or BgfFile class.

	"""
    # initialize
    pei1 = 0
    pei2 = 0
    myBGF1 = 0
    myBGF2 = 0

    # open BGF
    if isinstance(bgf_file1, bgf.BgfFile):
        myBGF1 = bgf_file1
    else:
        if not silent: print("opening bgf file 1.. " + str(bgf_file1))
        myBGF1 = bgf.BgfFile(bgf_file1)

    if isinstance(bgf_file2, bgf.BgfFile):
        myBGF2 = bgf_file2
    else:
        if not silent: print("opening bgf file 2.. " + str(bgf_file2))
        myBGF2 = bgf.BgfFile(bgf_file2)

    if simple:
        # remove hydrogens only in carbon atoms
        remove_aNo1 = []
        remove_aNo2 = []
        for atom in myBGF1.a:
            if "C_" in atom.ffType:
                n_H = []
                for aNo in atom.CONECT:
                    if myBGF1.getAtom(aNo).is_hydrogen(): n_H.append(aNo)
                print("aNo: %s, n_H: %s in %s (1)" %
                      (atom.aNo, n_H, bgf_file1))
            if len(n_H) != 3:
                myBGF1.delAtoms(n_H)
                myBGF1.renumber()
        #print("saving mybgf1"); myBGF1.saveBGF(bgf_file1 + "mod")
        pei1 = myBGF1

        for atom in myBGF2.a:
            if "C_" in atom.ffType:
                n_H = []
                for aNo in atom.CONECT:
                    if myBGF2.getAtom(aNo).is_hydrogen(): n_H.append(aNo)
                print("aNo: %s, n_H: %s in %s (2)" %
                      (atom.aNo, n_H, bgf_file2))
            if len(n_H) != 3:
                myBGF2.delAtoms(n_H)
                myBGF2.renumber()
        #print("saving mybgf2"); myBGF2.saveBGF(bgf_file2 + "mod")
        pei2 = myBGF2
    else:
        pei1 = bgf_file1
        pei2 = bgf_file2

    # convert connection into dictionary
    d_graph1 = bgftools.getConnectionDict(pei1)
    d_graph2 = bgftools.getConnectionDict(pei2)
    if not silent: print("structures are converted to graph.")

    G1 = nx.Graph(d_graph1)
    G2 = nx.Graph(d_graph2)
    if not silent: print("graphs are loaded.")

    # check isomorphism
    if not silent: print("now comparing two graphs..")
    GM = isomorphism.GraphMatcher(G1, G2)
    result = GM.is_isomorphic()

    if not silent:
        print(result)
    else:
        return result
Ejemplo n.º 6
0
def do_work(qin, qout, simple):
    while True:
        x = qin.get()
        if x == None:
            break
        elif qin.qsize() < 2:
            break
        else:
            # initialize
            bgf_file1 = x[0]
            bgf_file2 = x[1]
            pei1 = 0
            pei2 = 0
            myBGF1 = 0
            myBGF2 = 0
            n_H = []
            pid = os.getpid()  # process id

            # open BGF
            myBGF1 = bgf.BgfFile(bgf_file1)
            myBGF2 = bgf.BgfFile(bgf_file2)

            if simple:
                # remove hydrogens only in carbon atoms
                remove_aNo1 = []
                remove_aNo2 = []
                for atom in myBGF1.a:
                    if "C_" in atom.ffType:
                        n_H = []
                        for aNo in atom.CONECT:
                            if myBGF1.getAtom(aNo).is_hydrogen():
                                n_H.append(myBGF1.a2i[aNo])
                        if len(n_H) != 3:
                            n_H.sort()
                            n_H.reverse()
                            myBGF1.delAtoms(n_H)
                            myBGF1.renumber()

                for atom in myBGF2.a:
                    if "C_" in atom.ffType:
                        n_H = []
                        for aNo in atom.CONECT:
                            if myBGF2.getAtom(aNo).is_hydrogen():
                                n_H.append(myBGF2.a2i[aNo])
                        if len(n_H) != 3:
                            n_H.sort()
                            n_H.reverse()
                            myBGF2.delAtoms(n_H)
                            myBGF2.renumber()

                # toss reduced BGFs to networkx
                pei1 = myBGF1
                pei2 = myBGF2

            else:
                # toss whole structures
                pei1 = bgf_file1
                pei2 = bgf_file2

            # to check reduced structures
            #pei1.saveBGF(bgf_file1 + "mod")
            #pei2.saveBGF(bgf_file2 + "mod")

            # convert connection into dictionary
            d_graph1 = bgftools.getConnectionDict(pei1)
            d_graph2 = bgftools.getConnectionDict(pei2)

            # convert dictionary into Graph
            G1 = nx.Graph(d_graph1)
            G2 = nx.Graph(d_graph2)

            # check isomorphism
            GM = isomorphism.GraphMatcher(G1, G2)
            result = GM.is_isomorphic()

            # collect outputs
            qout.put((pid, os.path.basename(bgf_file1),
                      os.path.basename(bgf_file2), result))

            # display the process
            sys.stdout.write("\rNumber of leftover: " +
                             "{0:>8d}".format(qin.qsize()) +
                             ", Number in qout: " +
                             "{0:>8d}".format(qout.qsize()))
            sys.stdout.flush()

    return 1
Ejemplo n.º 7
0
def check_isomorphism(directory, bgf_file, simple):
	"""
	check isomorphism of 'bgf_file' in all files in 'directory'
	"""

	#initialize
	structure_dir = os.path.abspath(directory)
	curr_dir = os.path.abspath(".")
	pei_file = glob.glob(structure_dir + "/*.bgf")
	for i in pei_file:
		if bgf_file in i:
			pei_file.remove(i)	# remove self bgf_file from pei_file

	pei_file.sort()
	n_pei_file = len(pei_file)
	print("The script will compare " + str(n_pei_file) + " files in the directory " + curr_dir)

	joblist = [];	# contains file pairlists
	n_joblist = 0;	# number of total jobs
	count = 0;	# job counter
	t1 = time.time(); t2 = 0;	# time progess

	print("Queueing Jobs..")
	print(n_pei_file)
	if n_pei_file == 0:
		return True;

	for i in range(0, n_pei_file):
		joblist.append([bgf_file, pei_file[i]]);
	n_joblist = len(joblist)

	myBGF1 = bgf.BgfFile(bgf_file)
	if simple:
		# remove hydrogens only in carbon atoms
		pei1 = bgftools.getBackbone(bgf_file1)
	else:
		pei1 = bgf_file1
	

	for job in joblist:
		# initialize
		bgf_file2 = job[1];		# filenames
		pei1 = 0; pei2 = 0; myBGF2 = 0; n_H = [];
		t2 = time.time()
		elapsed = t2 - t1
		estimated = elapsed * (n_joblist - count)
	
		# open BGF
		myBGF2 = bgf.BgfFile(bgf_file2)
	
		if simple:
			# remove hydrogens only in carbon atoms
			pei2 = bgftools.getBackbone(bgf_file2)
		else:
			# toss whole structures
			pei2 = bgf_file2
	
		# convert connection into dictionary
		d_graph1 = bgftools.getConnectionDict(pei1)
		d_graph2 = bgftools.getConnectionDict(pei2)
	
		# convert dictionary into Graph
		G1 = nx.Graph(d_graph1)
		G2 = nx.Graph(d_graph2)
	
		# check isomorphism
		GM = isomorphism.GraphMatcher(G1, G2)
		result = GM.is_isomorphic()

		if not result:
			return bgf_file2;
			#print bgf_file2

		# count
		count += 1;
	
		# display the process
		#sys.stdout.write("\rProgress: " + "{0:>8d}".format(count) + " / " + str(n_joblist) + " (" + str(estimated) + " sec left)"); sys.stdout.flush()

	# completing
	return True;
Ejemplo n.º 8
0
def do_work(directory, out_file, simple, silent=True):

    #initialize
    structure_dir = os.path.abspath(directory)
    curr_dir = os.path.abspath(".")
    pei_file = glob.glob(structure_dir + "/*.bgf")
    pei_file.sort()
    n_pei_file = len(pei_file)
    f_out_file = open(out_file, 'w')  # file for result
    joblist = []
    # contains filelists
    n_joblist = 0
    # number of total jobs
    count = 0
    # job counter
    l_index = []
    # for index

    f_out_file.write(str(sys.argv[0]) + " version " + str(version) + "\n")
    f_out_file.write("" + "\n")
    f_out_file.write("Job started at " + time.asctime(time.gmtime()) + " on " +
                     os.environ["HOSTNAME"] + " by " + os.environ["USER"] +
                     "\n")
    f_out_file.write("Command executed at " + os.getcwd() + "\n")
    f_out_file.write("Requested options: " + str(sys.argv) + "\n")
    f_out_file.write("" + "\n")

    if not silent: print("Queueing Jobs..")
    for i in range(0, n_pei_file):
        joblist.append(pei_file[i])
    n_joblist = len(joblist)

    for job in joblist:
        # initialize
        bgf_file = job
        myPEI = bgf.BgfFile()
        n_H = []

        # open BGF
        myBGF = bgf.BgfFile(bgf_file)

        # remove hydrogens
        pei = bgftools.getBackbone(myBGF, 0)

        # convert connection into dictionary
        d_graph = bgftools.getConnectionDict(pei)

        # convert dictionary into Graph
        G = nx.Graph(d_graph)

        # calculate the all shortest length path
        #d_dist = nx.floyd_warshall(G)	# seems a problem
        d_dist = nx.all_pairs_shortest_path_length(G)

        # count
        count += 1

        # get Wiener Index
        index = 0
        for key in d_dist.iterkeys():
            for key2 in d_dist[key].iterkeys():
                index += d_dist[key][key2]
        index = index / 2.0

        # find duplicates
        if index in l_index:
            output = os.path.basename(bgf_file) + "\t" + str(
                index) + "\t" + "duplicate" + "\n"
        else:
            output = os.path.basename(bgf_file) + "\t" + str(index) + "\n"

        # for index checking
        l_index.append(index)

        # write outputs
        f_out_file.write(output)

        # display the process
        if not silent:
            sys.stdout.write("\rProgress: " + "{0:>8d}".format(count) + " / " +
                             str(n_joblist))
            sys.stdout.flush()

    # check duplicates
    tmp = []
    dup = []
    for i in l_index:
        if i in tmp:
            dup.append(i)
        else:
            tmp.append(i)

    f_out_file.write("\nDuplicated Wiener Indices:\n")
    f_out_file.write(str(dup) + "\n")
    f_out_file.close()

    return 1
Ejemplo n.º 9
0
def do_work(directory, out_file, simple, silent=True):

    #initialize
    structure_dir = os.path.abspath(directory)
    curr_dir = os.path.abspath(".")
    pei_file = glob.glob(structure_dir + "/*.bgf")
    pei_file.sort()
    n_pei_file = len(pei_file)
    f_out_file = open(out_file, 'w')  # file for result
    f_out_file.write(str(sys.argv))
    joblist = []
    # contains file pairlists
    n_joblist = 0
    # number of total jobs
    count = 0
    # job counter
    t1 = time.time()
    t2 = 0
    # time progess

    if not silent: print("Queueing Jobs..")
    for i in range(0, n_pei_file):
        for j in range(i, n_pei_file):
            if not i == j:
                joblist.append([pei_file[i], pei_file[j]])
    n_joblist = len(joblist)

    for job in joblist:
        # initialize
        bgf_file1 = job[0]
        bgf_file2 = job[1]
        # filenames
        pei1 = 0
        pei2 = 0
        myBGF1 = 0
        myBGF2 = 0
        n_H = []
        t2 = time.time()
        elapsed = t2 - t1
        estimated = elapsed * (n_joblist - count)

        # open BGF
        myBGF1 = bgf.BgfFile(bgf_file1)
        myBGF2 = bgf.BgfFile(bgf_file2)

        if simple:
            # remove hydrogens only in carbon atoms
            remove_aNo1 = []
            remove_aNo2 = []
            for atom in myBGF1.a:
                if "C_" in atom.ffType:
                    n_H = []
                    for aNo in atom.CONECT:
                        if myBGF1.getAtom(aNo).is_hydrogen():
                            n_H.append(myBGF1.a2i[aNo])
                    if len(n_H) != 3:
                        n_H.sort()
                        n_H.reverse()
                        myBGF1.delAtoms(n_H)
                        myBGF1.renumber()

            for atom in myBGF2.a:
                if "C_" in atom.ffType:
                    n_H = []
                    for aNo in atom.CONECT:
                        if myBGF2.getAtom(aNo).is_hydrogen():
                            n_H.append(myBGF2.a2i[aNo])
                    if len(n_H) != 3:
                        n_H.sort()
                        n_H.reverse()
                        myBGF2.delAtoms(n_H)
                        myBGF2.renumber()

            # toss reduced BGFs to networkx
            pei1 = myBGF1
            pei2 = myBGF2

        else:
            # toss whole structures
            pei1 = bgf_file1
            pei2 = bgf_file2

        # convert connection into dictionary
        d_graph1 = bgftools.getConnectionDict(pei1)
        d_graph2 = bgftools.getConnectionDict(pei2)

        # convert dictionary into Graph
        G1 = nx.Graph(d_graph1)
        G2 = nx.Graph(d_graph2)

        # check isomorphism
        GM = isomorphism.GraphMatcher(G1, G2)
        result = GM.is_isomorphic()

        # count
        count += 1

        # write outputs
        output = os.path.basename(bgf_file1) + "\t" + os.path.basename(
            bgf_file2) + "\t" + str(result) + "\n"
        f_out_file.write(output)

        # display the process
        if not silent:
            sys.stdout.write("\rProgress: " + "{0:>8d}".format(count) + " / " +
                             str(n_joblist) + " (" + str(estimated) +
                             " sec left)")
            sys.stdout.flush()

    # completing
    f_out_file.close()

    return 1