Example #1
0
def run():
	print '--> mission Break Their Will'

	if not ship.enableDefense():
		return False

	if not overview.switchTo('lcs'):
		return False

	if not overview.lockTarget('repair_station'):
		return False

	if not general.openMissionDetails():
		return False

	if not drones.launchSentry():
		return False

	if not drones.engage():
		return False

	if not general.missionObjectiveComplete():
		return False

	if not drones.back():
		return False

	print '<-- mission Break Their Will\n'
	return True
def run():
    print "--> mission Intercept the Pirate Smugglers"

    result = None
    begin = time.time()
    while not result and time.time() - begin < 5:
        time.sleep(0.2)
        result = findAtFull("close")
    if result:
        mouse.leftClickAtP(result)

    if not ship.enableDefense():
        return False

    if not drones.launchSmall():
        return False

    if not general.openMissionDetails():
        return False

    ship.enableAfterburn()

    overview.seekAndDestory()

    if not general.missionObjectiveComplete():
        return False

    if not drones.back():
        return False

    print "<-- mission Intercept the Pirate Smugglers\n"
    return True
Example #3
0
    def Start(self):
        
        self.queue.put(lambda: self.FlexAID.root.withdraw())
        cmd.window('hide')
        self.queue.put(lambda: cmd.window('show'))
        cmd.refresh_wizard()

        # Display the ligand from the PDB file
        if self.DisplayLigand():
            self.FlexAID.DisplayMessage("Error while trying to display the ligand from PDB file", 1)
            self.Quit_Wizard(self.ErrorCode)
            return
                    
        # Color the Atoms based on their type
        if self.ColorByType():
            self.FlexAID.DisplayMessage("Error while coloring the ligand atoms by type", 1)
            self.Quit_Wizard(self.ErrorCode)
            return            

        # Mask everythign except modifyable object (ligand)
        self.exc = [self.LigDisplay]
        General.mask_Objects(self.exc)        

        self.selection_mode = cmd.get("mouse_selection_mode")
        cmd.set("mouse_selection_mode", 0) # set selection mode to atomic

        # remove any possible selection before selecting atoms
        cmd.deselect()

        self.ErrorCode += 1
def run():
	print '--> mission What Comes Around Goes Around'

	if not ship.enableDefense():
		return False

	if not drones.launchSmall():
		return False

	result = None
	begin = time.time()
	while not result and time.time() - begin < 5:
		time.sleep(0.2)
		result = findAtFull('close')
	if result:
		mouse.leftClickAtP(result)

	if not general.openMissionDetails():
		return False

	if not general.missionObjectiveComplete():
		return False

	if not drones.back():
		return False

	print '<-- mission What Comes Around Goes Around\n'
	return True
Example #5
0
    def Quit_Wizard(self, rv):
        
        try:
            cmd.delete(self.LigDisplay)
            cmd.delete(self.AtomDisplay)         

            cmd.deselect()

            if rv != 1:
                General.unmask_Objects(self.exc)
                cmd.set('mouse_selection_mode', self.selection_mode)
                #cmd.config_mouse('three_button_editing', 1)
      
            if rv > 0:
                self.FlexAID.WizardError = True
        
            self.FlexAID.ActiveWizard = None

            cmd.set_wizard()
            cmd.set_view(self.View)
            cmd.refresh()

            self.top.SATRunning(False)

            self.queue.put(lambda: self.top.AnchorRunning(False))
            self.queue.put(lambda: self.FlexAID.root.deiconify())
            self.queue.put(lambda: self.FlexAID.root.update())

        except:
            pass
Example #6
0
def run():
	print '--> mission The Guristas Spies'

	if not ship.enableDefense():
		return False

	if not overview.activateAccelerationGate():
		return False 

	if not drones.launchSmall():
		return False

	if not general.openMissionDetails():
		return False

	overview.seekAndDestory()

	if not general.missionObjectiveComplete():
		return False

	if not drones.back():
		return False

	print '<-- mission The Guristas Spies\n'
	return True
def run():
	print '--> mission The Space Telescope'

	if not ship.enableDefense():
		return False

	if not overview.activateAccelerationGate():
		return False

	if not overview.switchTo('lcs'):
		return False

	if not overview.lockTarget('guristas_space', 15):
		return False

	if not drones.launchSmall():
		return False

	ship.fireOnce()

	drones.engage()

	if not general.openMissionDetails():
		return False

	if not general.missionObjectiveComplete():
		return False

	if not drones.back():
		return False

	print '<-- mission The Space Telescope\n'
	return True
Example #8
0
def run():
	print '--> mission Deadly Arrival'

	if not ship.enableDefense():
		return False

	if not overview.switchTo('lco'):
		return False

	if not overview.lockTarget('ruined_structure'):
		return False

	if not ship.approach():
		return False

	if not ship.enableAfterburn():
		return False

	if not general.openMissionDetails():
		return False

	if not general.missionObjectiveComplete():
		return False

	print '<-- mission Deadly Arrival\n'
	return True
Example #9
0
def run():
	print '--> mission New Frontiers - Asnendanne (7 of 7)'

	if not ship.enableDefense():
		return False

	if not general.openMissionDetails():
		return False

	ship.enableAfterburn()

	if not overview.activateAccelerationGate():
		return False

	print 'pocket 1'

	if not drones.launchSmall():
		return False

	overview.seekAndDestory()

	if not general.missionObjectiveComplete():
		return False

	if not drones.back():
		return False


	print '<-- mission New Frontiers - Asnendanne (7 of 7)\n'
	return True
Example #10
0
def run():
	print '--> mission Retribution'

	if not ship.enableDefense():
		return False

	if not overview.activateAccelerationGate():
		return False

	if not overview.switchTo('lcs'):
		return False

	if not overview.lockTarget('guristas_outpost', 5):
		return False

	if not general.openMissionDetails():
		return False

	if not drones.launchSentry():
		return False

	ship.fireOnce()

	drones.engage()

	if not general.missionObjectiveComplete():
		return False

	if not drones.back():
		return False

	print '<-- mission Retribution\n'
	return True
def run():
	print '--> mission Unauthorized Military Presence'

	if not ship.enableDefense():
		return False

	if not general.openMissionDetails():
		return False

	ship.enableAfterburn()

	if not overview.activateAccelerationGate():
		return False

	if not overview.switchTo('battle'):
		return False

	if not overview.lockTarget('guristas_personnel'):
		return False

	if not ship.enableAfterburn():
		return False

	if not ship.approachFor(240):
		return False

	if not overview.lockTarget('guristas_personnel', 20):
		return False

	if not drones.launchSmall():
		return False

	if not ship.fireOnce():
		return False

	if not drones.engage():
		return False

	# mission item is in wreck
	while not findAtMissionDetails('v'):
		# collect
		overview.pickWreck()	

	if not general.missionObjectiveComplete():
		return False

	# sometimes we already has millitans
	# but the mission is not complete until all enemy dies
	overview.seekAndDestory()

	# just in case
	overview.pickTarget('guristas_personnel')

	if not drones.back():
		return False

	print '<-- mission Unauthorized Military Presence\n'
	return True
