Closed XeniaGabriela closed 3 years ago
Example on how to use the Timer() class in another class:
# -*- coding: utf-8 -*-
from timer import Timer
from config import config
from bin_map import BinGenome
from pathlib import Path
class AlgebraicGenome(object):
def __init__(self):
self.timer = Timer()
self.binary_4base = BinGenome().binary_4base_dict
# TODO: Find mistake, where is a 4 fold repetition????
def read_genome(self, text_file):
self.timer.start()
genome_lines = []
genome_bytes = b""
with open(text_file, "r") as f:
four_line_string = ""
line_number = 0
# iterate through lines in text file (line end defined by \n)
for line in f:
# omit first line with file declaration
if not line[0] == ">" and len(line) > 3:
line_number += 1
four_line_string += line.rstrip()
if line_number % 4 == 0:
genome_lines.append(four_line_string)
four_line_string = ""
print(len(genome_lines))
for genome_line in genome_lines:
char_pos = 0
# add converted character groups to binary string
while char_pos < len(genome_line):
character_quadruple = genome_line[char_pos:char_pos + 4]
genome_bytes += self.binary_4base[character_quadruple]
char_pos += 4
# write bytes to binary file
with open(Path(str(text_file).replace('.fa', '') + '_binary.fa'), 'wb') as new_file:
print(new_file)
new_file.write(genome_bytes)
new_file.close()
self.timer.stop()
return genome_bytes
Create file timer.py with