Skip to content

Commit

Permalink
Move code in src/
Browse files Browse the repository at this point in the history
Use browserify to build a single js
  • Loading branch information
drpowell committed Sep 25, 2016
1 parent 04595d2 commit 356715f
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 84 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ help:

compile:
@echo "### Compiling .coffee to .js ###"
coffee -c .
browserify -t coffeeify src/main.coffee -o build.js

debug:
@echo "### Compiling .coffee to .js ###"
watchify -v --debug -t coffeeify src/main.coffee -o build.js

demo: compile
@echo "### Copying .example files ###"
Expand All @@ -31,4 +35,4 @@ install: demo
@echo "http://$(HOSTNAME)/~$(USER)/fripan/pan.html"




9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ FriPan is a web-based tool for exploring the pan-genome of multiple bacterial ge

## Installation

Ensure you have CoffeeScript >= 1.4 installed:
Ensure you have CoffeeScript >= 1.4 & browserify installed:

npm install -g coffee-script
npm install -g browserify

Install the code and run the demo code:
Install the code and build

git clone https://github.com/drpowell/FriPan
cd FriPan
npm install webworkify
make compile

Run the demo code:
make demo
./server.sh
firefox http://localhost:8030/pan.html
Expand Down
26 changes: 0 additions & 26 deletions mds-worker.coffee

This file was deleted.

18 changes: 1 addition & 17 deletions pan.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,7 @@
<link rel="stylesheet" type="text/css" href="pan.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">

<!-- jQuery -->
<script type="text/javascript" src="lib/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="lib/jquery-ui.js"></script>

<!-- D3 -->
<script type="text/javascript" src="lib/d3.v3.min.js"></script>
<script type="text/javascript" src="lib/colorbrewer.v1.min.js"></script>

<!-- numeric js -->
<script type="text/javascript" src="lib/numeric-1.2.6.js"></script>

<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="pan.js"></script>
<script type="text/javascript" src="tree.js"></script>
<script type="text/javascript" src="mds-plot.js"></script>
<script type="text/javascript" src="gene-matrix.js"></script>

<script type="text/javascript" src="build.js"></script>
</head>

<body>
Expand Down
3 changes: 2 additions & 1 deletion server.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh

echo "Run: coffee -c -w . &"
echo "Run: make compile"
echo " (or 'make debug' for development)"
echo "http://localhost:8030/pan.html"
python -mSimpleHTTPServer 8030

2 changes: 1 addition & 1 deletion gene-matrix.coffee → src/gene-matrix.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@ class GeneMatrix
@dispatch.order_changed() if @dispatch


@GeneMatrix=GeneMatrix
module.exports = GeneMatrix
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions src/main.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('./lib/jquery-1.8.2.min.js')
require('./lib/jquery-ui.js')
require('./lib/d3.v3.min.js')
# require('./lib/colorbrewer.v1.min.js')
# require('./lib/numeric-1.2.6.js')

require('./pan.coffee')
4 changes: 2 additions & 2 deletions mds-plot.coffee → src/mds-plot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,5 @@ class BarGraph
.attr("height", (d) => @height - @y(d.val))
.on('click', (d) => if @opts.click? then @opts.click(d))

@ScatterPlot = ScatterPlot
@BarGraph = BarGraph
module.exports.ScatterPlot = ScatterPlot
module.exports.BarGraph = BarGraph
34 changes: 34 additions & 0 deletions src/mds-worker.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# importScripts('util.js')
# importScripts('lib/numeric-1.2.6.js')
# importScripts('mds.js')
# importScripts('gene-matrix.js')

Util = require('./util.coffee')
MDS = require('./mds.coffee')
GeneMatrix = require('./gene-matrix.coffee')

process = (ev) ->
#console.log ev.data
if ev.data.init?
#console.log "WORKER: Initialized!"
@matrix = GeneMatrix.from_hash(ev.data.init)
else if ev.data.msg?
msg = ev.data.msg
if msg.mds?
range = msg.mds
#our_log "WORKER: #{range}"
comp = MDS.MDS.pca_gene(@matrix, range)
postMessage(comp)
else if msg.dist?
range = msg.dist
#our_log "WORKER: #{range}"
dist_arr = MDS.Distance.distance(@matrix, range)
postMessage(dist_arr)
else
Util.our_log "WORKER: Unknown method:",msg
else
Util.our_log "WORKER: Unknown message:",ev.data

