def simplify_lines_AllFrame(Input_MoleParentFolder_interval, GroupNum, MoleculeNum, threshold):

	fileContents = mfc.read_molecule_files_frominf(Input_MoleParentFolder_interval, GroupNum, MoleculeNum)
	connect_count, frames, cells = mfc.get_frame_count_mole(fileContents)

	straightness = []

	for FrameNum in frames:
		

		## readin molecule backbone information
		fileContents_setframe = mfc.get_content_setframe(fileContents, FrameNum)
		CellNumList_setframe = mfc.get_CellNumbers(fileContents_setframe)

		## create a list to store turning points of simplify polyline
		list_knots = [0, len(CellNumList_setframe)-1]

		## generate the simplify line
		simplify_test = simplify_line_test(CellNumList_setframe, threshold)
		while(simplify_test):

			## for first recursion
			if len(list_knots) == 2:
				re_time = [0]
				simplify_line(CellNumList_setframe, 0, len(CellNumList_setframe)-1, list_knots, threshold, re_time)
			## downstream recursion
			else:
				## for each segment
				for i in range(0, len(list_knots) - 2):
					## check whether this segment need more recursion
					if simplify_test_list[i]:
						re_time = [0]
						simplify_line(CellNumList_setframe, list_knots[i], list_knots[i+1], list_knots, threshold, re_time)
			list_knots = sorted(list(set(list_knots)))

			## check every segment, decide simplify polyline creation is finished or not
			simplify_test_list = []
			for i in range(0, len(list_knots) - 2):
				simplify_test_list.append(simplify_line_test(CellNumList_setframe[list_knots[i]:(list_knots[i+1] + 1)], threshold))
			simplify_test = max(simplify_test_list)
		
		## get turning positions
		turning_knots_cellnums = list(np.array(CellNumList_setframe)[list_knots])

		## get information related to turning angles
		theta_list = angle_list(turning_knots_cellnums)
		theta_max = max(map(abs, theta_list))
		theta_sum = sum(map(abs, theta_list))
		theta_net = sum(theta_list)
	
		## estimation
		test_score_sum = float(1 / math.exp((abs(theta_net) + theta_max) / 2))

		## result report
		straightness.append([FrameNum, theta_net, theta_max, test_score_sum])

	return straightness
def surrounding_OneMolecule_allframe(Input_MoleParentFolder, Output_moleParentFolder, Output_plotParentFolder, GroupNum, MoleculeNum, backbone_range = 2, close = False):

	GroupNum = int(GroupNum)
	MoleculeNum = int(MoleculeNum)
	
	fileContents = mfc.read_molecule_files_frominf(Input_MoleParentFolder, GroupNum, MoleculeNum)
	connect_count, frames, cells = mfc.get_frame_count_mole(fileContents)
	for FrameNum in frames:
		surrounding_OneMolecule(Input_MoleParentFolder, Output_moleParentFolder, Output_plotParentFolder, GroupNum, FrameNum, MoleculeNum, [-1,-1], backbone_range, close)
def surrounding_OneMolecule_allframe(Input_MoleParentFolder,
                                     Output_moleParentFolder,
                                     Output_plotParentFolder,
                                     GroupNum,
                                     MoleculeNum,
                                     backbone_range=2,
                                     close=False):

    GroupNum = int(GroupNum)
    MoleculeNum = int(MoleculeNum)

    fileContents = mfc.read_molecule_files_frominf(Input_MoleParentFolder,
                                                   GroupNum, MoleculeNum)
    connect_count, frames, cells = mfc.get_frame_count_mole(fileContents)
    for FrameNum in frames:
        surrounding_OneMolecule(Input_MoleParentFolder,
                                Output_moleParentFolder,
                                Output_plotParentFolder, GroupNum, FrameNum,
                                MoleculeNum, [-1, -1], backbone_range, close)
def Effected_OneMolecule(Input_MoleParentFolder_interval, Input_SurroundingParentFolder_interval, GroupNum, MoleculeNum, layer, threshold):

#def main(argv):

