Skip to content

Commit

Permalink
Merge branch 'master' into fix_links_on_sample_page
Browse files Browse the repository at this point in the history
  • Loading branch information
yunfangjuan committed Oct 11, 2017
2 parents d645851 + c816930 commit 60a005d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 55 deletions.
12 changes: 12 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ AllCops:
Metrics/MethodLength:
Enabled: false

ModuleLength:
Enabled: false

ClassLength:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Metrics/BlockLength:
Enabled: false

Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ Then open `http://localhost:3000` in your browser.
Everything you need to get started should be in `./bin/setup`. If there's anything missing, please edit and submit a pull request.


## User account logins

In development environment, you can use the following credentials to login for all the non-read operations:

```
email: fake@example.com
password: password
```

After you deploy the code, you will have to log into the rails console into the cluster and create a user credential as follows:

```
idseq-web yf$ bin/shell <cluster> "rails c"
Running via Spring preloader in process 2608
Loading alpha environment (Rails 5.1.4)
irb(main):001:0> User.create(email: 'your@email.com', password: 'yourpass', password_confirmation: 'yourpass', authentication_token: 'your_auth_token')
```

You can then log in with the emaill/password you specified.

## Testing

```
Expand Down Expand Up @@ -70,7 +90,7 @@ In addition to using your local `development` instance, you may obtain access to
1. `chmod 600 idseq_<env>_key.pem`
1. `ssh-add idseq_<env>_key.pem`

You can now run a non-interactive command on a cloud web container like so
You can now run a non-interactive command on a cloud web container like so
`bin/clam <env> 'echo $HOSTNAME'`


Expand All @@ -89,11 +109,11 @@ Sometimes you may be prompted to run a migration or configuration command like `

## DB backup/restore within and across environments

1. Backup your local `development` DB into a local file:
1. Backup your local `development` DB into a local file:
`docker-compose exec web mysqldump -h db -u root idseq_development > idseq_development.sql`
1. Backup cloud `alpha` DB into a local file:
1. Backup cloud `alpha` DB into a local file:
`bin/clam alpha 'mysqldump -h $RDS_ADDRESS -u $DB_USERNAME --password=$DB_PASSWORD idseq_alpha | gzip -c' | gzip -dc > idseq_alpha.sql`
1. Overwrite your local `development` DB with data from given backup file:
1. Overwrite your local `development` DB with data from given backup file:
`docker-compose run web "cat idseq_alpha.sql | mysql -h db -u root --database idseq_development"`


Expand Down
4 changes: 1 addition & 3 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def index
def show
@report_details = report_details(@report)
@view_level = params[:view_level] ? params[:view_level].downcase : 'genus'
taxon_zscores = compute_taxon_zscores(@report)
@taxonomy_details = taxonomy_details(@view_level, @report, params, taxon_zscores)
@highest_tax_counts = highest_tax_counts(@view_level, @report, taxon_zscores)
@highest_tax_counts, @taxonomy_details = taxonomy_details(@view_level, @report, params)
end

# GET /reports/new
Expand Down
89 changes: 42 additions & 47 deletions app/lib/report_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# rubocop:disable ModuleLength
module ReportHelper
def external_report_info(report, view_level, params)
taxon_zscores = compute_taxon_zscores(report)
data = {}
data[:report_details] = report_details(report)
data[:taxonomy_details] = taxonomy_details(view_level, report, params, taxon_zscores)
data[:highest_tax_counts] = highest_tax_counts(view_level, report, taxon_zscores)
htc, td = taxonomy_details(view_level, report, params)
data[:highest_tax_counts] = htc
data[:taxonomy_details] = td
data[:view_level] = view_level
data
end
Expand Down Expand Up @@ -33,53 +32,63 @@ def compute_taxon_zscores(report)
zscore_array
end

