###BQI sensitivity 
suppressMessages(suppressWarnings(library(readxl)))
mtm <- read.csv('LumpedAggedInfPCMet2017-01-18.csv',na='empty')

suppressMessages(suppressWarnings(library(tidyverse)))

sp <- mtm %>% select(11:190) 

sp <- ceiling(sp) #to round-up decimal point in abundance

f <- data.frame(taxa=colnames(sp),
                freq=colSums(sp!=0), 
                row.names = 1:180)#get the number of cores were each taxon occur#
f1 <- f %>% 
  dplyr::filter(freq>20) ##list of taxa taxa that occur in >20 cores

suppressMessages(suppressWarnings(library(vegan)))#for rarefy function

sp1 <- sp %>% 
  dplyr::filter(rowSums(.)>10) %>% ##remove core with < 10 individuals
  mutate(ES50 = rarefy(.,50)) %>%  #get the ES50 rarefied richness based on Hurlbert's (1971) formulation.
  select(match(f1$taxa,names(sp)),ES50) %>% ##select taxa that occur in >20 cores
  gather(sp,abund,-ES50) ##convert into long format
## Warning in rarefy(., 50): Requested 'sample' was larger than smallest site
## maximum (11)
sensi <- 
  sp1 %>% 
  group_by(sp) %>% 
  dplyr::filter(abund>0) %>% 
  arrange(ES50) %>% 
  mutate(p=percent_rank(cumsum(abund))) %>% 
  slice(which.min(abs(p - .05))) %>% 
  data.frame(.) %>% 
  select(ES50,sp) %>% 
  rename(sensitivity = ES50) %>% 
  arrange(sensitivity) %>% 
  print()
##     sensitivity                          sp
## 1      2.834466                     insecta
## 2      3.000000         austrohelice.crassa
## 3      3.077834                       acari
## 4      3.490246         halopyrgus.pupoides
## 5      3.529997             platyhelminthes
## 6      3.529997              scolecolepides
## 7      3.536116                potamopyrgus
## 8      3.946303           amphibola.crenata
## 9      4.000000                   amphipoda
## 10     4.000000                  nereididae
## 11     4.041686                 oligochaeta
## 12     4.137612          arthritica.bifurca
## 13     4.205882          perrierina.turneri
## 14     4.289742           hemiplax.hirtipes
## 15     4.345280           cyclomactra.ovata
## 16     4.389320                     isopoda
## 17     4.518259          cossura.consimilis
## 18     4.601518           paphies.australis
## 19     4.759824     austrovenus.stutchburyi
## 20     4.821661                 hemigrapsus
## 21     4.854189                capitellidae
## 22     4.912292       austrominius.modestus
## 23     4.941088                halicarcinus
## 24     4.946660                      mysida
## 25     5.000000                     aonides
## 26     5.000000                    copepoda
## 27     5.000000                   crustacea
## 28     5.000000                   edwardsia
## 29     5.000000                  paraonidae
## 30     5.000000                    polydora
## 31     5.000000                  scolelepis
## 32     5.000000                   sipuncula
## 33     5.000000                   spionidae
## 34     5.193795       scoloplos.cylindrifer
## 35     5.254085      cominella.glandiformis
## 36     5.269761         arcuatula.senhousia
## 37     5.277008                 neoguraleus
## 38     5.301273     heteromastus.filiformis
## 39     5.319901                    nematoda
## 40     5.411891                  tanaidacea
## 41     5.413415                    bivalvia
## 42     5.447561                   boccardia
## 43     5.461037                  nephtyidae
## 44     5.501570                    nemertea
## 45     5.514867            macomona.liliana
## 46     5.591030              theora.lubrica
## 47     5.592201                   mytilidae
## 48     5.645542                  glyceridae
## 49     5.649394                   ostreidae
## 50     5.756257    acanthochitona.zelandica
## 51     5.793631 phyllochaetopterus.socialis
## 52     5.960030                   notoacmea
## 53     6.000000                   alpheidae
## 54     6.000000                chaetognatha
## 55     6.000000                      diloma
## 56     6.000000                  hesionidae
## 57     6.000000                   ostracoda
## 58     6.000000                  prionospio
## 59     6.051427                     cumacea
## 60     6.116779                      chiton
## 61     6.185118                    syllidae
## 62     6.461486                  actiniidae
## 63     6.524159                  manayunkia
## 64     6.566734                micrelenchus
## 65     6.629825                  gastropoda
## 66     6.640793                cirratulidae
## 67     6.653736         fellaster.zelandiae
## 68     6.666177                    magelona
## 69     6.671291                  pectinaria
## 70     6.748442                  notomastus
## 71     6.748442              sphaerodoridae
## 72     6.808314           orbinia.papillosa
## 73     6.884615          cominella.adspersa
## 74     6.980392                  goniadidae
## 75     7.000000                     hiatula
## 76     7.000000               lumbrineridae
## 77     7.000000                  maldanidae
## 78     7.000000                   nuculidae
## 79     7.000000               phyllodocidae
## 80     7.000000                  polychaeta
## 81     7.000000                  sabellidae
## 82     7.000000                    travisia
## 83     7.000000                 zeacumantus
## 84     7.014095              pseudarcopagia
## 85     7.014095                      xymene
## 86     7.026593          haminoea.zelandiae
## 87     7.050644                sigalionidae
## 88     7.063788             opisthobranchia
## 89     7.177206                  cirripedia
## 90     7.378918                      lasaea
## 91     7.546874                  orbiniidae
## 92     7.803396               holothuroidea
## 93     7.834791                      amalda
## 94     7.850135                  polynoidae
## 95     8.000000                 crangonidae
## 96     8.009559           armandia.maculata
## 97     8.009559                  turbonilla
## 98     8.104911                 stomatopoda
## 99     8.252611                    venerida
## 100    8.362918                dorvilleidae
## 101    8.600554                      owenia
## 102    8.751210                     mysella
## 103    8.800419                   phoronida
## 104    8.855065                  diplodonta
## 105    8.988635           euterebra.tristis
## 106    9.141197         nassarius.burchardi
## 107    9.449098                    anthozoa
## 108    9.606022            scalibregmatidae
## 109    9.606022                terebellidae
## 110    9.629968                  nebaliacea