#	try:                                
#        	opts, args = getopt.getopt(argv, "hs:e:", ["help", "grammar="])
#    	except getopt.GetoptError:          
#        	usage()                         
#        	sys.exit(2)   

#	GroupNum = int(args[0])	
#	MoleculeNum = int(args[1])
#	layer = int(args[2])
	
	GroupNum = int(GroupNum)	
	MoleculeNum = int(MoleculeNum)
	layer = int(layer)

	print GroupNum, MoleculeNum

	GroupName = "group1-" + str(GroupNum) + "-inca34-outputs/"

	
	
	GroupFolder = mfc.get_grouppath(GroupNum, Input_MoleParentFolder_interval)
	if os.path.exists(GroupFolder):
		molecule_fileContents = mfc.read_molecule_files_frominf(Input_MoleParentFolder_interval, GroupNum, MoleculeNum)
		connect_count, frames, cells = mfc.get_frame_count_mole(molecule_fileContents)
		for frame in frames:
			BackbonefileContents = mfc.get_content_setframe(molecule_fileContents, frame)
			
			FrameNum = frame
			surrounding_fileName = "molecule" + str(MoleculeNum) + "_frame" + str(FrameNum) + ".txt"
			SurroundingReadPath = os.path.join(Input_SurroundingParentFolder_interval, GroupName, surrounding_fileName)
			SurrID = open(SurroundingReadPath, 'r')
			SurroundingfileContents = SurrID.readlines()

			ConditionList = determine_within_forbackbone(BackbonefileContents, SurroundingfileContents, layer, threshold)
			

			## plotting
			fig = plt.figure(frame)
			BackboneIntensity = mfc.get_IntenAll(BackbonefileContents)
		
			attention_x = list(map(multiply, range(0, len(BackboneIntensity)), ConditionList))
			attention_x = filter(lambda x:x is not 0, attention_x)
			if ConditionList[0] != 0:
				attention_x.insert(0, 0)

			attention_y = map(multiply, BackboneIntensity, ConditionList)
			if attention_y.count(0) != 0:
				attention_y = filter(lambda y:y is not 0, attention_y)
		
			line, = plt.plot(BackboneIntensity, label = "intensity porfile")
			dot, = plt.plot(attention_x, attention_y, "ro", color = "red")

			Title = "Group" + str(GroupNum) + "_Molecule" + str(MoleculeNum) + " Backbone1 intensity" + "(default threshold" + str(threshold) + ")"
			plt.title(Title)			

			plt.xlabel("position(orientation depend on alignment)/pixels")
			plt.ylabel("intensity")

#			plt.legend([line], "intensity porfile", loc = 4)
			plt.legend([line, dot],  ["intensity porfile", "effected pixels(from layer"+str(layer) + ")"])
#			print ConditionList

		plt.show()
	else:
		print "group not exist!"
