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

Convert print statements to logger.debug statements #31

Merged
merged 6 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ Examples
--------
Download Example Data
`````````````````````
Let's download some example data from `openSNP <https://opensnp.org>`_:
First, let's setup logging to get some helpful output:

>>> import logging, sys
>>> logger = logging.getLogger()
>>> logger.setLevel(logging.DEBUG)
>>> logger.addHandler(logging.StreamHandler(sys.stdout))

Now we're ready to download some example data from `openSNP <https://opensnp.org>`_:

>>> from snps.resources import Resources
>>> r = Resources()
Expand Down
32 changes: 19 additions & 13 deletions src/snps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
# set version string with Versioneer
from snps._version import get_versions

import logging

logger = logging.getLogger(__name__)

__version__ = get_versions()["version"]
del get_versions

Expand Down Expand Up @@ -319,7 +323,7 @@ def _assign_par_snps(self):
break

except Exception as err:
print(err)
logger.warning(err)

def _assign_snp(self, rsid, alleles, chrom):
# only assign SNP if positions match (i.e., same build)
Expand Down Expand Up @@ -607,7 +611,7 @@ def remap_snps(self, target_assembly, complement_bases=True):
snps = self.snps

if snps.empty:
print("No SNPs to remap")
logger.debug("No SNPs to remap")
return chromosomes_remapped, chromosomes_not_remapped
else:
chromosomes = snps["chrom"].unique()
Expand All @@ -616,7 +620,7 @@ def remap_snps(self, target_assembly, complement_bases=True):
valid_assemblies = ["NCBI36", "GRCh37", "GRCh38", 36, 37, 38]

if target_assembly not in valid_assemblies:
print("Invalid target assembly")
logger.debug("Invalid target assembly")
return chromosomes_remapped, chromosomes_not_remapped