def run():
	print '--> mission Eliminate the Pirate Campers'

	if not ship.enableDefense():
		return False

	if not drones.launchSentry():
		return False

	if not overview.switchTo('battle'):
		return False

	begin = time.time()
	result = None
	while not result and time.time() - begin < 5:
		result = findAtFull('close')
	if result:
		mouse.leftClickAtP(result)

	# kill small

	count = 0
	while count < 4:
		result = overview.lockTarget('s', 1)
		if result:
			count += 1

	begin = time.time()
	while time.time() - begin < 120:
		overview.lockTarget('s', 1)
		ship.fireOnce()
		drones.engage()

	if not drones.back():
		return False

	# seek and destory

	if not general.openMissionDetails():
		return False

	ship.enableAfterburn()

	if not drones.launchSmall():
		return False

	overview.seekAndDestory()

	if not general.missionObjectiveComplete():
		return False

	if not drones.back():
		return False

	print '<-- mission Eliminate the Pirate Campers\n'
	return True
Example #13
0
def start_with_computer(enabled):
    """If enabled, create shortcut to start application with computer.
    If disabled, then delete the shortcut."""
    if not enabled:
        if os.path.lexists(autostart_path):
            FileUtilities.delete(autostart_path)
        return
    if os.path.lexists(autostart_path):
        return
    import shutil
    General.makedirs(os.path.dirname(autostart_path))
    shutil.copy(launcher_path, autostart_path)
    os.chmod(autostart_path, 0755)
    if General.sudo_mode():
        General.chownself(autostart_path)
Example #14
0
def activateAccelerationGate():
	switchTo('pilot')
	general.enterStarMap()

	print '--> activate acceleration gate\n'

	mouse.leftDownAtP(panel.center(panel.Full))
	mouse.move(500, 200)
	mouse.leftUp()

	result = findTarget('acceleration_gate')
	if not result:
		return False
	mouse.leftClickAtP(result)
	key.pressEx(sc.Activate)

	print 'wait to activate gate'
	while not findAtDashboard('warp_drive_active'):
		result = findAtFull('close')
		if not result:
			result = findAtFull('ok')
		if result:
			mouse.leftClickAtP(result)
			mouse.moveTo(result[0], result[1] - 200)
		handleDangerousAction();
		key.pressEx(sc.Activate)
		time.sleep(1)

	print 'wait until reach location'
	while findAtDashboard('warp_drive_active'):
		time.sleep(0.5)

	result = findAtFull('close')
	if not result:
		result = findAtFull('ok')
	if result:
		mouse.leftClickAtP(result)
		mouse.moveTo(result[0], result[1] - 200)
	time.sleep(1)

	while findAtDashboard('warp_drive_active'):
		time.sleep(0.5)

	print '<-- activate acceleration gate\n'

	general.exitStarMap()
	
	return True
def getAvgMov(track, **kwargs):

	SPEED_CONVERSION_FACTOR = kwargs["speedConversionFactor"]

	avgMov = []
	diff = []
	frameDistanceBetweenFrames = []
	positionalDistanceBetweenFrames = []
	for index in range(0, len(track.x) - 1):
		#the number of frames by which two adjacent elements in track are separated
		frameDistanceBetweenFrames = track.t[index + 1] - track.t[index] 
		#the absolute positional distance between adjacent tracked frames
		positionalDistanceBetweenFrames = gen.getDistance(track.x[index], track.x[index + 1], track.y[index], track.y[index + 1])
		#the distance traveled per frame between adjacent tracked frames
		diff.append(positionalDistanceBetweenFrames / frameDistanceBetweenFrames)
	return gen.nanMean(diff) * SPEED_CONVERSION_FACTOR
Example #16
0
 def __init__(self, action_element):
     """Initialize file search"""
     self.regex = action_element.getAttribute('regex')
     assert(isinstance(self.regex, (str, unicode, types.NoneType)))
     self.nregex = action_element.getAttribute('nregex')
     assert(isinstance(self.nregex, (str, unicode, types.NoneType)))
     self.wholeregex = action_element.getAttribute('wholeregex')
     assert(isinstance(self.wholeregex, (str, unicode, types.NoneType)))
     self.nwholeregex = action_element.getAttribute('nwholeregex')
     assert(isinstance(self.nwholeregex, (str, unicode, types.NoneType)))
     self.search = action_element.getAttribute('search')
     self.object_type = action_element.getAttribute('type')
     self.path = os.path.expanduser(FileUtilities.expandvars(
         action_element.getAttribute('path')))
     if 'nt' == os.name and self.path:
         # convert forward slash to backslash for compatibility with getsize()
         # and for display.  Do not convert an empty path, or it will become
         # the current directory (.).
         self.path = os.path.normpath(self.path)
     self.ds = {}
     if 'deep' == self.search:
         self.ds['regex'] = self.regex
         self.ds['nregex'] = self.nregex
         self.ds['cache'] = General.boolstr_to_bool(
             action_element.getAttribute('cache'))
         self.ds['command'] = action_element.getAttribute('command')
         self.ds['path'] = self.path
     if not any([self.object_type, self.regex, self.nregex, self.wholeregex, self.nwholeregex]):
         # If the filter is not needed, bypass it for speed.
         self.get_paths = self._get_paths
Example #17
0
def run():
	print '--> mission New Frontiers - Raw Materials (6 of 7)'

	if not station.openInventory():
		return False

	if not station.loadItem('mission_cargo'):
		return False

	if not station.closeInventory():
		return False

	if not station.undock():
		return False

	pilot.autopilot()

	if not general.setMissionWaypoint():
		return False

	if not station.startConversation(agent):
		return False

	if not station.completeMission():
		return False

	if not station.undock():
		return False

	pilot.autopilot()


	print '<-- mission New Frontiers - Raw Materials (6 of 7)\n'
	return True
Example #18
0
    def Save_SessionFile(self, SaveFile):

        if len(SaveFile) > 0:
            
            SaveFile = os.path.normpath(SaveFile)
            
            if General.validate_String(SaveFile, '.nrgfs', True, True, False):
                self.DisplayMessage("  ERROR: Could not save the file because you entered an invalid name.", 2)
                return
            
            if self.ValidateSaveProject(SaveFile, 'Session'):
                self.DisplayMessage("  ERROR: The file can only be saved at its default location", 2)
                return
            
            try:
                out = open(SaveFile, 'wb')
                pickle.dump(self.SessionPath, out)
                
                for Tab in self.listTabs:
                    pickle.dump(Tab.Vars, out)
                out.close()
                
                self.SaveSessionFile = SaveFile
                self.DisplayMessage("  The session '" + os.path.split(SaveFile)[1] + "' was saved successfully.", 2)
                
            except pickle.PicklingError:
                self.DisplayMessage("  ERROR: Could not properly save the session: " + str(sys.exc_info()), 2)
            except:
                self.DisplayMessage("  Unexpected error: " + str(sys.exc_info()), 2)
def run():
	print '--> mission Technological Secrets 2'

	# this is a transport mission
	if not (station.openInventory() \
		and station.loadItem('dna_sample') \
		and station.closeInventory()):
		return False

	if not station.undock():
		return False

	pilot.autopilot()

	if not general.setMissionWaypoint():
		return False

	if not station.startConversation(agent):
		return False

	if not station.completeMission():
		return False

	if not station.undock():
		return False

	pilot.autopilot()

	print '<-- mission Technological Secrets 2\n'
	return True