def surrounding_OneMolecule(Input_MoleParentFolder, Output_moleParentFolder, Output_plotParentFolder, GroupNum, FrameNum, MoleculeNum, select_region = [-1,-1], backbone_range = 2, close = False):

	GroupNum = int(GroupNum)
	FrameNum = int(FrameNum) 
	MoleculeNum = int(MoleculeNum)
	
	### data input
	grayData_array = fi.get_grayData_frominf(GroupNum, FrameNum)
	display_array1 = copy.deepcopy(grayData_array)
	display_array2 = copy.deepcopy(grayData_array)
	display_array3 = copy.deepcopy(grayData_array)

	fileContents = mfc.read_molecule_files_frominf(Input_MoleParentFolder, GroupNum, MoleculeNum)
	inframe = mfc.find_mole_setframe(fileContents, FrameNum)
	if(not(inframe)):
		print "the molecule is not in this frame! "
		exit(1)
	
	molecule_contents_setframe = mfc.get_content_setframe(fileContents, FrameNum)

	CellNumList_setframe = mfc.get_CellNumbers(molecule_contents_setframe)
	
	heads, tails = mfc.boundary_x(ncol, CellNumList_setframe)
	x_min = mfc.get_coordinate(heads[0],ncol)[1]
	x_max = mfc.get_coordinate(tails[0],ncol)[1]

	if(select_region[0] == -1): select_region[0] = x_min
	if(select_region[1] == -1): select_region[1] = x_max 
	

	CellNumList_setframe_region = []
	for cellnum in CellNumList_setframe:
		inregion = mfc.find_cell_inregion_x(cellnum, select_region, ncol)
		if(inregion):
			CellNumList_setframe_region.append(cellnum)
	
	if(len(CellNumList_setframe) != 0):
		### surrounding intensity acquisition
		surrounding_intensity_circle_1 = surrounding_intensity_circle(CellNumList_setframe_region, grayData_array, display_array1, 2, 1, close)
		surrounding_intensity_circle_2 = surrounding_intensity_circle(CellNumList_setframe_region, grayData_array, display_array2, 2, 2, close)
		surrounding_intensity_circle_3 = surrounding_intensity_circle(CellNumList_setframe_region, grayData_array, display_array3, 2, 3, close)
		
		surrounding_coordinate_1 = surrounding_circle(CellNumList_setframe_region, 2, 1, close)
		surrounding_coordinate_2 = surrounding_circle(CellNumList_setframe_region, 2, 2, close)
		surrounding_coordinate_3 = surrounding_circle(CellNumList_setframe_region, 2, 3, close)

		length = len(surrounding_intensity_circle_3)
		surrounding = np.zeros((length, 6), dtype = int)
		surrounding[0:(len(surrounding_intensity_circle_1)),0] = surrounding_intensity_circle_1
		surrounding[0:(len(surrounding_intensity_circle_2)),1] = surrounding_intensity_circle_2
		surrounding[0:(len(surrounding_intensity_circle_3)),2] = surrounding_intensity_circle_3
		surrounding[0:(len(surrounding_coordinate_1)),3] = surrounding_coordinate_1
		surrounding[0:(len(surrounding_coordinate_2)),4] = surrounding_coordinate_2
		surrounding[0:(len(surrounding_coordinate_3)),5] = surrounding_coordinate_3
	
		### data output
		GroupName = "group1-" + str(GroupNum) + "-inca34-outputs/"
		FrameName = "frame" + str(FrameNum)
		output_groupfolder = os.path.join(Output_moleParentFolder, GroupName)

		## for storing data
		if(not(os.path.exists(output_groupfolder))):
			os.makedirs(output_groupfolder)
		os.chdir(output_groupfolder)
	
		filename = "molecule" + str(MoleculeNum) + "_frame" + str(FrameNum) + ".txt"
		fileid = open(filename, "w")
		fileid.write(" ".join(["intensity1", "intensity2", "intensity3", "coordinate1", "coordinate2", "coordinate3", "\n"]))
		for line in range(0, length):
			line_str = map(str, surrounding[line, :])
			line_str.append("\n")
			fileid.write(" ".join(line_str))
		fileid.close()
		
		print "group", GroupNum, "_frame", FrameNum, "_molecule", MoleculeNum, ": surrounding file created!"
	
		## plotting the frame
		mole_start_position = mfc.get_coordinate(CellNumList_setframe[0],ncol)
		text_position = [mole_start_position[0]-15, mole_start_position[1]]
		text = "mole_" + str(MoleculeNum)
		
		plt.figure(FrameNum, figsize=(16, 12), dpi = 100)
    		mergeplot = plt.imshow(display_array1)
    		mergeplot.set_cmap(colormap)
		plt.text(text_position[1], text_position[0], text, color = "yellow", size = 'medium', weight = 400)
    		plt.colorbar()
    		mergeplot.set_clim([np.min(display_array3), image_max])
	
		framename_pdf = "molecule" + str(MoleculeNum) + "_frame" + str(FrameNum) + ".pdf"
		framename_png = "molecule" + str(MoleculeNum) + "_frame" + str(FrameNum) + ".png"
		plt.savefig(framename_pdf)
		plt.savefig(framename_png)