module.exports = (self) ->
self.addEventListener('message',(ev) -> process(ev))
10 changes: 6 additions & 4 deletions mds.coffee → src/mds.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
numeric = require('./lib/numeric-1.2.6.js')
Util = require('./util.coffee')

class MDS
# The "data" are rows, the "dimensions" are columns
# Matrix has 1 row per "strain", 1 column per "cluster". Each entry is a boolean
Expand Down Expand Up @@ -79,10 +82,9 @@ class Distance
#console.log "matrix(c("+dist.map((r) -> ""+r)+"), byrow=T, nrow=#{dist.length}"
#mat.strains().forEach((s1,i) -> mat.strains().map((s2,j) -> console.log s1,s2,dist[i][j]))
#console.log mat.strains(),dist[0]
our_log "Distance took : #{new Date - t1}ms"
Util.our_log "Distance took : #{new Date - t1}ms"

dist


@MDS = MDS
@Distance = Distance
module.exports.MDS = MDS
module.exports.Distance = Distance
39 changes: 22 additions & 17 deletions pan.coffee → src/pan.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
work = require('webworkify');
Util = require('./util.coffee')
GeneMatrix = require('./gene-matrix.coffee')
Tree = require('./tree.coffee')
Plot = require('./mds-plot.coffee')

# A worker that will only compute new values if the web worker is not busy
# and the parameters have changed
Expand Down Expand Up @@ -50,7 +55,7 @@ class MDSHandler
constructor: (@matrix, @think_elem) ->
@dispatch = d3.dispatch("redraw")
@_current_range = null
worker = new Worker('mds-worker.js')
worker = work(require('./mds-worker.coffee'))
worker.postMessage(init: @matrix.as_hash())
@latest_worker = new LatestWorker(worker, () => {mds: @_current_range})
@latest_worker.on('updated', (comp) => @redraw(comp))
Expand Down Expand Up @@ -95,7 +100,7 @@ class MDSHandler
class DendrogramWrapper
constructor: (@widget, @matrix, @think_elem) ->
@_current_range = null
worker = new Worker('mds-worker.js')
worker = work(require('./mds-worker.coffee'))
worker.postMessage(init: @matrix.as_hash())
@latest_worker = new LatestWorker(worker, () => {dist: @_current_range})
@latest_worker.on('updated', (d) => @_calc_done(d))
Expand All @@ -119,7 +124,7 @@ class DendrogramWrapper
@latest_worker.update()

_calc_done: (dist_arr) ->
@tree = new TreeBuilder(dist_arr)
@tree = new Tree.TreeBuilder(dist_arr)
@redraw()

redraw: () ->
Expand Down Expand Up @@ -529,13 +534,13 @@ class Pan
@matrix.on('order_changed', () => @redraw())

