Example #1
0
    def fusion(self):
        get_adjacencies = Extremities_and_adjacencies()
        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]
        chrm_num_1 = random.randint(0, len(self.linear_chromosomes) - 1)
        chrm_num_2 = random.randint(0, len(self.linear_chromosomes) - 1)
        while chrm_num_1 == chrm_num_2:
            chrm_num_2 = random.randint(0, len(self.linear_chromosomes) - 1)

        telomeres_chrm1 = [
            element for element in self.linear_chromosomes[chrm_num_1]
            if type(element) is not tuple
        ]
        telomeres_chrm2 = [
            element for element in self.linear_chromosomes[chrm_num_2]
            if type(element) is not tuple
        ]
        telo1_num = random.randint(0, len(telomeres_chrm1) - 1)
        telo2_num = random.randint(0, len(telomeres_chrm2) - 1)

        telo1 = telomeres_chrm1[telo1_num]
        telo2 = telomeres_chrm2[telo2_num]

        if telo1 < telo2:
            new_adj = (telo1, telo2)
        else:
            new_adj = (telo2, telo1)

        # perfrom operation
        self.state.remove(telo1)
        self.state.remove(telo2)
        self.state.append(new_adj)

        print('fusion:   ', get_adjacencies.adjacencies_to_genome(self.state))
Example #2
0
    def fission(self):
        get_adjacencies = Extremities_and_adjacencies()
        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]
        chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)
        adjacencies = [
            element for element in self.linear_chromosomes[chrm_num]
            if type(element) is tuple
        ]

        while len(adjacencies) == 0:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)
            adjacencies = [
                element for element in self.linear_chromosomes[chrm_num]
                if type(element) is tuple
            ]

        adj_num = random.randint(0, len(adjacencies) - 1)

        adj = adjacencies[adj_num]

        self.state.remove(adj)
        self.state.append(adj[0])
        self.state.append(adj[1])

        print('fission:   ', get_adjacencies.adjacencies_to_genome(self.state))
Example #3
0
    def balanced_translocation(self):
        get_adjacencies = Extremities_and_adjacencies()
        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]
        chrm_num_1 = random.randint(0, len(self.linear_chromosomes) - 1)

        while len(self.linear_chromosomes[chrm_num_1]) < 3:
            chrm_num_1 = random.randint(0, len(self.linear_chromosomes) - 1)

        chrm_num_2 = random.randint(0, len(self.linear_chromosomes) - 1)
        while chrm_num_1 == chrm_num_2 or len(
                self.linear_chromosomes[chrm_num_2]) < 3:
            chrm_num_2 = random.randint(0, len(self.linear_chromosomes) - 1)

        adjacencies_chrm1 = [
            element for element in self.linear_chromosomes[chrm_num_1]
            if type(element) is tuple
        ]
        adjacencies_chrm2 = [
            element for element in self.linear_chromosomes[chrm_num_2]
            if type(element) is tuple
        ]
        adj1_num = random.randint(0, len(adjacencies_chrm1) - 1)
        adj2_num = random.randint(0, len(adjacencies_chrm2) - 1)

        adj1 = adjacencies_chrm1[adj1_num]
        adj2 = adjacencies_chrm2[adj2_num]

        if adj1[0] < adj2[1]:
            new_adj1 = (adj1[0], adj2[1])
        else:
            new_adj1 = (adj2[1], adj1[0])

        if adj1[1] < adj2[0]:
            new_adj2 = (adj1[1], adj2[0])
        else:
            new_adj2 = (adj2[0], adj1[1])

        # perfrom operation
        self.state.remove(adj1)
        self.state.remove(adj2)
        self.state.append(new_adj1)
        self.state.append(new_adj2)

        print('balanced translocation:   ',
              get_adjacencies.adjacencies_to_genome(self.state))
Example #4
0
    def unbalanced_translocation(self):
        get_adjacencies = Extremities_and_adjacencies()
        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]

        chrm_num_1 = random.randint(0, len(self.linear_chromosomes) - 1)

        # chromosome 1 should not be a single gene chromosome otherwise the operation would amount to a fusion
        while len(self.linear_chromosomes[chrm_num_1]) < 3:
            chrm_num_1 = random.randint(0, len(self.linear_chromosomes) - 1)

        chrm_num_2 = random.randint(0, len(self.linear_chromosomes) - 1)
        while chrm_num_1 == chrm_num_2:
            chrm_num_2 = random.randint(0, len(self.linear_chromosomes) - 1)

        adjacencies_chrm1 = [
            element for element in self.linear_chromosomes[chrm_num_1]
            if type(element) is tuple
        ]
        telomeres_chrm2 = [
            element for element in self.linear_chromosomes[chrm_num_2]
            if type(element) is not tuple
        ]
        adj1_num = random.randint(0, len(adjacencies_chrm1) - 1)
        adj2_num = random.randint(0, len(telomeres_chrm2) - 1)

        adj1 = adjacencies_chrm1[adj1_num]
        telo2 = telomeres_chrm2[adj2_num]

        if telo2 < adj1[0]:
            new_adj1 = (telo2, adj1[0])
        else:
            new_adj1 = (adj1[0], telo2)

        # perfrom operation
        self.state.remove(adj1)
        self.state.remove(telo2)
        self.state.append(new_adj1)
        self.state.append(adj1[1])

        print('unbaanced translocation:   ',
              get_adjacencies.adjacencies_to_genome(self.state))
from Class_wrDCJ_Node import Node
from networkx import DiGraph
from Class_extremities_and_adjacencies import Extremities_and_adjacencies

get_genome = Extremities_and_adjacencies()


def build_hash_table(current_node, hash_table, adjacenciesB, weights):
    node = current_node

    # if the previous operation was a cicularization (i.e. a trp0) do:

    if node.join_adjacency != 0:

        operations = node.get_reinsertion_operations(adjacenciesB)

        for operation in operations:

            child_state = node.take_action(operation)[0]  # perform operation
            check_hash_table = check_hash_key(
                child_state, hash_table
            )  # check whether the intermediate create exists already

            # if it is a trp1 type operation
            if node.join_adjacency in operation[0]:
                operation_type = 'trp1'
                operation_weight = 0.5 * weights[1]
                #operation_weight = 1 * weights[1]

            # else it is a trp2 type operation
            else:
Example #6
0
from networkx import all_shortest_paths
from Class_wrDCJ_Node import Node
from Class_extremities_and_adjacencies import Extremities_and_adjacencies
from Class_Network_wrDCJ import Network

import GenomeEvolve

number_of_simulations = 1

results = []
genomeB = [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28],
           [29, 30, 31, 32, 33, 34, 35], [36, 37, 38, 39, 40, 41, 42],
           [43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56]]
#genomeB = [[1, 2,3,4, 5, 6, 7],[8,9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31, 32, 33, 34, 35], [36, 37, 38, 39, 40, 41, 42]]
get_adjacencies = Extremities_and_adjacencies()
adjacencies_genomeB = get_adjacencies.adjacencies_ordered_and_sorted(genomeB)

for i in range(0, number_of_simulations):
    genomeB_copy = genomeB.copy()
    genomeA = get_adjacencies.adjacencies_to_genome(
        GenomeEvolve.evolve_genome(genomeB_copy))

    adjacencies_genomeA = get_adjacencies.adjacencies_ordered_and_sorted(
        genomeA)
    # Create start and target node
    start_node = Node(adjacencies_genomeA)
    target_node = Node(adjacencies_genomeB)

    # Construct entire network
    construct_network = Network(start_node, target_node, adjacencies_genomeB)