Example #20
0
 def __init__(self, action_element):
     """Initialize file search"""
     self.regex = action_element.getAttribute('regex')
     assert(isinstance(self.regex, (str, unicode, types.NoneType)))
     self.nregex = action_element.getAttribute('nregex')
     assert(isinstance(self.nregex, (str, unicode, types.NoneType)))
     self.search = action_element.getAttribute('search')
     self.object_type = action_element.getAttribute('type')
     self.path = os.path.expanduser(os.path.expandvars(
         action_element.getAttribute('path')))
     if 'nt' == os.name and self.path:
         # convert forward slash to backslash for compatibility with getsize()
         # and for display.  Do not convert an empty path, or it will become
         # the current directory (.).
         self.path = os.path.normpath(self.path)
     self.ds = {}
     if 'deep' == self.search:
         self.ds['regex'] = self.regex
         self.ds['nregex'] = self.nregex
         self.ds['cache'] = General.boolstr_to_bool(
             action_element.getAttribute('cache'))
         self.ds['command'] = action_element.getAttribute('command')
         self.ds['path'] = self.path
     if self.object_type:
         if 'f' == self.object_type:
             self.object_filter = os.path.isfile
         elif 'd' == self.object_type:
             self.object_filter = os.path.isdir
         else:
             raise RuntimeError('unsupported type %s in %s' % \
                 (self.object_type, self.action_element))
     else:
         # faster to bypass
         self.get_paths = self._get_paths
def getVelocity(track, **kwargs):
	SPEED_CONVERSION_FACTOR = kwargs["speedConversionFactor"]

	age = getAge(track)
	xTravel = track.x[-1] - track.x[0]
	yTravel = track.y[-1] - track.y[0]
	return (gen.pythagorean(xTravel, yTravel) / age) * SPEED_CONVERSION_FACTOR
Example #22
0
 def Btn_Create_Clicked(self):
     
     name = self.ProjName.get().strip()
     if name != '':
         
         if General.validate_String(name, '', False, True, False):
             print('  ERROR: The name \'' + name + '\' is invalid. Try again.')
             return
         
         Project_Dir = os.path.join(self.ActualDirPath,name)
         
         if not os.path.isdir(Project_Dir):
             try:
                 os.makedirs(Project_Dir)
             except OSError:
                 print('  ERROR: Could not create the project: \'' + name + '\' under that directory.')
                 return 
                 
             self.Update_ProjectFile(Project_Dir, name)
             
             self.top.Project_Dir = Project_Dir
             self.top.ProjectName = name       
             
             self.Quit()
             self.After_Quit()
             
             print('  Successfully created the project: \'' + name + '\'')
             print('  The project was loaded by default.')
 
         else:
             print('  The project: \'' + name + '\' already exists.')
def getMP(track, **kwargs):
	travel = []
	xDiffs = gen.getDifferenceArray(track.x)
	yDiffs = gen.getDifferenceArray(track.y)

	for x, y in zip(xDiffs, yDiffs):
		travel.append(gen.pythagorean(x, y))

	totalTravel = np.sum(travel)

	xDistance = track.x[-1] - track.x[0]
	yDistance = track.y[-1] - track.y[0]

	if totalTravel == 0:
		return 0

	return gen.pythagorean(xDistance, yDistance) / totalTravel
Example #24
0
 def __flush(self):
     """Write information to disk"""
     if not self.purged:
         self.__purge()
     if not os.path.exists(Common.options_dir):
         General.makedirs(Common.options_dir)
     mkfile = not os.path.exists(Common.options_file)
     _file = open(Common.options_file, 'wb')
     try:
         self.config.write(_file)
     except IOError, e:
         print e
         from errno import ENOSPC
         if e.errno == ENOSPC:
             print "Error: disk is full writing configuration '%s'" % Common.options_file
         else:
             raise
Example #25
0
def get_proc_swaps():
    """Return the output of 'swapon -s'"""
    # Usually 'swapon -s' is identical to '/proc/swaps'
    # Here is one exception: https://bugs.launchpad.net/ubuntu/+source/bleachbit/+bug/1092792
    (rc, stdout, _) = General.run_external(["swapon", "-s"])
    if 0 == rc:
        return stdout
    print 'debug: "swapoff -s" failed so falling back to /proc/swaps'
    return open("/proc/swaps").read()
Example #26
0
    def Enter(self):

        if General.validate_String(self.SmilesName.get(), '', False, False, True):
            self.top.DisplayMessage("  ERROR: Could not process the smiles string because you entered an invalid name.", 2)
            return
            
        #Test : CCC[C@@H](O)CC\C=C\C=C\C#CC#C\C=C\CO
        self.inputWindow.destroy()
        self.top.SmilesRunning(False, True)
Example #27
0
    def Add_List(self, Items, BGColors):
        
        for n in range(0, len(Items)):
            for i in range(0, self.nCol):
                self.Columns[self.ColNames[i]]['List'].insert(END, General.repeat(' ', self.Columns[self.ColNames[i]]['Spacer']) + str(Items[n][i]))

                if BGColors[n][i] != None:
                    self.Columns[self.ColNames[i]]['List'].itemconfig(self.Columns[self.ColNames[i]]['List'].size()-1,
                                                                      bg=BGColors[n][i])
Example #28
0
def is_process_running_wmic(name):
    """Return boolean whether process (like firefox.exe) is running

    Works on Windows XP Professional but not on XP Home
    """

    clean_name = re.sub("[^A-Za-z\.]", "_", name).lower()
    args = ["wmic", "path", "win32_process", "where", "caption='%s'" % clean_name, "get", "Caption"]
    (_, stdout, _) = General.run_external(args)
    return stdout.lower().find(clean_name) > -1
Example #29
0
 def Add(self, Item, BGColor):
     
     for i in range(0, self.nCol):
         try:
             self.Columns[self.ColNames[i]]['List'].insert(END, General.repeat(' ', self.Columns[self.ColNames[i]]['Spacer']) + str(Item[i]))
             
             if BGColor[i] != None:
                 self.Columns[self.ColNames[i]]['List'].itemconfig(self.Columns[self.ColNames[i]]['List'].size()-1, bg=BGColor[i])
         except:
             pass
Example #30
0
def is_process_running_wmic(name):
    """Return boolean whether process (like firefox.exe) is running

    Works on Windows XP Professional but not on XP Home
    """

    clean_name = re.sub('[^A-Za-z\.]', '_', name).lower()
    args = ['wmic', 'path', 'win32_process', 'where', "caption='%s'" % clean_name, 'get', 'Caption']
    (_, stdout, _) = General.run_external(args)
    return stdout.lower().find(clean_name) > -1
Example #31
0
 
 if loginServer == False:
     sendmsg("DataX","LOGIN la78zg294dap7swpe6wx ")
     loginServer = True
     
 if nick.lower() == "datax":
     ircmsg = ircmsg.split(" :",1)[1]
     if "ECHO PENDING " in ircmsg:
         sendmsg("datax","ECHO " + ircmsg.split("ECHO PENDING")[1])
     elif "JOIN " in ircmsg:
         joinchan(ircmsg.split(" ")[-1])
     elif "SEND" in ircmsg and "PRIVMSG" in ircmsg:
         sendmsg(ircmsg.split(" :")[0].split(" ")[-1]," " + ircmsg.split(":")[1])
 
 #Get titles of items if in special channels
 urlData = General.urlInfo(data_channels,channel,ircmsg)
 if urlData != None:
     sendmsg(channel,urlData)
     
 #Copy of auto op
 for i in auto_ops:
     data = i.split(',')
     if data[0].lower() == nick.lower()  or data[0].lower() == hostmask.lower():
         if data[1].lower() == channel or data[1] == "all":
             if data[2] == "kick":
                 kickuser(channel,nick,"Sorry you are autokicked")
 
 #Anti iovoidbot inforce :D
 if ircmsg.find("?!enforce +b") != -1 and ircmsg.find("unaffiliated/bowserinator") != -1:
     sendmsg(channel,"?!unenforce +b *!*@unaffiliated/bowserinator")
     kickuser(channel,"IovoidBot","Damn you iovoid")
