__author__ = 'meta'

import xml.etree.cElementTree as ET
from random import shuffle
import random
import os


class plagueVirus():

    def concat(args):
        string = ""
        for each in args:
            string += str(each)
        return string

    def atrophyMuscles(self):
        """Mutates muscle tissue voxels according to some probability in each layer
        """
        for dna_file in os.listdir("./population"):
            if dna_file.endswith(".vxa"):
                print dna_file

                tree = ET.ElementTree(file="./population/" + dna_file)
                root = tree.getroot()
                layers = root.find('VXC').find('Structure').find('Data').findall('Layer')
                probability = 0.1

                dna_list = ""
                for layer in layers:
                    dna_list = list(layer.text.strip())
                    index_list = list(range(0,99))
                    shuffle(index_list)
                    mutations_count = 5
                    position_to_check = 0
                    for i in range(mutations_count):
                        found_mutation = False
                        not_found = False

                        while not found_mutation:
                            index_number = index_list[position_to_check]
                            tissue = dna_list[index_number]
                            if tissue == '3' or tissue == '4':
                                if random.random() < probability:
                                    dna_list[index_number] = '2'
                                    print "Atrophied a muscle at index " + str(index_number) + " to " + str(dna_list[index_number]) + "."
                                else:
                                    print "Muscle at index " + str(index_number) + " unchanged."
                                    pass
                                found_mutation = True
                            else:
                                position_to_check += 1
                                # print "Trying next index.."
                                if position_to_check == len(index_list):
                                    not_found = True
                                    # print "I could not find a muscle voxel!"
                                    break
                        if not_found:
                            print "I could not find a muscle voxel!"
                            break
                    layer.text = self.concat(dna_list)
                    tree.write("./population/" + dna_file)

    def initialize(self):
        self.atrophyMuscles()

virus = plagueVirus()
virus.initialize()
@@ -140,16 +140,22 @@
  concat(
+self, 
 args):%0A 
@@ -739,10 +739,8 @@
 y = 
-0.
 1%0A%0A 
@@ -865,139 +865,8 @@
 ())%0A
-                    index_list = list(range(0,99))%0A                    shuffle(index_list)%0A                    mutations_count = 5%0A
     
@@ -928,16 +928,20 @@
    for i
+ndex
  in rang
@@ -946,243 +946,24 @@
 nge(
-mutations_count):%0A                        found_mutation = False%0A                        not_found = False%0A%0A                        while not found_mutation:%0A                            index_number = index_list%5Bposition_to_check%5D%0A    
+len(dna_list)):%0A
     
@@ -1009,20 +1009,10 @@
 ndex
-_number%5D%0A   
+%5D%0A
     
@@ -1031,17 +1031,16 @@
         
- 
 if tissu
@@ -1094,20 +1094,16 @@
         
-    
 if rando
@@ -1114,16 +1114,17 @@
 ndom() %3C
+=
  probabi
@@ -1153,36 +1153,32 @@
                 
-  
-  
 dna_list%5Bindex_n
@@ -1179,27 +1179,16 @@
 ndex
-_number
 %5D = '
-2
+-
 '%0A
-    
     
@@ -1259,31 +1259,24 @@
  + str(index
-_number
 ) + %22 to %22 +
@@ -1294,23 +1294,16 @@
 st%5Bindex
-_number
 %5D) + %22.%22
@@ -1327,38 +1327,30 @@
             
-    
 else:%0A
-    
             
@@ -1409,15 +1409,8 @@
 ndex
-_number
 ) + 
@@ -1459,609 +1459,81 @@
     
-    
 pass%0A
-                                found_mutation = True%0A                            else:%0A                                position_to_check += 1%0A                                # print %22Trying next index..%22%0A                                if position_to_check == len(index_list):%0A                                    not_found = True%0A                                    # print %22I could not find a muscle voxel!%22%0A                                    break%0A                        if not_found:%0A                            print %22I could not find a muscle voxel!%22%0A                            break
+%0A                        else:%0A                            continue%0A
 %0A   

remove old redundant code, simplify probability function