Example #7
0
    def build_hash_table(self, current_node):
        t0 = time.time()

        adjacenciesB = self.adjacenciesB

        get_adjacencies = Extremities_and_adjacencies()
        #print()
        node = current_node
        #print()
        #print()
        #print('current node and state: ')
        #print(node)
        #print(node.state)
        #print('____________________________________________________________________________________')

        #if the genome has a circular intermediate (i.e all of its children will be linear)
        if node.next_operation != 0:
            operation_type = None

            operations = []
            # if point of cut = previous point of join:
            if node.next_operation_weight == 0.5:

                operations.append(node.next_operation)
                operation_type = 'trp1'

            elif node.next_operation_weight == 1.5:
                operations = []
                if type(node.next_operation) is list:
                    for operation in node.next_operation:
                        operations.append(operation)
                else:
                    operations.append(node.next_operation)
                operation_type = 'trp2'

            else:
                print(
                    'You have got a problem with the .next_operation weights')

            for operation in operations:

                child_state = node.take_action(operation)[0]

                check_hash_table = Network.check_hash_key(self, child_state)

                if check_hash_table[0]:
                    child = check_hash_table[1]
                    node.children.append(child)
                    node.children_weights.append(node.next_operation_weight)
                    node.children_operations.append(
                        (operation, operation_type))
                # print()
                # print('Operation: ', operation)
                # print('Type: ', operation_type)
                # print(get_adjacencies.adjacencies_to_genome(node.state), '  ---->    ',
                #      get_adjacencies.adjacencies_to_genome(child_state))

                else:
                    #remember the child will consist of linear chromosomes only because it is the result of a forced reinsertion
                    child = Node(child_state)
                    hash_key = hash(str(child.state))
                    self.hash_table.update({hash_key: child})
                    # print('#T: ', self.hash_table)
                    node.children.append(child)
                    node.children_weights.append(node.next_operation_weight)
                    node.children_operations.append(
                        (operation, operation_type))
                    #  print()
                    #  print('Operation: ', operation)
                    #  print('Type: ', operation_type)
                    #  print(get_adjacencies.adjacencies_to_genome(node.state), '  ---->    ',
                    #        get_adjacencies.adjacencies_to_genome(child_state))

                    Network.build_hash_table(self, child)

        #if the genome has no circular intermediates (i.e. some of its children may have circular chromosomes)
        else:

            operations = node.get_legal_operations(adjacenciesB)

            for operation in operations:

                operation_result = node.take_action(operation)
                child_state = operation_result[0]
                op_type = operation_result[1]

                check_hash_table = Network.check_hash_key(self, child_state)

                if check_hash_table[0]:
                    child = check_hash_table[1]
                    node.children.append(child)

                    # if the operation is a trp0
                    child.find_chromosomes(child.state)
                    if len(child.circular_chromosomes) != 0:
                        node.children_weights.append(0.5)
                        node.children_operations.append((operation, 'trp0'))
                    #   print()
                    #   print('Operation: ', operation)
                    #   print('Type: ', 'trp0')
                    #   print(get_adjacencies.adjacencies_to_genome(node.state), '  ---->    ',
                    #         get_adjacencies.adjacencies_to_genome(child_state))

                    else:
                        #node.children_weights.append(1)
                        #if op_type == 'fis' or op_type == 'fus' or op_type == 'u_trl':
                        #    operation_type = op_type
                        #else:
                        #    operation_type = node.find_operation_type(operation)

                        if op_type == 'fis':
                            operation_type = op_type
                            op_weight = 2

                        elif op_type == 'fus':
                            operation_type = op_type
                            op_weight = 2

                        elif op_type == 'u_trl':
                            operation_type = op_type
                            op_weight = 1.5

                        else:
                            operation_type = node.find_operation_type(
                                operation)
                            if operation_type == 'inv':
                                op_weight = 1
                            elif operation_type == 'b_trl':
                                op_weight = 1.5
                            else:
                                print(
                                    "There's a problem at the .find_optype node function"
                                )

                        node.children_weights.append(op_weight)

                        node.children_operations.append(
                            (operation, operation_type))
                    #   print()
                    #   print('Operation: ', operation)
                    #   print('Type: ', operation_type)
                    #   print('weight: ', op_weight)
                    #   print(get_adjacencies.adjacencies_to_genome(node.state), '  ---->    ',
                    #         get_adjacencies.adjacencies_to_genome(child_state))
                    #   print(node.children_weights)

                else:
                    child = Node(child_state)

                    # check whether a circular chromosome has been created
                    child.find_chromosomes(child.state)

                    # if a circular chromosome has been created:
                    if len(child.circular_chromosomes) != 0:

                        legal_operation = child.get_legal_reinsertion_operation(
                            operation, self.adjacenciesB)

                        if legal_operation:
                            child.next_operation = legal_operation
                            child.next_operation_weight = 0.5
                            hash_key = hash(str(child.state))
                            self.hash_table.update({hash_key: child})
                            # print('#T: ', self.hash_table)
                            node.children.append(child)
                            node.children_operations.append(
                                (operation, 'trp0'))
                            node.children_weights.append(0.5)
                            #   print()
                            #   print('Operation: ', operation)
                            #   print('Type: ', 'trp0')
                            #   print(get_adjacencies.adjacencies_to_genome(node.state), '  ---->    ',
                            #         get_adjacencies.adjacencies_to_genome(child_state))

                            Network.build_hash_table(self, child)
                        else:
                            child.next_operation = child.get_illegal_decircularization_operation(
                                self.adjacenciesB)

                            child.next_operation_weight = 1.5
                            hash_key = hash(str(child.state))
                            self.hash_table.update({hash_key: child})
                            # print('#T: ', self.hash_table)
                            node.children.append(child)
                            node.children_operations.append(
                                (operation, 'trp0'))
                            node.children_weights.append(0.5)
                            # print()
                            # print('Operation: ', operation)
                            # print('Type: ', 'trp0')
                            # print(get_adjacencies.adjacencies_to_genome(node.state), '  ---->    ',
                            #       get_adjacencies.adjacencies_to_genome(child_state))

                            Network.build_hash_table(self, child)

                    # else if no circular chromosome has been created:
                    else:

                        hash_key = hash(str(child.state))
                        self.hash_table.update({hash_key: child})
                        # print('#T: ', self.hash_table)
                        node.children.append(child)
                        '''
                        if op_type == 'fis' or op_type == 'fus' or op_type == 'u_trl':
                            operation_type = op_type
                        else:
                            operation_type = node.find_operation_type(operation)

                        node.children_weights.append(1)
                        node.children_operations.append((operation, operation_type))
                        print()
                        print('Operation: ', operation)
                        print('Type: ', operation_type)
                        print(get_adjacencies.adjacencies_to_genome(node.stalen(child.circular_chromosomes)te), '  ---->    ',
                              get_adjacencies.adjacencies_to_genome(child_state))
                        
                        '''

                        if op_type == 'fis':
                            operation_type = op_type
                            op_weight = 2

                        elif op_type == 'fus':
                            operation_type = op_type
                            op_weight = 2

                        elif op_type == 'u_trl':
                            operation_type = op_type
                            op_weight = 1.5
                        else:
                            operation_type = node.find_operation_type(
                                operation)
                            if operation_type == 'inv':
                                op_weight = 1
                            elif operation_type == 'b_trl':
                                op_weight = 1.5
                            else:
                                print(
                                    "There's a problem at the .find_optype node function"
                                )

                        node.children_weights.append(op_weight)

                        node.children_operations.append(
                            (operation, operation_type))

                        Network.build_hash_table(self, child)
Example #8
0
    def evolve(self,
               num_inv=0,
               num_fus=0,
               num_fis=0,
               num_b_trl=0,
               num_u_trl=0,
               num_trp1=0,
               num_trp2=0):
        get_adjacencies = Extremities_and_adjacencies()
        total = num_b_trl + num_fis + num_fus + num_inv + num_trp1 + num_trp2 + num_u_trl
        inv_n = num_inv
        fus_n = num_fus
        fis_n = num_fis
        b_trl_n = num_b_trl
        u_trl_n = num_u_trl
        trp1_n = num_trp1
        trp2_n = num_trp2

        operations = ['inv', 'trp1', 'trp2', 'b_trl', 'u_trl', 'fis', 'fus']

        while total > 0:
            operation = random.choice(operations)

            if operation == 'inv':
                if inv_n > 0:
                    # print('current state = ', get_adjacencies.adjacencies_to_genome(self.state))
                    self.inversion()
                    inv_n -= 1
                    total -= 1
                else:
                    pass

            elif operation == 'trp1':
                if trp1_n > 0:
                    # print('current state = ', get_adjacencies.adjacencies_to_genome(self.state))
                    self.transposition1()
                    trp1_n -= 1
                    total -= 1
                else:
                    pass

            elif operation == 'trp2':
                if trp2_n > 0:
                    #  print('current state = ', get_adjacencies.adjacencies_to_genome(self.state))
                    self.transposition2()
                    trp2_n -= 1
                    total -= 1
                else:
                    pass

            elif operation == 'b_trl':
                if b_trl_n > 0:
                    #  print('current state = ', get_adjacencies.adjacencies_to_genome(self.state))
                    self.balanced_translocation()
                    b_trl_n -= 1
                    total -= 1
                else:
                    pass

            elif operation == 'u_trl':
                if u_trl_n > 0:
                    #   print('current state = ', get_adjacencies.adjacencies_to_genome(self.state))
                    self.unbalanced_translocation()
                    u_trl_n -= 1
                    total -= 1
                else:
                    pass

            elif operation == 'fis':
                if fis_n > 0:
                    #   print('current state = ', get_adjacencies.adjacencies_to_genome(self.state))
                    self.fission()
                    fis_n -= 1
                    total -= 1
                else:
                    pass

            elif operation == 'fus':
                if fus_n > 0:
                    #    print('current state = ', get_adjacencies.adjacencies_to_genome(self.state))
                    self.fusion()
                    fus_n -= 1
                    total -= 1
                else:
                    pass
Example #9
0
    def inversion(self):

        get_adjacencies = Extremities_and_adjacencies()
        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]
        chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        # if it is a single gene or two gene chromosome, choose another one
        while len(self.linear_chromosomes[chrm_num]) < 4:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        adjacencies = [
            element for element in self.linear_chromosomes[chrm_num]
            if type(element) is tuple
        ]

        adj1_num = random.randint(0, len(adjacencies) - 1)
        adj2_num = random.randint(0, len(adjacencies) - 1)

        while adj1_num == adj2_num:
            adj2_num = random.randint(0, len(adjacencies) - 1)

        adj1 = adjacencies[adj1_num]
        adj2 = adjacencies[adj2_num]

        if adj1[0] < adj2[0]:
            new_adj1 = (adj1[0], adj2[0])
        else:
            new_adj1 = (adj2[0], adj1[0])

        if adj1[1] < adj2[1]:
            new_adj2 = (adj1[1], adj2[1])
        else:
            new_adj2 = (adj2[1], adj1[1])

        #perfrom operation

        self.state.remove(adj1)
        self.state.remove(adj2)
        self.state.append(new_adj1)
        self.state.append(new_adj2)

        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]

        while len(self.circular_chromosomes) > 0:

            self.state.append(adj1)
            self.state.append(adj2)
            self.state.remove(new_adj1)
            self.state.remove(new_adj2)

            get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
            self.linear_chromosomes = get_chromosomes[0]
            self.circular_chromosomes = get_chromosomes[1]

            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)
            while len(self.linear_chromosomes[chrm_num]) < 4:
                chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)
            adjacencies = [
                element for element in self.linear_chromosomes[chrm_num]
                if type(element) is tuple
            ]

            adj1_num = random.randint(0, len(adjacencies) - 1)
            adj2_num = random.randint(0, len(adjacencies) - 1)

            while adj1_num == adj2_num:
                adj2_num = random.randint(0, len(adjacencies) - 1)

            adj1 = adjacencies[adj1_num]
            adj2 = adjacencies[adj2_num]

            if adj1[0] < adj2[0]:
                new_adj1 = (adj1[0], adj2[0])
            else:
                new_adj1 = (adj2[0], adj1[0])

            if adj1[1] < adj2[1]:
                new_adj2 = (adj1[1], adj2[1])
            else:
                new_adj2 = (adj2[1], adj1[1])

            # perfrom operation

            self.state.remove(adj1)
            self.state.remove(adj2)
            self.state.append(new_adj1)
            self.state.append(new_adj2)

            get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
            self.linear_chromosomes = get_chromosomes[0]
            self.circular_chromosomes = get_chromosomes[1]

        print('inversion:   ',
              get_adjacencies.adjacencies_to_genome(self.state))
Example #10
0
    def transposition(self, linear_chromosomes):
        get_adjacencies_and_chromosomes = Extremities_and_adjacencies()
        #  print('CHROMOSOMES Before transposition 1: ', get_adjacencies_and_chromosomes.find_chromosomes(self.state))
        get_adjacencies_and_chromosomes = Extremities_and_adjacencies()

        chromosome_number = random.randint(0, len(linear_chromosomes) - 1)
        chromosome = linear_chromosomes[chromosome_number]
        chromosome_length = len(chromosome)
        to_exclude = []

        while chromosome_length < 4:
            to_exclude.append(linear_chromosomes.index(chromosome))
            # print('Transposition a')
            #  print('THE LIST: ', [i for i in range(0, len(linear_chromosomes)) if i not in to_exclude])
            #  print(linear_chromosomes)
            chromosome_number = random.choice([
                i for i in range(0, len(linear_chromosomes))
                if i not in to_exclude
            ])

            chromosome = linear_chromosomes[chromosome_number]
            chromosome_length = len(chromosome)

        try:
            adjacencies = [
                element for element in chromosome if type(element) is tuple
            ]

        except:
            print(
                'There were not any chromosomes with more than 2 sequence blocks'
            )

        adj1_num = random.randint(0, len(adjacencies) - 1)
        adj2_num = random.randint(0, len(adjacencies) - 1)

        while adj1_num == adj2_num:
            adj2_num = random.randint(0, len(adjacencies) - 1)

        adj1 = adjacencies[adj1_num]
        adj2 = adjacencies[adj2_num]

        # order extremities in the adjacency tuple
        if adj1[0] < adj2[0]:
            new_adj1 = (adj1[0], adj2[0])
        else:
            new_adj1 = (adj2[0], adj1[0])

        if adj1[1] < adj2[1]:
            new_adj2 = (adj1[1], adj2[1])
        else:
            new_adj2 = (adj2[1], adj1[1])

        # perform_operation
        self.state.remove(adj1)
        self.state.remove(adj2)
        self.state.append(new_adj1)
        self.state.append(new_adj2)

        chromosomes = get_adjacencies_and_chromosomes.find_chromosomes(
            self.state)
        if len(chromosomes[1]) == 0:

            # reverse operation
            self.state.append(adj1)
            self.state.append(adj2)
            self.state.remove(new_adj1)
            self.state.remove(new_adj2)

            # order extremities in the adjacency tuple
            if adj1[0] < adj2[1]:
                new_adj1 = (adj1[0], adj2[1])
            else:
                new_adj1 = (adj2[1], adj1[0])

            if adj1[1] < adj2[0]:
                new_adj2 = (adj1[1], adj2[0])
            else:
                new_adj2 = (adj2[0], adj1[1])

            # perform_operation
            self.state.remove(adj1)
            self.state.remove(adj2)
            self.state.append(new_adj1)
            self.state.append(new_adj2)

        # get operation notation
        if adj1[0] < adj2[0]:
            operationA = (adj1, adj2)
        else:
            operationA = (adj2, adj1)

        if new_adj1[0] < new_adj2[0]:
            operationB = (new_adj1, new_adj2)
        else:
            operationB = (new_adj2, new_adj1)

        chromosomes = get_adjacencies_and_chromosomes.find_chromosomes(
            self.state)
        if new_adj1 in chromosomes[1][0]:
            circular_adjacency = new_adj1
            exsision_adjacency = new_adj2
        else:
            circular_adjacency = new_adj2
            exsision_adjacency = new_adj1

        operation1 = ((operationA, operationB), 'trp0')
        intermediate1 = self.state[:]

        ###############
        # Reinsertion DCJ
        adj1 = circular_adjacency
        adj2 = exsision_adjacency

        counter = 0
        while adj2 == exsision_adjacency:
            if counter > 2:
                adj2 = chromosomes[0][0][0]
            else:
                chromosome_number = random.randint(0, len(chromosomes[0]) - 1)
                chromosome = chromosomes[0][chromosome_number]
                chromosome_length = len(chromosome)
                to_exclude = []
                while chromosome_length < 3:
                    to_exclude.append(linear_chromosomes.index(chromosome))
                    #  print('Transposition b')
                    ##  print('THE LIST: ', [i for i in range(0, len(linear_chromosomes)) if i not in to_exclude])
                    #  print(linear_chromosomes)
                    chromosome_number = random.choice([
                        i for i in range(0, len(linear_chromosomes))
                        if i not in to_exclude
                    ])
                    chromosome = chromosomes[0][chromosome_number]
                    chromosome_length = len(chromosome)

                adjacencies = [
                    element for element in chromosome if type(element) is tuple
                ]

                adj2_num = random.randint(0, len(adjacencies) - 1)
                adj2 = adjacencies[adj2_num]
                counter += 1

        # order extremities in adjacency tuple
        if type(adj2) is not tuple:
            if adj1[0] < adj2:
                new_adj1 = (adj1[0], adj2)
            else:
                new_adj1 = (adj2, adj1[0])
            new_adj2 = adj1[1]
            # perfrom operation
            self.state.remove(adj1)
            self.state.remove(adj2)
            self.state.append(new_adj1)
            self.state.append(new_adj2)

            if adj1[0] < adj2:
                operationA = (adj1, adj2)
            else:
                operationA = (adj2, adj1)

            if new_adj1[0] < new_adj2:
                operationB = (new_adj1, new_adj2)
            else:
                operationB = (new_adj2, new_adj1)

            operation2 = ((operationA, operationB), 'trp1')
            intermediate2 = self.state[:]

        else:
            try:
                if adj1[0] < adj2[0]:
                    new_adj1 = (adj1[0], adj2[0])
                else:
                    new_adj1 = (adj2[0], adj1[0])
            except TypeError:
                print("transcroption problem in line 293")
                print('adj1: ', adj1)
                print('adj2: ', adj2)

            if adj1[1] < adj2[1]:
                new_adj2 = (adj1[1], adj2[1])
            else:
                new_adj2 = (adj2[1], adj1[1])

            # perfrom operation
            self.state.remove(adj1)
            self.state.remove(adj2)
            self.state.append(new_adj1)
            self.state.append(new_adj2)

            if adj1[0] < adj2[0]:
                operationA = (adj1, adj2)
            else:
                operationA = (adj2, adj1)

            if new_adj1[0] < new_adj2[0]:
                operationB = (new_adj1, new_adj2)
            else:
                operationB = (new_adj2, new_adj1)

            operation2 = ((operationA, operationB), 'trp1')
            intermediate2 = self.state[:]

        return [(operation1, intermediate1), (operation2, intermediate2)]
