Skip to content

Commit

Permalink
Merge pull request #40 from clockor2/documentation
Browse files Browse the repository at this point in the history
📝 add fetch example
  • Loading branch information
LeoFeatherstone authored Oct 1, 2023
2 parents bc8e10c + 73c3260 commit a754bee
Show file tree
Hide file tree
Showing 6 changed files with 2,800 additions and 254 deletions.
15 changes: 15 additions & 0 deletions docs/examples/fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Here, we demonstrate how to read in a tree from a url using a `fetch()` request from the fetch API. In this case, we are reading in a list of trees used for testing.

```typescript
let url = 'https://raw.githubusercontent.com/clockor2/phylojs/main/test/data/egTree.nwk'
let newick;

fetch(url)
.then(res => res.text())
.then(txt => {newick = txt})

// log first 99 characters to show newick is defined
console.log(newick.slice(0,99))
// Returns:
// (Jeddah-1_KF917527_camel_2013-11-08:0.0000013865,Jeddah-1_KF958702_human_2013-11-05:0.0000013652,((
```
39 changes: 38 additions & 1 deletion docs/examples/test/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//////// Testing examples for documentation /////////
/////////////////////////////////////////////////////

import { readNewick, readTreesFromPhyloXML, writeNewick, Tree } from '@phylojs';
import { readNewick, readTreesFromPhyloXML, readTreesFromNewick, writeNewick, Tree } from '@phylojs';

describe('Examples', () => {
test('RTTR', () => {
Expand Down Expand Up @@ -192,6 +192,43 @@ describe('Examples', () => {
expect(inNewick).not.toEqual(outNewick)
})

test('multiple trees with newick', () => {
// Using two small trees here

const inNewick = '((A:1,B:1):1,C:1);\n((A:1,B:1):1,C:1);'
let trees = readTreesFromNewick(inNewick);

// Operate on trees using array methods. E.g. Reroot, ladderise, and scale branch lengths randomly

trees.forEach(t => t.reroot(t.nodeList[4])) // arbitrarily to 4th node
trees.forEach(t => t.ladderise())
trees.forEach(t => t.nodeList.forEach(
n => n.branchLength ? n.branchLength *= Math.floor(10*Math.random() + 1) : 0
))

// write output
let outNewick = trees.map(t => writeNewick(t)).join('\n')

expect(inNewick).not.toEqual(outNewick)
})

// COMMENTED OUT BC JEST DOESN'T LIKE FETCH API
// test('multiple trees with fetch', () => {
// // Using two small trees here

// let url = 'https://raw.githubusercontent.com/clockor2/phylojs/main/test/data/egTree.nwk'
// let newick;

// fetch(url)
// .then(res => res.text())
// .then(txt => {newick = txt})

// // log first 99 characters to show newick is defined
// console.log(newick.slice(0,99))
// // Returns:
// // (Jeddah-1_KF917527_camel_2013-11-08:0.0000013865,Jeddah-1_KF958702_human_2013-11-05:0.0000013652,((
// })

test('getBranchLengthRatio()', () => {
function getBranchLengthRatio(tree: Tree): number {

Expand Down
7 changes: 5 additions & 2 deletions docs/examples/treeArray.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ In the below example, we parse two trees from phyloXML format, reroot them, resc
</phylogeny>
</phyloxml>`;

let in = fetch('https://raw.githubusercontent.com/clockor2/phylojs/main/test/data/egTree.nwk')
.then(res => res.text())
.then(nwk => console.log(nwk))
// Read trees
let trees = readTreesFromPhyloXML(inPhyloXML);

//let trees = readTreesFromPhyloXML(inPhyloXML);
// Operate on trees using array methods. E.g. reroot, ladderise, and scale branch lengths randomly
trees.forEach(t => t.reroot(t.nodeList[4])) // arbitrarily on branch to 4th node
trees.forEach(t => t.ladderise())
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ nav:
- examples/index.md
- Overview of the Tree Class: examples/tree.md
- Visualise: examples/visualise.md
- Fetching Trees fron a URL: examples/fetch.md
- Finding the MRCA: examples/mrca.md
- Root-to-tip regression: examples/rttr.md
- Rerooting: examples/reroot.md
Expand Down
Loading

0 comments on commit a754bee

Please sign in to comment.