#		plt.show()
		plt.close()


		## save plots to another folder
		output_groupfolder_plot = os.path.join(Output_plotParentFolder, GroupName)
		if(not(os.path.exists(output_groupfolder_plot))):
			os.makedirs(output_groupfolder_plot)
		framename_pdf_plot = framename_pdf
		framename_png_plot = framename_png
		shutil.copyfile(os.path.join(output_groupfolder,framename_pdf),os.path.join(output_groupfolder_plot,framename_pdf_plot))
		shutil.copyfile(os.path.join(output_groupfolder,framename_png),os.path.join(output_groupfolder_plot,framename_png_plot))
		os.remove(os.path.join(output_groupfolder,framename_pdf))
		os.remove(os.path.join(output_groupfolder,framename_png))


	else:
		print("selected region wrong! no pixel of the molecule in this region..")
def surrounding_OneMolecule(Input_MoleParentFolder,
                            Output_moleParentFolder,
                            Output_plotParentFolder,
                            GroupNum,
                            FrameNum,
                            MoleculeNum,
                            select_region=[-1, -1],
                            backbone_range=2,
                            close=False):

    GroupNum = int(GroupNum)
    FrameNum = int(FrameNum)
    MoleculeNum = int(MoleculeNum)

    ### data input
    grayData_array = fi.get_grayData_frominf(GroupNum, FrameNum)
    display_array1 = copy.deepcopy(grayData_array)
    display_array2 = copy.deepcopy(grayData_array)
    display_array3 = copy.deepcopy(grayData_array)

    fileContents = mfc.read_molecule_files_frominf(Input_MoleParentFolder,
                                                   GroupNum, MoleculeNum)
    inframe = mfc.find_mole_setframe(fileContents, FrameNum)
    if (not (inframe)):
        print "the molecule is not in this frame! "
        exit(1)

    molecule_contents_setframe = mfc.get_content_setframe(
        fileContents, FrameNum)

    CellNumList_setframe = mfc.get_CellNumbers(molecule_contents_setframe)

    heads, tails = mfc.boundary_x(ncol, CellNumList_setframe)
    x_min = mfc.get_coordinate(heads[0], ncol)[1]
    x_max = mfc.get_coordinate(tails[0], ncol)[1]

    if (select_region[0] == -1): select_region[0] = x_min
    if (select_region[1] == -1): select_region[1] = x_max

    CellNumList_setframe_region = []
    for cellnum in CellNumList_setframe:
        inregion = mfc.find_cell_inregion_x(cellnum, select_region, ncol)
        if (inregion):
            CellNumList_setframe_region.append(cellnum)

    if (len(CellNumList_setframe) != 0):
        ### surrounding intensity acquisition
        surrounding_intensity_circle_1 = surrounding_intensity_circle(
            CellNumList_setframe_region, grayData_array, display_array1, 2, 1,
            close)
        surrounding_intensity_circle_2 = surrounding_intensity_circle(
            CellNumList_setframe_region, grayData_array, display_array2, 2, 2,
            close)
        surrounding_intensity_circle_3 = surrounding_intensity_circle(
            CellNumList_setframe_region, grayData_array, display_array3, 2, 3,
            close)

        surrounding_coordinate_1 = surrounding_circle(
            CellNumList_setframe_region, 2, 1, close)
        surrounding_coordinate_2 = surrounding_circle(
            CellNumList_setframe_region, 2, 2, close)
        surrounding_coordinate_3 = surrounding_circle(
            CellNumList_setframe_region, 2, 3, close)

        length = len(surrounding_intensity_circle_3)
        surrounding = np.zeros((length, 6), dtype=int)
        surrounding[0:(len(surrounding_intensity_circle_1)),
                    0] = surrounding_intensity_circle_1
        surrounding[0:(len(surrounding_intensity_circle_2)),
                    1] = surrounding_intensity_circle_2
        surrounding[0:(len(surrounding_intensity_circle_3)),
                    2] = surrounding_intensity_circle_3
        surrounding[0:(len(surrounding_coordinate_1)),
                    3] = surrounding_coordinate_1
        surrounding[0:(len(surrounding_coordinate_2)),
                    4] = surrounding_coordinate_2
        surrounding[0:(len(surrounding_coordinate_3)),
                    5] = surrounding_coordinate_3

        ### data output
        GroupName = "group1-" + str(GroupNum) + "-inca34-outputs/"
        FrameName = "frame" + str(FrameNum)
        output_groupfolder = os.path.join(Output_moleParentFolder, GroupName)

        ## for storing data
        if (not (os.path.exists(output_groupfolder))):
            os.makedirs(output_groupfolder)
        os.chdir(output_groupfolder)

        filename = "molecule" + str(MoleculeNum) + "_frame" + str(
            FrameNum) + ".txt"
        fileid = open(filename, "w")
        fileid.write(" ".join([
            "intensity1", "intensity2", "intensity3", "coordinate1",
            "coordinate2", "coordinate3", "\n"
        ]))
        for line in range(0, length):
            line_str = map(str, surrounding[line, :])
            line_str.append("\n")
            fileid.write(" ".join(line_str))
        fileid.close()

        print "group", GroupNum, "_frame", FrameNum, "_molecule", MoleculeNum, ": surrounding file created!"

        ## plotting the frame
        mole_start_position = mfc.get_coordinate(CellNumList_setframe[0], ncol)
        text_position = [mole_start_position[0] - 15, mole_start_position[1]]
        text = "mole_" + str(MoleculeNum)

        plt.figure(FrameNum, figsize=(16, 12), dpi=100)
        mergeplot = plt.imshow(display_array1)
        mergeplot.set_cmap(colormap)
        plt.text(text_position[1],
                 text_position[0],
                 text,
                 color="yellow",
                 size='medium',
                 weight=400)
        plt.colorbar()
        mergeplot.set_clim([np.min(display_array3), image_max])

        framename_pdf = "molecule" + str(MoleculeNum) + "_frame" + str(
            FrameNum) + ".pdf"
        framename_png = "molecule" + str(MoleculeNum) + "_frame" + str(
            FrameNum) + ".png"
        plt.savefig(framename_pdf)
        plt.savefig(framename_png)
        #		plt.show()
        plt.close()

        ## save plots to another folder
        output_groupfolder_plot = os.path.join(Output_plotParentFolder,
                                               GroupName)
        if (not (os.path.exists(output_groupfolder_plot))):
            os.makedirs(output_groupfolder_plot)
        framename_pdf_plot = framename_pdf
        framename_png_plot = framename_png
        shutil.copyfile(
            os.path.join(output_groupfolder, framename_pdf),
            os.path.join(output_groupfolder_plot, framename_pdf_plot))
        shutil.copyfile(
            os.path.join(output_groupfolder, framename_png),
            os.path.join(output_groupfolder_plot, framename_png_plot))
        os.remove(os.path.join(output_groupfolder, framename_pdf))
        os.remove(os.path.join(output_groupfolder, framename_png))

    else:
        print(
            "selected region wrong! no pixel of the molecule in this region..")
