|
| 1 | +__author__ = "rpgv" |
| 2 | +__all__ = ["load_aptacom_full", "load_aptacom_xy"] |
| 3 | + |
| 4 | +from pyaptamer.datasets._loaders._hf_loader import load_hf_dataset |
| 5 | + |
| 6 | +filter_map = { |
| 7 | + "protein_target": ("target_chemistry", ["Protein", "peptide"]), |
| 8 | + "small_target": ( |
| 9 | + "target_chemistry", |
| 10 | + ["Small Organic", "Small Molecule", "Molecule"], |
| 11 | + ), |
| 12 | + "dna_apt": ( |
| 13 | + "aptamer_chemistry", |
| 14 | + [ |
| 15 | + "DNA", |
| 16 | + "L-DNA", |
| 17 | + "ssDNA", |
| 18 | + "2',4'-BNA/LNA-DNA", |
| 19 | + "5-uracil-modified-DNA", |
| 20 | + "dsDNA", |
| 21 | + ], |
| 22 | + ), |
| 23 | + "rna_apt": ( |
| 24 | + "aptamer_chemistry", |
| 25 | + [ |
| 26 | + "RNA", |
| 27 | + "2'-F-RNA", |
| 28 | + "2'-NH2-RNA", |
| 29 | + "L-RNA", |
| 30 | + "2'-O-Me-RNA", |
| 31 | + "ssRNA", |
| 32 | + "2'-fluoro/amino-RNA", |
| 33 | + "2'-fluoro-RNA", |
| 34 | + "2'-amino-RNA", |
| 35 | + "2'-fluoro/O-Me-RNA", |
| 36 | + "5-uracil-modified-RNA", |
| 37 | + "4'-thio-RNA", |
| 38 | + ], |
| 39 | + ), |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +def filter_columns(ds, columns=None): |
| 44 | + """ " Selects columns to keep on dataset |
| 45 | + Parameters: |
| 46 | + ----------- |
| 47 | + ds: pd dataframe, required |
| 48 | + Pandas dataframe to filter |
| 49 | + columns: list, optional, default=None |
| 50 | + If empty returns entire AptaCom dataset, otherwise |
| 51 | + returns only the selected columns from the |
| 52 | + AptaCom dataset |
| 53 | + Returns: |
| 54 | + -------- |
| 55 | + object: pandas dataframe object with |
| 56 | + the selected columns |
| 57 | + """ |
| 58 | + |
| 59 | + if columns is not None: |
| 60 | + ds = ds[columns] |
| 61 | + return ds |
| 62 | + |
| 63 | + |
| 64 | +def prepare_xy(ds): |
| 65 | + """ " Prepares dataset for usage as training data |
| 66 | + Parameters: |
| 67 | + ----------- |
| 68 | + ds: pandas dataframe, required |
| 69 | +
|
| 70 | + Returns: |
| 71 | + -------- |
| 72 | + Pandas dataframe object processed for training |
| 73 | + with columns "aptamer_sequence", "target_sequence", |
| 74 | + "new_affinity" and a total of 709 rows |
| 75 | + """ |
| 76 | + ds.dropna( |
| 77 | + subset=["aptamer_sequence", "target_sequence", "new_affinity"], inplace=True |
| 78 | + ) |
| 79 | + ds = ds[["aptamer_sequence", "target_sequence", "new_affinity"]] |
| 80 | + return ds |
| 81 | + |
| 82 | + |
| 83 | +def load_aptacom_full(select_columns=None): |
| 84 | + """Loads a AptaCom dataset from hugging face |
| 85 | + with customizable options. |
| 86 | +
|
| 87 | + Parameters: |
| 88 | + ----------- |
| 89 | + select_columns: list, optional, default=None |
| 90 | + A list used to filter the columns dataset features. |
| 91 | + Defaults to empty, which returns the complete dataset. |
| 92 | + Column names: |
| 93 | + ['reference', |
| 94 | + 'aptamer_chemistry', |
| 95 | + 'aptamer_name', |
| 96 | + 'target_name', |
| 97 | + 'aptamer_sequence', |
| 98 | + 'origin', |
| 99 | + 'target_chemistry', |
| 100 | + 'external_id', |
| 101 | + 'target_sequence', |
| 102 | + 'new_affinity'] |
| 103 | +
|
| 104 | + Returns: |
| 105 | + -------- |
| 106 | + object: A pandas dataframe with 5556 rows in total. |
| 107 | + The returned object contains the dataset, possibly |
| 108 | + filtered with different columns. |
| 109 | + """ |
| 110 | + aptacom = load_hf_dataset("AptaCom", store=False) |
| 111 | + dataset = filter_columns(aptacom, columns=select_columns) |
| 112 | + |
| 113 | + return dataset |
| 114 | + |
| 115 | + |
| 116 | +def load_aptacom_xy(return_X_y=False): |
| 117 | + """Loads Aptacom dataset for training |
| 118 | +
|
| 119 | + Parameters: |
| 120 | + ---------- |
| 121 | + return_X_y: bool, optional, default = False |
| 122 | + If true returns X (aptamer and target sequence) |
| 123 | + and y (new_affinity) otherwise returns a |
| 124 | + pandas dataframe containing the three columns |
| 125 | +
|
| 126 | + Returns: |
| 127 | + -------- |
| 128 | + Either a pandas dataframe with three columns |
| 129 | + or two pandas dataframe objects with two and one |
| 130 | + columns respectively. |
| 131 | + """ |
| 132 | + aptacom = load_hf_dataset("AptaCom", store=False) |
| 133 | + dataset = prepare_xy(aptacom) |
| 134 | + if return_X_y: |
| 135 | + X = dataset[["aptamer_sequence", "target_sequence"]] |
| 136 | + y = dataset[["new_affinity"]] |
| 137 | + return X, y |
| 138 | + return dataset |
0 commit comments