0

我想将库Figue中的返回数组转换为D3 Phylogram。我构建了这个函数,但没有工作:

function convert(input, rootName) {
        // top level
        if (Array.isArray(input)) {
            return {
                "name": rootName,
                "branchset": input.map(convert)
            };
        }
        // node
        else {
            ['left', 'right'].forEach(function(side) {
                if (input[side]) {
                    input.branchset = input.branchset || [];
                    input.branchset.push(convert(input[side]));
                    delete input[side];
                }
            });
            return input;
        }
    }

下面是D3的数据示例:

{
        "branchset": [
          {
            "name": "", "length": 0.05,
            "branchset": [
              {
                "name": "", "length": 0.05,
                "branchset": [ { "name": "A", "length": 0.05 }, { "name": "B", "length": 0.05 } ]
              },
              {
                "name": "", "length": 0.05,
                "branchset": [ { "name": "C", "length": 0.05 }, { "name": "D", "length": 0.05 } ]
              }
            ]      
          },
          {
            "name": "", "length": 0.1, 
            "branchset": [ { "name": "E", "length": 0.05 }, { "name": "F", "length": 0.05 }
            ]
          },
          {
            "name": "", "length": 0.05,
            "branchset": [
              {
                "name": "", "length": 0.05,
                "branchset": [ { "name": "G", "length": 0.05 }, { "name": "H", "length": 0.05 } ]         
              },
              { "name": "I", "length": 0.1 }
            ]
          }
        ],
        "name": ""
      } 

在这里,从Figue返回的数组和平(使用我上面的函数):

{"label":-1,"dist":2.0373900000000003,"centroid":[0.0000027272727272704085,-0.0000018181818181937295],"size":11,"depth":6,"branchset":[{"label":-1,"dist":2.0373900000000003,"centroid":[-0.10734300000000001,-0.18521900000000002],"size":10,"depth":5,"branchset":[{"label":-1,"dist":1.38918,"centroid":[-0.00701333333333334,-0.4115955555555556],"size":9,"depth":4,"branchset":[{"label":-1,"dist":0,"centroid":[-1.01031,-0.18522],"size":4,"depth":3,"branchset":[{"label":-1,"dist":0,"centroid":[-1.01031,-0.18522000000000002],"size":3,"depth":2, ....
4

0 回答 0