if isinstance(target_assembly, int):
Expand Down Expand Up @@ -655,7 +659,7 @@ def remap_snps(self, target_assembly, complement_bases=True):
}
)
else:
print(
logger.debug(
"Chromosome {} not remapped; "
"removing chromosome from SNPs for consistency".format(chrom)
)
Expand Down Expand Up @@ -712,11 +716,13 @@ def _remapper(self, task):
mapped_region = mapping["mapped"]["seq_region_name"]

if orig_region != mapped_region:
print("discrepant chroms")
logger.debug("discrepant chroms")
continue

if orig_range_len != mapped_range_len:
print("discrepant coords") # observed when mapping NCBI36 -> GRCh38
logger.debug(
"discrepant coords"
) # observed when mapping NCBI36 -> GRCh38
continue

# find the SNPs that are being remapped for this mapping
Expand Down Expand Up @@ -898,7 +904,7 @@ def _load_snps_helper(
discrepant_genotypes_threshold,
save_output,
):
print("Loading " + os.path.relpath(file))
logger.debug("Loading " + os.path.relpath(file))
discrepant_positions, discrepant_genotypes = self._add_snps(
SNPs(file),
discrepant_snp_positions_threshold,
Expand Down Expand Up @@ -1044,12 +1050,12 @@ def _add_snps(
source = [s.strip() for s in snps._source.split(",")]

if not snps._build_detected:
print("build not detected, assuming build {}".format(snps._build))
logger.debug("build not detected, assuming build {}".format(snps._build))

if not self._build:
self._build = build
elif self._build != build:
print(
logger.debug(
"build / assembly mismatch between current build of SNPs and SNPs being loaded"
)

Expand All @@ -1073,7 +1079,7 @@ def _add_snps(
prefix = "{}_".format(clean_str(self._name))

if 0 < len(discrepant_positions) < discrepant_snp_positions_threshold:
print(
logger.debug(
"{} SNP positions were discrepant; keeping original positions".format(
str(len(discrepant_positions))
)
Expand All @@ -1089,7 +1095,7 @@ def _add_snps(
),
)
elif len(discrepant_positions) >= discrepant_snp_positions_threshold:
print(
logger.debug(
"too many SNPs differ in position; ensure same genome build is being used"
)
return discrepant_positions, discrepant_genotypes
Expand Down Expand Up @@ -1138,7 +1144,7 @@ def _add_snps(
]

if 0 < len(discrepant_genotypes) < discrepant_genotypes_threshold:
print(
logger.debug(
"{} SNP genotypes were discrepant; marking those as null".format(
str(len(discrepant_genotypes))
)
Expand All @@ -1154,7 +1160,7 @@ def _add_snps(
),
)
elif len(discrepant_genotypes) >= discrepant_genotypes_threshold:
print(
logger.debug(
"too many SNPs differ in their genotype; ensure file is for same "
"individual"
)
Expand Down
8 changes: 6 additions & 2 deletions src/snps/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
import snps
from snps.utils import save_df_as_csv, clean_str

import logging

logger = logging.getLogger(__name__)


class Reader:
""" Class for reading and parsing raw data / genotype files. """
Expand Down Expand Up @@ -155,7 +159,7 @@ def __call__(self):
else:
return pd.DataFrame(), ""
except Exception as err:
print(err)
logger.warning(err)
return pd.DataFrame(), ""

@classmethod
Expand Down Expand Up @@ -686,7 +690,7 @@ def read_vcf(self, file):

# snps does not yet support multi-sample vcf.
if len(vcf_reader.samples) > 1:
print(
logger.debug(
"Multiple samples detected in the vcf file, please use a single sample vcf."
)
return df, "vcf"
Expand Down
22 changes: 13 additions & 9 deletions src/snps/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
from snps.ensembl import EnsemblRestClient
from snps.utils import create_dir, Singleton

import logging

logger = logging.getLogger(__name__)


class Resources(metaclass=Singleton):
""" Object used to manage resources required by `snps`. """
Expand Down Expand Up @@ -131,7 +135,7 @@ def get_reference_sequences(
valid_assemblies = ["NCBI36", "GRCh37", "GRCh38"]

if assembly not in valid_assemblies:
print("Invalid assembly")
logger.debug("Invalid assembly")
return {}

if not self._reference_chroms_available(assembly, chroms):
Expand Down Expand Up @@ -297,7 +301,7 @@ def _load_assembly_mapping_data(filename):

return assembly_mapping_data
except Exception as err:
print(err)
logger.warning(err)
return {}

def _get_paths_reference_sequences(
Expand Down Expand Up @@ -472,14 +476,14 @@ def _get_path_assembly_mapping_data(
if not os.path.exists(destination) or not self._all_chroms_in_tar(
chroms, destination
):
print("Downloading {}".format(os.path.relpath(destination)))
logger.debug("Downloading {}".format(os.path.relpath(destination)))

try:
self._download_assembly_mapping_data(
destination, chroms, source_assembly, target_assembly, retries
)
except Exception as err:
print(err)
logger.warning(err)
return ""

return destination
Expand Down Expand Up @@ -527,7 +531,7 @@ def _all_chroms_in_tar(self, chroms, filename):
if chrom + ".json" not in members:
return False
except Exception as err:
print(err)
logger.warning(err)
return False

return True
Expand All @@ -554,7 +558,7 @@ def _load_codigo46_resources(self, rsid_map, chrpos_map):

return d
except Exception as err:
print(err)
logger.warning(err)
return {}

def _get_path_codigo46_rsid_map(self):
Expand Down Expand Up @@ -613,7 +617,7 @@ def _download_file(self, url, filename, compress=False, timeout=30):
else:
f.write(data)
except urllib.error.URLError as err:
print(err)
logger.warning(err)
destination = ""
# try HTTP if an FTP error occurred
if "ftp://" in url:
Expand All @@ -624,7 +628,7 @@ def _download_file(self, url, filename, compress=False, timeout=30):
timeout=timeout,
)
except Exception as err:
print(err)
logger.warning(err)
return ""

return destination
Expand All @@ -638,7 +642,7 @@ def _print_download_msg(path):
path : str
path to file being downloaded
"""
print("Downloading " + os.path.relpath(path))
logger.debug("Downloading " + os.path.relpath(path))


class ReferenceSequence:
Expand Down
11 changes: 7 additions & 4 deletions src/snps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import pandas as pd

import snps
import logging

logger = logging.getLogger(__name__)


class Parallelizer:
Expand Down Expand Up @@ -106,7 +109,7 @@ def create_dir(path):
try:
os.makedirs(path, exist_ok=True)
except Exception as err:
print(err)
logger.warning(err)
return False

if os.path.exists(path):
Expand Down Expand Up @@ -156,7 +159,7 @@ def save_df_as_csv(
destination = filename
else:
destination = os.path.join(path, filename)
print("Saving " + os.path.relpath(destination))
logger.debug("Saving " + os.path.relpath(destination))

if prepend_info:
s = (
Expand Down Expand Up @@ -190,10 +193,10 @@ def save_df_as_csv(

return destination
except Exception as err:
print(err)
logger.warning(err)
return ""
else:
print("no data to save...")
logger.debug("no data to save...")
return ""


Expand Down