Skip to content

Commit

Permalink
common names list
Browse files Browse the repository at this point in the history
  • Loading branch information
ndvan committed Oct 2, 2018
1 parent 989ffa6 commit ebc6243
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/cns.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/cns.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the cns controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
94 changes: 94 additions & 0 deletions app/controllers/cns_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class CnsController < ApplicationController
def new
@cn = Cn.new
end

def create
@cn = current_or_guest_user.cns.build(cn_params)
if @cn.save
name = params[:name].empty? ? @cn.file.original_filename : params[:name]
@list = @cn.create_list(name: name, description: params[:description])
flash[:success] = @list.name + " list is created!"

loaded_from_file = {}
raw = Paperclip.io_adapters.for(@cn.file).read.split("\n")
loaded_from_file["commonnames"] = raw.map do |n|
n.to_s.strip.encode('UTF-8', {
:invalid => :replace,
:undef => :replace,
:replace => '?'
})
end

loaded_from_file["multiple_match"] = @cn.multiple_match

mapping_response = {}
mapping_response = Req.post( Rails.configuration.x.sv_NCBI_common_name,
loaded_from_file.to_json,
:content_type => :json )
if mapping_response.empty?
mapping_response = Req.post( Rails.configuration.x.sv_ITIS_common_name,
loaded_from_file.to_json,
:content_type => :json )
end

if mapping_response.empty?
mapping_response = Req.post( Rails.configuration.x.sv_TROPICOS_commmon_name,
loaded_from_file.to_json,
:content_type => :json )
end

extracted_response = {}
begin
extracted_response["scientificNames"] = mapping_response["result"].map do |n|
n["matched_names"].map do |m|
m["scientific_name"]
end
end.flatten
rescue
extracted_response["scientificNames"] = []
end

@list.update_attributes(extracted: extracted_response.to_json)

resolved_response = Req.post( Rails.configuration.x.sv_OToL_TNRS_wrapper,
extracted_response.to_json,
:content_type => :json )

if resolved_response.empty?
extracted_for_gnr = extracted_response
extracted_for_gnr["fuzzy_match"] = true
extracted_for_gnr["multiple_match"] = false
resolved_response = Req.post( Rails.configuration.x.sv_GNR_TNRS_wrapper,
extracted_for_gnr.to_json,
:content_type => :json )
end

if resolved_response.empty?
error = Req.post( Rails.configuration.x.sv_OToL_TNRS_wrapper,
extracted_response.to_json,
{:content_type => :json},
true )
fail_record = Failure.last.id
end

@list.update_attributes(resolved: resolved_response.to_json,
resolving_error: error,
possible_failure_record: fail_record)

redirect_to list_path(@list)
else
render action: "new"
end
end

private

def cn_params
begin
params.require(:cn).permit(:file, :multiple_match)
rescue
{}
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/cns_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CnsHelper
end
24 changes: 24 additions & 0 deletions app/models/cn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Cn < ApplicationRecord
belongs_to :user

has_one :list, as: :resource, dependent: :destroy

has_attached_file :file, default_url: "/images/:style/missing.png"

default_scope -> { order(created_at: :desc) }