@mdsDimension = 1
@mdsBarGraph = new BarGraph(
@mdsBarGraph = new Plot.BarGraph(
elem: '#mds-bargraph'
click: (d) =>
@mdsDimension=+d.lbl
@mds.redispatch()
)
@scatter = new ScatterPlot(
@scatter = new Plot.ScatterPlot(
elem: '#mds'
width: 500
height: 399
Expand All @@ -559,7 +564,7 @@ class Pan
)
@mds.update(null)

dendrogramWidget = new Dendrogram(
dendrogramWidget = new Tree.Dendrogram(
elem: '#dendrogram'
width: 600
height: 400
Expand Down Expand Up @@ -673,9 +678,9 @@ class Pan
#console.log "domain size=#{domain.length} sizes=#{sizes}. Best=#{best}"
scale.range(b[best])
if domain.length>best
log_error("Not enough colours in #{scheme}, max=#{best}. Need #{domain.length}")
Util.log_error("Not enough colours in #{scheme}, max=#{best}. Need #{domain.length}")
else
log_error("Bad colour scheme")
Util.log_error("Bad colour scheme")

strain_colour = []
for s in strains
Expand Down Expand Up @@ -755,7 +760,7 @@ parse_proteinortho = (tsv) ->
# ------------------------------------------------------------
#
get_stem = () ->
get_url_params() || 'pan'
Util.get_url_params() || 'pan'

process_gene_order = (matrix, json) ->
strains = d3.keys(json).sort()
Expand All @@ -768,7 +773,7 @@ process_gene_order = (matrix, json) ->
load_json = (matrix) ->
d3.json("#{get_stem()}.json", (err, json) ->
if (err)
log_warn("Missing '#{get_stem()}.json', trying deprecated .descriptions file")
Util.log_warn("Missing '#{get_stem()}.json', trying deprecated .descriptions file")
load_desc(matrix)
else
process_gene_order(matrix,json.gene_order) if json.gene_order?
Expand All @@ -791,7 +796,7 @@ load_desc = (matrix) ->
if match
matrix.set_desc(match[1], match[2])
else
log_error "BAD LINE: #{l}"
Util.log_error "BAD LINE: #{l}"
)
)

Expand Down Expand Up @@ -826,7 +831,7 @@ class StrainInfo
set_info: (@matrix) ->
@columns = d3.keys(@matrix[0])
if 'ID' != @columns.shift()
log_error("No ID column in #{get_stem()}.strains")
Util.log_error("No ID column in #{get_stem()}.strains")
@columns = []
return

Expand All @@ -835,23 +840,23 @@ class StrainInfo
for c in @columns
s[c] = '_not-set_'

log_info "Read info on #{@matrix.length}. Columns=#{@columns}"
Util.log_info "Read info on #{@matrix.length}. Columns=#{@columns}"
for row in @matrix
s = @find_strain_by_name(row['ID'])
if !s?
log_error "Unable to find strain for #{row['ID']}"
Util.log_error "Unable to find strain for #{row['ID']}"
else
for c in @columns
s[c] = row[c]

setup_download = (sel) ->
d3.selectAll(".svg-download")
.on("mousedown", (e) -> download_svg(d3.event.target))
.on("mousedown", (e) -> Util.download_svg(d3.event.target))

init = () ->
document.title = "FriPan : #{get_stem()}"
$(".hdr .title").append("<span class='title'>: #{get_stem()}</span>")
setup_nav_bar()
Util.setup_nav_bar()

url = "#{get_stem()}.proteinortho"
d3.tsv(url, (data) ->
Expand All @@ -878,4 +883,4 @@ init = () ->
setup_download(".svg_download")
)

$(document).ready(() -> add_browser_warning() ; init() )
$(document).ready(() -> Util.add_browser_warning() ; init() )
File renamed without changes.
4 changes: 2 additions & 2 deletions tree.coffee → src/tree.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,6 @@ class Dendrogram
@opts.callback[typ](args)


window.Dendrogram = Dendrogram

window.TreeBuilder = TreeBuilder
module.exports.Dendrogram = Dendrogram
module.exports.TreeBuilder = TreeBuilder
21 changes: 11 additions & 10 deletions util.coffee → src/util.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
@our_log = (o) ->
root = module.exports

root.our_log = (o) ->
if console? && console.log?
console.log.apply(console, if !!arguments.length then arguments else [this])
else if opera? && opera.postError?
opera.postError(o || this)

@log_info = (o) -> log_msg("INFO", arguments)
@log_warn = (o) -> log_msg("WARN", arguments)
@log_error = (o) -> log_msg("ERROR", arguments)
@log_debug = (o) ->
root.log_info = (o) -> log_msg("INFO", arguments)
root.log_warn = (o) -> log_msg("WARN", arguments)
root.log_error = (o) -> log_msg("ERROR", arguments)
root.log_debug = (o) ->
log_msg("DEBUG", arguments) if window.debug?

# Our internal log allowing a log type
Expand All @@ -33,15 +35,15 @@ html_warning = """
</div>
"""

@setup_nav_bar = () ->
root.setup_nav_bar = () ->
#about = $(require("../templates/about.hbs")(version: degust_version))
#$('#about-modal').replaceWith(about)
$("a.log-link").click(() -> $('.log-list').toggle())

#window.debug ?= get_url_vars()["debug"]

# Display a popup warning, or fill in a warning box if using IE
@add_browser_warning = () ->
root.add_browser_warning = () ->
if window.navigator.userAgent.indexOf("MSIE ")>=0
outer = $('.browser-warning-outer')
if outer.length==0
Expand All @@ -50,11 +52,10 @@ html_warning = """
outer = $('.browser-warning-outer')
outer.append(html_warning)

@get_url_params = () ->
root.get_url_params = () ->
hash = window.location.search
hash.substring(1) # remove '?'


# ------------------------------------------------------------
# SVG Downloading
#
Expand Down Expand Up @@ -107,5 +108,5 @@ class SVG
.attr("href-lang", "image/svg+xml")
.attr("href", "data:image/svg+xml;base64,\n" + btoa(html))

@download_svg = SVG.download_svg
root.download_svg = SVG.download_svg
# ------------------------------------------------------------

0 comments on commit 356715f

Please sign in to comment.