Example #11
0
 def __init__(self, target_genome):
     get_adjacencies_and_chromosomes = Extremities_and_adjacencies()
     self.state = get_adjacencies_and_chromosomes.create_adjacency_list(
         target_genome)
Example #12
0
    def inversion(self, linear_chromosomes):

        get_adjacencies_and_chromosomes = Extremities_and_adjacencies()

        chromosome_number = random.randint(0, len(linear_chromosomes) - 1)
        chromosome = linear_chromosomes[chromosome_number]
        #print('the chromosome: ', chromosome)
        chromosome_length = len(chromosome)
        #print('chromosome length: ', chromosome_length)

        to_exclude = []

        # print('CHROMOSOMES Before Inversion: ', get_adjacencies_and_chromosomes.find_chromosomes(self.state))

        while chromosome_length < 3:

            to_exclude.append(linear_chromosomes.index(chromosome))
            #   print('Inversion')
            #   print('THE LIST: ', [i for i in range(0, len(linear_chromosomes)) if i not in to_exclude])
            #   print(linear_chromosomes)
            chromosome_number = random.choice([
                i for i in range(0, len(linear_chromosomes))
                if i not in to_exclude
            ])
            chromosome = linear_chromosomes[chromosome_number]
            chromosome_length = len(chromosome)

        if chromosome_length == 3:
            #print('is executing')

            adjacencies = [
                element for element in chromosome if type(element) is tuple
            ]
            telomeres = [
                element for element in chromosome if type(element) is not tuple
            ]

            adj = adjacencies[0]
            telo = random.choice(telomeres)
            if int(telo) == int(adj[0]):
                if telo < adj[1]:
                    new_adj = (telo, adj[1])
                else:
                    new_adj = (adj[1], telo)
                new_telo = adj[0]

            else:
                if telo < adj[0]:
                    new_adj = (telo, adj[0])
                else:
                    new_adj = (adj[0], telo)
                new_telo = adj[1]
            operationA = (adj, telo)
            operationB = (new_adj, new_telo)

            self.state.remove(adj)
            self.state.remove(telo)
            self.state.append(new_adj)
            self.state.append(new_telo)

            operation = (operationA, operationB, 'inv')
            intermediate = self.state
        # print('operation: ', operation)
        ## print('done')

        else:

            try:
                adjacencies = [
                    element for element in chromosome if type(element) is tuple
                ]
            except:
                print(
                    'There were not any chromosomes with more than 2 sequence blocks'
                )

            adj1_num = random.randint(0, len(adjacencies) - 1)
            adj2_num = random.randint(0, len(adjacencies) - 1)

            while adj1_num == adj2_num:
                adj2_num = random.randint(0, len(adjacencies) - 1)

            adj1 = adjacencies[adj1_num]
            adj2 = adjacencies[adj2_num]

            #order extremities in the adjacency tuple
            if adj1[0] < adj2[0]:
                new_adj1 = (adj1[0], adj2[0])
            else:
                new_adj1 = (adj2[0], adj1[0])

            if adj1[1] < adj2[1]:
                new_adj2 = (adj1[1], adj2[1])
            else:
                new_adj2 = (adj2[1], adj1[1])

            #perform_operation
            self.state.remove(adj1)
            self.state.remove(adj2)
            self.state.append(new_adj1)
            self.state.append(new_adj2)

            chromosomes = get_adjacencies_and_chromosomes.find_chromosomes(
                self.state)
            if len(chromosomes[1]) != 0:

                #reverse operation
                self.state.append(adj1)
                self.state.append(adj2)
                self.state.remove(new_adj1)
                self.state.remove(new_adj2)

                # order extremities in the adjacency tuple
                if adj1[0] < adj2[1]:
                    new_adj1 = (adj1[0], adj2[1])
                else:
                    new_adj1 = (adj2[1], adj1[0])

                if adj1[1] < adj2[0]:
                    new_adj2 = (adj1[1], adj2[0])
                else:
                    new_adj2 = (adj2[0], adj1[1])

                # perform_operation
                self.state.remove(adj1)
                self.state.remove(adj2)
                self.state.append(new_adj1)
                self.state.append(new_adj2)

            # get operation notation
            if adj1[0] < adj2[0]:
                operationA = (adj1, adj2)
            else:
                operationA = (adj2, adj1)

            if new_adj1[0] < new_adj2[0]:
                operationB = (new_adj1, new_adj2)
            else:
                operationB = (new_adj2, new_adj1)

            operation = ((operationA, operationB), 'inv')
            intermediate = self.state[:]

    #  print('CHROMOSOMES After: ', get_adjacencies_and_chromosomes.find_chromosomes(self.state))
    #  print('operation: ',operation)
        return (operation, intermediate)
