Commit a738d017 authored by Lucianna's avatar Lucianna

Refactor + Stability improvement + cleanup for map_aic and util

parent 2128b85b
Pipeline #27 failed with stages
in 9 seconds
import os
import sys
mypath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.abspath(mypath + '/../'))
# mapAIC.py
#
#
# Reads weights from a file, maps them to codons in arbitrary strings of mRNA
#
# Author: Lucianna Osucha (email:lucianna@vulpinedesigns.com)
# Author: Lucianna Osucha (lucianna@vulpinedesigns.com)
from __init__ import *
import sys
sys.path.append('../')
from util.mRNA import *
from util import mRNA
from struct import *
try:
m_weights = open("codonWeights.dat", "rb")
m_weights = open(mypath + "/codonWeights.dat", "rb")
except OSError as e:
print("[!!FATAL!!] Error opening \"codonWeights.dat\"\n"
, file=sys.stderr)
......@@ -20,10 +21,10 @@ except OSError as e:
def mapAICs(rna:mRNA) -> mRNA:
def mapAICs(rna:mRNA.mRNA) -> mRNA.mRNA:
for i in rna.code:
m_weights.seek(i * 8)
rna.baseWeights.append(unpack('<d', m_weights.read(8)))
rna.baseWeights.append(unpack('<d', m_weights.read(8))[0])
return rna
......@@ -5,26 +5,32 @@
# Author: Lucianna Osucha (email:lucianna@vulpinedesigns.com)
from __init__ import *
import sys
import argparse
from struct import *
from util.mRNA import *
from util import mRNA
filename = mypath + '/codonWeights.dat'
try:
m_file = open('codonWeights.dat','r+b')
m_file = open(filename,'r+b')
except OSError as e:
print("\"codonWeights.dat\" not found!\n"
print("\"" + filename + "\" not found!\n"
+ "Constructing new file...", file=sys.stderr)
m_file = open('codonWeights.dat','x+b')
m_file = open(filename,'x+b')
# Pack 64 doubles, all set to 0.0, into a bytearray and write to the file
m_file.write(bytearray(pack('<' + 'd'*64, *([0.0]*64))))
sys.exit()
parser = argparse.ArgumentParser(description="A helper utility for populating "
+ "the \"codonWeights.dat\" file"
+ "the \"" + filename + "\" file"
, epilog="File Formatting Example:\n\nAUG 1000\nCUG 140\nAUC 15.3"
, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('infile', nargs='*', type=argparse.FileType('r')
, help="Read weights stored in a file and update \"codonWeights.dat\" with "
, help="Read weights stored in a file and update \""+ filename + "\" with "
+ "the new data. Leave empty to print out currently stored data")
......@@ -34,7 +40,7 @@ args = parser.parse_args()
if args.infile == []:
for i in range(64):
print(codonIndex(i) + " ", *unpack('<d', m_file.read(8)))
print(mRNA.deindexCodon(i) + " ", *unpack('<d', m_file.read(8)))
sys.exit()
......@@ -44,5 +50,5 @@ for n_file in args.infile:
codon = n_file.read(4)
if codon == None or codon == "":
break
m_file.seek(indexCodon(codon) * 8)
m_file.seek(mRNA.indexCodon(codon) * 8)
m_file.write(bytearray(pack('<d', float(n_file.readline()))))
# AminoAcids.py
#
# Utils for dealing with Amino Acid Chains
#
# Authored by: Lucianna Osucha (email:lucianna@vulpinedesigns.com)
from mRNA import *
const aminoAlphabet:str = '*RHKDESTNQCUGPAVILMFYW'
const aminoCodons:list = [3, 8, 3, 8, 16, 16, 18, 16, 1, 6, 1, 6, 7, 7, 7, 7, 0, 20
, 0, 20, 17, 19, 17, 19, 0, 10, 21, 10, 6, 6, 6, 6, 5, 4, 5, 4, 15, 15, 15
, 15, 12, 12, 12, 12, 14, 14, 14, 14, 9, 2, 9, 2, 17, 17, 17, 17, 1, 1, 1, 1
, 13, 13, 13, 13]
class AminoAcidChain:
def __init__(self, seq:list[int]):
self.sequence:list[int] = seq
self.length:int = len(seq)
self.postTranslationalModificationSites:list = []
def __init__(self, seq:str):
self.__init__([aminoAlphabet.index(i) for i in seq])
......@@ -6,7 +6,7 @@
from mRNA import *
import mRNA
metadataTypes:list = ['originMRNA', 'locFlags']
......@@ -58,7 +58,7 @@ class AminoAcidChain:
@classmethod
def fromMRNA(cls, seq):
if isinstance(seq, mRNA):
if isinstance(seq, mRNA.mRNA):
seq = seq.code
if isinstance(seq, str):
seq = indexRNA(seq)
......
......@@ -3,11 +3,10 @@
# Collection of utilities for general use in the INITIATOR_SET module of the
# GUESS
#
# Authorship:
# -Lucianna Osucha (lucianna@vulpinedesigns.com)
# Author: Lucianna Osucha (lucianna@vulpinedesigns.com)
**Contents:**
-*mRNA.py* Utils for dealing with strands of mRNA
-*AminoAcids.py* Utils for dealing with chains of Amino Acids
-*AminoChain.py* Utils for dealing with chains of Amino Acids
......@@ -11,7 +11,7 @@ bases = ['A', 'U', 'G', 'C'];
def codonIndex(index:int) -> str:
def deindexCodon(index:int) -> str:
result = bases[int(index/16)]
result += bases[int((index/4))%4]
result += bases[index%4]
......@@ -19,7 +19,7 @@ def codonIndex(index:int) -> str:
def rnaIndex(code:list) -> str:
def deindexRNA(code:list) -> str:
result = ''
for i in range (0, len(code), 3):
result = result + codonIndex(code[i])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment