7
7
# ----------------------------------------------------------------------------
8
8
9
9
import skbio
10
+ import pandas as pd
10
11
11
12
from .. import LSMatFormat
12
13
@@ -24,3 +25,34 @@ def _1(data: skbio.DistanceMatrix) -> LSMatFormat:
24
25
@plugin .register_transformer
25
26
def _2 (ff : LSMatFormat ) -> skbio .DistanceMatrix :
26
27
return skbio .DistanceMatrix .read (str (ff ), format = 'lsmat' , verify = False )
28
+
29
+
30
+ @plugin .register_transformer
31
+ def _3 (ff : LSMatFormat ) -> pd .Series :
32
+ dm = skbio .DistanceMatrix .read (str (ff ), format = 'lsmat' , verify = False )
33
+ series = dm .to_series ()
34
+ assert series .size != 0 , ("Distance Matrix must contain more"
35
+ "than one sample" )
36
+ return series
37
+
38
+
39
+ @plugin .register_transformer
40
+ def _4 (data : pd .Series ) -> skbio .DistanceMatrix :
41
+ ids = data .index .get_level_values (0 ).unique ().union (
42
+ data .index .get_level_values (1 ).unique (), sort = False ).values
43
+ dm_df = pd .DataFrame (data = [], index = ids , columns = ids )
44
+ for index , row in dm_df .iterrows ():
45
+ dm_df .loc [index , index ] = float (0 )
46
+ for col in dm_df .columns :
47
+ if dm_df .loc [index , col ] != 0 :
48
+ try :
49
+ dm_df .loc [index , col ] = data [index , col ]
50
+ dm_df .loc [col , index ] = data [index , col ]
51
+ except KeyError :
52
+ dm_df .loc [index , col ] = data [col , index ]
53
+ dm_df .loc [col , index ] = data [col , index ]
54
+ dm = skbio .DistanceMatrix (dm_df , ids = dm_df .index )
55
+ ff = LSMatFormat ()
56
+ with ff .open () as fh :
57
+ dm .write (fh , format = 'lsmat' )
58
+ return ff
0 commit comments