Example #13
0
    def evolve_with_random_rearrangements(self, number_of_rearrangements):
        rearrangements = ['inv', 'trp', 'b_trl', 'u_trl', 'fus', 'fis']
        get_adjacencies_and_chromosomes = Extremities_and_adjacencies()
        #genome_adjacencies = get_adjacencies_and_chromosomes.create_adjacency_list(target_genome)

        rearrangement_series = []

        while number_of_rearrangements > 0:
            #print('NUMBER OF REARRANGEMENTS: ', number_of_rearrangements)
            chromosomes = get_adjacencies_and_chromosomes.find_chromosomes(
                self.state)

            linear_chromosomes = chromosomes[0]
            if len(chromosomes[0]) < 2:  #if it is a single chromosome genome
                if len(chromosomes[0][0]) < 5:
                    operation = 'inv'

                else:
                    chr_lens = [len(x) for x in linear_chromosomes]

                    if max(chr_lens) < 4:
                        operation = 'inv'
                    else:
                        operation = random.choice(['inv', 'trp'])

        # elif len(chromosomes[0]) == 2 and (len(chromosomes[0][0])==2 or len(chromosomes[0][1])==2): # if the genome consists of two chromosomes but one chromosomes is a single gene chromosome
        #    operation = random.choice(['inv', 'trp', 'u_trl', 'fis', 'fus'])
            else:
                chr_lens = [len(x) for x in linear_chromosomes]
                chrm_longer_than_2_SB = [x for x in chr_lens if x > 3]
                chrm_longer_than_1_SB = [x for x in chr_lens if x > 2]

                if len(chrm_longer_than_2_SB) < 2:  #cannot be trp
                    if len(chrm_longer_than_1_SB) < 2:  #cannot be b_trl:
                        if len(chrm_longer_than_1_SB
                               ) < 1:  #cannot be u_trl, fis, inv
                            operation = 'fus'
                        else:
                            operation = random.choice(
                                ['inv', 'u_trl', 'fus', 'fis'])
                    else:
                        operation = random.choice(
                            ['inv', 'b_trl', 'u_trl', 'fus', 'fis'])
                else:
                    operation = random.choice(rearrangements)

                # if max(chr_lens) < 4: #cannot be trp
                #     number_of_chromosomes_with_more_than_one_sequence_block = 0
                #     for element in chr_lens:
                #         if element > 2:
                #             number_of_chromosomes_with_more_than_one_sequence_block+=1
                #     if number_of_chromosomes_with_more_than_one_sequence_block >= 2:
                #         operation = random.choice(['inv', 'b_trl' ,'u_trl', 'fis', 'fus'])
                #     else:
                #         operation = random.choice(['inv', 'u_trl', 'fis', 'fus'])
                # else:
                #     number_of_chromosomes_with_more_than_one_sequence_block = 0
                #     for element in chr_lens:
                #         if element > 2:
                #             number_of_chromosomes_with_more_than_one_sequence_block += 1
                #     if number_of_chromosomes_with_more_than_one_sequence_block >= 2:
                #         operation = random.choice(['inv', 'trp','b_trl', 'u_trl', 'fis', 'fus'])
                #     else:
                #         operation = random.choice(['inv', 'trp','u_trl', 'fis', 'fus'])
                #

                # number_of_chromosomes_with_more_than_one_sequence_block = 0
                #
                # for element in chromosomes[0]:
                #     if len(element) > 2:
                #         number_of_chromosomes_with_more_than_one_sequence_block+=1
                # if number_of_chromosomes_with_more_than_one_sequence_block >= 2:
                #     operation = random.choice(rearrangements)
                # else:
                #     operation = random.choice(['inv', 'trp', 'u_trl', 'fis', 'fus'])

            if operation == 'inv':
                execution = self.inversion(linear_chromosomes)
                #print('REACHES THIS POINT')
                rearrangement_series.append(execution)
                number_of_rearrangements -= 1
                #print('NUMBER OF REARRANGEMENTS after: ', number_of_rearrangements)

            elif operation == 'trp':
                execution = self.transposition(linear_chromosomes)
                for element in execution:
                    rearrangement_series.append(element)
                number_of_rearrangements -= 1

            elif operation == 'b_trl':
                execution = self.balanced_translocation(linear_chromosomes)
                rearrangement_series.append(execution)
                number_of_rearrangements -= 1

            elif operation == 'u_trl':
                execution = self.unbalanced_translocation(linear_chromosomes)
                rearrangement_series.append(execution)
                number_of_rearrangements -= 1

            elif operation == 'fus':
                execution = self.fusion(linear_chromosomes)
                rearrangement_series.append(execution)
                number_of_rearrangements -= 1

            elif operation == 'fis':
                execution = self.fission(linear_chromosomes)
                rearrangement_series.append(execution)
                number_of_rearrangements -= 1

        return rearrangement_series
Example #14
0
def run(args):
    genomeA_file = args.source_genome
    genomeB_file = args.target_genome
    weight_ratios_file = args.ratios
    stdoutOrigin = sys.stdout
    sys.stdout = open(args.output_file, 'w')
    #outfile = open(args.output_file, 'w')
    with open(genomeA_file) as csv:
        line = [element.strip('\n').split(',') for element in csv]
    genomeA = []

    for element in line:
        element = list(map(int, element))
        genomeA.append(element)

    with open(genomeB_file) as csv:
        line = [element.strip('\n').split(',') for element in csv]
    genomeB = []

    for element in line:
        element = list(map(int, element))
        genomeB.append(element)

    with open(weight_ratios_file) as csv:
        line = [element.strip('\n').split(',') for element in csv]
    weight_ratios = []

    for element in line:
        element = list(map(int, element))
        weight_ratios.append(element)

    get_adjacencies = Extremities_and_adjacencies()
    adjacencies_genomeA = get_adjacencies.adjacencies_ordered_and_sorted(genomeA)


    adjacencies_genomeB = get_adjacencies.adjacencies_ordered_and_sorted(genomeB)

    #Create start and target node
    start_node = Node(adjacencies_genomeA)
    target_node = Node(adjacencies_genomeB)

    hash_table = {}
    hash_key_start = hash(str(start_node.state))
    hash_key_target = hash(str(target_node.state))
    hash_table.update({hash_key_start:start_node})
    hash_table.update({hash_key_target:target_node})

    #finding rearrangement weights
    max_number = max(weight_ratios[0])
    weights = []
    for element in weight_ratios[0]:
        if element == 0:
            weights.append(max_number^2)
        else:
            weights.append(max_number/element)

    New_Network_wrDCJ.build_hash_table(start_node, hash_table, adjacencies_genomeB, weights)

    network = New_Network_wrDCJ.build_network(hash_table)

    shortest_paths = (list(all_shortest_paths(network, start_node, target_node, weight='weight')))

    j = 1
    tot_b_trl = 0
    tot_u_trl = 0
    tot_inv = 0
    tot_trp1 = 0
    tot_trp2 = 0
    tot_fus = 0
    tot_fis = 0

    Paths_state = []
    Paths_state_weight = []
    # print(shortest_paths[0][4].children_weights[2])
    for path in shortest_paths:
        path_state = []
        path_state_weight = []

        i = 0
        while i < len(path):
            current = path[i]
            if i == 0:
                operation_type = 'none, this is the source genome'
                operation_weight = 'N/A'
                operation = 'N/A'
            else:
                x = path[i - 1].children.index(current)

                operation_type = path[i - 1].children_operations[x][1]
                operation_weight = path[i - 1].children_weights[x]
                operation = path[i - 1].children_operations[x][0]

            adjacencies = current.state
            genome = get_adjacencies.adjacencies_to_genome(adjacencies)
            path_state_weight.append((genome, ((operation_type, operation), operation_weight)))

            path_state.append((genome, (operation_type, operation)))

            i += 1
        Paths_state.append((path_state))
        Paths_state_weight.append(path_state_weight)

    for path in shortest_paths:

        i = 0
        b_trl = 0
        u_trl = 0
        inv = 0
        trp1 = 0
        trp2 = 0
        fus = 0
        fis = 0
        while i < len(path):

            current = path[i]
            if i == 0:
                pass
            else:
                x = path[i - 1].children.index(current)
                operation_type = path[i - 1].children_operations[x][1]
                if operation_type == 'b_trl':
                    b_trl += 1
                elif operation_type == 'u_trl':
                    u_trl += 1
                elif operation_type == 'inv':
                    inv += 1
                elif operation_type == 'trp1':
                    trp1 += 1
                elif operation_type == 'trp2':
                    trp2 += 1
                elif operation_type == 'fus':
                    fus += 1
                elif operation_type == 'fis':
                    fis += 1
            i += 1

        tot_b_trl += b_trl
        tot_u_trl += u_trl
        tot_inv += inv
        tot_trp1 += trp1
        tot_trp2 += trp2
        tot_fus += fus
        tot_fis += fis
        j += 1


    print('############################################################################################################')
    print()
    print('Source Genome: ', genomeA)
    print('Target Genome: ', genomeB)
    print()
    print('Number of most parsimonious solutions: ', len(shortest_paths))
    print()
    print('Average number of each operation per solution:')
    print('Inversions: ', int(tot_inv/len(shortest_paths)), '  Transpositions type 1: ', int(tot_trp1/len(shortest_paths)), '  Transpositions type 2: ', int(tot_trp2/len(shortest_paths)), '  Balanced translocations: ', int(tot_b_trl/len(shortest_paths)), '  Unbalanced translocations: ', int(tot_u_trl/len(shortest_paths)),
          '  Fusions: ', int(tot_fus/len(shortest_paths)),
          '  Fissions: ', int(tot_fis/len(shortest_paths)))
    print()
    print()
    print('Solutions: ')
    print()
    path_counter = 1
    for path in Paths_state:
        print('Solution number ', path_counter)
        for genome in path:
            print(genome)
        path_counter+=1
        print()
    print()
    print('############################################################################################################')



    ###############################
    # JUST FOR TESTING

    solution = [([[1, 2, 3, 4, 15], [-8, -7, 6, -5, -14, -13, -12], [9, 11],
                  [-20, -19, -18, -17, -16, -32, 10, -31, -30, -29, -28, -27], [21, 22, 23, 24, 25, 26], [-33],
                  [34, 35, 36, 37, 38, 39, 40]], ('none, this is the source genome', 'N/A')), (
                [[1, 2, 3, 4, 15], [-8, -7, -6, -5, -14, -13, -12], [9, 11],
                 [-20, -19, -18, -17, -16, -32, 10, -31, -30, -29, -28, -27], [21, 22, 23, 24, 25, 26], [-33],
                 [34, 35, 36, 37, 38, 39, 40]], ('inv', (((5.5, 6.5), (6, 7)), ((5.5, 6), (6.5, 7))))), (
                [[1, 2, 3, 4, 15], [-8, -7, -6, -5, -14, -13, -12], [9, 11], [16, 17, 18, 19, 20],
                 [21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, -10, 32, 33], [34, 35, 36, 37, 38, 39, 40]],
                ('u_trl', (((16, 32.5), 33), ((32.5, 33), 16)))), (
                [[1, 2, 3, 4, 5, 6, 7, 8], [9, 11], [12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26],
                 [27, 28, 29, 30, 31, -10, 32, 33], [34, 35, 36, 37, 38, 39, 40]],
                ('b_trl', (((4.5, 15), (5, 14.5)), ((4.5, 5), (14.5, 15))))), (
                [[1, 2, 3, 4, 5, 6, 7, 8], [9, 11], [12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26],
                 [27, 28, 29, 30, 31, 32, 33], [34, 35, 36, 37, 38, 39, 40], ['o', 10]],
                ('trp0', (((10, 32), (10.5, 31.5)), ((10, 10.5), (31.5, 32))))), (
                [[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19, 20],
                 [21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 32, 33], [34, 35, 36, 37, 38, 39, 40]],
                ('trp1', (((9.5, 11), (10, 10.5)), ((9.5, 10), (10.5, 11)))))]

    paths_operations = []
    for element in Paths_state:
        path_operations = [y for (x, y) in element]

        paths_operations.append(path_operations)

    solution_operations = [d for (c, d) in solution]

    path_types = []
    sol_types = [a for a, b in solution_operations]
    for element in paths_operations:
        types = [c for c, d in element]
        path_types.append(types)

    indexes = []
    counter = 0
    for element in path_types:
        if element == sol_types:
            indexer = path_types.index(element)
            counter += 1
            indexes.append(indexer)

    # for element in indexes:
    #     for x in Paths_state[element]:
    #         print(x)
    #     print()
    # print('*****')
    # for element in solution:
    #     print(element)

    print('sol len: ', len(solution))
    print('shortest path len: ',len(shortest_paths[0]))
    print('counter', counter)

    print('And the answer is... ', solution_operations in paths_operations)

    print('Source genome: ',genomeA)
    print('Target genome: ', genomeB)
    print()
    print('Solution: ', solution)
    ##########################################################################################################
    sys.stdout.close()
    sys.stdout=stdoutOrigin
Example #15
0
from Class_extremities_and_adjacencies import Extremities_and_adjacencies

from Class_Evolve import Node_evolve
get_adjacencies = Extremities_and_adjacencies()
'''
genomeA = [[1,2,4,3]]
genomeB = [[1, 2,3,4, 5,6,7],[8,9, 10, 11, 12], [13, 14, 15]]


adjacencies_genomeA = get_adjacencies.adjacencies_ordered_and_sorted(genomeA)
adjacencies_genomeB = get_adjacencies.adjacencies_ordered_and_sorted(genomeB)

print('Adjacencies of the genomes: ')
print('Genome A: ', adjacencies_genomeA)
print('Genome B: ', adjacencies_genomeB)
print('____________________________________')
print()
print()





genome = Node_evolve(state=adjacencies_genomeB)
print(get_adjacencies.adjacencies_to_genome(genome.state))
print()
genome.evolve(num_inv=1, num_b_trl=1, num_fis=1, num_fus=1, num_trp1=1, num_trp2=0, num_u_trl=1)
print()
print(get_adjacencies.adjacencies_to_genome(genome.state))

'''
Example #16
0
    def trp2(self):
        get_adjacencies = Extremities_and_adjacencies()

        # exsision and circularization

        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]
        chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        # if it is a single gene or two gene chromosome, choose another one
        while len(self.linear_chromosomes[chrm_num]) < 4:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        # if there is only one adjacency

        adjacencies = [
            element for element in self.linear_chromosomes[chrm_num]
            if type(element) is tuple
        ]

        adj1_num = random.randint(0, len(adjacencies) - 1)
        adj2_num = random.randint(0, len(adjacencies) - 1)

        while adj1_num == adj2_num:
            adj2_num = random.randint(0, len(adjacencies) - 1)

        adj1 = adjacencies[adj1_num]
        adj2 = adjacencies[adj2_num]

        if adj1[0] < adj2[1]:
            new_adj1 = (adj1[0], adj2[1])
        else:
            new_adj1 = (adj2[1], adj1[0])

        if adj1[1] < adj2[0]:
            new_adj2 = (adj1[1], adj2[0])
        else:
            new_adj2 = (adj2[0], adj1[1])

        # perfrom operation
        self.state.remove(adj1)
        self.state.remove(adj2)
        self.state.append(new_adj1)
        self.state.append(new_adj2)

        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]

        if len(self.circular_chromosomes) == 0:
            self.state.append(adj1)
            self.state.append(adj2)
            self.state.remove(new_adj1)
            self.state.remove(new_adj2)

            if adj1[0] < adj2[0]:
                new_adj1 = (adj1[0], adj2[0])
            else:
                new_adj1 = (adj2[0], adj1[0])

            if adj1[1] < adj2[1]:
                new_adj2 = (adj1[1], adj2[1])
            else:
                new_adj2 = (adj2[1], adj1[1])

            self.state.remove(adj1)
            self.state.remove(adj2)
            self.state.append(new_adj1)
            self.state.append(new_adj2)

            get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
            self.linear_chromosomes = get_chromosomes[0]
            self.circular_chromosomes = get_chromosomes[1]

        if new_adj1 in self.circular_chromosomes[0]:
            join = new_adj1
            excision = new_adj2
        else:
            join = new_adj2
            excision = new_adj1

        print('trp0:   ', get_adjacencies.adjacencies_to_genome(self.state))

        # decircularization and reinsertion

        adj1 = join
        chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)
        # if it is a single gene chromosome, choose another chromosome
        while len(self.linear_chromosomes[chrm_num]) == 2:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        if len(self.linear_chromosomes[chrm_num]) == 3:
            adjacencies = [
                element for element in self.linear_chromosomes[chrm_num]
                if type(element) is tuple
            ]
            adj2 = adjacencies[0]

        else:
            adjacencies = [
                element for element in self.linear_chromosomes[chrm_num]
                if type(element) is tuple
            ]
            adj2_num = random.randint(0, len(adjacencies) - 1)
            adj2 = adjacencies[adj2_num]

        while adj2 == excision:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)
            # if it is a single gene chromosome, choose another chromosome
            while len(self.linear_chromosomes[chrm_num]) == 2:
                chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

            if len(self.linear_chromosomes[chrm_num]) == 3:
                adjacencies = [
                    element for element in self.linear_chromosomes[chrm_num]
                    if type(element) is tuple
                ]
                adj2 = adjacencies[0]

            else:
                adjacencies = [
                    element for element in self.linear_chromosomes[chrm_num]
                    if type(element) is tuple
                ]
                adj2_num = random.randint(0, len(adjacencies) - 1)
                adj2 = adjacencies[adj2_num]

        if adj1[0] < adj2[1]:
            new_adj1 = (adj1[0], adj2[1])
        else:
            new_adj1 = (adj2[1], adj1[0])

        if adj1[1] < adj2[0]:
            new_adj2 = (adj1[1], adj2[0])
        else:
            new_adj2 = (adj2[0], adj1[1])

        # perfrom operation
        self.state.remove(adj1)
        self.state.remove(adj2)
        self.state.append(new_adj1)
        self.state.append(new_adj2)

        print('trp1:   ', get_adjacencies.adjacencies_to_genome(self.state))