Example #32
0
    def checkParam( self, typ, time2go, duration ):

        if not typ or time2go < 0 or duration < 0 or not General.checkActionType( typ ):
            return False
        return True
Example #33
0
    def Btn_StartGetCleft_Clicked(self):

        if self.top.ValidateProcessRunning():
            return

        if self.Validate_Fields():
            self.DisplayMessage("  ERROR: Not all fields are validated.", 2)
            return

        try:
            TmpFile = os.path.join(self.top.GetCleftProject_Dir, 'tmp.pdb')

            if self.PyMOL:
                if cmd.count_atoms(self.defaultOption.get()) == 0:
                    self.DisplayMessage(
                        "  ERROR: No atoms found for object/selection '" +
                        self.defaultOption.get() + "'", 2)
                    return

                cmd.save(TmpFile, self.defaultOption.get(), 1)

        except:
            self.DisplayMessage(
                "  ERROR: Could not save the object/selection '" +
                self.defaultOption.get() + "'", 2)
            return

        if self.ResiduValue.get() != '':

            # with HETATM groups
            if General.store_Residues(self.listResidues, TmpFile, 1) == -1:
                self.DisplayMessage(
                    "  ERROR: Could not retrieve list of residues for object/selection '"
                    + self.defaultOption.get() + "'", 2)
                return

            if self.listResidues.count(self.ResiduValue.get()) == 0:
                self.DisplayMessage(
                    "  ERROR: The residue entered could not be found in the object/selection '"
                    + self.defaultOption.get() + "'", 2)
                self.EntryResidu.config(bg=self.top.Color_Red)
                return

            self.EntryResidu.config(bg=self.top.Color_White)

        self.DisplayMessage(
            "  Analyzing clefts for object/selection '" +
            self.defaultOption.get() + "'...", 0)

        #TmpFile = '/Users/francisgaudreault/1stp.pdb'
        Command_Line = '"' + self.top.GetCleftExecutable + '" -p "' + TmpFile + '"' + self.Get_Arguments(
        )

        # Clear temporary clefts
        self.Btn_Clear_Clicked()

        self.top.ProcessError = False
        self.top.ProcessRunning = True

        self.GetCleftRunning(True)

        # Run GetCleft
        StartRun = Start(self, self.queue, Command_Line)
Example #34
0
def remove_gap_columns(id2seq, refSeq=None, refStr=None, minGapRatio=1.0):
    """
    Remove alignment columns with too many gaps
    
    minGapRatio                 -- Remove columns with gap ratio greater than minGapRatio [0-1]
    
    Return {
        'id2seq': {id1:aligned_seq1, id2:aligned_seq2, ...},
        'refSeq': refSeq, # if refSeq != None
        'refStr': refStr  # if refStr != None
    }
    """
    keys = list(id2seq.keys())
    alignment_list = []
    for key in keys:
        alignment_list.append(id2seq[key].replace('~', '-').replace(
            ':', '-').replace('.', '-').upper().replace('U', 'T'))
    align_columns = collect_columns(alignment_list)
    Len = len(id2seq[keys[0]])
    if refStr is None:
        refBpmap = {}
    else:
        refBpmap = Structure.dot2bpmap(refStr)

    columns_to_remove = []
    for i in range(Len):
        if i in columns_to_remove:
            continue
        #print( i, align_columns[i].count('-'), len(align_columns[i]) )
        if align_columns[i].count('-') / len(align_columns[i]) >= minGapRatio:
            columns_to_remove.append(i)
            if i + 1 in refBpmap:
                columns_to_remove.append(refBpmap[i + 1] - 1)

    columns_to_remove.sort()
    #print(columns_to_remove)
    clean_id2seq = {}
    for key in id2seq:
        clean_id2seq[key] = ""
        cur_seq = id2seq[key]
        for i in range(Len):
            if not General.bi_search(i, columns_to_remove):
                clean_id2seq[key] += cur_seq[i]

    return_value = {'id2seq': clean_id2seq}

    if refSeq:
        _refSeq = ""
        for i in range(Len):
            if not General.bi_search(i, columns_to_remove):
                _refSeq += refSeq[i]
        return_value['refSeq'] = _refSeq

    if refStr:
        _refStr = ""
        for i in range(Len):
            if not General.bi_search(i, columns_to_remove):
                _refStr += refStr[i]
        _Len = len(_refStr)
        #_refStr = Structure.ct2dot(Structure.dot2ct(_refStr), _Len)
        return_value['refStr'] = _refStr

    return return_value
