diff --git a/cdna/cdna.py b/cdna/cdna.py index e6c5720ed4996edd5c785fbd22638a8c61376738..1796db57e00671ab0227ecd91768b5ada9bb2a80 100644 --- a/cdna/cdna.py +++ b/cdna/cdna.py @@ -1,5 +1,6 @@ """cDNA generator.""" import warnings +from typing import Optional, List, Dict, Any import pandas as pd from Bio import SeqIO from Bio.Seq import Seq @@ -21,13 +22,13 @@ def complement(res: str) -> str: """ translate_dict = {"A": "T", "T": "A", "U": "A", "G": "C", "C": "G"} - if res not in translate_dict.keys(): + if res not in translate_dict: print(f"Unknown character, {res}") raise ValueError return translate_dict[res] -def seq_complement(sequence: str) -> str or None: +def seq_complement(sequence: str) -> Optional[str]: """Return the corresponding cDNA sequence by finding the complementary \ base pairs and returning the reversed sequence. @@ -58,10 +59,10 @@ class CDNAGen: self.output_csv = ocsv # variables - self.csv_df = None - self.fasta_dict = None - self.fasta_records = None - self.gtf_df = None + self.csv_df = pd.DataFrame() + self.fasta_dict: Dict[str, Any] = {} + self.fasta_records: List[SeqRecord] = [] + self.gtf_df = pd.DataFrame() self.run() def run(self) -> None: @@ -118,8 +119,8 @@ class CDNAGen: """ self.gtf_df["complement"] = self.gtf_df["priming_site"].apply( - lambda x: seq_complement(x) - ) + seq_complement + ) def read_primingsite(self, sequence: str, end: int) -> None: """Read a fasta file from a given start character. @@ -159,7 +160,7 @@ class CDNAGen: """ df_csv = pd.read_csv(self.cpn, index_col=False) - df_csv = df_csv.reset_index() # make sure indexes pair with number of rows # noqa: E501 + df_csv = df_csv.reset_index() # make sure indexes pair with number of rows # noqa: E501 self.csv_df = df_csv def read_gtf(self) -> None: @@ -232,8 +233,7 @@ class CDNAGen: Returns: None """ - self.gtf_df[["cdna_ID", "Transcript_Copy_Number"]].to_csv( - self.output_csv, index=False - ) + df_to_save = self.gtf_df[["cdna_ID", "Transcript_Copy_Number"]] + df_to_save.to_csv(self.output_csv, index=False) print(f"Copy number csv file successfully written to: \ {self.output_csv}") diff --git a/cdna/cli.py b/cdna/cli.py index 86f1babf669c3aeadb136922d036ca442eeef8a1..ce568d8548cc1e058c77204f90c600b0f1fcb17b 100644 --- a/cdna/cli.py +++ b/cdna/cli.py @@ -1,15 +1,17 @@ +"""Receive command line arguments.""" + import argparse import logging -from cdna import CDNAGen +from cdna.cdna import CDNAGen -def cdna_parser() -> None: - """Parser for cDNA generator +def cdna_parser() -> CDNAGen: + """Parse sequences for cDNA generator. Parses command line arguments for cDNA generation. - Returns: None + Returns: CDNAGen instance """ parser = argparse.ArgumentParser( @@ -48,7 +50,8 @@ def cdna_parser() -> None: if __name__ == "__main__": logging.basicConfig( - format='[%(asctime)s: %(levelname)s] %(message)s (module "%(module)s")', + format='[%(asctime)s: %(levelname)s] %(message)s \ + (module "%(module)s")', level=logging.INFO, ) LOG = logging.getLogger(__name__) diff --git a/tests/test_cdna.py b/tests/test_cdna.py index 5ad8caa2b55d5159d1e2a90591a8d07f2ab5cdb1..102f4238186c483d1f71c487e4820bb27e9b750f 100644 --- a/tests/test_cdna.py +++ b/tests/test_cdna.py @@ -1,19 +1,25 @@ -# imports +"""Tests for cDNA functions.""" import pytest from cdna.cdna import complement, seq_complement + @pytest.mark.parametrize( "test_input,expected", [("A", "T")] ) -def test_complement_param(test_input, expected): # we need to pass the lists to the test function... +# we need to pass the lists to the test function... +def test_complement_param(test_input, expected): + """Test complement() function.""" assert complement(test_input) == expected + @pytest.mark.parametrize( "test_input,expected", [("AA", "TT")] ) -def test_seq_complement_param(test_input, expected): # we need to pass the lists to the test function... +# we need to pass the lists to the test function... +def test_seq_complement_param(test_input, expected): + """Test seq_complement() function.""" assert seq_complement(test_input) == expected @@ -23,13 +29,16 @@ def test_seq_complement_param(test_input, expected): # we need to pass the list [(1, ValueError)] ) def test_complement_param_failing(test_input, expected): + """Test complement() fail function.""" with pytest.raises(expected): complement(test_input) + @pytest.mark.parametrize( "test_input,expected", [("11", ValueError)] ) -def test_complement_param_failing(test_input, expected): +def test_seq_complement_param_failing(test_input, expected): + """Test seq_complement() fail function.""" with pytest.raises(expected): seq_complement(test_input)