44
55import numpy as np
66import pandas as pd
7- from glycowork .glycan_data .loader import df_species as taxonomy
7+ from glycowork .glycan_data .loader import df_species , df_glycan , build_custom_df
88from tqdm import tqdm
99
1010from gifflar .utils import iupac2smiles
1111
1212
13- def get_taxonomy (root : Path | str ) -> pd .DataFrame :
14- """
15- Download full taxonomy data, process it, and save it as a tsv file.
16-
17- Args:
18- root: The root directory to save the data to.
19-
20- Returns:
21- The processed taxonomy data.
22- """
23- if not (p := (root / Path ("taxonomy.tsv" ))).exists ():
24- mask = []
25- # convert to IUPAC to SMILES and build a mask to remove not-convertable molecules.
26- for i in tqdm (taxonomy ["glycan" ]):
27- smiles = iupac2smiles (i )
28- mask .append (smiles is not None )
29- tax = taxonomy [mask ]
30- tax .to_csv (p , sep = "\t " , index = False )
31- return pd .read_csv (p , sep = "\t " )
32-
33-
3413def get_taxonomic_level (
3514 root : Path | str ,
3615 level : Literal ["Domain" , "Kingdom" , "Phylum" , "Class" , "Order" , "Family" , "Genus" , "Species" ]
@@ -47,7 +26,7 @@ def get_taxonomic_level(
4726 """
4827 if not (p := (root / Path (f"taxonomy_{ level } .tsv" ))).exists ():
4928 # Chop to taxonomic level of interest and remove invalid rows
50- tax = get_taxonomy ( root ) [["glycan" , level ]]
29+ tax = df_species [["glycan" , level ]]
5130 tax .rename (columns = {"glycan" : "IUPAC" }, inplace = True )
5231 tax [tax [level ] == "undetermined" ] = np .nan
5332 tax .dropna (inplace = True )
@@ -83,45 +62,17 @@ def get_tissue(root: Path | str) -> Path:
8362 """
8463 if not (p := (root / Path ("tissue.tsv" ))).exists ():
8564 # Process the data and remove unnecessary columns
86- df = pd .read_csv ("datasets/tissue_multilabel_df.csv" )
87- df .rename (columns = {"glycan" : "IUPAC" }, inplace = True )
88- df .dropna (inplace = True )
89-
90- df ["split" ] = np .random .choice (["train" , "val" , "test" ], df .shape [0 ], p = [0.7 , 0.2 , 0.1 ])
91- df .to_csv (p , sep = "\t " , index = False )
92- return p
93-
94-
95- def get_immunogenicity (root : Path | str ) -> Path :
96- """
97- Download immunogenicity data, process it, and save it as a tsv file.
98-
99- Args:
100- root: The root directory to save the data to.
101-
102- Returns:
103- The filepath of the processed immunogenicity data.
104- """
105- root = Path (root )
106- if not (p := (root / "immunogenicity.tsv" )).exists ():
107- # Download the data
108- urllib .request .urlretrieve ("https://torchglycan.s3.us-east-2.amazonaws.com/downstream/glycan_immunogenicity.csv" , p .with_suffix (".csv" ))
109-
110- # Process the data and remove unnecessary columns
111- df = pd .read_csv (p .with_suffix (".csv" ))[["glycan" , "immunogenicity" ]]
65+ df = build_custom_df (df_glycan , "df_tissue" )[["glycan" , "tissue_sample" ]]
11266 df .rename (columns = {"glycan" : "IUPAC" }, inplace = True )
67+ df .drop_duplicates (inplace = True )
11368 df .dropna (inplace = True )
11469
11570 # One-hot encode the individual classes and collate them for glycans that are the same
116- classes = {n : i for i , n in enumerate (df ["immunogenicity" ].unique ())}
117- df ["label" ] = df ["immunogenicity" ].map (classes )
118- df ["split" ] = np .random .choice (["train" , "val" , "test" ], df .shape [0 ], p = [0.7 , 0.2 , 0.1 ])
71+ df = pd .concat ([df ["IUPAC" ], pd .get_dummies (df ["tissue_sample" ])], axis = 1 )
72+ df = df .groupby ('IUPAC' ).agg ("sum" ).reset_index ()
11973
120- df . drop ( "immunogenicity " , axis = 1 , inplace = True )
74+ df [ "split" ] = np . random . choice ([ "train " , "val" , "test" ], df . shape [ 0 ], p = [ 0.7 , 0.2 , 0.1 ] )
12175 df .to_csv (p , sep = "\t " , index = False )
122- with open (root / "immunogenicity_classes.tsv" , "w" ) as f :
123- for n , i in classes .items ():
124- print (n , i , sep = "\t " , file = f )
12576 return p
12677
12778
@@ -137,16 +88,15 @@ def get_glycosylation(root: Path | str) -> Path:
13788 """
13889 root = Path (root )
13990 if not (p := root / "glycosylation.tsv" ).exists ():
140- urllib .request .urlretrieve ("https://torchglycan.s3.us-east-2.amazonaws.com/downstream/glycan_properties.csv" , p .with_suffix (".csv" ))
141- df = pd .read_csv (p .with_suffix (".csv" ))[["glycan" , "link" ]]
91+ df = df_glycan [["glycan" , "glycan_type" ]]
14292 df .rename (columns = {"glycan" : "IUPAC" }, inplace = True )
14393 df .dropna (inplace = True )
14494
145- classes = {n : i for i , n in enumerate (df ["link " ].unique ())}
146- df ["label" ] = df ["link " ].map (classes )
95+ classes = {n : i for i , n in enumerate (df ["glycan_type " ].unique ())}
96+ df ["label" ] = df ["glycan_type " ].map (classes )
14797 df ["split" ] = np .random .choice (["train" , "val" , "test" ], df .shape [0 ], p = [0.7 , 0.2 , 0.1 ])
14898
149- df .drop ("link " , axis = 1 , inplace = True )
99+ df .drop ("glycan_type " , axis = 1 , inplace = True )
150100 df .to_csv (p , sep = "\t " , index = False )
151101 with open (root / "glycosylation_classes.tsv" , "w" ) as f :
152102 for n , i in classes .items ():
@@ -164,12 +114,12 @@ def get_spectrum(root: Path | str) -> Path:
164114 Returns:
165115 The filepath of the processed spectrum data.
166116 """
167- root = Path (root )
168- #if not (p := root / "spectrum.tsv").exists():
169- p = root / "spectrum.tsv"
170- df = pd .read_csv (Path ("/" ) / "scratch" / "SCRATCH_SAS" / "roman" / "Gothenburg" / "GIFFLAR" / "spectrum_pred_512_small .tsv" , sep = "\t " )
171- df .to_csv (p , sep = "\t " , index = False )
172- return p
117+ # root = Path(root)
118+ # if not (p := root / "spectrum.tsv").exists():
119+ # p = root / "spectrum.tsv"
120+ # df = pd.read_csv(Path("/") / "scratch" / "SCRATCH_SAS" / "roman" / "Gothenburg" / "GIFFLAR" / "spectrum_pred_2048 .tsv", sep="\t")
121+ # df.to_csv(p, sep="\t", index=False)
122+ return Path ( "/" ) / "scratch" / "SCRATCH_SAS" / "roman" / "Gothenburg" / "GIFFLAR" / "spectrum_pred_2048.tsv"
173123
174124
175125def get_dataset (data_config : dict , root : Path | str ) -> dict :
@@ -190,9 +140,7 @@ def get_dataset(data_config: dict, root: Path | str) -> dict:
190140 path = get_taxonomic_level (root , name_fracs [1 ])
191141 case "Tissue" :
192142 path = get_tissue (root )
193- case "Immunogenicity" :
194- path = get_immunogenicity (root )
195- case "Glycosylation" :
143+ case "Glycosylation" | "Linkage" :
196144 path = get_glycosylation (root )
197145 case "Spectrum" :
198146 path = get_spectrum (root )
0 commit comments