diff --git a/codes/counter.asm b/codes/counter.asm new file mode 100644 index 0000000..9e4443e --- /dev/null +++ b/codes/counter.asm @@ -0,0 +1,8 @@ +define: + 0 [ABC][DE] + 1 [AB][CDE] + +data: + 0001111 + +instructions: diff --git a/rule110.asm b/codes/rule110.asm similarity index 88% rename from rule110.asm rename to codes/rule110.asm index d8d19fa..ee7630a 100644 --- a/rule110.asm +++ b/codes/rule110.asm @@ -9,6 +9,6 @@ instructions: {D*E*A*F*} # mark 01 {D*E*A*B*C*G*} # mark 11 {DEABCG} # remove mark 11 - {A*B*C*} {D*E*} # write 0 + {A*B*C*}{D*E*} # write 0 {DEAF} # remove mark 01 {B*C*D*E*} # write 1 \ No newline at end of file diff --git a/dnasimd b/dnasimd new file mode 120000 index 0000000..a8c92b0 --- /dev/null +++ b/dnasimd @@ -0,0 +1 @@ +src/main.py \ No newline at end of file diff --git a/src/SIMDDNA/__init__.py b/src/SIMDDNA/__init__.py index afff55f..637c328 100644 --- a/src/SIMDDNA/__init__.py +++ b/src/SIMDDNA/__init__.py @@ -1,3 +1,3 @@ -import ascii as ascii -import molecule as molecule -import register as register \ No newline at end of file +from . import ascii as ascii +from . import molecule as molecule +from . import register as register \ No newline at end of file diff --git a/src/SIMDDNA/ascii.py b/src/SIMDDNA/ascii.py index fbd22a1..94f908f 100644 --- a/src/SIMDDNA/ascii.py +++ b/src/SIMDDNA/ascii.py @@ -3,8 +3,8 @@ # @autor Lukáš Plevač # @brief defines function from print structions in ASCII reprezentation -from molecule import isComplementary -from molecule import nothing +from .molecule import isComplementary +from .molecule import nothing ## # Return number of bases to bind with main chain 0 diff --git a/src/SIMDDNA/assembly.py b/src/SIMDDNA/assembly.py index 7170ad9..71ccb97 100644 --- a/src/SIMDDNA/assembly.py +++ b/src/SIMDDNA/assembly.py @@ -1,9 +1,100 @@ +import re +from . import molecule + class Assembly: def __init__(self, asm): - # remove whitespaces - asm = asm.strip() # load macros from file self.macros = self.parseMacros(asm) + self.data = self.parseData(asm) + self.ins = self.parseInstructions(asm) + + def getMacros(self): + return self.macros + + def getData(self): + return self.data + + def getInstructions(self): + return self.ins def parseMacros(self, asm): - \ No newline at end of file + asm = asm.splitlines() + + is_in = False + macros = [] + for ins in asm: + ins = re.sub("\s+" , " ", ins.strip()) # remove whitespaces + ins = ins.split("#")[0] # remove comments + + if "define:" in ins: + is_in = True + elif ":" in ins and is_in: + break + elif is_in: + name = ins.split(" ")[0] + + if not name.isspace() and len(name) > 0: + val = ins.split(" ")[1] + + macros.append([name, val]) + + return macros + + def useMacros(self, text): + for macro in self.macros: + text = text.replace(macro[0], macro[1]) + + return text + + def parseData(self, asm): + asm = asm.splitlines() + + is_in = False + datas = [] + for ins in asm: + ins = re.sub("\s+" , " ", ins.strip()) # remove whitespaces + ins = ins.split("#")[0] # remove comments + + if "data:" in ins: + is_in = True + elif ":" in ins and is_in: + break + elif is_in: + reg = ins.split(" ") + + if len(reg) > 1: + print("WARNING: ASM parsing data molecule with space. All after space is ignored!") + + reg = reg[0] + + if not reg.isspace() and len(reg) > 0: + datas.append(molecule.parse(self.useMacros(reg))) + + return datas + + def parseInstructions(self, asm): + asm = asm.splitlines() + + is_in = False + gins = [] + for ins in asm: + ins = re.sub("\s+" , " ", ins.strip()) # remove whitespaces + ins = ins.split("#")[0] # remove comments + + if "instructions:" in ins: + is_in = True + elif ":" in ins and is_in: + break + elif is_in: + DNAins = ins.split(" ") + + DNAInsArray = [] + for DNAin in DNAins: + if not DNAin.isspace() and len(DNAin) > 0: + DNAInsArray.append(molecule.parse(self.useMacros(DNAin))) + + if len(DNAInsArray) > 0: + gins.append(DNAInsArray) + + return gins + diff --git a/src/SIMDDNA/register.py b/src/SIMDDNA/register.py index d7d5949..d63357a 100644 --- a/src/SIMDDNA/register.py +++ b/src/SIMDDNA/register.py @@ -3,8 +3,8 @@ # @autor Lukáš Plevač # @brief implments REGISTER from SIMD|DNA -import molecule -import ascii +from . import molecule +from . import ascii class Register: ## @@ -125,11 +125,11 @@ class Register: for pos in range(len(self.mol)): if molecule.isComplementary(self.mol.getBase(chainI, pos), self.mol.getBase(0, pos)) and self.mol.bindedCountAt(pos) == 1: # binde minimaly once bindScore += 1 - else: - bindScore = 0 - finalBindScore = max(finalBindScore, bindScore) + elif not molecule.isComplementary(self.mol.getBase(chainI, pos), self.mol.getBase(0, pos)): + finalBindScore = max(bindScore, finalBindScore) + bindScore = 0 - if finalBindScore < 2: + if max(bindScore, finalBindScore) < 2: self.mol.removeChain(chainI) done = False break diff --git a/src/main.py b/src/main.py old mode 100644 new mode 100755 index 2a4b4fd..4cf7a11 --- a/src/main.py +++ b/src/main.py @@ -1,48 +1,72 @@ +#!/usr/bin/env python3 from SIMDDNA.register import Register -from SIMDDNA import molecule from SIMDDNA.assembly import Assembly +from SIMDDNA.ascii import showMolecule +import argparse -asm = Assembly( -""" -define: - 0 [ABC][DE] - 1 {A}[BCDE] +parser = argparse.ArgumentParser(description='DNA|SIMD python simulator POC1') +parser.add_argument('assembly') +parser.add_argument('-s', '--spaceing', default=" ", help='space sentense between ascii char of DNA strands') +parser.add_argument('-v', '--verbose', help='show simulation step by step not only final', action='store_true', default=False) +parser.add_argument('-d', '--decode', help='use macros to decode final result', action='store_true', default=False) -data: - 1001111010 +args = parser.parse_args() -instructions: - {D*E*A*F*} # mark 01 - {D*E*A*B*C*G*} # mark 11 - {DEABCG} # remove mark 11 - {A*B*C*} {D*E*} # write 0 - {DEAF} # remove mark 01 - {B*C*D*E*} # write 1 -""" -) +# Open a file +file = open(args.assembly, mode='r') +asm = file.read() +file.close() + +asm = Assembly(asm) print("=================================") print("| Inital state |") print("=================================") -print("\n") +print("") regs = [] for data in asm.getData(): - regs.append(Register(molecule.parse(data))) - regs[-1].asciiShow(spaceing = " ") + regs.append(Register(data)) + regs[-1].asciiShow(spaceing = args.spaceing) iId = 0 for ins in asm.getInstructions(): - print("=================================") - print(f"| Instruction {iId} |") - print("=================================") - print("\n") + + if args.verbose: + print("") + print("=================================") + print(f"| Instruction {iId} |") + print("=================================") + print() + + for insc in ins: + insc.rawPrint() + + print() + print("Registers") + print("--------------------------------") + print() for reg in regs: reg.instruction(ins) - reg.asciiShow(spaceing = " ") + + if args.verbose: + reg.asciiShow(spaceing = args.spaceing) - iId += 1 \ No newline at end of file + iId += 1 + +print("") +print("=================================") +print("| FINAL state |") +print("=================================") +print("") + +for reg in regs: + reg.asciiShow(spaceing = args.spaceing) + +if args.decode: + # todo: implement it + pass \ No newline at end of file