def write(syn: Synapse, center_mapping_df: pd.DataFrame, error_tracker_synid: str): """Write center errors to a file Args: syn: Synapse connection center_mapping_df: Center mapping dataframe error_tracker_synid: Error tracking synapse id """ center_errors = get_center_invalid_errors(syn, error_tracker_synid) for center in center_mapping_df["center"]: logger.info(center) staging_synid = center_mapping_df["stagingSynId"][ center_mapping_df["center"] == center][0] with open(center + "_errors.txt", "w") as errorfile: if center not in center_errors: errorfile.write("No errors!") else: errorfile.write(center_errors[center]) ent = synapseclient.File(center + "_errors.txt", parentId=staging_synid) syn.store(ent) os.remove(center + "_errors.txt")
def store_full_maf(syn: Synapse, filepath: str, parentid: str): """Stores full maf file Args: syn: Synapse connection filepath: Path to file parentid: Synapse container id """ syn.store(synapseclient.File(filepath, parentId=parentid))
def set_evaluation_quota(syn: Synapse, evalid: int, **kwargs): """Sets evaluation submission limit quota. This WILL erase any old quota you had previously set. Note - round_start must be specified with either round_end or round_duration and number_of_rounds must be defined for the time limits to work. submission_limit will work without number_of_rounds. Args: syn: Synapse object evalid: Evaluation id **kwargs: round_start: Start of round (local time) in YEAR-MM-DDTHH:MM:SS format (ie. 2020-02-21T17:00:00) round_end: End of round (local time) in YEAR-MM-DDTHH:MM:SS format (ie. 2020-02-21T19:00:00) number_of_rounds: Number of rounds round_duration: Round duration in milliseconds submission_limit: Number of submissions allowed per team Returns: A synapseclient.Evaluation Examples: >>> set_evaluation_quota(syn, 12345, round_start="2020-02-21T17:00:00", round_end="2020-02-23T17:00:00", number_of_rounds=1, submission_limit=3) """ quota = _create_quota(**kwargs) evaluation = syn.getEvaluation(evalid) evaluation.quota = vars(quota) evaluation = syn.store(evaluation) return evaluation
def push_wiki(syn: Synapse, projectid: str, workdir: str = "./") -> typing.List[dict]: """Pushes Wiki from configuration Args: syn: Synapse connection project: synapseclient.Project or its id workdir: Location to download markdown files and wiki_config.json. Defaults to location of where code is being executed. Returns: Wiki Configuration:: [ { "id": "111", "title": "title", "parentId": "33333", "markdown_path": "home.md" }, {...} ] """ wiki_config = validate_config(workdir) for wiki_header in wiki_config: # no markdown path, nothing to update markdown_path = wiki_header.get('markdown_path') if not wiki_header.get('markdown_path'): print(f"Markdown not specified: {wiki_header['title']}") continue markdown_path = os.path.join(workdir, markdown_path) with open(markdown_path, 'r') as md_f: markdown = md_f.read() # Create new wiki page if id isn't specified if wiki_header.get('id') is not None: wiki = syn.getWiki(projectid, subpageId=wiki_header['id']) # Don't store if the wiki pages are the same if wiki.markdown == markdown: print(f"no updates: {wiki_header['title']}") continue print(f"Wiki updated: {wiki_header['title']}") else: wiki = synapseclient.Wiki(owner=projectid, title=wiki_header['title'], parentWikiId=wiki_header['parentId']) print(f"Wiki added: {wiki_header['title']}") wiki.markdown = markdown wiki = syn.store(wiki) # If new wiki page is added, must add to wiki_config.json wiki_header['id'] = wiki['id'] return wiki_config
def store_narrow_maf(syn: Synapse, filepath: str, maf_tableid: str): """ Stores the narrow maf in Synapse Table Args: syn: Synapse connection filepath: Path to maf file maf_tableid: database synid """ logger.info(f"STORING {filepath}") # database = syn.get(maf_tableid) try: update_table = synapseclient.Table(maf_tableid, filepath, separator="\t") syn.store(update_table) except SynapseTimeoutError: # This error occurs because of waiting for table to index. # Don't worry about this. pass
def annotate_submission(syn: Synapse, submissionid: str, annotation_dict: dict = None, status: str = None, is_private: bool = True, force: bool = False) -> MockResponse: """Annotate submission with annotation values from a dict Args: syn: Synapse object submissionid: Submission id annotation_dict: Annotation dict status: Submission Status is_private: Set annotations acl to private (default is True) force: Force change the annotation from private to public and vice versa. Returns: MockResponse """ sub_status = syn.getSubmissionStatus(submissionid) # Update the status as well if status is not None: sub_status.status = status if annotation_dict is None: annotation_dict = {} # Don't add any annotations that are None annotation_dict = { key: annotation_dict[key] for key in annotation_dict if annotation_dict[key] is not None } sub_status = update_single_submission_status(sub_status, annotation_dict, is_private=is_private, force=force) sub_status = update_submission_status(sub_status, annotation_dict) syn.store(sub_status) return MockResponse
def submit_to_challenge(filename, challenge, label, retry=True): try: client = Synapse() client.login() evaluation = client.getEvaluation(CODES[challenge]) filename = filename + '.gct' if challenge == 'sc1' else filename + '.zip' myfile = File(RESULTS_FOLDER + filename, parent=PROJECT_ID) myfile = client.store(myfile) client.submit(evaluation, myfile, name=label, teamName=TEAM) except: if retry: submit_to_challenge(filename, challenge, label, retry=False) else: print 'failed to submit', label, 'to', challenge
def create_entity(syn: Synapse, name: str, link: str, annotations: dict) -> File: """Creates evaluation queue Args: name: Name of queue Returns: a synapseclient.Evaluation """ file_ent = File(name=name, path=link, parentId="syn21897226", synapseStore=False) file_ent.annotations = annotations return syn.store(file_ent)
def create_evaluation_queue(syn: Synapse, name: str) -> Evaluation: """Creates evaluation queue Args: name: Name of queue Returns: a synapseclient.Evaluation """ queue = Evaluation(name=name, contentSource="syn21849255") try: queue = syn.store(queue) except Exception: url_name = quote(name) queue = syn.restGET(f"/evaluation/name/{url_name}") queue = Evaluation(**queue) return queue
def _update_wiki(syn: Synapse, entity_wiki_pages: Dict[str, Wiki], destination_wiki_pages: Dict[str, Wiki], force: bool = False, dryrun: bool = False, **kwargs) -> Dict[str, Wiki]: """Updates wiki pages. Args: entity_wiki_pages: Mapping between wiki title and synapseclient.Wiki destination_wiki_pages: Mapping between wiki title and synapseclient.Wiki force: This will update a page even if its the same. Default is False. **kwargs: Same parameters as mirrorwiki.replace_wiki_text """ mirrored_wiki = [] for title in entity_wiki_pages: # If destination wiki does not have the title page, do not update if destination_wiki_pages.get(title) is None: logger.info(f"Title doesn't exist at destination: {title}") continue # Generate new markdown text entity_wiki = entity_wiki_pages[title] destination_wiki = destination_wiki_pages[title] markdown = _replace_wiki_text(markdown=entity_wiki.markdown, **kwargs) if destination_wiki.markdown == markdown and not force: logger.info(f"No page updates: {title}") else: logger.info(f"Updating: {title}") destination_wiki.markdown = markdown mirrored_wiki.append(destination_wiki) # Should copy over the attachments every time because # someone could name attachments with the same name new_attachments = _copy_attachments(syn, entity_wiki) destination_wiki.update({'attachmentFileHandleIds': new_attachments}) if not dryrun: destination_wiki = syn.store(destination_wiki) return mirrored_wiki
def _create_table(syn: Synapse, name: str, col_config: List[dict], parent: str) -> Schema: """Create Synapse Table Args: syn: Synapse connection name: Table name col_config: Column dict configuration parent: Synapse id of project Returns: Stored Synapse Table """ cols = [synapseclient.Column(**col) for col in col_config] schema = synapseclient.Schema(name=name, columns=cols, parent=parent) schema = syn.store(schema) return schema
def test_wiki_with_none_attachments(): syn = Synapse(skip_checks=True) with patch.object(syn, 'restPOST'): w = Wiki(owner="syn1", markdown="markdown", attachments=None) syn.store(w)
secretAccessKey = accessKey['create_access_key_response']['create_access_key_result']['access_key']['secret_access_key'] ## Write to a local, temporary file fileName = userName try: remove(fileName) except: ## do nothing pass f = open(fileName, 'w') f.write("The following credentials provide access to the AWS S3 bucket: "+userName+"\n") f.write('accessKeyId: '+accessKeyId+'\n') f.write('secretAccessKey: '+secretAccessKey+'\n') f.close() ## Upload the file to Synapse synapseFile = File(fileName, parentId=synapseAccessKeyProjectId) synapseFile = syn.store(synapseFile) ## clean up the temporary file remove(fileName) ## create the ACL, giving the user 'read' access (and the admin ALL access) acl = {"resourceAccess":[{"accessType":["READ"],"principalId":partId}, \ {"accessType": ["CHANGE_PERMISSIONS", "DELETE", "CREATE", "UPDATE", "READ"], "principalId":ownPrincipalId}]} syn._storeACL(synapseFile.id, acl) ## ## Send to an SNS topic the list of Participants, including bucket id. ## batchSize = 8 start = 0 while (start<len(participantList)): message = dumps(participantList[start:(start+batchSize)], indent=2) if anyNewUsers:
] ## Write to a local, temporary file fileName = userName try: remove(fileName) except: ## do nothing pass f = open(fileName, "w") f.write("The following credentials provide access to the AWS S3 bucket: " + userName + "\n") f.write("accessKeyId: " + accessKeyId + "\n") f.write("secretAccessKey: " + secretAccessKey + "\n") f.close() ## Upload the file to Synapse synapseFile = File(fileName, parentId=synapseAccessKeyProjectId) synapseFile = syn.store(synapseFile) ## clean up the temporary file remove(fileName) ## create the ACL, giving the user 'read' access (and the admin ALL access) acl = { "resourceAccess": [ {"accessType": ["READ"], "principalId": partId}, { "accessType": ["CHANGE_PERMISSIONS", "DELETE", "CREATE", "UPDATE", "READ"], "principalId": ownPrincipalId, }, ] } syn._storeACL(synapseFile.id, acl) ##