Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template tests #163

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ resolution
- Added seed parameter to `downsample_fastq` component.
- Added bacmet database to `abricate` component.
- Added default docker option to avoid docker permission errors.
- Added tests for `mash_screen`, `mash_dist`, `mapping_patlas` related python
tiagofilipe12 marked this conversation as resolved.
Show resolved Hide resolved
templates
- Changed the default URL generated by inspect and report commands.

### Bug fixes
Expand Down
9 changes: 6 additions & 3 deletions docs/dev/create_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ Versioning and logging
FlowCraft has a specific ``logger``
(:func:`~flowcraft.templates.flowcraft_utils.flowcraft_base.get_logger`) and
versioning system that can be imported from
:mod:`flowcraft.templates.flowcraft_utils`: ::
:mod:`flowcraft.templates.flowcraft_utils` as follows: ::

# the module that imports the logger and the decorator class for versioning
# of the script itself and other software used in the script
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper

try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, MainWrapper

The ``try/except`` is used for test purposes.

Logger
^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/assembly_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
from collections import OrderedDict
from subprocess import PIPE

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/downsample_fastq.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@

from os.path import basename

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/fastqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
from subprocess import PIPE
from os.path import exists, join

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/fastqc_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@

from collections import OrderedDict

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/integrity_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@

from itertools import chain

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
48 changes: 32 additions & 16 deletions flowcraft/templates/mapping2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
import sys
from pympler.asizeof import asizeof

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand All @@ -46,18 +50,21 @@
JSON_LENGTH = '$lengthJson'
CUTOFF = '$cov_cutoff'
SAMPLE_ID = '$sample_id'
else:
DEPTH_TXT = sys.argv[1]
JSON_LENGTH = sys.argv[2]
CUTOFF = sys.argv[3]
SAMPLE_ID = sys.argv[4]

logger.debug("List of arguments given: {}".format([
DEPTH_TXT,
JSON_LENGTH,
CUTOFF,
SAMPLE_ID
]))

try:
logger.debug("List of arguments given: {}".format([
DEPTH_TXT,
JSON_LENGTH,
CUTOFF,
SAMPLE_ID
]))
except NameError:
# this is used for unit tests where this variables are not required to
# test some of the functions within this script
DEPTH_TXT, JSON_LENGTH, CUTOFF, SAMPLE_ID = None, None, None, None
logger.debug("Input variables 'DEPTH_TXT, JSON_LENGTH, CUTOFF and "
"SAMPLE_ID' are not defined. Are you using this for "
"unit tests?")

# check if all variables are assigned
if DEPTH_TXT and JSON_LENGTH and SAMPLE_ID and CUTOFF:
Expand Down Expand Up @@ -119,6 +126,12 @@ def generate_jsons(depth_dic_coverage, plasmid_length, cutoff):
----------
depth_dic_coverage: dict
dictionary with the coverage per position for each plasmid
plasmid_length: dict
dictionary with the correspondence between each reference plasmid and
its length
cutoff: float
the value of percentage identity that is used to discard entries in
the txt file generated by mash screen.

Returns
-------
Expand Down Expand Up @@ -161,14 +174,16 @@ def generate_jsons(depth_dic_coverage, plasmid_length, cutoff):
# array to store the values of coverage for each interval
array_of_cov = []
# the counter that is used to output the values per interval
reset_counter = 0
reset_counter = 1
# loop to generate dict_cov
logger.info("Generating plot data for plasmid: {}".format(ref))
for i in range(int(plasmid_length[ref])):
# checks if key for a given position is in dict and if so
# adds it to array of cov, otherwise it will add a 0
try:
array_of_cov.append(int(depth_dic_coverage[ref][str(i)]))
array_of_cov.append(int(
depth_dic_coverage[ref][str(i + 1)]
))
except KeyError:
array_of_cov.append(0)

