Skip to content

Commit

Permalink
rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Aug 30, 2014
1 parent 18a9310 commit bfa23be
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 165 deletions.
31 changes: 15 additions & 16 deletions application.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env ruby

require 'zen-grids'
require 'rack/timeout'
require 'sinatra'
require 'sinatra/base'
require 'sinatra/flash'
require 'sinatra/redirect_with_flash'
require 'haml'
require 'sass'
require 'childprocess'
require "zen-grids"
require "rack/timeout"
require "sinatra"
require "sinatra/base"
require "sinatra/flash"
require "sinatra/redirect_with_flash"
require "haml"
require "sass"
require "childprocess"

require_relative 'lib/seabase'
require_relative 'routes'
require_relative 'helpers'
require_relative "lib/seabase"
require_relative "routes"
require_relative "helpers"

configure do
register Sinatra::Flash
Expand All @@ -23,9 +23,8 @@
use Rack::Timeout
Rack::Timeout.timeout = 9_000_000

Compass.add_project_configuration(File.join(File.dirname(__FILE__),
'config',
'compass.config'))
Compass.add_project_configuration(File.join(File.dirname(__FILE__),
"config",
"compass.config"))
set :scss, Compass.sass_engine_options
end

2 changes: 1 addition & 1 deletion lib/seabase/gephi_csv_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def add_row(ary)
private

def url_id(id)
"http://seabase.core.cli.mbl.edu/transcript/%s" % id
"http://seabase.core.cli.mbl.edu/transcript/#{id}"
end
end
end
134 changes: 66 additions & 68 deletions lib/seabase/normalizer.rb
Original file line number Diff line number Diff line change
@@ -1,124 +1,122 @@
module Seabase
# Normalizes experiment data to number or molecules per embryo
class Normalizer
NUMBER_OF_EMBRYOS = 150
START_OF_ALPHABET = 64

def initialize(replicates, transcripts, mapping_count_data)
@replicates = id_to_obj_hash(replicates)
@transcripts = id_to_obj_hash(transcripts)
@data = mapping_count_data

@distinct_technical_replicates = Set.new
@technical_replicates = SetLookup.new
@lane_replicates = SetLookup.new
@replicate_transcripts = SetLookup.new

process_data

@stage_to_replicate = SetLookup.new
replicates.each {|r| @stage_to_replicate.add(r.stage, r)}
replicates.each { |r| @stage_to_replicate.add(r.stage, r) }
end

def id_to_obj_hash(objs)
result = {}
objs.each do |o|
result[o.id] = o
end
result
objs.each_with_object({}) { |o, h| h[o.id] = o }
end

def process_data
@data.each do |mapping_count, replicate_id, transcript_id|
replicate = @replicates[replicate_id]
transcript = @transcripts[transcript_id]
@distinct_technical_replicates.add(replicate.technical_replicate)
@technical_replicates.add(replicate.stage,
replicate.technical_replicate)
@lane_replicates.add([replicate.stage,
replicate.technical_replicate],
replicate.lane_replicate)
@replicate_transcripts.add([replicate.stage,
replicate.technical_replicate,
replicate.lane_replicate],
TranscriptContext.new(replicate,
transcript,
mapping_count))
populate_sets(replicate, transcript, mapping_count)
end
end

def technical_replicates(stage); @technical_replicates[stage] || []; end
def lane_replicates(stage, tr); @lane_replicates[[stage, tr]];end

def populate_sets(r, t, m)
@distinct_technical_replicates.add(r.technical_replicate)
@technical_replicates.add(r.stage, r.technical_replicate)
@lane_replicates.add([r.stage, r.technical_replicate], r.lane_replicate)
@replicate_transcripts.add(
[r.stage, r.technical_replicate, r.lane_replicate],
TranscriptContext.new(r, t, m)
)
end

def technical_replicates(stage)
@technical_replicates[stage] || []
end

def lane_replicates(stage, tr)
@lane_replicates[[stage, tr]]
end

def replicate_transcripts(stage, tr, lr)
@replicate_transcripts[[stage, tr, lr]]
end

def count_per_embryo(stage, technical_replicate)
if technical_replicate
technical_replicate_counts(stage, technical_replicate)
else
average_ignore_zeros(technical_replicates(stage).
map {|tr| technical_replicate_counts(stage, tr)})
average_ignore_zeros(
technical_replicates(stage).
map { |tr| technical_replicate_counts(stage, tr) }
)
end
end

def average_ignore_zeros(nums)
count = 0.0
total = 0
nums.each do |n|
if n != 0
count += 1
total += n
end
total = nums.reduce(0) do |res, n|
count += 1 unless n.zero?
res + n
end
count == 0 ? 0 : total/count
count.zero? ? count : total / count
end

def technical_replicate_counts(stage, technical_replicate)
lrs = lane_replicates(stage, technical_replicate)
lrs ? average_ignore_zeros(lrs.map { |lr| lane_replicate_counts(stage,
technical_replicate, lr) }) : nil
lrs && average_ignore_zeros(
lrs.map { |lr| lane_replicate_counts(stage, technical_replicate, lr) }
)
end

def lane_replicate_counts(stage, technical_replicate, lane_replicate)
watching = (stage == 1) and (technical_replicate == 1)
rts = replicate_transcripts(stage, technical_replicate, lane_replicate)
total = 0
if rts
rts.each do |lane_context|
total += normalize_rpkm(fpkm(lane_context.length,
lane_context.mapping_count,
lane_context.total_mapping),
lane_context.y_intercept,
lane_context.slope)
end
Array(rts).reduce(0) do |total, lane_context|
total + normalize_rpkm(
fpkm(lane_context.length, lane_context.mapping_count,
lane_context.total_mapping),
lane_context.y_intercept,
lane_context.slope
)
end
total
end

def fpkm(length, mapping_count, total_mapping)
(mapping_count * 1e9) / (length * total_mapping.to_f)
end

NUMBER_OF_EMBRYOS = 150


def normalize_rpkm(fpkm, y_intercept, slope)
((fpkm * slope + y_intercept) / NUMBER_OF_EMBRYOS) / 10
end

START_OF_ALPHABET = 64


def row(technical_replicate)
result = stages.map {|s| count_per_embryo(s, technical_replicate)}
result.unshift(technical_replicate ?
(technical_replicate + START_OF_ALPHABET).chr :
'Average')
end

def table(technical_replicate=nil)
(technical_replicate ? [] :
result = stages.map { |s| count_per_embryo(s, technical_replicate) }
row_name = "Average"
row_name = (technical_replicate +
START_OF_ALPHABET).chr if technical_replicate
result.unshift(row_name)
end

def table(technical_replicate = nil)
(technical_replicate ? [] :
@distinct_technical_replicates.sort.
map {|tr| row(tr)}).push(row(technical_replicate))
map { |tr| row(tr) }).push(row(technical_replicate))
end

def stages
@stage_to_replicate.keys.sort
end

def stages; @stage_to_replicate.keys.sort; end
end
end
Loading

0 comments on commit bfa23be

Please sign in to comment.