Módulo util.rw

  1. read_file(file_path)
  2. write_file(string, file_path)
  3. read_genbank(file_path)
  4. write_genbank(record, file_path)
  5. read_blast(file_path)
  6. write_blast(handle, file_path)
  7. read_json(file_path)
  8. write_json(dictionary, file_path)
  9. wrap_file(start_line, end_line, file_path)
  10. truncate_file(start, end, file_path)
  11. filter_file(regex, file_path)

A ideia deste módulo é permitir a leitura e escrita de ficheiros em diferentes formatos. Para todos os formatos, as funções respeitam a seguinte propriedade:


read(write(s)) = s

Nos ficheiros, pode ser feita a leitura e escrita de:

  • string
  • genbank
  • blast
  • json

def read_file(file_path):
    """
    Lê file_path e retorna uma string com o conteúdo do ficheiro.
    """
    fd = open(file_path, "r")
    string = fd.read()
    fd.close()
    return string

def write_file(string, file_path):
    """
    Grava a string em file_path
    """
    fd = open(file_path, "w")
    fd.write(string)
    os.fsync(fd)
    fd.close()

def read_genbank(file_path):
    """
    Lê file_path e retorna um record genbank.
    """
    return SeqIO.read(file_path, "gb")

def write_genbank(record, file_path):
    """
    Grava o record em file_path no formato genbank.
    """
    SeqIO.write(record, file_path, "gb")

def read_blast(file_path):
    """
    Lê file_path e retorna uma lista de blast records.
    """
    fd = open(file_path, "r")
    record = NCBIXML.read(fd)
    return record

def write_blast(handle, file_path):
    """
    Grava o blast record em file_path.
    """
    fd = open(file_path, "w")
    fd.write(handle.read())
    os.fsync(fd)
    fd.close()

def read_json(file_path):
    """
    Lê file_path e retorna dicionário.
    """
    fd = open(file_path, "r")
    dictionary = json.load(fd)
    fd.close()

    return dictionary

def write_json(dictionary, file_path):
    """
    Grava o dicionário em file_path no formato json.
    """
    fd = open(file_path, "w")
    json.dump(dictionary, fd, sort_keys=True, indent=2)
    os.fsync(fd)
    fd.close()

Por fim, mais três funções para edição de ficheiros:

  • wrap_file para adicionar linhas no início e fim de um ficheiro
  • truncate_file para remover linhas do início e fim de um ficheiro
  • filter_file para remover linhas de um ficheiro que fazem match com uma dada expressão regular
def wrap_file(start_line, end_line, file_path):
    """
    Adiciona uma linha no início do ficheiro e outra no fim.
    """
    fd = open(file_path, "r")
    lines = [start_line] + fd.readlines() + [end_line]
    fd.close()
    fd = open(file_path, "w")
    fd.writelines(lines)
    os.fsync(fd)
    fd.close()

def truncate_file(start, end, file_path):
    """
    Remove as 'start' primeiras linhas
    e as 'end' últimas linahs de um ficheiro.
    """
    fd = open(file_path, "r")
    lines = fd.readlines()
    fd.close()
    fd = open(file_path, "w")
    fd.writelines(lines[start:-end])
    os.fsync(fd)
    fd.close()

def filter_file(regex, file_path):
    """
    Remove as linhas que fazem match com o regex
    passado como argumento.
    """
    pattern = re.compile(regex)

    fd = open(file_path, "r")
    lines = fd.readlines()
    fd.close()
    new_lines = filter(lambda l : not pattern.search(l), lines)
    fd = open(file_path, "w")
    fd.writelines(new_lines)
    os.fsync(fd)
    fd.close()