Skip to content

Commit d79623d

Browse files
Add some missing Show instances
1 parent cff5312 commit d79623d

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/Bio/Phylogeny/Internal/PhyloXml.purs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ newtype XmlNode = XmlNode
4949

5050
derive instance eqXmlNode :: Eq XmlNode
5151

52+
instance showXmlNode :: Show XmlNode where
53+
show (XmlNode { name, value: Just v }) = name <> "=\"" <> v <> "\""
54+
show (XmlNode { name, attributes })
55+
| M.isEmpty attributes = name
56+
| otherwise = name <> ": " <> myShow attributes
57+
where
58+
myShow :: M.Map String Attribute -> String
59+
myShow attrs = "{"
60+
<>
61+
( foldl (\acc (k /\ v) -> acc <> " " <> k <> ": " <> show v) "" $
62+
(M.toUnfoldable attrs :: Array (Tuple String Attribute))
63+
)
64+
<> "}"
65+
5266
parsePhyloXml :: String -> Either ParseError Phylogeny
5367
parsePhyloXml input = runParser input phyloXmlParser
5468

@@ -57,15 +71,14 @@ phyloXmlParser = do
5771
tree' <- phyloxml
5872
case convert tree' of
5973
Right { meta, trees } -> do
74+
let withRefs = foldl updateRefs mempty trees
6075
pure
61-
$ toAnnotatedPhylogeny meta
62-
$ foldl
63-
( \acc@(PartialPhylogeny { maxRef }) p ->
64-
acc <> interpretIntermediate (maxRef + 1) p
65-
)
66-
mempty
67-
trees
76+
$ toAnnotatedPhylogeny meta withRefs
6877
Left err -> fail err
78+
where
79+
updateRefs :: PartialPhylogeny -> Tree PartialNode -> PartialPhylogeny
80+
updateRefs acc@(PartialPhylogeny { maxRef }) p =
81+
acc <> interpretIntermediate (maxRef + 1) p
6982

7083
branchLength :: M.Map String Attribute -> Maybe Number
7184
branchLength attrs =

src/Bio/Phylogeny/Internal/Types.purs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import Data.List as L
3232
import Data.Map as M
3333
import Data.Maybe (Maybe(..), fromMaybe)
3434
import Data.Traversable (class Traversable, sequenceDefault, traverse)
35-
import Data.Tuple (Tuple, uncurry)
35+
import Data.Tuple (Tuple(..))
3636
import Data.Tuple.Nested ((/\))
3737
import Parsing (ParserT)
3838

@@ -43,7 +43,7 @@ data Event
4343
| LateralGeneTransfer
4444
| Recombination
4545

46-
derive instance eqNodeType :: Eq Event
46+
derive instance eqEvent :: Eq Event
4747

4848
eventToString :: Event -> String
4949
eventToString Clade = "Clade"
@@ -52,6 +52,9 @@ eventToString Hybrid = "Hybrid"
5252
eventToString LateralGeneTransfer = "LateralGeneTransfer"
5353
eventToString Recombination = "Recombination"
5454

55+
instance showEvent :: Show Event where
56+
show = eventToString
57+
5558
type NodeName = String
5659

5760
type NodeIdentifier = Int
@@ -137,6 +140,13 @@ data Tree a
137140

138141
derive instance eqTree :: Eq a => Eq (Tree a)
139142

143+
instance showTree :: Show a => Show (Tree a) where
144+
show (Leaf l) = "L: " <> show l
145+
show (Internal v children) = "(I:" <> show v <> toStr children <> ")"
146+
where
147+
toStr :: forall x. Show x => Array x -> String
148+
toStr xs = foldl (\acc x -> acc <> " " <> show x) "" xs
149+
140150
instance functorTree :: Functor Tree where
141151
map f (Leaf n) = Leaf (f n)
142152
map f (Internal p cs) = Internal (f p) (map (map f) cs)
@@ -238,12 +248,12 @@ toPhylogeny (PartialPhylogeny phylogeny) =
238248

239249
toAnnotatedPhylogeny :: Array Metadata -> PartialPhylogeny -> Phylogeny
240250
toAnnotatedPhylogeny metadata (PartialPhylogeny phylogeny) =
241-
{ metadata: uncurry mergeMetadata <$> A.zip metadata phylogeny.metadata
251+
{ metadata: mergeMetadata <$> A.zip metadata phylogeny.metadata
242252
, network: fromMap phylogeny.network
243253
}
244254
where
245-
mergeMetadata :: Metadata -> Metadata -> Metadata
246-
mergeMetadata a b =
255+
mergeMetadata :: Tuple Metadata Metadata -> Metadata
256+
mergeMetadata (Tuple a b) =
247257
{ name: a.name <|> b.name
248258
, parent: max a.parent b.parent
249259
, rooted: a.rooted || b.rooted

0 commit comments

Comments
 (0)