Пример #1
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
Пример #2
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)
Пример #3
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
Пример #4
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;
Пример #5
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