Example #35
0
def call_covariation(query_seq,
                     query_dot,
                     model_name,
                     seqdbFn,
                     workdir=None,
                     nohmm=False,
                     cmsearchE=1,
                     cpu=20,
                     use_LSF=True,
                     LSF_parameters={},
                     progress=True,
                     clean=False):
    """
    Call covariation has two steps: 
        1. Search homology sequence from sequence databse;
        2. Call covaring base pairs
    
    query_seq               -- Query sequence
    query_dot               -- Query dot-bracket structure
    model_name              -- CM model name and R-scape output file name
    seqdbFn                 -- File name of sequence database
    workdir                 -- Working directory, if not provide a random directory will generate.
    nohmm                   -- cmsearch parameter
    cmsearchE               -- cmsearch E-value
    cpu                     -- Threads for cmcalibrate and cmsearch
    use_LSF                 -- Submit to LSF if True
    LSF_parameters          -- { 'queue': 'Z-ZQF', 'cpu': 20, 'job_name': 'cmsearch', 'logFn': '/dev/null', 'errFn': '/dev/null' }
    progress                -- Print the progress
    clean                   -- Clean the directory after running
    
    Return a list of covaring base pairs: 
        [ (left, right), ... ]
    """
    import os, General, Colors, shutil

    if workdir is None:
        randID = random.randint(1000000, 9000000)
        dirname = f"call_covariation_{randID}"
        workdir = os.path.join(os.environ['HOME'], dirname)
    if not os.path.exists(workdir):
        os.mkdir(workdir)

    new_seqdbFn = os.path.join(workdir, "seqDB.fa")
    sto_file = os.path.join(workdir, "input.sto")
    cm_file = os.path.join(workdir, "model.cm")
    output_sto = os.path.join(workdir, "output.sto")
    output_txt = os.path.join(workdir, "output.txt")
    R_scape_dir = os.path.join(workdir, "R-scape")

    ### Step 0. Prepare combined Fasta file
    if progress:
        print(Colors.f(f"The work directory is: {workdir}", fc='green'))
        print(Colors.f("Step 0. Prepare combined Fasta file", fc='green'))
    General.write_fasta({'input': query_seq}, new_seqdbFn)
    os.system(f'cat {seqdbFn} >> {new_seqdbFn}')

    ### Step 1. Build stockholm file
    if progress:
        print(Colors.f("Step 1. Build stockholm file", fc='green'))
    dot2sto({'input': [query_seq, query_dot]}, model_name, sto_file, mode='w')

    ### Step 2. Build cm file from sto file
    if progress:
        print(Colors.f("Step 2. Build cm file from sto file", fc='green'))
    cmbuild(sto_file, cm_file, verbose=False, showCMD=progress)

    ### Step 3. Calibrate file
    if progress:
        print(Colors.f("Step 3. Calibrate file", fc='green'))
    job = cmcalibrate(cm_file,
                      cpu=cpu,
                      verbose=False,
                      showCMD=progress,
                      use_LSF=use_LSF,
                      LSF_parameters={})
    job.wait()

    ### Step 4. Search with CM
    if progress:
        print(Colors.f("Step 4. Search with CM", fc='green'))
    job = cmsearch(cm_file,
                   new_seqdbFn,
                   output_txt,
                   output_sto,
                   cpu=cpu,
                   toponly=True,
                   nohmm=nohmm,
                   nohmmonly=True,
                   outputE=20,
                   acceptE=cmsearchE,
                   cut_ga=False,
                   rfam=False,
                   glocal=False,
                   verbose=False,
                   showCMD=progress,
                   use_LSF=use_LSF,
                   LSF_parameters={})
    job.wait()

    #### Step 5. R-scape Runing
    if progress:
        print(Colors.f("Step 5. R-scape Runing", fc='green'))
    R_scape(output_sto,
            R_scape_dir,
            outname=model_name,
            maxIdentity=0.985,
            minIndentity=0.500,
            F=0.5,
            gapthresh=0.5,
            two_set_test=True,
            fold=False,
            acceptE=0.05,
            nseqmin=5,
            verbose=False,
            showCMD=progress)

    #### Step 6. Read R-scape result
    rscape_file = os.path.join(R_scape_dir, f'{model_name}.cov')
    covary_bps = []
    if os.path.exists(rscape_file):
        rscape_list = read_RScape_result(rscape_file)
        id2seq, refStr, refAnnot = General.load_stockholm(output_sto)[0]
        input_id = [key for key in id2seq if key.startswith('input')][0]
        posDict = get_alignedPos2cleanPos_dict(id2seq[input_id])
        for bp in rscape_list:
            covary_bps.append((posDict[bp[0]], posDict[bp[1]]))
            l, r = covary_bps[-1]
            bp_base = query_seq[l - 1] + query_seq[r - 1]
            if bp_base.replace('U', 'T') not in ('AT', 'TA', 'GC', 'CG', 'GT',
                                                 'TG'):
                print(f"Warning: {bp_base} is not a RNA base pair")

    if clean:
        shutil.rmtree(workdir)

    return covary_bps
Example #36
0
def calc_covBP_from_sto(stoFn,
                        querySeq=None,
                        query_id_pattern=None,
                        full_seq=None,
                        allpair=False,
                        min_seqs=8,
                        min_score=0.4):
    """
    Calculate the RNAalignfold_stack score for all base pairs in stockholm
    Please note that the refseq in stockholm file should be consistent with querySeq you provide
    
    stoFn                   -- Stockholm file
    querySeq                -- Query sequence, the reference sequence (#=GC RF) will aligned with querySeq
                               If no querySeq provided, the ID match query_id_pattern will used as query sequence
    query_id_pattern        -- The query ID mattern, if querySeq is provided, will be ignored
    full_seq                -- If full_seq is provide and querySeq=None, the sequence index by query_id_pattern will 
                               be search in full_seq, the left and right index will be re-indexed
    allpair                 -- Calculate all base pairs, not only pairs in structure
    min_seqs                -- Minimum sequences in the alignments
    min_score               -- Minimum score to record
    
    Return [ [left, right, score],.... ]
    """
    import Structure, re
    id2seq_dict, refStr, refSeq = General.load_stockholm(stoFn)[0]
    if len(id2seq_dict) < min_seqs:
        return []
    columns = collect_columns([value for key, value in id2seq_dict.items()])
    alignLen = len(refSeq)
    if allpair:
        ct = []
        for i in range(alignLen):
            for j in range(i + 1, alignLen):
                ct.append([i, j])
    else:
        ct = Structure.dot2ct(refStr)
    covary_bps = []
    for b1, b2 in ct:
        rnaalignscore2 = calc_RNAalignfold_stack(columns, b1, b2)
        if rnaalignscore2 > min_score:
            covary_bps.append([b1, b2, round(rnaalignscore2, 3)])
    refSeq = refSeq.replace('~', '-').replace(':', '-').replace(
        '.', '-').upper().replace('U', 'T')
    if querySeq:
        refSeq_realigned, query_aligned = Structure.multi_alignment(
            [refSeq.replace('-', ''),
             querySeq.upper().replace('U', 'T')])
        ### 构建一个字典:从refSeq的坐标指向querySeq的坐标
        refseq2query = {}
        pos_ref, pos_refrealign = 0, 0
        query_pos = 0
        while pos_ref < len(refSeq):
            while pos_ref < len(refSeq) and refSeq[pos_ref] == '-':
                pos_ref += 1
            if pos_ref == len(refSeq): break
            while refSeq_realigned[pos_refrealign] == '-':
                if query_aligned[pos_refrealign] != '-':
                    query_pos += 1
                pos_refrealign += 1
            #if pos_refrealign==len(query_aligned): break
            refseq2query[pos_ref + 1] = query_pos + 1
            pos_ref += 1
            pos_refrealign += 1
            query_pos += 1
        i = 0
        while i < len(covary_bps):
            if covary_bps[i][0] in refseq2query and covary_bps[i][
                    1] in refseq2query:
                covary_bps[i][0] = refseq2query[covary_bps[i][0]]
                covary_bps[i][1] = refseq2query[covary_bps[i][1]]
                i += 1
            else:
                del covary_bps[i]
    elif query_id_pattern:
        tid_list = [
            name for name in id2seq_dict.keys()
            if re.match(query_id_pattern, name)
        ]
        assert len(tid_list) == 1, f"Error: {len(tid_list)} tid match pattern"
        query_tid = tid_list[0]
        clean_query_seq = id2seq_dict[query_tid].replace('~', '').replace(
            ':', '').replace('.', '').replace('-',
                                              '').upper().replace('U', 'T')
        if full_seq:
            full_seq = full_seq.replace('U', 'T')
            start_pos = full_seq.find(clean_query_seq)
            if start_pos == -1:
                raise RuntimeError('queryseq not in full_seq')
            if full_seq.find(clean_query_seq, start_pos + 1) != -1:
                raise RuntimeError('queryseq are find in multiple position')
        else:
            start_pos = 0
        aligned_query_seq = id2seq_dict[query_tid].upper().replace('U', 'T')
        alignedPos2cleanPos = get_alignedPos2cleanPos_dict(
            id2seq_dict[query_tid])
        i = 0
        while i < len(covary_bps):
            l, r = covary_bps[i][:2]
            if alignedPos2cleanPos[l] and alignedPos2cleanPos[r]:
                nl = alignedPos2cleanPos[l] + start_pos
                nr = alignedPos2cleanPos[r] + start_pos
                covary_bps[i][0] = nl
                covary_bps[i][1] = nr
                if full_seq:
                    assert aligned_query_seq[l - 1] == full_seq[
                        nl - 1], "Exception: seq not same with full_seq"
                    assert aligned_query_seq[r - 1] == full_seq[
                        nr - 1], "Exception: seq not same with full_seq"
                i += 1
            else:
                del covary_bps[i]
    else:
        raise RuntimeError('querySeq or query_id_pattern must be specified')
    return covary_bps
     bp[i + 1] = {}
     for t in tags_p:
         w2_t = t
         V[i + 1][t] = {}
         bp[i + 1][t] = {}
         l = {}
         for r in tags_curr:
             l[r] = {}
         for tT in tags_p2:
             w1_t = tT
             score = (V[i][tT][t]) + getScore(w1,w2,word,w4,w5,w1_t,w2_t)
             for r in tags_curr:
                 l[r][tT] = score[features_m[r]]
         for r in tags_curr:
             V[i + 1][t][r] = max(list(l[r].values()))
             bp[i + 1][t][r] = General.argmax(l[r])
     tags_p2 = tags_p
     tags_p = tags_curr
 V.pop(0)
 bp.pop(0)
 endMatrix = map(lambda x: x.values(), V[row_l-1].values())
 maxEnd = list(map(max, endMatrix))
 maxV = max(maxEnd)
 maxTIndex = maxEnd.index(maxV)
 maxT = list(V[row_l-1].keys())[maxTIndex]
 maxR = General.argmax(V[row_l-1][maxT])
 y = [0 for i in range(0, row_l)]
 y[row_l-1] = maxR
 y[row_l-2] = maxT
 for i in reversed(range(0, row_l-2)):
     y[i] = bp[i + 2][y[i + 1]][y[i + 2]]            
