Skip to content

Commit

Permalink
predict chip dict working
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jan 21, 2025
1 parent 2de93f8 commit 85c01cb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ dependencies = [
"snakemake >= 8",
"pandas",
"thefuzz",
"pyyaml >= 5.1",
# "pyyaml >= 5.1",
"ruamel.yaml",
"snakemake-executor-plugin-cluster-generic >= 1.0.9",
"graphviz"
]
Expand Down
30 changes: 21 additions & 9 deletions snakePipes/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import subprocess
import os
import re
import yaml
#import yaml
from ruamel.yaml import YAML
import glob
import sys
import shutil
Expand Down Expand Up @@ -88,8 +89,10 @@ def namesOKinR(sampleNames):


def load_configfile(configFiles, verbose, info='Config'):
yaml=YAML(typ='safe')
with open(configFiles, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
#config = yaml.load(f, Loader=yaml.FullLoader)
config = yaml.load(f)

config = sanity_dict_clean(config)

Expand All @@ -102,9 +105,15 @@ def load_configfile(configFiles, verbose, info='Config'):
return config


def write_configfile(configFile, config):
def write_configfile(configFile, config, trafo):
yaml=YAML(typ='safe')
yaml.default_flow_style = False
with open(configFile, 'w') as f:
yaml.dump(config, f, default_flow_style=False)
#yaml.dump(config, f, default_flow_style=False)
if trafo:
yaml.dump(config, f, transform=trafo)
else:
yaml.dump(config, f)


# returns all key-value pairs that are different from dict1 to dict2
Expand Down Expand Up @@ -632,7 +641,7 @@ def commonYAMLandLogs(baseDir, workflowDir, defaults, args, callingScript):
# save to configs.yaml in outdir
config = defaults
config.update(vars(args)) # This allows modifications of args after handling a user config file to still make it to the YAML given to snakemake!
write_configfile(os.path.join(args.outdir, '{}.config.yaml'.format(workflowName)), config)
write_configfile(os.path.join(args.outdir, '{}.config.yaml'.format(workflowName)), config, trafo=None)

# merge cluster config files: 1) global one, 2) workflow specific one, 3) user provided one
cfg = load_configfile(os.path.join(baseDir, "shared", "defaults.yaml"), False, "defaults")
Expand Down Expand Up @@ -719,7 +728,7 @@ def print_DAG(args, snakemake_cmd, callingScript, defaults):
config['verbose'] = False
write_configfile(
os.path.join(args.outdir,
'{}.config.yaml'.format(workflowName)), config)
'{}.config.yaml'.format(workflowName)), config, trafo=None)

DAGproc = subprocess.Popen(
snakemake_cmd + " --rulegraph -q ",
Expand All @@ -734,7 +743,7 @@ def print_DAG(args, snakemake_cmd, callingScript, defaults):
config['verbose'] = oldVerbose
write_configfile(
os.path.join(args.outdir, '{}.config.yaml'.format(workflowName)),
config)
config, trafo=None)


def logAndExport(args, workflowName):
Expand Down Expand Up @@ -794,6 +803,9 @@ def runAndCleanup(args, cmd, logfile_name):
if args.emailAddress:
sendEmail(args, 0)

def tr(s):
return s.replace('null', 'None')


def predict_chip_dict(wdir, input_pattern_str, bamExt, fromBAM=None):
"""
Expand Down Expand Up @@ -856,14 +868,14 @@ def predict_chip_dict(wdir, input_pattern_str, bamExt, fromBAM=None):
print("No control sample found!")

chip_dict_pred["chip_dict"][i] = {}
chip_dict_pred["chip_dict"][i]['Control'] = tmp if tmp != "" else None
chip_dict_pred["chip_dict"][i]['Control'] = tmp if tmp != "" else None
if re.match(".*(H3K4me1|H3K36me3|H3K9me3|H3K27me3).*", i, re.IGNORECASE):
chip_dict_pred["chip_dict"][i]['Broad'] = True
else:
chip_dict_pred["chip_dict"][i]['Broad'] = False

outfile = os.path.join(wdir, "chip_seq_sample_config.PREDICTED.yaml")
write_configfile(outfile, chip_dict_pred)
write_configfile(outfile, chip_dict_pred,trafo=tr)
print("---------------------------------------------------------------------------------------")
print("ChIPseq sample configuration is written to file ", outfile)
print("Please check and modify this file - this is just a guess! Then run the workflow with it.")
Expand Down
4 changes: 2 additions & 2 deletions snakePipes/snakePipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,15 @@ def updateConfig(args):
else:
sys.exit("Config file not found\n")
updatedDict = cof.merge_dicts(currentDict, d)
cof.write_configfile(os.path.join(baseDir, "shared", "defaults.yaml"), updatedDict)
cof.write_configfile(os.path.join(baseDir, "shared", "defaults.yaml"), updatedDict, trafo=None)

#update conda-prefix in snakemakeProfile
if args.condaEnvDir:
profilePath = cof.resolveSnakemakeProfile(d['snakemakeProfile'], baseDir)
f = open(profilePath / 'config.yaml')
pf = yaml.load(f, Loader=yaml.FullLoader)
pf['conda-prefix'] = args.condaEnvDir
cof.write_configfile(os.path.join(profilePath, "config.yaml"), pf)
cof.write_configfile(os.path.join(profilePath, "config.yaml"), pf, trafo=None)
f.close()

cof.load_configfile(
Expand Down
2 changes: 1 addition & 1 deletion snakePipes/workflows/ChIPseq/internals.snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ with open(samples_config, "r") as f:
exit(1)
del chip_dict_tmp

cf.write_configfile(os.path.join("chip_samples.yaml"), chip_dict)
cf.write_configfile(os.path.join("chip_samples.yaml"), chip_dict, trafo=None)

# create unique sets of control samples, ChIP samples with and without control
control_samples = set()
Expand Down

0 comments on commit 85c01cb

Please sign in to comment.