validates_attachment :file,
content_type: { content_type: [ "application/pdf",
"text/plain",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/msword",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ] },
size: { in: 0..2.megabytes, :message => "must be less than 2MB" }

validates_attachment_presence :file

def meta_origin
file.original_filename
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class User < ApplicationRecord
has_many :links, dependent: :destroy
has_many :documents, dependent: :destroy
has_many :onpls, dependent: :destroy
has_many :cns, dependent: :destroy
has_many :dcas, dependent: :destroy
has_many :taxonomies, dependent: :destroy
has_many :trees, dependent: :destroy
Expand All @@ -25,6 +26,9 @@ def lists
onpl_resource_lists = self.onpls.map {|l| l.list}
lists.concat onpl_resource_lists

cn_resource_lists = self.cns.map {|l| l.list}
lists.concat cn_resource_lists

dca_resource_lists = self.dcas.map {|l| l.list}
lists.concat dca_resource_lists

Expand Down
59 changes: 59 additions & 0 deletions app/views/cns/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<% provide(:title, "Create trees from common names in files that have one name per line") %>

<div class="container">
<h1>Extract common names from files that have one name per line</h1>
<%= form_for @cn, html: {multipart: true, class: "form-horizontal"} do |f| %>

<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<%= render 'shared/error_messages', object: f.object %>
</div>
</div>

<div class="form-group">
<%= f.label :file, for: "cn_file", class: "col-sm-3 control-label" do %>
<span data-toggle="tooltip" title="Accepted file extensions: .pdf, .txt, .doc, .docs, .xls or .xlsx">
<%= fa_icon "info-circle" %>
</span>
<span data-toggle="tooltip" title="Required field"><%= fa_icon "asterisk", text: "File" %></span>
<% end %>
<div class="col-sm-7">
<%= f.file_field :file, id: "file-dropzone" %>
</div>
</div>

<div class="form-group">
<%= label_tag "cn[multiple_match]", class: "col-sm-3 control-label" do %>
<span data-toggle="tooltip" title="The system will return multiple matches (if available) for each common name in the input list.">
<%= fa_icon "info-circle" %>
</span>
Multiple match
<% end %>
<div class="col-sm-4" style="margin-top: 7px;">
<%= f.check_box :multiple_match, {}, true, false %>
</div>
</div>

<div class="form-group">
<%= label_tag :name, for: "name", class: "col-sm-3 control-label" do %>
Name for list
<% end %>
<div class="col-sm-4">
<%= text_field_tag :name, nil, placeholder: "Name", class: "form-control" %>
</div>
</div>

<div class="form-group">
<%= label_tag :description, "Description", for: "description_", class: "col-sm-3 control-label" %>
<div class="col-sm-7">
<%= text_area :description, "", placeholder: "Description", class: "form-control", rows: 5 %>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<%= f.submit "Submit", class: "btn btn-default btn-lg btn-primary" %>
</div>
</div>
<% end %>
</div>
3 changes: 2 additions & 1 deletion app/views/lists/_new_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<div class="modal-body">
<div class="panel panel-primary emphasize-panel">
<div class="list-group">
<%= link_to "I have a text file with one name per line", new_onpl_path, class:"list-group-item" %>
<%= link_to raw("I have a text file with one <strong>scientific name</strong> per line"), new_onpl_path, class:"list-group-item" %>
<%= link_to raw("I have a text file with one <strong>common name</strong> per line"), new_cn_path, class:"list-group-item" %>
<%= link_to "I have a Darwin Core Archive (.zip)", new_dca_path, class:"list-group-item" %>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,8 @@
config.x.sv_Add_new_list = "http://phylo.cs.nmsu.edu:5005/phylotastic_ws/sls/insert_list"
config.x.sv_OToL_scale_tree = "http://phylo.cs.nmsu.edu:5009/phylotastic_ws/sc/ot/scale"
config.x.sv_Taxon_popular_species = "http://phylo.cs.nmsu.edu:5006/phylotastic_ws/ts/popular_species?taxon="
config.x.sv_NCBI_common_name = "http://phylo.cs.nmsu.edu:5013/phylotastic_ws/cs/ncbi/scientific_names"
config.x.sv_ITIS_common_name = "http://phylo.cs.nmsu.edu:5013/phylotastic_ws/cs/itis/scientific_names"
config.x.sv_TROPICOS_commmon_name = "http://phylo.cs.nmsu.edu:5013/phylotastic_ws/cs/tpcs/scientific_names"
end
end
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,8 @@
config.x.sv_Add_new_list = "http://phylo.cs.nmsu.edu:5007/phylotastic_ws/sls/insert_list"
config.x.sv_OToL_scale_tree = "http://phylo.cs.nmsu.edu:5009/phylotastic_ws/sc/ot/scale"
config.x.sv_Taxon_popular_species = "http://phylo.cs.nmsu.edu:5006/phylotastic_ws/ts/popular_species?taxon="
config.x.sv_NCBI_common_name = "http://phylo.cs.nmsu.edu:5013/phylotastic_ws/cs/ncbi/scientific_names"
config.x.sv_ITIS_common_name = "http://phylo.cs.nmsu.edu:5013/phylotastic_ws/cs/itis/scientific_names"
config.x.sv_TROPICOS_commmon_name = "http://phylo.cs.nmsu.edu:5013/phylotastic_ws/cs/tpcs/scientific_names"
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do

mount ActionCable.server => '/cable'

resources :trees, only: [:show, :update, :destroy] do
Expand All @@ -22,6 +22,7 @@
resources :links, only: [:new, :create]
resources :documents, only: [:new, :create]
resources :onpls, only: [:new, :create]
resources :cns, only: [:new, :create]
resources :dcas, only: [:new, :create] do
member do
get 'publish'
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20181001224846_create_cns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCns < ActiveRecord::Migration[5.1]
def change
create_table :cns do |t|
t.references :user, foreign_key: true, index: true
t.boolean :multiple_match, :default => false

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20181001225152_add_attachment_file_to_cns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddAttachmentFileToCns < ActiveRecord::Migration[5.1]
def self.up
change_table :cns do |t|
t.attachment :file
end
end

def self.down
remove_attachment :cns, :file
end
end
4 changes: 4 additions & 0 deletions interactive_test/common_names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cucumber
tomato
lettuce
pea
9 changes: 9 additions & 0 deletions interactive_test/common_names_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cattle
cat
goat
pig
sheep
duck
chicken
horse
domestic dog
9 changes: 9 additions & 0 deletions test/controllers/cns_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class CnsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get cns_new_url
assert_response :success
end

end
7 changes: 7 additions & 0 deletions test/fixtures/cns.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
user:

two:
user:
7 changes: 7 additions & 0 deletions test/models/cn_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CnTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit ebc6243

Please sign in to comment.