def simplify_lines_AllFrame(Input_MoleParentFolder_interval, GroupNum,
                            MoleculeNum, threshold):

    fileContents = mfc.read_molecule_files_frominf(
        Input_MoleParentFolder_interval, GroupNum, MoleculeNum)
    connect_count, frames, cells = mfc.get_frame_count_mole(fileContents)

    straightness = []

    for FrameNum in frames:

        ## readin molecule backbone information
        fileContents_setframe = mfc.get_content_setframe(
            fileContents, FrameNum)
        CellNumList_setframe = mfc.get_CellNumbers(fileContents_setframe)

        ## create a list to store turning points of simplify polyline
        list_knots = [0, len(CellNumList_setframe) - 1]

        ## generate the simplify line
        simplify_test = simplify_line_test(CellNumList_setframe, threshold)
        while (simplify_test):

            ## for first recursion
            if len(list_knots) == 2:
                re_time = [0]
                simplify_line(CellNumList_setframe, 0,
                              len(CellNumList_setframe) - 1, list_knots,
                              threshold, re_time)
            ## downstream recursion
            else:
                ## for each segment
                for i in range(0, len(list_knots) - 2):
                    ## check whether this segment need more recursion
                    if simplify_test_list[i]:
                        re_time = [0]
                        simplify_line(CellNumList_setframe, list_knots[i],
                                      list_knots[i + 1], list_knots, threshold,
                                      re_time)
            list_knots = sorted(list(set(list_knots)))

            ## check every segment, decide simplify polyline creation is finished or not
            simplify_test_list = []
            for i in range(0, len(list_knots) - 2):
                simplify_test_list.append(
                    simplify_line_test(
                        CellNumList_setframe[list_knots[i]:(list_knots[i + 1] +
                                                            1)], threshold))
            simplify_test = max(simplify_test_list)

        ## get turning positions
        turning_knots_cellnums = list(
            np.array(CellNumList_setframe)[list_knots])

        ## get information related to turning angles
        theta_list = angle_list(turning_knots_cellnums)
        theta_max = max(map(abs, theta_list))
        theta_sum = sum(map(abs, theta_list))
        theta_net = sum(theta_list)

        ## estimation
        test_score_sum = float(1 / math.exp((abs(theta_net) + theta_max) / 2))

        ## result report
        straightness.append([FrameNum, theta_net, theta_max, test_score_sum])

    return straightness