Example #38
0
def cmsearch(CMFile,
             seqdbFn,
             outTXT,
             outSto,
             cpu=0,
             toponly=False,
             nohmm=False,
             nohmmonly=False,
             outputE=20,
             acceptE=1,
             cut_ga=False,
             rfam=False,
             glocal=False,
             verbose=True,
             showCMD=True,
             use_LSF=False,
             LSF_parameters={}):
    """
    Search CM model from sequence database
    CMFile              -- CM file
    seqdbFn             -- File name of sequence database
    outTXT              -- Output txt file
    outSto              -- Output Stockholm file
    cpu                 -- How many threads to use
    toponly             -- Only search the top(forward) strand
    nohmm               -- skip all HMM filter stages, use only CM (slow)
    nohmmonly           -- never run HMM-only mode, not even for models with 0 basepairs
    outputE             -- report sequences <= this E-value threshold in output  [10.0]  (x>0)
    acceptE             -- consider sequences <= this E-value threshold as significant  [0.01]
    cut_ga              -- use CM's GA gathering cutoffs as reporting thresholds
    rfam                -- set heuristic filters at Rfam-level (fast)
    glocal              -- configure CM for glocal alignment [default: local]
    verbose             -- Show command and log information
    showCMD             -- Print the command
    use_LSF             -- Submit to LSF if True
    LSF_parameters      -- { 'queue': 'Z-ZQF', 'cpu': 20, 'job_name': 'cmsearch', 'logFn': '/dev/null', 'errFn': '/dev/null' }
    
    Require: cmsearch
    """
    import General
    import shutil

    cmsearch_exe = General.require_exec("cmsearch", exception=True)
    cmd = f"{cmsearch_exe} --notextw "
    if cpu > 0:
        cmd += f"--cpu {cpu} "
    if toponly:
        cmd += "--toponly "
    if nohmm:
        cmd += "--nohmm "
    if nohmmonly:
        cmd += "--nohmmonly "
    if cut_ga:
        cmd += "--cut_ga "
    if rfam:
        cmd += "--rfam "
    if glocal:
        cmd += "-g "
    cmd += f"-E {outputE} --incE {acceptE} -o {outTXT} -A {outSto} {CMFile} {seqdbFn}"

    if not verbose:
        cmd += " > /dev/null"
    if showCMD:
        import Colors
        print(Colors.f(cmd, fc='yellow'))

    if use_LSF:
        import Cluster
        job = Cluster.new_job(command=cmd,
                              queue=LSF_parameters.get('queue', 'Z-ZQF'),
                              cpu=LSF_parameters.get('cpu', 20),
                              job_name=LSF_parameters.get(
                                  'job_name', 'cmsearch'),
                              logFn=LSF_parameters.get('logFn', '/dev/null'),
                              errFn=LSF_parameters.get('errFn', '/dev/null'))
        #print(job.get_submit_command())
        job.submit()
        return job
    else:
        os.system(cmd)
Example #39
0
    def __init__(self, root, top, menuindex, Project_Dir, Install_Dir,
                 NRGsuite_Dir, OSid, PyMOL, Name, WINDOWWIDTH, WINDOWHEIGHT,
                 RootPrefs):

        self.Name = Name
        self.OSid = OSid
        self.PyMOL = PyMOL

        self.NRGsuite_Dir = NRGsuite_Dir
        self.Install_Dir = Install_Dir
        self.Project_Dir = Project_Dir

        self.Folders = [self.NRGsuite_Dir]
        if self.Project_Dir:
            self.Folders.append(self.Project_Dir)

        self.Set_Folders()
        self.Validate_Folders()

        self.Prefs = RootPrefs

        self.Color_Green = '#CCFFCC'
        self.Color_Grey = '#EDEDED'
        self.Color_Gray = '#EDEDED'
        self.Color_Blue = '#6699FF'
        self.Color_Red = '#FF9999'
        self.Color_White = '#FFFFFF'
        self.Color_Black = '#000000'

        self.root = root
        self.root.title(self.Name)

        self.top = top

        self.menuindex = menuindex
        if self.menuindex != -1:
            self.top.menuBar.component('NRGsuite-menu').entryconfig(
                self.menuindex, state='disabled')

        self.WINDOWWIDTH = WINDOWWIDTH
        self.WINDOWHEIGHT = WINDOWHEIGHT

        #self.root.geometry()   # Interface DIMENSIONS
        #self.root.maxsize(WINDOWWIDTH,WINDOWHEIGHT)
        self.root.minsize(self.WINDOWWIDTH, self.WINDOWHEIGHT)
        self.root.protocol('WM_DELETE_WINDOW', self.Quit)

        General.CenterWindow(self.root, self.WINDOWWIDTH, self.WINDOWHEIGHT)

        #==================================================================================
        #                 SET the default fonts of the interface
        #==================================================================================

        FontType = self.Prefs.GetFontType()
        FontSize = self.Prefs.GetFontSize()

        self.font_Title = tkFont.Font(family=FontType,
                                      size=FontSize,
                                      weight=tkFont.BOLD)
        self.font_Title_H = tkFont.Font(family=FontType,
                                        size=FontSize + 1,
                                        weight=tkFont.BOLD)
        self.font_Text = tkFont.Font(family=FontType, size=FontSize)
        self.font_Text_H = tkFont.Font(family=FontType, size=FontSize + 1)
        self.font_Text_I = tkFont.Font(family=FontType,
                                       size=FontSize,
                                       slant=tkFont.ITALIC)
        self.font_Text_U = tkFont.Font(family=FontType,
                                       size=FontSize,
                                       underline=True)

        self.ChildWindow = None
        self.ActiveFrame = None
        self.Run = None

        self.ProcessError = False
        self.ProcessRunning = False
        self.ProcessParsing = False
        self.ProcessDone = False
        self.ParseState = 10
        self.SimulateState = -1

        self.fMain = Frame(self.root)
        self.fMain.pack(expand=True)

        self.Def_Vars()
        self.Init_Vars()

        self.Frame_Main()

        self.Build_Tabs()
        self.MakeMenuBar()

        self.After_Init()

        self.Clean()