def taxonomy_details(view_level, _report, params, taxon_zscores)
sz = select_zscore(view_level, taxon_zscores)
def taxonomy_details(view_level, report, params)
taxon_zscores = compute_taxon_zscores(report)
wanted_level = view_level == 'species' ? TaxonCount::TAX_LEVEL_SPECIES : TaxonCount::TAX_LEVEL_GENUS
data = taxon_zscores.group_by { |h| h[:tax_level] == wanted_level && h[:hit_type] }
nt = data['NT'] || []
nr = data['NR'] || []
htc = highest_tax_counts(nt, nr)
# filter and sort the nt_scores
sort_report!(sz[:nt_zscores], params[:sort_by])
rp = resolve_params(params)
sz[:nt_zscores].keep_if do |h|
rp = resolve_params(params, htc)
nt.keep_if do |h|
(h[:tax_id] >= 0 &&
h[:zscore] >= rp[:nt_zscore_threshold][:start] &&
h[:zscore] < rp[:nt_zscore_threshold][:end] &&
h[:zscore] <= rp[:nt_zscore_threshold][:end] &&
h[:rpm] >= rp[:nt_rpm_threshold][:start] &&
h[:rpm] < rp[:nt_rpm_threshold][:end]
h[:rpm] <= rp[:nt_rpm_threshold][:end]
)
end
# cross-references nr_zscores by taxon id
nr_zscore_by_taxon = {}
sz[:nr_zscores].each do |h|
nr_zscore_by_taxon[h[:tax_id]] = h
end
# pull nt and nr scores for each taxon in order sorted above
sort_report!(nt, params[:sort_by])
nr_zscore_by_taxon = nr.group_by { |h| h[:tax_id] }
tax_details = []
sz[:nt_zscores].each do |h|
tax_details.push(nt_ele: h, nr_ele: nr_zscore_by_taxon[h[:tax_id]])
nt.each do |h|
x = nr_zscore_by_taxon[h[:tax_id]]
tax_details.push(nt_ele: h, nr_ele: x && x[0])
end
tax_details
[htc, tax_details]
end

def resolve_params(params)
def resolve_params(params, data_ranges)
new_params = {}
[:nt_zscore_threshold, :nt_rpm_threshold].each do |k|
s, e = params[k] ? params[k].split('-') : []
s, e = params[k] ? params[k].split(',') : []
ntnr, metric, _threshold = k.to_s.split "_"
dr_s = data_ranges[('lowest_' + ntnr + "_" + metric).to_sym]
dr_e = data_ranges[('highest_' + ntnr + "_" + metric).to_sym]
new_params[k] = {
start: Float(s || -1_000_000_000),
end: Float(e || 1_000_000_000) + 1.0
start: Float(s || dr_s),
end: Float(e || dr_e)
}
end
new_params
end

def highest_tax_counts(view_level, _report, taxon_zscores)
sz = select_zscore(view_level, taxon_zscores)
nt = sz[:nt_zscores]
nr = sz[:nr_zscores]
def highest_tax_counts(nt, nr)
nt_z = nt.map { |h| h[:zscore] }.sort
nr_z = nr.map { |h| h[:zscore] }.sort
nt_rpm = nt.map { |h| h[:rpm] }.sort
nr_rpm = nr.map { |h| h[:rpm] }.sort
{
highest_nt_zscore: nt.map { |h| h[:z_score] }.sort[-1] || 0,
highest_nr_zscore: nr.map { |h| h[:z_score] }.sort[-1] || 0,
highest_nt_rpm: nt.map { |h| h[:rpm] }.sort[-1] || 0,
highest_nr_rpm: nr.map { |h| h[:rpm] }.sort[-1] || 0
# if you change keys here, update data_ranges[...] in resolve_params(...)
lowest_nt_zscore: nt_z[0] || 0,
lowest_nr_zscore: nr_z[0] || 0,
lowest_nt_rpm: nt_rpm[0] || 0,
lowest_nr_rpm: nr_rpm[0] || 0,
highest_nt_zscore: nt_z[-1] || 0,
highest_nr_zscore: nr_z[-1] || 0,
highest_nt_rpm: nt_rpm[-1] || 0,
highest_nr_rpm: nr_rpm[-1] || 0
}
end

Expand All @@ -92,20 +101,6 @@ def sort_report!(nt_zscores, sort_by)
nt_zscores
end

def select_zscore(level, zscores)
wanted_level = level == :species ? TaxonCount::TAX_LEVEL_SPECIES : TaxonCount::TAX_LEVEL_GENUS
result = {
nr_zscores: [],
nt_zscores: []
}
zscores.each do |z|
next unless z[:tax_level] == wanted_level
t = z[:hit_type] == :NR || z[:hit_type] == "NR" ? :nr_zscores : :nt_zscores
result[t].push(z)
end
result
end

def compute_rpm(count, total_reads)
if count
count * 1e6 / total_reads.to_f
Expand All @@ -118,7 +113,7 @@ def compute_zscore(rpm, mean, stdev)
if rpm && stdev && stdev != 0
(rpm - mean) / stdev
elsif rpm
1e6
100
else
0
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/sample.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'open3'
require 'json'
# rubocop:disable ClassLength

class Sample < ApplicationRecord
self.per_page = 10
STATUS_CREATED = 'created'.freeze
Expand Down

0 comments on commit 60a005d

Please sign in to comment.