import os import pandas as pd import re from zipfile import ZipFile import boto3 import io data_dir = os.getcwd() output_path = os.getcwd() # species_list = ["rat_SD", "mouse_BALB_c", "mouse_C57BL_6", "human"] species_list = ["mouse_BALB_c", "mouse_C57BL_6", "rat_SD"] S3_BUCKET = "aws-hcls-ml" S3_SRC_PREFIX = "oas-paired-sequence-data/raw" S3_DEST_PREFIX = "oas-paired-sequence-data/processed" s3 = boto3.client("s3") # def calc_cdr_coordinates(row): # for i in range(1, 4): # for j in ["heavy", "light"]: # row[f"cdr{i}_aa_{j}_start"] = row[f"sequence_alignment_aa_{j}"].find( # row[f"cdr{i}_aa_{j}"] # ) # row[f"cdr{i}_aa_{j}_end"] = row[f"cdr{i}_aa_{j}_start"] + len( # row[f"cdr{i}_aa_{j}"] # ) # return row for species in species_list: print(f"Downloading {species} files") list_of_df = [] species_url_file = os.path.join(data_dir, species + "_oas_data_units.txt") with open(species_url_file, "r") as f: for csv_file in f.readlines(): print(csv_file) filename = os.path.basename(csv_file) run_id = str(re.search(r"^(.*)_[Pp]aired", filename)[1]) s3_key = os.path.join(S3_SRC_PREFIX, species, csv_file.strip()) obj = s3.get_object(Bucket=S3_BUCKET, Key=s3_key) run_data = pd.read_csv( io.BytesIO(obj["Body"].read()), header=1, compression="gzip", on_bad_lines="warn", low_memory=False, ) run_data = run_data[ [ "sequence_alignment_aa_heavy", "cdr1_aa_heavy", "cdr2_aa_heavy", "cdr3_aa_heavy", "sequence_alignment_aa_light", "cdr1_aa_light", "cdr2_aa_light", "cdr3_aa_light", ] ] run_data = run_data.dropna() # run_data = run_data.apply(calc_cdr_coordinates, axis=1) run_data.insert( 0, "pair_id", run_id + "_" + run_data.reset_index().index.map(str) ) list_of_df.append(run_data) species_df = pd.concat(list_of_df, ignore_index=True) print(f"{species} output summary:") print(species_df.head()) print(species_df.shape) output_file_name = os.path.join(output_path, "train.csv") print(f"Creating {output_file_name}") # species_df.to_csv(output_file_name, index=False, compression="zip") species_df.to_csv(output_file_name, index=False) zip_name = species + ".zip" print(f"Creating {zip_name}") with ZipFile(zip_name, "w") as myzip: myzip.write("train.csv") print( f"Uploading {zip_name} to {os.path.join('s3://', S3_BUCKET, S3_DEST_PREFIX)}" ) s3.upload_file(zip_name, S3_BUCKET, os.path.join(S3_DEST_PREFIX, zip_name)) print(f"Removing {output_file_name}") os.remove(output_file_name) print(f"Removing {zip_name}") os.remove(zip_name)