Example #40
0
def Build_Rfam_seed(input_sto,
                    input_seq,
                    seqDBFastaFn,
                    workdir,
                    toponly=True,
                    iteration=3,
                    blastFilter=None,
                    use_LSF=True,
                    cores=5,
                    verbose=True):
    """
    This function build a rfam seed according to https://docs.rfam.org/en/latest/building-families.html
    Repeat this process "iteration" times:
    Seed -[cmbuild]-> CM -[cmcalibrate]-> CM -[cmsearch]-> Stockholm -[Filter]-> Fasta -[cmalign]-> Stockholm -[cmbuild]-> CM
    
    input_sto               -- Seed stockholm file, contains single sequence and structure, output file of Covariation.dot2sto
    input_seq               -- The sequence in input_sto
    seqDBFastaFn            -- The target sequences, contains all homologous sequences
    workdir                 -- Work directory, all immediate files will be contained
    toponly                 -- Only search the positive strand
    iteration               -- How many times to repeat the cmsearch processes
    blastFilter             -- If set, will use blast result to filter the cmsearch target. The cmsearch coordinate will
                               be compared with blast result. The purpose is to prevent the cmsearch give too much weight 
                               on structure and ignore the sequence
                               { 'full_seq': "ACTG...",  # The full length sequence contain input_seq
                                'blastdb':"/path/to/blastdb", 
                                'flanking': 20, # Max distance between the cmsearch result and blast result
                                'blast_identity': 0.4, # Blast cutoff
                                'min_match': 5, # Minimun target
                                'ignore_seq': ['input'] # Which results to ignore
                                }
    use_LSF                 -- bsub or run in local
    cores                   -- How many cores to use
    verbose                 -- If print the information in the screen
    
    Return:
        Return a stockholm file with final alignment
    """
    import Alignment, Colors
    if blastFilter:
        input_start = blastFilter['full_seq'].find(input_seq)
        assert input_start != -1, f"input_seq not in full_seq, consider maintain consistent letter case and U/T"
        input_end = input_start + len(input_seq)
        start = max(input_start - blastFilter.get('flanking', 20), 0)
        end = min(input_end + blastFilter.get('flanking', 20),
                  len(blastFilter['full_seq']))
        hits = Alignment.blast_sequence_V2(
            blastFilter['full_seq'][start:end],
            blastFilter['blastdb'],
            verbose=verbose,
            task='blastn',
            maxhit=5000,
            perc_identity=blastFilter['blast_identity'])
        target_valid_range = {}
        for hit in hits:
            if hit.hit_acc not in target_valid_range:
                target_valid_range[hit.hit_acc] = (hit.hit_from, hit.hit_to)
        if len(target_valid_range) < blastFilter.get('min_match', 5):
            if verbose:
                print(Colors.f("Failed: Too little blast target", 'red'))
            return False
        else:
            if verbose:
                print(f"Blast: {len(target_valid_range)} results found")

    for it_num in range(iteration):
        if verbose: print(f"===========>>> Iteration {it_num}  <<<===========")

        ### Define files
        if it_num == 0:
            input_sto = input_sto
        else:
            input_sto = os.path.join(workdir, f'cmalign_output_{it_num-1}.sto')
        input_cm = os.path.join(workdir, f'cmsearch_input_{it_num}.cm')
        cmsearch_output_txt = os.path.join(workdir,
                                           f'cmsearch_output_{it_num}.txt')
        cmsearch_output_sto = os.path.join(workdir,
                                           f'cmsearch_output_{it_num}.sto')
        cmalign_input_fa = os.path.join(workdir, f'cmalign_input_{it_num}.fa')
        cmalign_output_sto = os.path.join(workdir,
                                          f'cmalign_output_{it_num}.sto')

        ### Build CM model
        cmbuild(input_sto, input_cm)
        h = cmcalibrate(input_cm,
                        use_LSF=use_LSF,
                        LSF_parameters={'cpu': cores})
        if use_LSF: h.wait()

        ### cmsearch
        tmp_fa, tmp_annot = General.load_fasta(seqDBFastaFn,
                                               load_annotation=True)
        tmp_fa['input'] = input_seq
        General.write_fasta(tmp_fa, os.path.join(workdir, 'tmp_ref.fa'),
                            tmp_annot)
        del tmp_fa
        h = cmsearch(input_cm, os.path.join(workdir, 'tmp_ref.fa'), \
            cmsearch_output_txt, cmsearch_output_sto, cpu=cores, toponly=toponly, nohmm=False, nohmmonly=False, \
            outputE=20, acceptE=1, cut_ga=False, rfam=False, glocal=False, verbose=True, showCMD=True, use_LSF=use_LSF,
            LSF_parameters={'cpu': cores})
        if use_LSF: h.wait()
        os.remove(os.path.join(workdir, 'tmp_ref.fa'))

        ### Read cmsearch result
        id2seq_dict, refStr, refSeq = General.load_stockholm(
            cmsearch_output_sto)[0]
        refID = [key for key in id2seq_dict if key.startswith('input')][0]
        refSeq = id2seq_dict[refID]

        ### Filter results not in blast hits
        if blastFilter:
            for key in list(id2seq_dict.keys()):
                if '/' in key:
                    true_id, start_end = key.rsplit('/', 1)
                    start, end = start_end.split('-')
                    start = int(start)
                    end = int(end)
                    if true_id in target_valid_range:
                        valid_start, valid_end = target_valid_range[true_id]
                        if (end < valid_start or start > valid_end):
                            del id2seq_dict[key]
                    elif true_id in blastFilter.get('ignore_seq', ['input']):
                        if verbose: print("Ignored:", true_id)
                    else:
                        if verbose:
                            print("Killed:", Colors.f(true_id, fc='red'))
                        del id2seq_dict[key]
                else:
                    if verbose: print("Killed:", Colors.f(true_id, fc='green'))
                    del id2seq_dict[key]

        ### Remove highly similar sequences
        uniq_seq = collapse_sequences(id2seq_dict,
                                      refSeq,
                                      max_identity=0.98,
                                      min_match_identity=0.2,
                                      max_indel_ratio=0.5)
        ### Remove sequences with too many base pairs broken
        clean_id2seq = remove_bpbreak_sequences(uniq_seq,
                                                refStr,
                                                maxBpBreakRatio=0.2,
                                                maxBpDeletRatio=0.5,
                                                maxBpBreakCount=99,
                                                maxBpDeletion=99)
        ### Remove sequences with nucleotides other than AUCG
        for key in list(clean_id2seq.keys()):
            v = clean_id2seq[key].replace('~', '-').replace(':', '-').replace(
                '.', '-').upper().replace('U', 'T')
            if len(v) != v.count('A') + v.count('T') + v.count('C') + v.count(
                    'G') + v.count('-'):
                del clean_id2seq[key]
        pure_fasta = {
            k: v.replace('~', '').replace(':', '').replace('.', '').replace(
                '-', '').upper().replace('U', 'T')
            for k, v in clean_id2seq.items()
        }
        ### Remove highly similar sequences
        collapsed_pure_fasta = Alignment.cd_hit_est(pure_fasta,
                                                    identity=0.98,
                                                    verbose=True)
        clean_id2seq = {k: clean_id2seq[k] for k in collapsed_pure_fasta}
        if len(clean_id2seq) == 0:
            #### No homologous sequence to call covariation in the sequence db
            if verbose:
                print(Colors.f("Failed: No homologous sequences left", 'red'))
            return False
        ### Remove alignment columns with too many gaps
        returnvalues = remove_gap_columns(clean_id2seq,
                                          refSeq=refSeq,
                                          refStr=refStr,
                                          minGapRatio=1.0)

        ### cmalign
        GS_DE = read_sto_DE(cmsearch_output_sto)  # read description
        small_seqdb = {
            k: v.replace('-', '')
            for k, v in returnvalues['id2seq'].items()
        }
        small_seqdb['input'] = input_seq
        General.write_fasta(small_seqdb,
                            os.path.join(workdir, 'cmalign_input.fa'), GS_DE)
        h = cmalign(input_cm,
                    os.path.join(workdir, 'cmalign_input.fa'),
                    cmalign_output_sto,
                    cpu=cores,
                    glocal=False,
                    outformat='Stockholm',
                    mxsize=1028.0,
                    verbose=True,
                    showCMD=True,
                    use_LSF=use_LSF,
                    LSF_parameters={'cpu': cores})
        if use_LSF: h.wait()

    return cmalign_output_sto
import General, Structure, Visual, SARS2, Covariation

wuhan_id = 'NC_045512.2'

#shape = General.load_shape('/Share2/home/zhangqf7/lipan/SARS2/icSHAPE/2020-06-01-process/virus.shape')[wuhan_id]
shape = General.load_shape(
    '/Share/home/zhangqf8/sunlei/data/SARS2/20200529-SARS2-huh7/SARS2-huh7/Processing/8.calcSHAPE/virus-w50.shape'
)[wuhan_id]

seq = General.load_fasta(
    '/Share2/home/zhangqf7/lipan/SARS2/sequence/SARS2.fa')[wuhan_id]
dot = General.load_dot(
    '/Share2/home/zhangqf7/lipan/SARS2/2nd_structures/Rfam/structures.dot')

utr_5 = [1, 299]
utr_3 = [29536, 29870]
seq5 = seq[utr_5[0] - 1:utr_5[1]]
shape5 = shape[utr_5[0] - 1:utr_5[1]]
seq3 = seq[utr_3[0] - 1:utr_3[1]]
shape3 = shape[utr_3[0] - 1:utr_3[1]]

dot['RF03120_UTR5'][0] == seq5
dot['RF03125_UTR3'][0] == seq3

########  预测二级结构

prob3, pfs3 = Structure.partition(seq3,
                                  shape_list=shape3,
                                  si=-0.4,
                                  sm=1.5,
                                  md=300,
Example #42
0
def main():
    start_time = time.time()
    f = open('Kakilog.log', 'a+', encoding='utf-8')
    now = datetime.now()
    print("Log started: ", now, file=f)
    # read config file
    config = configparser.ConfigParser()
    try:
        config.read('config.ini', encoding='utf-8')
    except:
        config.read('config.ini', encoding='utf-8-sig')

    title = 'KakiRaid'
    if int(config['DEFAULT']['AdjustWindow']) == 1:
        General.adjust_window(title, [0, 0])
    elif int(config['DEFAULT']['AdjustWindow']) == 2:
        General.adjust_window(title, [245, 123])
    window = General.get_window_coordinate(title)  # x, y, w, h
    window.append(start_time)  # index 4

    auto_legend_count = int(config['DEFAULT']['AutoLegendCount'])
    if int(config['DEFAULT']['ModeSelection']) == 0:
        count = 0
        max_count = int(config['DEFAULT']['MainLoopCount'])
        current_floor = 0
        stat = dict()
        stat['Total_Resources'] = 0
        stat['Total_Monster'] = 0
        stat['Total_Loot_Curse'] = 0
        stat['Total_Loot_Other'] = 0
        stat['Total_Camp'] = 0
        stat['Total_Ruin'] = 0
        window.append(stat)  # index 5
        window.append(config)  # index 6
        window.append(f)
        keys = [
            window[6]['Baidu_API']['API_ID'],
            window[6]['Baidu_API']['API_KEY'],
            window[6]['Baidu_API']['SECRET_KEY']
        ]
        while count < max_count:
            print('\n', file=f)
            f.flush()
            temp_start_time = time.time()
            if count != 0:
                if current_floor > 500:
                    time.sleep(130)
                elif current_floor > 400:
                    time.sleep(120)
                elif current_floor > 300:
                    time.sleep(95)
                elif current_floor > 200:
                    time.sleep(85)
                else:
                    time.sleep(75)
            General.failure_detect(window)
            curse_page_result = General.curse_page_detect(window)
            if not curse_page_result:
                stuck_result = General.stuck_detect(window)
                curse_page_result = General.curse_page_detect(
                    window, 400)  # 400 as count to bypass repeated detect
                while stuck_result != 0 and not curse_page_result:
                    if stuck_result == 1:
                        # To add support for network turbulence and portal stuck?
                        print('Progress stuck at unknown, quitting program..',
                              file=f)
                        f.close()
                        General.send_email('Kaki script stuck, quit program.')
                        sys.exit()
                    elif stuck_result == 2:
                        print(
                            'Progress stuck at relic augmentation, try resolving..',
                            file=f)
                        pyautogui.click(x=window[0] + (320 - 245),
                                        y=window[1] + (213 - 123),
                                        duration=0.1)
                        stuck_result = General.stuck_detect(window)
                        if stuck_result == 1:
                            General.toggle_auto_path_finding(window)
                            stuck_result = General.stuck_detect(window)
                        else:
                            stuck_result = 0
            else:
                curse_images = General.get_curse_image(window)
                curses = General.parse_curse_image(curse_images, keys)
                General.select_curse(window, curses)
            General.resource_completion_detect(window)
            General.map_management(window)
            elapsed_time = time.time() - temp_start_time
            count += 1
            print("Curse " + str(count) + " selected!", file=f)
            print('time elapsed: ' + str(elapsed_time) + ' seconds.', file=f)
            time.sleep(10)
            if current_floor == 0:
                current_floor = General.floor_detection(window)
            else:
                current_floor += 5
                print('Current Floor: ' + str(current_floor) + '\n', file=f)
        f.close()
        General.send_email('Kaki script quit with time out')
        print('Quit with time out')
    elif int(config['DEFAULT']['ModeSelection']) == 1:
        General.auto_legend(window, auto_legend_count)
    elif int(config['DEFAULT']['ModeSelection']) == 2:
        grind_count = int(config['Void_Island']['Count'])
        if grind_count == 0:
            grind_count = 9999
        while grind_count > 0:
            General.void_island_grind(window)
            print('Void Island Completed', file=f)
            f.flush()
            grind_count -= 1