from networkx import all_shortest_paths
from Class_wrDCJ_Node import Node
from Class_extremities_and_adjacencies import Extremities_and_adjacencies
import New_Network_wrDCJ
import GenomeEvolve
import time
t0 = time.time()

number_of_simulations = 10000

results = []
genomeB = [[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20], [21, 22,23,24,25,26,27,28,29,30], [31,32,33,34,35,36,37,38,39,40], [41,42,43,44,45,46,47,48,49,50]]

weight_ratios = [[1,1,1,1,1,1]]

get_adjacencies = Extremities_and_adjacencies()
adjacencies_genomeB = get_adjacencies.adjacencies_ordered_and_sorted(genomeB)

number_of_solutions_found = 0

for i in range(0, number_of_simulations):
    print('simultation: ', i)
    genomeB_copy = genomeB[:]
    evolution_simulation = GenomeEvolve.get_evolved_genome_and_solution(genomeB_copy)
    genomeA = evolution_simulation[1]
    sorting_scenario = evolution_simulation[2]

    adjacencies_genomeA = get_adjacencies.adjacencies_ordered_and_sorted(genomeA)

    # Create start and target node
    start_node = Node(adjacencies_genomeA)
Example #18
0
 def __init__(self, state=None):
     self.state = state
     get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
     self.linear_chromosomes = get_chromosomes[0]
     self.circular_chromosomes = get_chromosomes[1]
     get_adjacencies = Extremities_and_adjacencies()
Example #19
0
randomized = 'Random weight ratios'
one_to_one = 'One to one weight ratios'
same_as_solution = 'Same as solution weight ratios'
#####################
number_of_simulations = 10000
number_of_sequence_blocks = 9
number_of_rearrangements = 8
type_of_weight_ratio = same_as_solution




results = []
t0 = time.time()

get_adjacencies = Extremities_and_adjacencies()

number_of_solutions_found = 0
get_adjacencies_and_genomes = Extremities_and_adjacencies()

for i in range(0, number_of_simulations):
    #print('SIMULATION NUMBER: ', i, '*****************************************************************')
   # print('num of sb, ', number_of_sequence_blocks)
    target_genome = GenomeEvolver.create_target_genome(number_of_sequence_blocks)
    evolving_genome = Evolve(target_genome)
    rearrangement_series = evolving_genome.evolve_with_random_rearrangements(number_of_rearrangements)
    reverse_the_series = GenomeEvolver.reverse_rearrangement_series(target_genome, rearrangement_series)
    source_genome = reverse_the_series[1]
    solution = reverse_the_series[2]
    target_adjacencies = get_adjacencies_and_genomes.adjacencies_ordered_and_sorted(target_genome)
    source_adjacencies = get_adjacencies_and_genomes.adjacencies_ordered_and_sorted(source_genome)
Example #20
0
    def transposition2(self):
        get_adjacencies = Extremities_and_adjacencies()

        # exsision and circularization

        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]
        chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        # if it is a single gene or two gene chromosome, choose another one
        while len(self.linear_chromosomes[chrm_num]) < 6:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        adjacencies = [
            element for element in self.linear_chromosomes[chrm_num]
            if type(element) is tuple
        ]

        adj1_num = random.randint(0, len(adjacencies) - 1)
        adj2_num = random.randint(0, len(adjacencies) - 1)

        # To ensure single gene circular chromosomes are not formed because then there is only one adj so cut and not != join
        while adj1_num == adj2_num or (int(adjacencies[adj2_num][0])) == int(
                adjacencies[adj1_num][0]) or (int(
                    adjacencies[adj2_num][0])) == int(
                        adjacencies[adj1_num][1]) or (int(
                            adjacencies[adj2_num][1])) == int(
                                adjacencies[adj1_num][0]) or (int(
                                    adjacencies[adj2_num][1])) == int(
                                        adjacencies[adj1_num][1]):
            adj2_num = random.randint(0, len(adjacencies) - 1)

        adj1 = adjacencies[adj1_num]
        adj2 = adjacencies[adj2_num]

        if adj1[0] < adj2[1]:
            new_adj1 = (adj1[0], adj2[1])
        else:
            new_adj1 = (adj2[1], adj1[0])

        if adj1[1] < adj2[0]:
            new_adj2 = (adj1[1], adj2[0])
        else:
            new_adj2 = (adj2[0], adj1[1])

        # perfrom operation
        self.state.remove(adj1)
        self.state.remove(adj2)
        self.state.append(new_adj1)
        self.state.append(new_adj2)

        get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
        self.linear_chromosomes = get_chromosomes[0]
        self.circular_chromosomes = get_chromosomes[1]

        # if the above does not result in the formation of a cicular chromosome join the adjacencies differently
        # NTS you can actually take the above out and just start with this while loop...
        while len(self.circular_chromosomes) == 0:
            self.state.append(adj1)
            self.state.append(adj2)
            self.state.remove(new_adj1)
            self.state.remove(new_adj2)

            if adj1[0] < adj2[0]:
                new_adj1 = (adj1[0], adj2[0])
            else:
                new_adj1 = (adj2[0], adj1[0])

            if adj1[1] < adj2[1]:
                new_adj2 = (adj1[1], adj2[1])
            else:
                new_adj2 = (adj2[1], adj1[1])

            self.state.remove(adj1)
            self.state.remove(adj2)
            self.state.append(new_adj1)
            self.state.append(new_adj2)

            get_chromosomes = Node_evolve.find_chromosomes(self, self.state)
            self.linear_chromosomes = get_chromosomes[0]
            self.circular_chromosomes = get_chromosomes[1]

        if new_adj1 in self.circular_chromosomes[0]:
            join = new_adj1
            excision = new_adj2
        else:
            join = new_adj2
            excision = new_adj1

        print('trp0:   ', get_adjacencies.adjacencies_to_genome(self.state))

        # decircularization and reinsertion

        adj1_num = random.randint(0, len(self.circular_chromosomes[0]) - 1)
        adj1 = self.circular_chromosomes[0][adj1_num]

        while adj1 == join:
            adj1_num = random.randint(0, len(self.circular_chromosomes[0]) - 1)
            adj1 = self.circular_chromosomes[0][adj1_num]

        chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        while len(self.linear_chromosomes[chrm_num]) == 2:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

        if len(self.linear_chromosomes[chrm_num]) == 3:
            adjacencies = [
                element for element in self.linear_chromosomes[chrm_num]
                if type(element) is tuple
            ]
            adj2 = adjacencies[0]

        else:
            adjacencies = [
                element for element in self.linear_chromosomes[chrm_num]
                if type(element) is tuple
            ]
            adj2_num = random.randint(0, len(adjacencies) - 1)
            adj2 = adjacencies[adj2_num]

        while adj2 == excision:
            chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)
            # if it is a single gene chromosome, choose another chromosome
            while len(self.linear_chromosomes[chrm_num]) == 2:
                chrm_num = random.randint(0, len(self.linear_chromosomes) - 1)

            if len(self.linear_chromosomes[chrm_num]) == 3:
                adjacencies = [
                    element for element in self.linear_chromosomes[chrm_num]
                    if type(element) is tuple
                ]
                adj2 = adjacencies[0]

            else:
                adjacencies = [
                    element for element in self.linear_chromosomes[chrm_num]
                    if type(element) is tuple
                ]
                adj2_num = random.randint(0, len(adjacencies) - 1)
                adj2 = adjacencies[adj2_num]

        if adj1[0] < adj2[1]:
            new_adj1 = (adj1[0], adj2[1])
        else:
            new_adj1 = (adj2[1], adj1[0])

        if adj1[1] < adj2[0]:
            new_adj2 = (adj1[1], adj2[0])
        else:
            new_adj2 = (adj2[0], adj1[1])

        # perfrom operation
        self.state.remove(adj1)
        self.state.remove(adj2)
        self.state.append(new_adj1)
        self.state.append(new_adj2)
        print('trp2:   ', get_adjacencies.adjacencies_to_genome(self.state))
