conversion newick to graphml using python












1















I would like to convert a tree from newick to a format like graphml, that I can open with cytoscape.



So, I have a file "small.newick" that contain:



((raccoon:1,bear:6):0.8,((sea_lion:11.9, seal:12):7,((monkey:100,cat:47):20, weasel:18):2):3,dog:25);


So far, I did that way (Python 3.6.5 |Anaconda):



from Bio import Phylo
import networkx
Tree = Phylo.read("small.newick", 'newick')
G = Phylo.to_networkx(Tree)
networkx.write_graphml(G, 'small.graphml')


image1



There is a problem with the Clade, that I can fix using this code:



from Bio import Phylo
import networkx

def clade_names_fix(tree):
for idx, clade in enumerate(tree.find_clades()):
if not clade.name:
clade.name=str(idx)

Tree = Phylo.read("small.newick", 'newick')
clade_names_fix(Tree)
G = Phylo.to_networkx(Tree)
networkx.write_graphml(G, 'small.graphml')


Giving me something that seem nice enough:



image2



My questions are:




  • Is that a good way to do it? It seem weird to me that the function does not take care of the internal node names


  • If you replace one node name with a string long enough, it will be trimmed by the command Phylo.to_networkx(Tree). How to avoid that?



Example: substitution of "dog" by "test_tring_that_create_some_problem_later_on"



image3










share|improve this question





























    1















    I would like to convert a tree from newick to a format like graphml, that I can open with cytoscape.



    So, I have a file "small.newick" that contain:



    ((raccoon:1,bear:6):0.8,((sea_lion:11.9, seal:12):7,((monkey:100,cat:47):20, weasel:18):2):3,dog:25);


    So far, I did that way (Python 3.6.5 |Anaconda):



    from Bio import Phylo
    import networkx
    Tree = Phylo.read("small.newick", 'newick')
    G = Phylo.to_networkx(Tree)
    networkx.write_graphml(G, 'small.graphml')


    image1



    There is a problem with the Clade, that I can fix using this code:



    from Bio import Phylo
    import networkx

    def clade_names_fix(tree):
    for idx, clade in enumerate(tree.find_clades()):
    if not clade.name:
    clade.name=str(idx)

    Tree = Phylo.read("small.newick", 'newick')
    clade_names_fix(Tree)
    G = Phylo.to_networkx(Tree)
    networkx.write_graphml(G, 'small.graphml')


    Giving me something that seem nice enough:



    image2



    My questions are:




    • Is that a good way to do it? It seem weird to me that the function does not take care of the internal node names


    • If you replace one node name with a string long enough, it will be trimmed by the command Phylo.to_networkx(Tree). How to avoid that?



    Example: substitution of "dog" by "test_tring_that_create_some_problem_later_on"



    image3










    share|improve this question



























      1












      1








      1








      I would like to convert a tree from newick to a format like graphml, that I can open with cytoscape.



      So, I have a file "small.newick" that contain:



      ((raccoon:1,bear:6):0.8,((sea_lion:11.9, seal:12):7,((monkey:100,cat:47):20, weasel:18):2):3,dog:25);


      So far, I did that way (Python 3.6.5 |Anaconda):



      from Bio import Phylo
      import networkx
      Tree = Phylo.read("small.newick", 'newick')
      G = Phylo.to_networkx(Tree)
      networkx.write_graphml(G, 'small.graphml')


      image1



      There is a problem with the Clade, that I can fix using this code:



      from Bio import Phylo
      import networkx

      def clade_names_fix(tree):
      for idx, clade in enumerate(tree.find_clades()):
      if not clade.name:
      clade.name=str(idx)

      Tree = Phylo.read("small.newick", 'newick')
      clade_names_fix(Tree)
      G = Phylo.to_networkx(Tree)
      networkx.write_graphml(G, 'small.graphml')


      Giving me something that seem nice enough:



      image2



      My questions are:




      • Is that a good way to do it? It seem weird to me that the function does not take care of the internal node names


      • If you replace one node name with a string long enough, it will be trimmed by the command Phylo.to_networkx(Tree). How to avoid that?



      Example: substitution of "dog" by "test_tring_that_create_some_problem_later_on"



      image3










      share|improve this question
















      I would like to convert a tree from newick to a format like graphml, that I can open with cytoscape.



      So, I have a file "small.newick" that contain:



      ((raccoon:1,bear:6):0.8,((sea_lion:11.9, seal:12):7,((monkey:100,cat:47):20, weasel:18):2):3,dog:25);


      So far, I did that way (Python 3.6.5 |Anaconda):



      from Bio import Phylo
      import networkx
      Tree = Phylo.read("small.newick", 'newick')
      G = Phylo.to_networkx(Tree)
      networkx.write_graphml(G, 'small.graphml')


      image1



      There is a problem with the Clade, that I can fix using this code:



      from Bio import Phylo
      import networkx

      def clade_names_fix(tree):
      for idx, clade in enumerate(tree.find_clades()):
      if not clade.name:
      clade.name=str(idx)

      Tree = Phylo.read("small.newick", 'newick')
      clade_names_fix(Tree)
      G = Phylo.to_networkx(Tree)
      networkx.write_graphml(G, 'small.graphml')


      Giving me something that seem nice enough:



      image2



      My questions are:




      • Is that a good way to do it? It seem weird to me that the function does not take care of the internal node names


      • If you replace one node name with a string long enough, it will be trimmed by the command Phylo.to_networkx(Tree). How to avoid that?



      Example: substitution of "dog" by "test_tring_that_create_some_problem_later_on"



      image3







      python networkx bioconductor phylogeny cytoscape






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 11:45







      Gildas

















      asked Nov 13 '18 at 11:37









      GildasGildas

      38429




      38429
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Looks like you got pretty far on this already. I can only suggest a few alternatives/extensions to your approach...





          1. Unfortunately, I couldn't find a Cytoscape app that can read this format. I tried searching for PHYLIP, NEWICK and PHYLO. You might have more luck:




            • http://apps.cytoscape.org/




          2. There is an old Cytoscape 2.x plugin that could read this format, but to run this you would need to install Cytoscape 2.8.3, import the network, then export as xGMML (or save as CYS) and then try to open in Cytoscape 3.7 in order to migrate back into the land of living code. Then again, if 2.8.3 does what you need for this particular case, then maybe you don't need to migrate:




            • http://apps.cytoscape.org/apps/phylotree




          3. The best approach is programmatic, which you already explored. Finding an R or Python package that turns NEWICK into iGraph or GraphML is a solid strategy. Note that there are updated and slick Cytoscape libs in those languages as well, so you can do all label cleanup, layout, data visualization, analysis, export, etc all within the scripting environment:




            • https://bioconductor.org/packages/release/bioc/html/RCy3.html

            • https://py2cytoscape.readthedocs.io/en/latest/








          share|improve this answer
























          • Hi. Thank you for the time you spend. I actually had the same approach, and also did not find any app. I think sing an old version of cytoscape is not a good idea, and if this package was not maintained, if is also no good solution. I went to python and R, and actually developed two little scripts that do the job. I think I will share that here, in case someone also need such conversion.

            – Gildas
            Nov 18 '18 at 20:47



















          0














          After some research, I actually found a solution that work.
          I decided to provide the link here for you, dear reader:
          going to github






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280197%2fconversion-newick-to-graphml-using-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Looks like you got pretty far on this already. I can only suggest a few alternatives/extensions to your approach...





            1. Unfortunately, I couldn't find a Cytoscape app that can read this format. I tried searching for PHYLIP, NEWICK and PHYLO. You might have more luck:




              • http://apps.cytoscape.org/




            2. There is an old Cytoscape 2.x plugin that could read this format, but to run this you would need to install Cytoscape 2.8.3, import the network, then export as xGMML (or save as CYS) and then try to open in Cytoscape 3.7 in order to migrate back into the land of living code. Then again, if 2.8.3 does what you need for this particular case, then maybe you don't need to migrate:




              • http://apps.cytoscape.org/apps/phylotree




            3. The best approach is programmatic, which you already explored. Finding an R or Python package that turns NEWICK into iGraph or GraphML is a solid strategy. Note that there are updated and slick Cytoscape libs in those languages as well, so you can do all label cleanup, layout, data visualization, analysis, export, etc all within the scripting environment:




              • https://bioconductor.org/packages/release/bioc/html/RCy3.html

              • https://py2cytoscape.readthedocs.io/en/latest/








            share|improve this answer
























            • Hi. Thank you for the time you spend. I actually had the same approach, and also did not find any app. I think sing an old version of cytoscape is not a good idea, and if this package was not maintained, if is also no good solution. I went to python and R, and actually developed two little scripts that do the job. I think I will share that here, in case someone also need such conversion.

              – Gildas
              Nov 18 '18 at 20:47
















            1














            Looks like you got pretty far on this already. I can only suggest a few alternatives/extensions to your approach...





            1. Unfortunately, I couldn't find a Cytoscape app that can read this format. I tried searching for PHYLIP, NEWICK and PHYLO. You might have more luck:




              • http://apps.cytoscape.org/




            2. There is an old Cytoscape 2.x plugin that could read this format, but to run this you would need to install Cytoscape 2.8.3, import the network, then export as xGMML (or save as CYS) and then try to open in Cytoscape 3.7 in order to migrate back into the land of living code. Then again, if 2.8.3 does what you need for this particular case, then maybe you don't need to migrate:




              • http://apps.cytoscape.org/apps/phylotree




            3. The best approach is programmatic, which you already explored. Finding an R or Python package that turns NEWICK into iGraph or GraphML is a solid strategy. Note that there are updated and slick Cytoscape libs in those languages as well, so you can do all label cleanup, layout, data visualization, analysis, export, etc all within the scripting environment:




              • https://bioconductor.org/packages/release/bioc/html/RCy3.html

              • https://py2cytoscape.readthedocs.io/en/latest/








            share|improve this answer
























            • Hi. Thank you for the time you spend. I actually had the same approach, and also did not find any app. I think sing an old version of cytoscape is not a good idea, and if this package was not maintained, if is also no good solution. I went to python and R, and actually developed two little scripts that do the job. I think I will share that here, in case someone also need such conversion.

              – Gildas
              Nov 18 '18 at 20:47














            1












            1








            1







            Looks like you got pretty far on this already. I can only suggest a few alternatives/extensions to your approach...





            1. Unfortunately, I couldn't find a Cytoscape app that can read this format. I tried searching for PHYLIP, NEWICK and PHYLO. You might have more luck:




              • http://apps.cytoscape.org/




            2. There is an old Cytoscape 2.x plugin that could read this format, but to run this you would need to install Cytoscape 2.8.3, import the network, then export as xGMML (or save as CYS) and then try to open in Cytoscape 3.7 in order to migrate back into the land of living code. Then again, if 2.8.3 does what you need for this particular case, then maybe you don't need to migrate:




              • http://apps.cytoscape.org/apps/phylotree




            3. The best approach is programmatic, which you already explored. Finding an R or Python package that turns NEWICK into iGraph or GraphML is a solid strategy. Note that there are updated and slick Cytoscape libs in those languages as well, so you can do all label cleanup, layout, data visualization, analysis, export, etc all within the scripting environment:




              • https://bioconductor.org/packages/release/bioc/html/RCy3.html

              • https://py2cytoscape.readthedocs.io/en/latest/








            share|improve this answer













            Looks like you got pretty far on this already. I can only suggest a few alternatives/extensions to your approach...





            1. Unfortunately, I couldn't find a Cytoscape app that can read this format. I tried searching for PHYLIP, NEWICK and PHYLO. You might have more luck:




              • http://apps.cytoscape.org/




            2. There is an old Cytoscape 2.x plugin that could read this format, but to run this you would need to install Cytoscape 2.8.3, import the network, then export as xGMML (or save as CYS) and then try to open in Cytoscape 3.7 in order to migrate back into the land of living code. Then again, if 2.8.3 does what you need for this particular case, then maybe you don't need to migrate:




              • http://apps.cytoscape.org/apps/phylotree




            3. The best approach is programmatic, which you already explored. Finding an R or Python package that turns NEWICK into iGraph or GraphML is a solid strategy. Note that there are updated and slick Cytoscape libs in those languages as well, so you can do all label cleanup, layout, data visualization, analysis, export, etc all within the scripting environment:




              • https://bioconductor.org/packages/release/bioc/html/RCy3.html

              • https://py2cytoscape.readthedocs.io/en/latest/









            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 20:30









            AlexanderPicoAlexanderPico

            1255




            1255













            • Hi. Thank you for the time you spend. I actually had the same approach, and also did not find any app. I think sing an old version of cytoscape is not a good idea, and if this package was not maintained, if is also no good solution. I went to python and R, and actually developed two little scripts that do the job. I think I will share that here, in case someone also need such conversion.

              – Gildas
              Nov 18 '18 at 20:47



















            • Hi. Thank you for the time you spend. I actually had the same approach, and also did not find any app. I think sing an old version of cytoscape is not a good idea, and if this package was not maintained, if is also no good solution. I went to python and R, and actually developed two little scripts that do the job. I think I will share that here, in case someone also need such conversion.

              – Gildas
              Nov 18 '18 at 20:47

















            Hi. Thank you for the time you spend. I actually had the same approach, and also did not find any app. I think sing an old version of cytoscape is not a good idea, and if this package was not maintained, if is also no good solution. I went to python and R, and actually developed two little scripts that do the job. I think I will share that here, in case someone also need such conversion.

            – Gildas
            Nov 18 '18 at 20:47





            Hi. Thank you for the time you spend. I actually had the same approach, and also did not find any app. I think sing an old version of cytoscape is not a good idea, and if this package was not maintained, if is also no good solution. I went to python and R, and actually developed two little scripts that do the job. I think I will share that here, in case someone also need such conversion.

            – Gildas
            Nov 18 '18 at 20:47













            0














            After some research, I actually found a solution that work.
            I decided to provide the link here for you, dear reader:
            going to github






            share|improve this answer




























              0














              After some research, I actually found a solution that work.
              I decided to provide the link here for you, dear reader:
              going to github






              share|improve this answer


























                0












                0








                0







                After some research, I actually found a solution that work.
                I decided to provide the link here for you, dear reader:
                going to github






                share|improve this answer













                After some research, I actually found a solution that work.
                I decided to provide the link here for you, dear reader:
                going to github







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 18 '18 at 22:03









                GildasGildas

                38429




                38429






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280197%2fconversion-newick-to-graphml-using-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine