Skip to content
Snippets Groups Projects
Commit 7549c162 authored by Kathleen Moriarty's avatar Kathleen Moriarty
Browse files

fix: python good practices

parent 12ef1d8e
No related branches found
No related tags found
1 merge request!17Issue 7
...@@ -3,13 +3,17 @@ ...@@ -3,13 +3,17 @@
Simulate the sequencing of reads on the template of terminal fragments and simulates reads of these fragments. Simulate the sequencing of reads on the template of terminal fragments and simulates reads of these fragments.
Author: Kathleen Moriarty Author: Kathleen Moriarty
""" """
# Imports from built-in modules
from random import choices
from typing import List
from pathlib import Path
def read_sequencing( def read_sequencing(
frag_file_name, frag_file_name: Path,
output_file_name, output_file_name: Path = Path.cwd() / 'output_reads.txt',
num_reads, num_reads: int = 1000,
read_len, read_len: int = 80,
) -> None: ) -> None:
"""Reads a fasta-formatted file of terminal fragments and simulates reads. """Reads a fasta-formatted file of terminal fragments and simulates reads.
...@@ -24,18 +28,15 @@ def read_sequencing( ...@@ -24,18 +28,15 @@ def read_sequencing(
ends of the terminal fragments as .txt. ends of the terminal fragments as .txt.
Args: Args:
frag_file_name (string): input file of terminal fragments frag_file_name: input file path of terminal fragments
output_file_name (string): file name where to store the output output_file_name: output file path where to store the output
num_reads: number of total reads to simulate num_reads: number of total reads to simulate
read_len: integer of identical read length read_len: integer of identical read length
""" """
# Import classes
from random import choices
from typing import List
# Read data from terminal fragment file # Read data from terminal fragment file
# Store fragment descriptions in a list # Store fragment descriptions in a list
frag_desc = [] # type: List[str] frag_desc = [] # type: List[str]
with open(frag_file_name, 'r') as f: with open(frag_file_name, 'r') as f:
frag_line = f.readline() frag_line = f.readline()
...@@ -93,9 +94,4 @@ def read_sequencing( ...@@ -93,9 +94,4 @@ def read_sequencing(
# Write read to file and original fragment description # Write read to file and original fragment description
fw.write(frag_desc[frag_list.index(frag)]) fw.write(frag_desc[frag_list.index(frag)])
fw.write(tmp_read + "\n") fw.write(tmp_read + "\n\n")
read_sequencing(frag_file_name = "../tests/resources/test_terminal_fragments.txt",
output_file_name = "reads.txt",
num_reads=90,
read_len=10)
\ No newline at end of file
"""Test Issue 7.""" """Test Read Sequencing Functionality."""
# imports from built-in modules
from pathlib import Path
# imports from third-party modules
import pandas as pd import pandas as pd
# imports from _this_ package
from src.read_sequencing import read_sequencing from src.read_sequencing import read_sequencing
def test_read_sequencing(tmpdir): def test_read_sequencing(tmpdir):
"""Tests the correct number of reads were generated.""" """Tests the correct number of reads were generated."""
read_sequencing( read_sequencing(
frag_file_name='./tests/resources/test_terminal_fragments.txt', frag_file_name=Path('./tests/resources/test_terminal_fragments.txt'),
num_reads=80, num_reads=80,
read_len=10, read_len=10,
output_file_name=tmpdir / 'reads.txt' output_file_name=tmpdir / 'reads.txt'
) )
df_out = pd.read_table(tmpdir / 'reads.txt', header=None) df_out = pd.read_table(tmpdir / 'reads.txt', header=None)
assert df_out.shape[0] == 80 assert df_out.shape[0] == 80 * 3
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment