def __get_neighbours_in_P(self, pos, input_data): """ Returns all neighbours to which you can go from a matrix cell. Args: pos: the position from which you want get all neighbours input_data: the input for which you create the traceback Returns: neighbours of the cell at position pos """ neighbours = [] start = AlignmentOutputData.table_vertical_gaps[pos.y][pos.x] if pos.y >= 0: matrix_p = AlignmentOutputData.table_vertical_gaps[pos.y - 1][pos.x] matrix_d = AlignmentOutputData.table_values[pos.y - 1][pos.x] up_in_p = start == matrix_p + input_data.gap_beta up_in_d = start == matrix_d + input_data.gap_opening if up_in_p: neighbours.append(Vector(pos.x, pos.y - 1).create(self.MATRIX_LBL_VERTICAL_GAPS)) if up_in_d: neighbours.append(Vector(pos.x, pos.y - 1).create(self.MATRIX_LBL_MAIN)) return neighbours
def __get_neighbours_in_Q(self, pos, input_data): """ Returns all neighbours to which you can go from a matrix cell. Args: pos: the position from which you want get all neighbours input_data: the input for which you create the traceback Returns: neighbours of the cell at position pos """ neighbours = [] start = AlignmentOutputData.table_horizontal_gaps[pos.y][pos.x] if pos.y >= 0: matrix_q = AlignmentOutputData.table_horizontal_gaps[pos.y][pos.x - 1] matrix_d = AlignmentOutputData.table_values[pos.y][pos.x - 1] left_in_q = start == matrix_q + input_data.gap_beta left_in_d = start == matrix_d + input_data.gap_opening if left_in_q: neighbours.append(Vector(pos.x - 1, pos.y).create(self.MATRIX_LBL_HORIZONTAL_GAPS)) if left_in_d: neighbours.append(Vector(pos.x - 1, pos.y).create(self.MATRIX_LBL_MAIN)) return neighbours
def _get_neighbours(self, pos, input_data): """ Returns all neighbours to which you can go from a matrix cell. Args: pos: the position from which you want get all neighbours input_data: the input for which you create the traceback Returns: neighbours of the cell at position pos """ neighbours = [] start = AlignmentOutputData.table_values[pos.y][pos.x] diagonal = float(strings.NAN) up = float(strings.NAN) left = float(strings.NAN) cur_char_seq_1 = strings.EMPTY cur_char_seq_2 = strings.EMPTY if pos.y - 1 >= 0 and pos.x - 1 >= 0: diagonal = AlignmentOutputData.table_values[pos.y - 1][pos.x - 1] if pos.y - 1 >= 0: up = AlignmentOutputData.table_values[pos.y - 1][pos.x] if pos.x - 1 >= 0: left = AlignmentOutputData.table_values[pos.y][pos.x - 1] if pos.y - 1 >= 0: cur_char_seq_1 = input_data.sequence_a[pos.y - 1] if pos.x - 1 >= 0: cur_char_seq_2 = input_data.sequence_b[pos.x - 1] matching = start == diagonal + input_data.cost_function.get_value( cur_char_seq_1, cur_char_seq_2) deletion = start == up + input_data.gap_cost insertion = start == left + input_data.gap_cost if matching: neighbours.append(Vector(pos.x - 1, pos.y - 1)) if insertion: neighbours.append(Vector(pos.x - 1, pos.y)) if deletion: neighbours.append(Vector(pos.x, pos.y - 1)) return neighbours
def traceback_one(self, path, input_data): """ Returns one traceback for a given input. It is based on the pseudo-code of Prof. Dr. Backofen http://www.bioinf.uni-freiburg.de//Lehre/Courses/2013_SS/V_RNA/slides/nussinov.pdf (slide 13) The code is just reversing the Nussinov recursion function. Args: path: list in which you store the traceback input_data: the input for which you create the traceback """ current = path[-1] if current.x <= current.y+1: # case 1: j<=i -> checking if already at inner diagonal or further return elif Output.table_values[current.y][current.x] == Output.table_values[current.y][current.x-1]: # case 2 path.append(Vector(current.x - 1, current.y)) # going one step to the left self.traceback_one(path, input_data) return else: # case 3: checks for complementary bases on the left current_base = input_data.sequence[current.x-1] # -1 because width of table is: len(sequence) + 1 # for all k: i<=k<j for k in range(current.y, current.x-1-input_data.loop_length): base = input_data.sequence[k] # S_k and S_j complementary if pairs.complementary(base, current_base): current_value = Output.table_values[current.y][current.x] value_sum = Output.table_values[current.y][k] + Output.table_values[k+1][current.x-1] + 1 # if N_{i,j} = N_{i,k-1} + N_{k+1,j-1} + 1 if current_value == value_sum: self.base_pairs.append((k+1, current.x)) # create vectors for new positions in the matrix from which to start left = Vector(k, current.y) bottom = Vector(current.x-1, k+1) left_path = copy.copy(path) bottom_path = copy.copy(path) left_path.append(left) bottom_path.append(bottom) # jump to new positions self.traceback_one(left_path, input_data) self.traceback_one(bottom_path, input_data) return
def get_data(self, cost_function, gap_cost, sequence_a, sequence_b): """ Returns relevant data needed for example for the Feng-Doolittle algorithm. Args: cost_function: initialized evaluation function used to evaluate alignment gap_cost: costs for a gap sequence_a: first sequence sequence_b: second sequence Returns: a tuple with the alignment score, the number of gaps and the length of the alignment """ self.__set_parameters(cost_function, gap_cost, sequence_a, sequence_b) self.__initialize_global() self._compute_alignments() backtracking = Backtracking() paths = self._traceback( Vector(self._tbl_len_x - 1, self._tbl_len_y - 1), False, backtracking) gaps = self._count_gaps(paths[0]) return (AlignmentOutputData.table_values[self._tbl_len_y - 1][self._tbl_len_x - 1], gaps, len(paths[0]) - 1)
def get_right_axis(self): self.update_axis() if self.right_axis is not None: right = Vector(self.right_axis[0], self.right_axis[1]) return right else: raise pygame.error("right axis is 'None'")
def get_left_axis(self): self.update_axis() if self.left_axis is not None: left = Vector(self.left_axis[0], self.left_axis[1]) return left else: raise pygame.error("left axis is 'None'")
def __init__(self, sprite_name, pos=None, sprite_group=None): BaseSprite.__init__(self, sprite_name, sprite_group) OBJECT_MANAGER.instance.add(self) if pos is not None: self.rect.centerx = pos[0] self.rect.centery = pos[1] self.pos = Vector(pos[0], pos[1])
def run(self, seq1_fasta_fn, seq2_fasta_fn, subst_matrix_fn, cost_gap_open, complete_traceback): """ Calculate optimal alignment(s) with Needleman-Wunsch algorithm and returns outputs of this algorithm for testing purposes. Args: seq1_fasta_fn: path to fasta file containing first sequence seq2_fasta_fn: path to fasta file containing second sequence subst_matrix_fn: path to substitution matrix cost_gap_open: cost to open a gap complete_traceback: If True, return all optimal alignments. Otherwise choose a random alignment. Returns: tuple of (id_seq1: fasta id of first sequence, seq1: first sequence, id_seq2: fasta id of second sequence, seq2: second sequence, score: score of optimal alignment, [(aln_string_seq1, aln_string_seq2), ...]: list of tuples containing optimal alignments) """ self.__evaluate_parameters(seq1_fasta_fn, seq2_fasta_fn, subst_matrix_fn, cost_gap_open, complete_traceback) self.__initialize_global() self._compute_alignments() backtracking = Backtracking() if self._traceback_mode == self.TRACEBACK_ALL: AlignmentOutputData.paths = self._traceback( Vector(self._tbl_len_x - 1, self._tbl_len_y - 1), True, backtracking) self._create_alignments() else: AlignmentOutputData.paths = self._traceback( Vector(self._tbl_len_x - 1, self._tbl_len_y - 1), False, backtracking) self._create_alignments() self._output() return (self._data.ids[0], self._data.sequence_a, self._data.ids[1], self._data.sequence_b, AlignmentOutputData.table_values[self._tbl_len_y - 1][self._tbl_len_x - 1], AlignmentOutputData.alignments)
def __init__(self, name, rows, cols, pos, sprite_group): BaseAnimatedSprite.__init__(self, name=name, rows=rows, cols=cols, colorkey=-1, sprite_group=sprite_group) OBJECT_MANAGER.instance.add(self) self.pos = Vector(pos[0], pos[1]) self.rect.center = pos
def __init__(self, angle, pos): BaseObject.__init__(self, "pink_bullet.png", pos, SPRITE_MANAGER.instance) rads = radians(angle) self.direction = Vector(sin(rads), cos(rads)).normal() self.delete = False BULLET_MANAGER.instance.add(self) self.speed = 30 self.collided = False self._rotate(angle)
def __init__(self, sprite_name, controller, pos): BaseAnimatedObject.__init__( self, name=sprite_name, rows=9, cols=1, pos=pos, sprite_group=SPRITE_MANAGER.instance) # old image is used for quicker "rotations" of images, also stops the image from # being distorted when multiple images are rotated over and over agian. self.old_image = self.image self.joystick = controller # in degrees, 0 is top, 90 is left, 180 is down, 270 is right self.facing_direction = 0 self.moving_direction = 0 self.bullet_count = 0 self._rotate(self.facing_direction) self.radius = self.rect.centerx - self.rect.x self.begun_movement = False self.play_thrust = False # current trail could also be kicked out to trail manager, I think... self.current_trail = Trail(color1=(0, 255, 230), color2=(255, 0, 230), color3=(0, 255, 0)) self.trails = Trail_Manager() self.velocity = Vector(0, 0) self.acceleration = Vector(0, 0) self.impulse = False self.land = False # this isn't the proper place for cycle logic. Should kick this out somehow. self.can_cycle = -1 # ship properties.. self.mass = MASS self.thrust = THRUST self.engine_thrust = Vector(0, 0) self.first_pass = True self.camera_center = CameraCenter("Bullet.png", self)
def calc_new_position(self, cur_pos): if self.impulse: self._resolve_direction_vector() else: self.engine_thrust.x, self.engine_thrust.y = 0, 0 # calculate acceleration.x drag_force = Vector( -DRAG_COEFFICIENT * self.velocity.x, -DRAG_COEFFICIENT * self.velocity.y) self.acceleration.x = (self.engine_thrust.x + drag_force.x)/self.mass self.acceleration.y = (self.engine_thrust.y + drag_force.y)/self.mass self.velocity.x = self.velocity.x + (self.acceleration.x * CLOCK.get_elasped()) self.velocity.y = self.velocity.y + (self.acceleration.y * CLOCK.get_elasped()) # might have to subtract this value.. dx = (0.5 * self.acceleration.x * CLOCK.get_elasped()**2) + (self.velocity.x * CLOCK.get_elasped()) dy = (0.5 * self.acceleration.y * CLOCK.get_elasped()**2) + (self.velocity.y * CLOCK.get_elasped()) return cur_pos.x + dx, cur_pos.y + dy
def run(self, seq_fasta_fn): """ Fold RNA with Nussinov algorithm. Args: seq_fasta_fn: path to fasta file containing sequence Returns: tuple of (id_seq: fasta id of sequence, seq: sequence, structure: dot-bracket string of optimal folding) """ self.__evaluate_parameters(seq_fasta_fn) self.__initialize() self._compute_structures() backtracking = NussinovBacktracking() PredictionOutputData.structures = self._traceback( Vector(self._tbl_len_x - 1, 0), backtracking) self._create_structures() self._output() return (self._data.id, self._data.sequence, PredictionOutputData.dot_bracket_structures[0])
def __init__(self, sprite_name, controller, pos): BaseObject.__init__(self, sprite_name, pos, SPRITE_MANAGER.instance) # assumes an already initialized controller # TODO: Create virtual controller - currently we rely on PS3 controller self.joystick = controller self.land = False self.can_shoot = 1 self.can_move = 1 self.trajectory = Vector(0, 0) self.begun_movement = False # Could eliminate this by having the player already moving at start... # TODO: get rid of this line of code, since we will be using a rect mask for collisions... self.radius = self.rect.centerx - self.rect.x self.old_hspeed, self.old_vspeed = 0, 0 self.hspeed, self.vspeed = 0, 0 self.fire_sound = load_sound("321102__nsstudios__laser1.wav") self.health = HEALTH self.bullet_group = GroupWithOwner(owner=self.name, group_manager=BULLET_GROUP_MANAGER)
def _get_new_pos(self, trajectory): new = self.pos - Vector(-trajectory[0], -trajectory[1]) self.pos += (new - self.pos).normal() * self.speed return self.pos
def __init__(self): self.updated_buttons = False self.left_axis = Vector(0, 0) self.right_axis = Vector(0, 0)
def getPlayerDirection(self): rads = radians(self.facing_direction) return Vector(sin(rads), cos(rads)).normal()
def getVelocity(self): return Vector(self.velocity.x, self.velocity.y)
def getPosition(self): return Vector(self.pos.x, self.pos.y)
def get_axes(self): self.update_axis() if self.left_axis is not None and self.right_axis is not None: left = Vector(self.left_axis[0], self.left_axis[1]) right = Vector(self.right_axis[0], self.right_axis[1]) return left, right
def _get_neighbours(self, pos, input_data): """ Returns all neighbours to which you can go from a matrix cell. Args: pos: the position from which you want get all neighbours input_data: the input for which you create the traceback Returns: neighbours of the cell at position pos """ neighbours = [] if pos.matrix_label == self.MATRIX_LBL_HORIZONTAL_GAPS: return self.__get_neighbours_in_Q(pos, input_data) elif pos.matrix_label == self.MATRIX_LBL_VERTICAL_GAPS: return self.__get_neighbours_in_P(pos, input_data) start = AlignmentOutputData.table_values[pos.y][pos.x] diagonal = float(strings.NAN) matrix_p = float(strings.NAN) matrix_q = float(strings.NAN) up = float(strings.NAN) left = float(strings.NAN) cur_char_seq_1 = strings.EMPTY cur_char_seq_2 = strings.EMPTY if pos.y - 1 >= 0 and pos.x - 1 >= 0: diagonal = AlignmentOutputData.table_values[pos.y - 1][pos.x - 1] if pos.y > 0: matrix_p = AlignmentOutputData.table_vertical_gaps[pos.y][pos.x] if pos.x > 0: matrix_q = AlignmentOutputData.table_horizontal_gaps[pos.y][pos.x] # marginal case if pos.y - 1 >= 0 and pos.x == 0: up = AlignmentOutputData.table_values[pos.y - 1][pos.x] # marginal case if pos.x - 1 >= 0 and pos.y == 0: left = AlignmentOutputData.table_values[pos.y][pos.x - 1] if pos.y - 1 >= 0: cur_char_seq_1 = input_data.sequence_a[pos.y - 1] if pos.x - 1 >= 0: cur_char_seq_2 = input_data.sequence_b[pos.x - 1] if cur_char_seq_1 != strings.EMPTY and cur_char_seq_2 != strings.EMPTY: matching = start == diagonal + input_data.cost_function.get_value(cur_char_seq_1, cur_char_seq_2) else: matching = False #matching = start == diagonal + (0 if cur_char_seq_1 == cur_char_seq_2 else -1) change_to_p = start == matrix_p change_to_q = start == matrix_q # marginal cases deletion = start == up + input_data.gap_beta insertion = start == left + input_data.gap_beta if matching: neighbours.append(Vector(pos.x - 1, pos.y - 1).create(self.MATRIX_LBL_MAIN)) if change_to_p: neighbours.append(Vector(pos.x, pos.y).create(self.MATRIX_LBL_VERTICAL_GAPS)) if change_to_q: neighbours.append(Vector(pos.x, pos.y).create(self.MATRIX_LBL_HORIZONTAL_GAPS)) # marginal case if insertion: neighbours.append(Vector(pos.x - 1, pos.y).create(self.MATRIX_LBL_MAIN)) # marginal case if deletion: neighbours.append(Vector(pos.x, pos.y - 1).create(self.MATRIX_LBL_MAIN)) # marginal case if not (matching or change_to_p or change_to_q or insertion or deletion) and (pos.x != 0 or pos.y != 0): neighbours.append(Vector(0, 0).create(self.MATRIX_LBL_MAIN)) return neighbours