Expand All @@ -178,7 +193,8 @@ def generate_jsons(depth_dic_coverage, plasmid_length, cutoff):
int(sum(array_of_cov)/len(array_of_cov))
)
# reset counter
reset_counter = 0
reset_counter = 1
array_of_cov = []
else:
# if counter is less than interval then sums 1
reset_counter += 1
Expand Down
15 changes: 10 additions & 5 deletions flowcraft/templates/mashdist2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
import json
import os

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand All @@ -56,10 +60,8 @@ def send_to_output(master_dict, mash_output, sample_id, assembly_file):
master_dict: dict
dictionary that stores all entries for a specific query sequence
in multi-fasta given to mash dist as input against patlas database
last_seq: str
string that stores the last sequence that was parsed before writing to
file and therefore after the change of query sequence between different
rows on the input file
assembly_file: str
the string with the assembly file name
mash_output: str
the name/path of input file to main function, i.e., the name/path of
the mash dist output txt file.
Expand Down Expand Up @@ -129,6 +131,8 @@ def main(mash_output, hash_cutoff, sample_id, assembly_file):
to the results outputs
sample_id: str
The name of the sample.
assembly_file: str
The name of the assembly file
"""

input_f = open(mash_output, "r")
Expand Down Expand Up @@ -165,6 +169,7 @@ def main(mash_output, hash_cutoff, sample_id, assembly_file):
# assures that file is closed in last iteration of the loop
send_to_output(master_dict, mash_output, sample_id, assembly_file)


if __name__ == "__main__":

main(MASH_TXT, HASH_CUTOFF, SAMPLE_ID, ASSEMBLY_IN)
16 changes: 11 additions & 5 deletions flowcraft/templates/mashscreen2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
import os
import json

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand All @@ -42,9 +46,10 @@
logger.debug("MASH_TXT: {}".format(MASH_TXT))
logger.debug("SAMPLE_ID: {}".format(MASH_TXT))


@MainWrapper
def main(mash_output, sample_id):
'''
"""
converts top results from mash screen txt output to json format

Parameters
Expand All @@ -55,7 +60,7 @@ def main(mash_output, sample_id):
sample_id: str
sample name

'''
"""
logger.info("Reading file : {}".format(mash_output))
read_mash_output = open(mash_output)

Expand Down Expand Up @@ -94,7 +99,7 @@ def main(mash_output, sample_id):
# estimated copy number
copy_number = int(float(v[1]) / median_cutoff)
# assure that plasmid as at least twice the median coverage depth
if float(v[1]) > median_cutoff:
if float(v[1]) >= median_cutoff:
filtered_dic["_".join(k.split("_")[0:3])] = [
round(float(v[0]),2),
copy_number
Expand All @@ -103,7 +108,8 @@ def main(mash_output, sample_id):
"Exported dictionary has {} entries".format(len(filtered_dic)))
else:
# if no entries were found raise an error
logger.error("No matches were found using mash screen for the queried reads")
logger.error("No matches were found using mash screen for the queried "
"reads")

output_json.write(json.dumps(filtered_dic))
output_json.close()
Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/megahit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@

from subprocess import PIPE

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/metaspades.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@

from subprocess import PIPE

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
8 changes: 5 additions & 3 deletions flowcraft/templates/pATLAS_consensus_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
import os
import json

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, MainWrapper

logger = get_logger(__file__)

Expand Down Expand Up @@ -80,12 +83,11 @@ def main(list_of_jsons):

json_dic = {
"patlas_mashscreen": json_dict
# TODO add information for report webapp
}

with open(".report.json", "w") as json_report:
json_report.write(json.dumps(json_dic, separators=(",", ":")))


if __name__ == "__main__":
main(LIST_OF_FILES)
main(LIST_OF_FILES)
6 changes: 5 additions & 1 deletion flowcraft/templates/pipeline_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

from os.path import join

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/process_abricate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@

from subprocess import PIPE

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/process_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@
import json
import operator

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
6 changes: 5 additions & 1 deletion flowcraft/templates/process_assembly_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@
from subprocess import PIPE
from collections import OrderedDict

from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
try:
from flowcraft_utils.flowcraft_base import get_logger, MainWrapper
except ImportError:
from flowcraft.templates.flowcraft_utils.flowcraft_base import get_logger, \
MainWrapper

logger = get_logger(__file__)

Expand Down
Loading