|
| 1 | +# For barcoded ONT samples, make new sub sample folder and mv the corresponding barcoded fastq. |
| 2 | +import os |
| 3 | +import requests |
| 4 | +import json |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | +ACCESS = 0o775 |
| 9 | + |
| 10 | +ONT_path = "/igo/staging/promethion/" |
| 11 | + |
| 12 | +def get_numbers_from_string(input_string): |
| 13 | + # Use regular expression to find numbers at the end of the string |
| 14 | + match = re.search(r'\d+$', input_string) |
| 15 | + # Return the matched numbers or None if no match is found |
| 16 | + return match.group() if match else None |
| 17 | + |
| 18 | +def get_project_ID_from_sample(sample_name): |
| 19 | + match = re.match(r"^(\d+)(_([A-Za-z]))?", sample_name) |
| 20 | + if match: |
| 21 | + if match.group(3): # If the second part is a letter |
| 22 | + return f"{match.group(1)}_{match.group(3)}" |
| 23 | + else: # Otherwise, return only the first part |
| 24 | + return match.group(1) |
| 25 | + return None |
| 26 | + |
| 27 | +# get barcode information from lims |
| 28 | +def get_sample_barcode(pool_id): |
| 29 | + sample_dict = {} |
| 30 | + lims_endpoint = "https://igolims.mskcc.org:8443/LimsRest/getPoolsBarcodes?poolId=" |
| 31 | + response = requests.get(lims_endpoint + pool_id, auth = ("pms", "tiagostarbuckslightbike"), verify = False) |
| 32 | + response_data = json.loads(response.text.encode("utf8")) |
| 33 | + for sample in response_data: |
| 34 | + project_id = "Project_" + str(get_project_ID_from_sample(sample["librarySample"])) |
| 35 | + sample_dict[sample["librarySample"]] =[project_id ,"barcode" + str(get_numbers_from_string(sample["sampleBarcode"]["barcodId"]))] |
| 36 | + print(sample_dict) |
| 37 | + return sample_dict |
| 38 | + |
| 39 | +# given sample:[project,barcode] dictionary per pool and pool path, create sample folder if not exist and generate cmd for mv and rename folder |
| 40 | +def mv_fastq(sample_dict, parent_folder_name, pool_name): |
| 41 | + for key, value in sample_dict.items(): |
| 42 | + project = value[0] |
| 43 | + barcode = value[1] |
| 44 | + # create sub sample folder if not exist |
| 45 | + sample_folder = "/igo/staging/promethion/{}/{}".format(project, key) |
| 46 | + if not os.path.exists(sample_folder): |
| 47 | + print("creating folder: " + sample_folder) |
| 48 | + # os.makedirs(sample_folder, ACCESS) |
| 49 | + |
| 50 | + source_fastq_pass = "{}/{}/*/fastq_pass/{}".format(parent_folder_name, pool_name, barcode) |
| 51 | + source_fastq_fail = "{}/{}/*/fastq_fail/{}".format(parent_folder_name, pool_name, barcode) |
| 52 | + cmd1 = "mv {} {}/fastq_pass".format(source_fastq_pass, sample_folder) |
| 53 | + cmd2 = "mv {} {}/fastq_fail".format(source_fastq_fail, sample_folder) |
| 54 | + |
| 55 | + print(cmd1) |
| 56 | + print(cmd2) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + # Usage: python deliver_Barcoded_ONT.py [project_directory] |
| 61 | + # example: python deliver_Barcoded_ONT.py Project_15710_D_16975 |
| 62 | + project_name = sys.argv[1] |
| 63 | + parent_folder_path = ONT_path + project_name |
| 64 | + for i in os.listdir(parent_folder_path): |
| 65 | + sample_dict = get_sample_barcode(i) |
| 66 | + mv_fastq(sample_dict, parent_folder_path, i) |
0 commit comments