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

Merge from dev #41

Merged
merged 11 commits into from
Jan 16, 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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ install:
- pip install .[r]

script:
- pytest --cov ./ test/test_*.py
- pytest --disable-warnings --show-capture=no --cov ./ --cov-report term --cov-report xml test/test_*.py

after_success:
- codecov
- codecov -f coverage.xml
- python-codacy-coverage -r coverage.xml
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.DEFAULT_GOAL := pypitest

test3:
python3 -m pytest -n 3 --disable-warnings --show-capture=no --cov ./ test/test_*.py
python3 -m pytest -n 3 --disable-warnings --show-capture=no --cov=ngs_toolkit --cov-report xml test/test_*.py --lf

test2:
python2 -m pytest -n 3 --disable-warnings --show-capture=no --cov ./ test/test_*.py
python2 -m pytest -n 3 --disable-warnings --show-capture=no --cov=ngs_toolkit --cov-report xml test/test_*.py --lf

test: test3

coverage: test
codecov
codecov -f coverage.xml
python-codacy-coverage -r coverage.xml

build: test
Expand Down
636 changes: 636 additions & 0 deletions ngs_toolkit/analysis.py

Large diffs are not rendered by default.

37 changes: 15 additions & 22 deletions ngs_toolkit/atacseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,10 @@ def get_peak_gccontent_length(
sites = pybedtools.BedTool(bed_file)

if fasta_file is None:
from ngs_toolkit.general import get_genome_reference
_LOGGER.info("Reference genome FASTA file was not given, will try to get it.")
self._check_organism_genome(self)
_LOGGER.info("Getting genome FASTA file for organism '{}', genome '{}'. "
.format(self.organism, self.genome))
fasta_file = get_genome_reference(organism=self.organism, genome_assembly=self.genome)
fasta_file = self.get_annotations(steps=['fasta'])['fasta_file']

nuc = sites.nucleotide_content(fi=fasta_file).to_dataframe(comment="#")[["score", "blockStarts"]]
nuc.columns = ["gc_content", "length"]
Expand Down Expand Up @@ -828,19 +826,15 @@ def get_peak_gene_annotation(self, tss_file=None, max_dist=100000):
cols = [6, 8, 9]

if tss_file is None:
from ngs_toolkit.general import get_tss_annotations
_LOGGER.info("Reference TSS file was not given, will try to get TSS annotations.")
self._check_organism_genome(self)
_LOGGER.info("Getting TSS annotations for organism '{}', genome '{}'. "
.format(self.organism, self.genome))
tss = pybedtools.BedTool.from_dataframe(
get_tss_annotations(organism=self.organism, genome_assembly=self.genome)
.iloc[:, list(range(6))])
else:
# extract only relevant columns
tss = pd.read_csv(tss_file, header=None, sep="\t")
tss = tss.iloc[:, list(range(6))]
tss = pybedtools.BedTool.from_dataframe(tss)
tss_file = self.get_annotations(steps=['tss'])['tss_file']

# extract only relevant columns
tss = pd.read_csv(tss_file, header=None, sep="\t")
tss = tss.iloc[:, list(range(6))]
tss = pybedtools.BedTool.from_dataframe(tss)

if isinstance(self.sites, str):
self.sites = pybedtools.BedTool(self.sites)
Expand Down Expand Up @@ -916,15 +910,14 @@ def get_peak_genomic_location(
:returns: A dataframe with genomic context annotation for the peak set.
"""
from ngs_toolkit.general import bed_to_index

if genomic_context_file is None:
from ngs_toolkit.general import get_genomic_context
_LOGGER.info("Reference genomic context file was not given, will try to get it.")
_LOGGER.info("Getting genomic context annotations for organism '{}', genome '{}'. "
.format(self.organism, self.genome))
context = pybedtools.BedTool.from_dataframe(
get_genomic_context(organism=self.organism, genome_assembly=self.genome))
else:
context = pybedtools.BedTool(genomic_context_file)
genomic_context_file = self.get_annotations(steps=['genomic_context'])['genomic_context_file']

context = pybedtools.BedTool(genomic_context_file)

if isinstance(self.sites, str):
self.sites = pybedtools.BedTool(self.sites)
Expand Down Expand Up @@ -1692,9 +1685,9 @@ def characterize_regions_function(
df[['chrom', 'start', 'end']].reset_index().to_csv(tsv_file, sep="\t", header=False, index=False)

# export gene names
clean = df['gene_name'].str.split(",").apply(pd.Series, 1).stack().drop_duplicates()
clean = clean[~clean.isin(['.', 'nan'])]
clean.to_csv(
clean_gene = df['gene_name'].str.split(",").apply(pd.Series, 1).stack().drop_duplicates()
clean_gene = clean_gene[~clean_gene.isin(['.', 'nan', ''])]
clean_gene.to_csv(
os.path.join(output_dir, "{}_genes.symbols.txt".format(prefix)),
index=False)
if "ensembl_gene_id" in df.columns:
Expand Down Expand Up @@ -1749,7 +1742,7 @@ def characterize_regions_function(
# Enrichr
if 'enrichr' in steps:
_LOGGER.info("Running Enrichr for '{}'".format(prefix))
results = enrichr(df[['chrom', 'start', 'end', "gene_name"]])
results = enrichr(clean_gene.to_frame(name="gene_name"))
results.to_csv(
os.path.join(output_dir, "{}_regions.enrichr.csv".format(prefix)),
index=False, encoding='utf-8')
Expand Down
23 changes: 17 additions & 6 deletions ngs_toolkit/decorators.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
#!/usr/bin/env python

from functools import wraps
from ngs_toolkit import _LOGGER


def check_organism_genome(f):
from ngs_toolkit.general import Analysis

@wraps(f)
def wrapper(*args, **kwds):
Analysis._check_organism_genome(args[0])
attrs = ['organism', 'genome']
msg = "Analysis does not have 'organism' or 'genome' attributes set."
hint = " You can set them with analysis.set_organism_genome, for example."
r1 = all([hasattr(args[0], attr) for attr in attrs])
r2 = all([getattr(args[0], attr) is not None for attr in attrs])
if not all([r1, r2]):
_LOGGER.error(msg + hint)
raise AttributeError(msg)
return f(*args, **kwds)
return wrapper


def check_has_sites(f):
from ngs_toolkit.general import Analysis

@wraps(f)
def wrapper(*args, **kwds):
Analysis._check_has_sites(args[0])
attrs = ['sites']
msg = "Analysis object does not have a `sites` attribute."
hint = " Produce one with analysis.get_consensus_sites for example."
r1 = all([hasattr(args[0], attr) for attr in attrs])
r2 = all([getattr(args[0], attr) is not None for attr in attrs])
if not all([r1, r2]):
_LOGGER.error(msg + hint)
raise AttributeError(msg)
return f(*args, **kwds)
return wrapper
Loading