def Effected_OneMolecule(Input_MoleParentFolder_interval,
                         Input_SurroundingParentFolder_interval, GroupNum,
                         MoleculeNum, layer, threshold):

    #def main(argv):

    #	try:
    #        	opts, args = getopt.getopt(argv, "hs:e:", ["help", "grammar="])
    #    	except getopt.GetoptError:
    #        	usage()
    #        	sys.exit(2)

    #	GroupNum = int(args[0])
    #	MoleculeNum = int(args[1])
    #	layer = int(args[2])

    GroupNum = int(GroupNum)
    MoleculeNum = int(MoleculeNum)
    layer = int(layer)

    print GroupNum, MoleculeNum

    GroupName = "group1-" + str(GroupNum) + "-inca34-outputs/"

    GroupFolder = mfc.get_grouppath(GroupNum, Input_MoleParentFolder_interval)
    if os.path.exists(GroupFolder):
        molecule_fileContents = mfc.read_molecule_files_frominf(
            Input_MoleParentFolder_interval, GroupNum, MoleculeNum)
        connect_count, frames, cells = mfc.get_frame_count_mole(
            molecule_fileContents)
        for frame in frames:
            BackbonefileContents = mfc.get_content_setframe(
                molecule_fileContents, frame)

            FrameNum = frame
            surrounding_fileName = "molecule" + str(
                MoleculeNum) + "_frame" + str(FrameNum) + ".txt"
            SurroundingReadPath = os.path.join(
                Input_SurroundingParentFolder_interval, GroupName,
                surrounding_fileName)
            SurrID = open(SurroundingReadPath, 'r')
            SurroundingfileContents = SurrID.readlines()

            ConditionList = determine_within_forbackbone(
                BackbonefileContents, SurroundingfileContents, layer,
                threshold)

            ## plotting
            fig = plt.figure(frame)
            BackboneIntensity = mfc.get_IntenAll(BackbonefileContents)

            attention_x = list(
                map(multiply, range(0, len(BackboneIntensity)), ConditionList))
            attention_x = filter(lambda x: x is not 0, attention_x)
            if ConditionList[0] != 0:
                attention_x.insert(0, 0)

            attention_y = map(multiply, BackboneIntensity, ConditionList)
            if attention_y.count(0) != 0:
                attention_y = filter(lambda y: y is not 0, attention_y)

            line, = plt.plot(BackboneIntensity, label="intensity porfile")
            dot, = plt.plot(attention_x, attention_y, "ro", color="red")

            Title = "Group" + str(GroupNum) + "_Molecule" + str(
                MoleculeNum
            ) + " Backbone1 intensity" + "(default threshold" + str(
                threshold) + ")"
            plt.title(Title)

            plt.xlabel("position(orientation depend on alignment)/pixels")
            plt.ylabel("intensity")

            #			plt.legend([line], "intensity porfile", loc = 4)
            plt.legend([line, dot], [
                "intensity porfile",
                "effected pixels(from layer" + str(layer) + ")"
            ])
#			print ConditionList

        plt.show()
    else:
        print "group not exist!"