Example #21
0
from Class_extremities_and_adjacencies import Extremities_and_adjacencies
from Class_Network_wrDCJ import Network


from Class_GraphTheory_weighted import GraphTheory

#genomeA = [[1, 2, 3, 5, 6, 4, 7, -8, 9]]
#genomeB = [[1, 2,3 ,4,5,6,7, 8, 9]]
#genomeA = [[1,-3,2,5,6,4,7]]
#genomeB = [[1, 2,3 ,4 , 5, 6, 7]]
#genomeA = [[1,-3,-2, 4, 5,6,9,7], [8, 10],[ 11, 12]]
#genomeB = [[1, 2,3 ,4 , 5, 6, 7], [8, 9, 10,11, 12]]
genomeA = [[1, 6, 7, 4, 5, 2, 3, -8, 9]]
genomeB = [[1, 2,3 ,4,5,6,7, 8, 9]]
#from genes to adjacencies
get_adjacencies = Extremities_and_adjacencies()
adjacencies_genomeA = get_adjacencies.adjacencies_ordered_and_sorted(genomeA)
adjacencies_genomeB = get_adjacencies.adjacencies_ordered_and_sorted(genomeB)

print('Adjacencies of the genomes: ')
print('Genome A: ', adjacencies_genomeA)
print('Genome B: ', adjacencies_genomeB)
print('____________________________________')
print()
print()




#Create start and target node
start_node = Node(adjacencies_genomeA)
Example #22
0
def run(args):
    genomeA_file = args.source_genome
    genomeB_file = args.target_genome
    weight_ratios_file = args.ratios
    stdoutOrigin = sys.stdout
    sys.stdout = open(args.output_file, 'w')
    #outfile = open(args.output_file, 'w')
    with open(genomeA_file) as csv:
        line = [element.strip('\n').split(',') for element in csv]
    genomeA = []

    for element in line:
        element = list(map(int, element))
        genomeA.append(element)

    with open(genomeB_file) as csv:
        line = [element.strip('\n').split(',') for element in csv]
    genomeB = []

    for element in line:
        element = list(map(int, element))
        genomeB.append(element)

    with open(weight_ratios_file) as csv:
        line = [element.strip('\n').split(',') for element in csv]
    weight_ratios = []

    for element in line:
        element = list(map(int, element))
        weight_ratios.append(element)

    get_adjacencies = Extremities_and_adjacencies()
    adjacencies_genomeA = get_adjacencies.adjacencies_ordered_and_sorted(genomeA)


    adjacencies_genomeB = get_adjacencies.adjacencies_ordered_and_sorted(genomeB)

    #Create start and target node
    start_node = Node(adjacencies_genomeA)
    target_node = Node(adjacencies_genomeB)

    hash_table = {}
    hash_key_start = hash(str(start_node.state))
    hash_key_target = hash(str(target_node.state))
    hash_table.update({hash_key_start:start_node})
    hash_table.update({hash_key_target:target_node})

    #finding rearrangement weights
    max_number = max(weight_ratios[0])
    weights = []
    for element in weight_ratios[0]:
        if element == 0:
            weights.append(max_number^2)
        else:
            weights.append(max_number/element)

    New_Network_wrDCJ.build_hash_table(start_node, hash_table, adjacencies_genomeB, weights)

    network = New_Network_wrDCJ.build_network(hash_table)

    shortest_paths = (list(all_shortest_paths(network, start_node, target_node, weight='weight')))

    j = 1
    tot_b_trl = 0
    tot_u_trl = 0
    tot_inv = 0
    tot_trp1 = 0
    tot_trp2 = 0
    tot_fus = 0
    tot_fis = 0

    Paths_state = []
    Paths_state_weight = []
    # print(shortest_paths[0][4].children_weights[2])
    for path in shortest_paths:
        path_state = []
        path_state_weight = []

        i = 0
        while i < len(path):
            current = path[i]
            if i == 0:
                operation_type = 'none, this is the source genome'
                operation_weight = 'N/A'
                operation = 'N/A'
            else:
                x = path[i - 1].children.index(current)

                operation_type = path[i - 1].children_operations[x][1]
                operation_weight = path[i - 1].children_weights[x]
                operation = path[i - 1].children_operations[x][0]

            adjacencies = current.state
            genome = get_adjacencies.adjacencies_to_genome(adjacencies)
            path_state_weight.append((genome, ((operation_type, operation), operation_weight)))

            path_state.append((genome, (operation_type, operation)))

            i += 1
        Paths_state.append((path_state))
        Paths_state_weight.append(path_state_weight)

    for path in shortest_paths:

        i = 0
        b_trl = 0
        u_trl = 0
        inv = 0
        trp1 = 0
        trp2 = 0
        fus = 0
        fis = 0
        while i < len(path):

            current = path[i]
            if i == 0:
                pass
            else:
                x = path[i - 1].children.index(current)
                operation_type = path[i - 1].children_operations[x][1]
                if operation_type == 'b_trl':
                    b_trl += 1
                elif operation_type == 'u_trl':
                    u_trl += 1
                elif operation_type == 'inv':
                    inv += 1
                elif operation_type == 'trp1':
                    trp1 += 1
                elif operation_type == 'trp2':
                    trp2 += 1
                elif operation_type == 'fus':
                    fus += 1
                elif operation_type == 'fis':
                    fis += 1
            i += 1

        tot_b_trl += b_trl
        tot_u_trl += u_trl
        tot_inv += inv
        tot_trp1 += trp1
        tot_trp2 += trp2
        tot_fus += fus
        tot_fis += fis
        j += 1


    print('############################################################################################################')
    print()
    print('Source Genome: ', genomeA)
    print('Target Genome: ', genomeB)
    print()
    print('Number of most parsimonious solutions: ', len(shortest_paths))
    print()
    print('Average number of operations per solution: ', float(tot_inv/len(shortest_paths))+float(tot_trp1/len(shortest_paths))+float(2*(tot_trp2/len(shortest_paths)))+float(tot_b_trl/len(shortest_paths))+float(tot_u_trl/len(shortest_paths))+float(tot_fis/len(shortest_paths))+float(tot_fus/len(shortest_paths)))
    print()
    print('Average number of each operation per solution:')
    print('Inversions: ', float(tot_inv/len(shortest_paths)), '  Transpositions type 1: ', float(tot_trp1/len(shortest_paths)), '  Transpositions type 2: ', float(tot_trp2/len(shortest_paths)), '  Balanced translocations: ', float(tot_b_trl/len(shortest_paths)), '  Unbalanced translocations: ', float(tot_u_trl/len(shortest_paths)),
          '  Fusions: ', float(tot_fus/len(shortest_paths)),
          '  Fissions: ', float(tot_fis/len(shortest_paths)))
    print()
    print()
    print('Solutions: ')
    print()
    path_counter = 1
    for path in Paths_state:
        print('Solution number ', path_counter)
        for genome in path:
            print(genome)
        path_counter+=1
        print()
    print()
    print('############################################################################################################')

    sys.stdout.close()
    sys.stdout=stdoutOrigin