def samples_search_by_name(args): sample = Samples() try: result = sample.search(args.studyId, name=args.name) print(result) except ServerResponseException as e: print(str(e), file=sys.stderr)
def individuals_create(args): # 1st create individual # TODO: Change this method to load the individuals from phenotips format or ped """ Call: python pyCGA [--host hostname] sid individuals create --studyId studyId --name paticipantId --gender --fatherId --motherId --family --wellIds :param args: """ individual = Individuals() sample = Samples() try: individualId = individual.create(args.studyId, args.name, args.family, args.fatherId, args.motherId, args.gender)[0]["id"] well_ids = args.wellIds.split(",") for well in well_ids: try: sampleId = sample.search(args.studyId, name=well)[0]["id"] print(sampleId) try: sample.update(str(sampleId), individualId=str(individualId)) except ServerResponseException as e: print(str(e), file=sys.stderr) except ServerResponseException as e: print(str(e), file=sys.stderr) except ServerResponseException as e: print(str(e), file=sys.stderr)
def get_all_samples_names(studyId): """ :param study: """ sample = Samples() result = sample.search(studyId=studyId, include="projects.studies.samples.name") return [r["name"] for r in result]
def create_samples(args): try: sample = Samples() result = sample.create(args.studyId, args.name, "Manually_added", "")[0] print(result) except ServerResponseException as e: print(str(e), file=sys.stderr)
def samples_search_by_annotation(args): sample = Samples() try: result = sample.search_by_annotation(args.studyId, args.variableSetName, *args.queries) for r in result: # print(int(AnnotationSet(*[annotation for annotation in r["annotationSets"] if args.variableSetName in annotation["attributes"]["annotateSetName"]][0]["annotations"]).get_json()["id"])) print(AnnotationSet(*[annotation for annotation in r["annotationSets"] if args.variableSetName in annotation["attributes"]["annotateSetName"]][0]["annotations"]).get_json()["name"]) except ServerResponseException as e: print(str(e), file=sys.stderr)
def samples_annotate(args): try: sample = Samples() sample_id = sample.search(args.studyId, name=args.sampleName)[0]["id"] result = sample.annotate(sample_id, args.variableSetName, args.annotationSetName, args.studyId, args.jsonFile) print(result) except ServerResponseException as e: print(str(e), file=sys.stderr)
def samples_share(args): sample = Samples() files = Files() ids_sample = [s.rstrip("\n").split("\t")[0] for s in open(args.samples).readlines()] for id in ids_sample: # This line return the root folder root = str(files.search(studyId=args.studyId, sampleIds=id, description="INTAKE")[0]["id"]) files.share(args.user, root) try: sample.share(args.user, id, include="projects.studies.samples.name") except ServerResponseException as e: print(str(e), file=sys.stderr)
def check_samples_acl(studyId, userId, group=None): """ Method to check permisions for one user :param studyId: :param userId: """ if group is not None: userId += "," + group sample = Samples() options = {"acl.userId": userId} result = sample.search(studyId=studyId, **options) if not result: return 'Your user don\'t have permissions in any of the Sample currently available in this Study, please check it with the administrator' else: return [sample["name"] for sample in result if sample["acl"]]
def get_annotation_sample(study_id, sample_id, variable_set_name): """ :param study_id: :param sample_id: :param variable_set_name: :return: """ variable = Variables() sample = Samples() samples_info = sample.search(studyId=study_id, id=sample_id) sample_annotations = [] variableSetId = variable.search(studyId=study_id, name=variable_set_name)[0]["id"] for sample_info in samples_info: sample_annotations.append(extract_annotation(sample_info, variableSetId)) return sample_annotations
def files_search_by_sample(args): file_instance = Files() sample = Samples() for sampleName in args.sampleName: try: sampleId = sample.search(args.studyId, name=sampleName)[0]["id"] results = file_instance.search(sampleIds=str(sampleId), studyId=args.studyId, bioformat=args.fileType, include="projects.studies.files.path,projects.studies.files.sampleIds", path=args.path ) for r in results: print(sampleName +"\t"+ r["path"]) except ServerResponseException as e: print(str(e), file=sys.stderr)
def setUp(self): unregistered_session = {"host": host, "sid": ""} self.user = Users(unregistered_session, instance=catalog_instance) sid = self.user.login_method("pfurio", pwd)[0]["sessionId"] self.token = {"user": "******", "host": host, "sid": str(sid), "instance": catalog_instance, "debug": True} self.file_connector = Files(token=self.token) self.sample_connector = Samples(token=self.token) self.individual_connector = Individuals(token=self.token) self.job_connector = Jobs(token=self.token) self.variable_connector = Variables(token=self.token)
def individuals_search(args): """ Call: python pyCGA [--host hostname] sid individuals search --studyId studyId --name particiantId :param args: """ individual = Individuals() sample = Samples() individual_results = individual.search(studyId=args.studyId, name=args.name) for individual_result in individual_results: gender = individual_result["gender"] individualId = individual_result["id"] print("ParticipantId in Catalog: "+ str(individualId)) try: sample_result = sample.search(studyId=args.studyId, individualId=str(individualId)) for sample in sample_result: print("Sample Name: " + sample["name"]) except ServerResponseException as e: print(str(e), file=sys.stderr)
def share_samples(studyId, list_of_samples_file, user_id): file_instance = Files() sample_instance = Samples() summary = {} fd = open(list_of_samples_file) for line in fd: aline = line.rstrip("\n").split("\t") if aline: sample_name = aline[0] root_folder = aline[1] print(sample_name) sample_objs = sample_instance.search(studyId=studyId, name=sample_name) for sample_obj in sample_objs: sample_id = str(sample_obj["id"]) folders_to_share = file_instance.search(studyId=studyId, path=root_folder, sampleIds=sample_id ) for folder_to_share in folders_to_share: file_id = str(folder_to_share["id"]) file_instance.share(userId=user_id, fileId=file_id) sample_instance.share(userId=user_id, sampleId=sample_id) summary[sample_name] = [folders_to_share]
def id_converter(studyID, inputIDType, outputIDType, IdBatch=None, Id=None): if IdBatch is None and Id is None: raise Exception("Error, it was not found any id to convert") if Id: ids = Id else: ids = [line.split("\t")[0].replace("\n","") for line in open(IdBatch).readlines()] for id in ids: if inputIDType == "CatalogIndividualId": if outputIDType == "IndividualName": individual = Individuals() individual_info = individual.info(id)[0] yield [id, str(individual_info["name"])] elif outputIDType == "SampleName": sample = Samples() sample_info = sample.search(studyID, individualId=id) for info in sample_info: yield [id, info["name"]] elif outputIDType == "CatalogSampleId": sample = Samples() sample_info = sample.search(studyID, individualId=id) for info in sample_info: yield [id, str(info["id"])] if inputIDType == "IndividualName": if outputIDType == "CatalogIndividualId": individual = Individuals() individual_info = individual.search(studyID, name=id)[0] yield [id, str(individual_info["id"])] if outputIDType == "SampleName": sample = Samples() individual = Individuals() individual_info = individual.search(studyID, name=id)[0] sample_info = sample.search(studyID, individualId=str(individual_info["id"])) for info in sample_info: yield [id, info["name"]] if outputIDType == "CatalogSampleId": sample = Samples() individual = Individuals() individual_info = individual.search(studyID, name=id)[0] sample_info = sample.search(studyID, individualId=str(individual_info["id"])) for info in sample_info: yield [id, str(info["id"])] if inputIDType == "CatalogSampleId": if outputIDType == "CatalogIndividualId": sample = Samples() sample_info = sample.info(id)[0] yield [id, str(sample_info["individualId"])] if outputIDType == "SampleName": sample = Samples() sample_info = sample.info(id)[0] yield [id, str(sample_info["name"])] if outputIDType == "IndividualName": sample = Samples() sample_info = sample.info(id)[0] individual = Individuals() individual_info = individual.info(str(sample_info["individualId"]))[0] yield [id, str(individual_info["name"])] if inputIDType == "SampleName": if outputIDType == "CatalogIndividualId": sample = Samples() sample_info = sample.search(studyID, name=id)[0] yield [id, str(sample_info["individualId"])] if outputIDType == "CatalogSampleId": sample = Samples() sample_info = sample.search(studyID, name=id)[0] yield [id, str(sample_info["id"])] if outputIDType == "IndividualName": sample = Samples() sample_info = sample.search(studyID, name=id)[0] individual = Individuals() individual_info = individual.info(str(sample_info["individualId"]))[0] yield [id, str(individual_info["name"])]