本文整理汇总了Python中ngi_pipeline.database.classes.CharonSession类的典型用法代码示例。如果您正苦于以下问题:Python CharonSession类的具体用法?Python CharonSession怎么用?Python CharonSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CharonSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestCommunicate
class TestCommunicate(unittest.TestCase):
def setUp(self):
# Create the test project
self.project_id = "P100000"
self.project_name = "P.Mayhem_14_01"
self.project_data = dict(projectid=self.project_id, name=self.project_name, status=None)
self.session = CharonSession()
response = self.session.post(self.session.construct_charon_url('project'),
data=json.dumps(self.project_data))
assert response.status_code == 201, "Could not create test project in Charon: {}".format(response.reason)
project = response.json()
assert project['projectid'] == self.project_id, "Test project ID is incorrect"
def tearDown(self):
# Remove the test project
response = self.session.delete(self.session.construct_charon_url('project', self.project_id))
assert response.status_code == 204, "Could not delete test project from Charon: {}".format(response.reason)
def test_get_project_id_from_name(self):
# Check that it matches
self.assertEqual(self.project_id, get_project_id_from_name(self.project_name))
def test_rebuild_project_obj_from_charon(self):
# Create fake project / sample / libprep / seqrun
pass
示例2: fetch_charon
def fetch_charon(context, project, threshold, all_samples):
"""
Will fetch samples of the specified project from Charon and print the concordance
"""
try:
# get result from charon
charon_session = CharonSession()
result = charon_session.project_get_samples(project)
samples = {}
for sample in result.get('samples'):
sample_id = sample.get('sampleid')
concordance = float(sample.get('genotype_concordance'))
status = sample.get('genotype_status')
# exclude samples which were not yet checked
if status is not None:
samples[sample_id] = (concordance, status)
# print output
if not all_samples and samples:
print 'Samples below threshold: {}%'.format(threshold)
for sample in sorted(samples.keys()):
concordance, status = samples[sample]
# if --all, we don't care about threshold
if all_samples or concordance <= threshold:
# do not print 0%
if concordance != 0:
print '{} {}% {}'.format(sample, concordance, status)
except Exception, e:
log.error("Can't fetch Charon. Error says: {}".format(str(e)))
示例3: get_finished_seqruns_for_sample
def get_finished_seqruns_for_sample(project_id, sample_id,
include_failed_libpreps=False):
"""Find all the finished seqruns for a particular sample.
:param str project_id: The id of the project
:param str sample_id: The id of the sample
:returns: A dict of {libprep_01: [seqrun_01, ..., seqrun_nn], ...}
:rtype: dict
"""
charon_session = CharonSession()
sample_libpreps = charon_session.sample_get_libpreps(projectid=project_id,
sampleid=sample_id)
libpreps = collections.defaultdict(list)
for libprep in sample_libpreps['libpreps']:
if libprep.get('qc') != "FAILED" or include_failed_libpreps:
libprep_id = libprep['libprepid']
for seqrun in charon_session.libprep_get_seqruns(projectid=project_id,
sampleid=sample_id,
libprepid=libprep_id)['seqruns']:
seqrun_id = seqrun['seqrunid']
aln_status = charon_session.seqrun_get(projectid=project_id,
sampleid=sample_id,
libprepid=libprep_id,
seqrunid=seqrun_id).get('alignment_status')
if aln_status == "DONE":
libpreps[libprep_id].append(seqrun_id)
else:
LOG.debug('Skipping seqrun "{}" due to alignment_status '
'"{}"'.format(seqrun_id, aln_status))
else:
LOG.info('Skipping libprep "{}" due to qc status '
'"{}"'.format(libprep, libprep.get("qc")))
return dict(libpreps)
示例4: recreate_project_from_db
def recreate_project_from_db(analysis_top_dir, project_name, project_id):
project_dir = os.path.join(analysis_top_dir, "DATA", project_name)
project_obj = NGIProject(name=project_name,
dirname=project_name,
project_id=project_id,
base_path=analysis_top_dir)
charon_session = CharonSession()
try:
samples_dict = charon_session.project_get_samples(project_id)["samples"]
except CharonError as e:
raise RuntimeError("Could not access samples for project {}: {}".format(project_id, e))
for sample in samples_dict:
sample_id = sample.get("sampleid")
sample_dir = os.path.join(project_dir, sample_id)
sample_obj = project_obj.add_sample(name=sample_id, dirname=sample_id)
sample_obj.status = sample.get("status", "unknown")
try:
libpreps_dict = charon_session.sample_get_libpreps(project_id, sample_id)["libpreps"]
except CharonError as e:
raise RuntimeError("Could not access libpreps for project {} / sample {}: {}".format(project_id,sample_id, e))
for libprep in libpreps_dict:
libprep_id = libprep.get("libprepid")
libprep_obj = sample_obj.add_libprep(name=libprep_id, dirname=libprep_id)
libprep_obj.status = libprep.get("status", "unknown")
try:
seqruns_dict = charon_session.libprep_get_seqruns(project_id, sample_id, libprep_id)["seqruns"]
except CharonError as e:
raise RuntimeError("Could not access seqruns for project {} / sample {} / "
"libprep {}: {}".format(project_id, sample_id, libprep_id, e))
for seqrun in seqruns_dict:
# e.g. 140528_D00415_0049_BC423WACXX
seqrun_id = seqrun.get("seqrunid")
seqrun_obj = libprep_obj.add_seqrun(name=seqrun_id, dirname=seqrun_id)
seqrun_obj.status = seqrun.get("status", "unknown")
return project_obj
示例5: update_coverage_for_sample_seqruns
def update_coverage_for_sample_seqruns(project_id, sample_id, piper_qc_dir):
"""Find all the valid seqruns for a particular sample, parse their
qualimap output files, and update Charon with the mean autosomal
coverage for each.
:param str piper_qc_dir: The path to the Piper qc dir (02_preliminary_alignment_qc at time of writing)
:param str sample_id: The sample name (e.g. P1170_105)
:raises OSError: If the qc path specified is missing or otherwise inaccessible
:raises RuntimeError: If you specify both the seqrun_id and fcid and they don't match
:raises ValueError: If arguments are incorrect
"""
seqruns_by_libprep = get_finished_seqruns_for_sample(project_id, sample_id)
charon_session = CharonSession()
for libprep_id, seqruns in seqruns_by_libprep.iteritems():
for seqrun_id in seqruns:
label = "{}/{}/{}/{}".format(project_id, sample_id, libprep_id, seqrun_id)
ma_coverage = _parse_mean_coverage_from_qualimap(piper_qc_dir, sample_id, seqrun_id)
LOG.info('Updating project/sample/libprep/seqrun "{}" in '
'Charon with mean autosomal coverage "{}"'.format(label, ma_coverage))
try:
charon_session.seqrun_update(projectid=project_id,
sampleid=sample_id,
libprepid=libprep_id,
seqrunid=seqrun_id,
mean_autosomal_coverage=ma_coverage)
except CharonError as e:
error_text = ('Could not update project/sample/libprep/seqrun "{}" '
'in Charon with mean autosomal coverage '
'"{}": {}'.format(label, ma_coverage, e))
LOG.error(error_text)
if not config.get('quiet'):
mail_analysis(project_name=project_id, sample_name=sample_id,
engine_name="piper_ngi", level="ERROR", info_text=error_text)
示例6: recurse_status_for_sample
def recurse_status_for_sample(project_obj, set_status, update_done=False):
"""Set seqruns under sample to have status "set_status"
"""
charon_session = CharonSession()
project_id = project_obj.project_id
for sample_obj in project_obj:
# There's only one sample but this is an iterator
sample_id = sample_obj.name
for libprep_obj in sample_obj:
libprep_id = libprep_obj.name
for seqrun_obj in libprep_obj:
seqrun_id = seqrun_obj.name
label = "{}/{}/{}/{}".format(project_id, sample_id, libprep_id, seqrun_id)
LOG.info(('Updating status of project/sample/libprep/seqrun '
'"{}" to "{}" in Charon ').format(label, set_status))
try:
charon_session.seqrun_update(projectid=project_id,
sampleid=sample_id,
libprepid=libprep_id,
seqrunid=seqrun_id,
alignment_status=set_status)
except CharonError as e:
error_text =('Could not update status of project/sample/libprep/seqrun '
'"{}" in Charon to "{}": {}'.format(label, set_status, e))
LOG.error(error_text)
if not config.get('quiet'):
mail_analysis(project_name=project_id, sample_name=sample_obj.name,
level="ERROR", info_text=error_text)
示例7: write_to_charon_NGI_results
def write_to_charon_NGI_results(job_id, return_code, run_dir):
"""Update the status of a sequencing run after alignment.
:param NGIProject project_id: The name of the project, sample, lib prep, flowcell id
:param int return_code: The return code of the workflow process
:param string run_dir: the directory where results are stored (I know that I am running piper)
:raises RuntimeError: If the Charon database could not be updated
"""
charon_session = CharonSession()
# Consider moving this mapping to the CharonSession object or something
if return_code is None:
status = "RUNNING"
elif return_code == 0:
status = "DONE"
else:
## TODO we need to differentiate between COMPUTATION_FAILED and DATA_FAILED
## also there is IGNORE?
status = "COMPUTATION_FAILED"
try:
m_dict = STHLM_UUSNP_SAMPLE_RE.match(job_id).groupdict()
#m_dict = re.match(r'?P<project_name>\w\.\w+_\d+_\d+|\w{2}-\d+)_(?P<sample_id>[\w-]+)_(?P<libprep_id>\w|\w{2}\d{3}_\2)_(?P<seqrun_id>\d{6}_\w+_\d{4}_.{10})', job_id).groupdict()
project_id = get_project_id_from_name(m_dict['project_name'])
sample_id = m_dict['sample_id']
except (TypeError, AttributeError):
error_msg = "Could not parse project/sample ids from job id \"{}\"; cannot update Charon with results!".format(job_id)
raise RuntimeError(error_msg)
try:
charon_session.sample_update(project_id, sample_id, status=status)
except CharonError as e:
error_msg = ('Failed to update sample status to "{}" for sample "{}" '
'in Charon database: {}'.format(status, project_id, sample_id, e))
raise RuntimeError(error_msg)
示例8: check_for_preexisting_sample_runs
def check_for_preexisting_sample_runs(project_obj, sample_obj, restart_running_jobs, restart_finished_jobs):
"""If any analysis is undergoing or has completed for this sample's
seqruns, raise a RuntimeError.
:param NGIProject project_obj: The project object
:param NGISample sample_obj: The sample object
:param boolean restart_running_jobs: command line parameter
:param boolean restart_finished_jobs: command line parameter
:raise RuntimeError if the status is RUNNING or DONE and the flags do not allow to continue
"""
project_id = project_obj.project_id
sample_id = sample_obj.name
charon_session = CharonSession()
sample_libpreps = charon_session.sample_get_libpreps(projectid=project_id,
sampleid=sample_id)
for libprep in sample_libpreps['libpreps']:
libprep_id = libprep['libprepid']
for seqrun in charon_session.libprep_get_seqruns(projectid=project_id,
sampleid=sample_id,
libprepid=libprep_id)['seqruns']:
seqrun_id = seqrun['seqrunid']
aln_status = charon_session.seqrun_get(projectid=project_id,
sampleid=sample_id,
libprepid=libprep_id,
seqrunid=seqrun_id).get('alignment_status')
if (aln_status == "RUNNING" and not restart_running_jobs) or \
(aln_status == "DONE" and not restart_finished_jobs):
raise RuntimeError('Project/Sample "{}/{}" has a preexisting '
'seqrun "{}" with status "{}"'.format(project_obj,
sample_obj, seqrun_id, aln_status))
示例9: get_project_id_from_name
def get_project_id_from_name(project_name):
"""Given the project name ("Y.Mom_14_01") return the project ID ("P123")
:param str project_name: The human-friendly name of the project (e.g. "J.Doe_14_01")
:returns: The alphanumeric database-friendly name of the project (e.g. "P123")
:rtype: str
:raises RuntimeError: If there is some problem relating to the GET (HTTP Return code != 200)
:raises ValueError: If the project has no project id in the database or if the project does not exist in Charon
"""
charon_session = CharonSession()
try:
project_id = charon_session.project_get(project_name)
except CharonError as e:
if e.status_code == 404:
new_e = ValueError('Project "{}" missing from database: {}'.format(project_name, e))
new_e.status_code = 404
raise e
else:
raise
try:
return project_id['projectid']
except KeyError:
raise ValueError('Couldn\'t retrieve project id for project "{}"; '
'this project\'s database entry has no "projectid" value.'.format(project))
示例10: update_gtc_for_sample
def update_gtc_for_sample(project_id, sample_id, piper_gtc_path, config=None, config_file_path=None):
"""Find the genotype concordance file for this sample, if it exists,
and update the sample record in Charon with the value parsed from it.
:param str project_id: The id of the project
:param str sample_id: The id the sample
:param str piper_gtc_path: The path to the piper genotype concordance directory
:raises CharonError: If there is some Error -- with Charon
:raises IOError: If the path specified is missing or inaccessible
:raises ValueError: If the specified sample has no data in the gtc file
"""
gtc_file = os.path.join(piper_gtc_path, "{}.gt_concordance".format(sample_id))
try:
concordance_value = parse_genotype_concordance(gtc_file)[sample_id]
except KeyError:
raise ValueError('Concordance data for sample "{}" not found in gt '
'concordance file "{}"'.format(sample_id, gtc_file))
gtc_lower_bound = config.get("genotyping", {}).get("lower_bound_cutoff")
status_dict = {}
if gtc_lower_bound:
if concordance_value < concordance_value:
status_dict = {"genotype_status": "FAILED"}
else:
status_dict = {"genotype_status": "PASSED"}
charon_session = CharonSession()
charon_session.sample_update(projectid=project_id, sampleid=sample_id,
genotype_concordance=concordance_value,
**status_dict)
示例11: get_delivery_token_in_charon
def get_delivery_token_in_charon(self):
'''fetches delivery_token from Charon
'''
charon_session = CharonSession()
project_charon = charon_session.project_get(self.projectid)
if project_charon.get('delivery_token'):
return project_charon.get('delivery_token')
else:
return 'NO-TOKEN'
示例12: main
def main(demux_fcid_dir, restrict_to_projects=None, restrict_to_samples=None):
demux_fcid_dir = "/proj/a2014205/INBOX/140528_D00415_0049_BC423WACXX" # G.Grigelioniene_14_01
process_demultiplexed_flowcell(demux_fcid_dir, None, None)
time.sleep(60) #wait for 1 minutes
demux_fcid_dir = "/proj/a2014205/INBOX/140702_D00415_0052_AC41A2ANXX" # M.Kaller_14_06 sample P1171_102, P1171_104, P1171_106, P1171_108
process_demultiplexed_flowcell(demux_fcid_dir, None, None)
time.sleep(60) #wait for 1 minutes
demux_fcid_dir = "/proj/a2014205/INBOX/140905_D00415_0057_BC45KVANXX" # M.Kaller_14_06 sample P1171_102, P1171_104, P1171_106 ---- rerun
process_demultiplexed_flowcell(demux_fcid_dir, None, None)
time.sleep(60) #wait for 1 minutes
demux_fcid_dir = "/proj/a2014205/INBOX/140815_SN1025_0222_AC4HA6ACXX" # M.Kaller_14_05 sample P1170_101, P1170_103, P1170_105
process_demultiplexed_flowcell(demux_fcid_dir, None, None) # M.Kaller_14_08 sample P1272_101, P1272_104
time.sleep(60) #wait for 1 minutes
demux_fcid_dir = "/proj/a2014205/INBOX/140815_SN1025_0223_BC4HAPACXX" # M.Kaller_14_05 sample P1170_101, P1170_103, P1170_105
process_demultiplexed_flowcell(demux_fcid_dir, None, None) # M.Kaller_14_08 sample P1272_101, P1272_104
time.sleep(60) #wait for 1 minutes
demux_fcid_dir = "/proj/a2014205/INBOX/140919_SN1018_0203_BHA3THADXX" # M.Kaller_14_05 P1170_103, P1170_105 --- rerun
process_demultiplexed_flowcell(demux_fcid_dir, None, None)
time.sleep(60) #wait for 1 minutes
###UPPSALA
demux_fcid_dir = "/proj/a2014205/INBOX/140821_D00458_0029_AC45JGANXX" # uppsala run
process_demultiplexed_flowcell(demux_fcid_dir, None, None)
time.sleep(60) #wait for 1 minutes
demux_fcid_dir = "/proj/a2014205/INBOX/140917_D00458_0034_AC4FF3ANXX" # -- rerun
process_demultiplexed_flowcell(demux_fcid_dir, None, None)
time.sleep(60) #wait for 1 minutes
#and now a loop to update the DB
time.sleep(3800)
charon_session = CharonSession()
####charon_session.project_delete("ND-0522")
while True:
update_charon_with_local_jobs_status() ## this updated local_db and charon accordingly
# grab all projects from Charon
projects_dict = charon_session.projects_get_all()['projects']
for project_charon in projects_dict:
project_name = project_charon["name"]
project_dir = os.path.join("/proj/a2014205/nobackup/NGI/analysis_ready/DATA", project_name)
if os.path.isdir(project_dir):
projectObj = recreate_project_from_filesystem(project_dir, None)
launch_analysis_for_samples([projectObj])
time.sleep(3800)
示例13: main
def main():
args = cli_args()
cs = CharonSession()
cs.project_update(args.project_id,best_practice_analysis="hello_engine")
# it is actually picking up stdout and stderr as well
output = subprocess.check_output(["./nextflow", "run", "hello-ga.nf"])
print "The output is:"
print output
print "done"
示例14: write_to_charon_alignment_results
def write_to_charon_alignment_results(base_path, project_name, project_id, sample_id, libprep_id, seqrun_id):
"""Update the status of a sequencing run after alignment.
:param str project_name: The name of the project (e.g. T.Durden_14_01)
:param str project_id: The id of the project (e.g. P1171)
:param str sample_id: ...
:param str libprep_id: ...
:param str seqrun_id: ...
:raises RuntimeError: If the Charon database could not be updated
:raises ValueError: If the output data could not be parsed.
"""
charon_session = CharonSession()
try:
seqrun_dict = charon_session.seqrun_get(project_id, sample_id, libprep_id, seqrun_id)
except CharonError as e:
raise CharonError('Error accessing database for project "{}", sample {}; '
'could not update Charon while performing best practice: '
'{}'.format(project_name, sample_id, e))
piper_run_id = seqrun_id.split("_")[3]
seqrun_dict["lanes"] = 0
if seqrun_dict.get("alignment_status") == "DONE":
LOG.warn("Sequencing run \"{}\" marked as DONE but writing new alignment results; "
"this will overwrite the previous results.".format(seqrun_id))
# Find all the appropriate files
piper_result_dir = os.path.join(base_path, "ANALYSIS", project_name, "02_preliminary_alignment_qc")
try:
os.path.isdir(piper_result_dir) and os.listdir(piper_result_dir)
except OSError as e:
raise ValueError("Piper result directory \"{}\" inaccessible when updating stats to Charon: {}.".format(piper_result_dir, e))
piper_qc_dir_base = "{}.{}.{}".format(sample_id, piper_run_id, sample_id)
piper_qc_path = "{}*/".format(os.path.join(piper_result_dir, piper_qc_dir_base))
piper_qc_dirs = glob.glob(piper_qc_path)
if not piper_qc_dirs: # Something went wrong in the alignment or we can't parse the file format
raise ValueError("Piper qc directories under \"{}\" are missing or in an unexpected format when updating stats to Charon.".format(piper_qc_path))
# Examine each lane and update the dict with its alignment metrics
for qc_lane in piper_qc_dirs:
genome_result = os.path.join(qc_lane, "genome_results.txt")
# This means that if any of the lanes are missing results, the sequencing run is marked as a failure.
# We should flag this somehow and send an email at some point.
if not os.path.isfile(genome_result):
raise ValueError("File \"genome_results.txt\" is missing from Piper result directory \"{}\"".format(piper_result_dir))
# Get the alignment results for this lane
lane_alignment_metrics = parse_qualimap_results(genome_result)
# Update the dict for this lane
update_seq_run_for_lane(seqrun_dict, lane_alignment_metrics)
try:
# Update the seqrun in the Charon database
charon_session.seqrun_update(**seqrun_dict)
except CharonError as e:
error_msg = ('Failed to update run alignment status for run "{}" in project {} '
'sample {}, library prep {} to Charon database: {}'.format(seqrun_id,
project_name, sample_id, libprep_id, e))
raise CharonError(error_msg)
示例15: analyze
def analyze(analysis_object, config=None, config_file_path=None):
charon_session = CharonSession()
charon_pj=charon_session.project_get(analysis_object.project.project_id)
reference_genome=charon_pj.get('reference')
if charon_pj.get("sequencing_facility") == "NGI-S":
analysis_object.sequencing_facility="sthlm"
elif charon_pj.get("sequencing_facility") == "NGI-U":
analysis_object.sequencing_facility="upps"
else:
LOG.error("charon project not registered with stockholm or uppsala. Which config file should we use for the RNA pipeline ?")
raise RuntimeError
fastq_files=[]
if reference_genome and reference_genome != 'other':
for sample in analysis_object.project:
try:
charon_reported_status = charon_session.sample_get(analysis_object.project.project_id,
sample).get('analysis_status')
# Check Charon to ensure this hasn't already been processed
do_analyze=handle_sample_status(analysis_object, sample, charon_reported_status)
if not do_analyze :
continue
except CharonError as e:
LOG.error(e)
for libprep in sample:
charon_lp_status=charon_session.libprep_get(analysis_object.project.project_id, sample.name, libprep.name).get('qc')
do_analyze=handle_libprep_status(analysis_object, libprep, charon_lp_status)
if not do_analyze :
continue
else:
for seqrun in libprep:
charon_sr_status=charon_session.seqrun_get(analysis_object.project.project_id, sample.name, libprep.name, seqrun.name).get('alignment_status')
do_analyze=handle_seqrun_status(analysis_object, seqrun, charon_sr_status)
if not do_analyze :
continue
else:
seqrun.being_analyzed=True
sample.being_analyzed = sample.being_analyzed or True
# filter out index files from analysis
for fastq_file in filter(lambda f: not is_index_file(f), seqrun.fastq_files):
fastq_path=os.path.join(analysis_object.project.base_path, "DATA", analysis_object.project.project_id, sample.name, libprep.name, seqrun.name, fastq_file)
fastq_files.append(fastq_path)
if not fastq_files:
LOG.error("No fastq files obtained for the analysis fo project {}, please check the Charon status.".format(analysis_object.project.name))
else :
if analysis_object.restart_running_jobs:
stop_ongoing_analysis(analysis_object)
fastq_dir=preprocess_analysis(analysis_object, fastq_files)
sbatch_path=write_batch_job(analysis_object, reference_genome, fastq_dir)
job_id=start_analysis(sbatch_path)
analysis_path=os.path.join(analysis_object.project.base_path, "ANALYSIS", analysis_object.project.project_id, 'rna_ngi')
record_project_job(analysis_object.project, job_id, analysis_path)