# Avoid problem using a custom distance function in ClusteringTree?

**URL:** <https://community.wolfram.com/t/avoid-problem-using-a-custom-distance-function-in-clusteringtree/9746>\
**Category:** Questions\
**Tags:** biological-sciences, graphs-and-networks, statistics-and-probability, machine-learning\
**Created:** [December 12, 2016, 3:13am UTC](https://community.wolfram.com/t/avoid-problem-using-a-custom-distance-function-in-clusteringtree/9746 "2016-12-12T03:13:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlosernesto](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/carlosernesto/32/10000_2.png) [@carlosernesto](https://community.wolfram.com/u/carlosernesto)\
**Post date:** [December 12, 2016, 3:13am UTC](https://community.wolfram.com/t/avoid-problem-using-a-custom-distance-function-in-clusteringtree/9746/1 "2016-12-12T03:13:06Z")

</div>

I’ve been trying to use the function ClusteringTree to test how well it works with DNA sequences. This function uses edit distance to group sequences, and to make it more “realistic” I tried to use a custom distance function which takes into account gaps (right now I have included a gap opening cost, which is high, and a gap extension cost, which is low, at the end of the post is the custom distance function).

When I use the option DistanceFunction with my custom function the following error message appears:

"ClusteringTree was unable to compute positive and real pairwise distances for … "

I also tried with the built in function HammingDistance, just to check, and I got the same result, so I don’t think the problem was with my custom function.

I would appreciate if someone has an idea of how to include a custom distance function in ClusteringTree, Dendrogram, or a way which to use it in hierarchical clustering.

Thanks

If it helps, the code I used was:

```
ClusteringTree[data, 
 DistanceFunction -> dnaDistance2]

```

And the distance function I used is:

```
dnaDistance2[s1_, s2_] :=
 
 Block[
  
  {misM = OptionValue["mismatchScore"], gapOp = 10, gapExt = .1, 
   iUBsymbols = {"A", "T", "C", "G", "W", "S", "M", "K", "R", "Y", 
     "B", "D", "H", "V", "N", "X"}, prescore, substitutionscore, 
   gapScore, totalScore},
  
  prescore = 
   Transpose[
     Characters[{s1, s2}]] /. {{x_, x_} /; x != "-" -> 
      0, {a_, b_} /; ContainsAll[iUBsymbols, {a, b}] && a != b -> 
      misM};
  
  substitutionscore = Total[Cases[prescore, _Integer]];
  
  gapScore = Total[
    Abs[gapOp*SequenceCount[#, {"DG" ..}] - gapOp] + 
       gapExt*Count[#, x_ /; x != "DG"]
      & /@
     Flatten[
      Split[#, 
         If[{#1, #2} === {"G2", "G1"} || {#1, #2} === {"G1", "G2"}, 
           False, True] &] & /@
       SequenceCases[
        (prescore /. {{"-", "-"} -> "DG", {"-", _} -> 
            "G1", {_, "-"} -> "G2"}),
        {Repeated["G1" | "G2" | "DG"]}]
      , 1]
    ];
  
  totalScore = substitutionscore + gapScore;
  
  Ceiling[totalScore]
  ]

```

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [December 12, 2016, 1:39pm UTC](https://community.wolfram.com/t/avoid-problem-using-a-custom-distance-function-in-clusteringtree/9746/2 "2016-12-12T13:39:43Z")

</div>

That’s because of this I think:

```
EuclideanDistance[1, 2]
HammingDistance[1, 2]

```

the first one gives a number, while the second doesn’t… So in order to fix it you have to put data in a certain format:

```
ClusteringTree[{1, 2, 5}, DistanceFunction -> EuclideanDistance]
ClusteringTree[{{1}, {2}, {5}}, DistanceFunction -> HammingDistance]

```

---

<div class="post-metadata">

**Author:** ![carlosernesto](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/carlosernesto/32/10000_2.png) [@carlosernesto](https://community.wolfram.com/u/carlosernesto)\
**Post date:** [December 14, 2016, 8:14pm UTC](https://community.wolfram.com/t/avoid-problem-using-a-custom-distance-function-in-clusteringtree/9746/3 "2016-12-14T20:14:16Z")

</div>

Thanks!  
My data was an association with strings as values, so I transformed them into lists with one element, as in your example, and it worked.

---

<div class="post-metadata">

**Author:** ![carlosernesto](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/carlosernesto/32/10000_2.png) [@carlosernesto](https://community.wolfram.com/u/carlosernesto)\
**Post date:** [December 15, 2016, 7:36am UTC](https://community.wolfram.com/t/avoid-problem-using-a-custom-distance-function-in-clusteringtree/9746/4 "2016-12-15T07:36:33Z")

</div>

Update:  
The results I got were a bit odd, and I though it was because of my distance function, but when I checked my distance function with the data in a different format it gave me always a distance of 0.

---

<div class="post-metadata">

**Author:** ![carlosernesto](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/carlosernesto/32/10000_2.png) [@carlosernesto](https://community.wolfram.com/u/carlosernesto)\
**Post date:** [December 15, 2016, 7:56am UTC](https://community.wolfram.com/t/avoid-problem-using-a-custom-distance-function-in-clusteringtree/9746/5 "2016-12-15T07:56:18Z")

</div>

Last update:

I just realized I was using the wrong set of data, haha.

If it helps to explain, in case anyone has a similar problem, I was using sequences of different lengths when my custom function only worked with sequences of equal lengths.
