Advertisement
mattiv

Associated_species

Sep 7th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const settings = require( '../system/settings' )
  2. const request  = require('superagent')
  3. const Promise  = require('bluebird')
  4.  
  5. let country = settings.country
  6. let sources = settings.sources
  7. let accessToken = settings.lajifi_accessToken
  8. let pageSize = 5000
  9.  
  10. function getAssociatedSpecies( order, scientificName )
  11. {
  12.     // --- Species catalog is in a local cache
  13.     //
  14.     let speciesData = require('../../data/catalogs/' + order + '/speciesdata.js' )
  15.  
  16.     // --- Get the taxonId for the species from the species catalog
  17.     //
  18.     let speciesId = speciesData[scientificName].id
  19.  
  20.     //--- Get all gatherings, which contain the given species
  21.     //
  22.     request
  23.     .get('https://api.laji.fi/v0/warehouse/query/list' )
  24.     .query({
  25.         taxonId: speciesId,
  26.         sourceId: settings.sources,
  27.         countryId: settings.country,  
  28.         selected: 'gathering.gatheringId',          
  29.         pageSize: pageSize,  
  30.         access_token: settings.lajifi_accessToken })
  31.         .then( res =>
  32.         {
  33.             // --- Number of gatherings, can be 1000's for many Lepidoptera species
  34.             //
  35.             console.log( res.body.results.length + ' gatherings for ' + scientificName )
  36.  
  37.             // --- Associated species are collected in spData
  38.             //
  39.             let spData = {}
  40.  
  41.             // --- For parallel execution, collect all gatheringId's in an array
  42.             //  
  43.             let gatheringQueries = []            
  44.  
  45.             let gatheringQueries = res.body.results.map( item =>
  46.             {
  47.                 return { gatheringId: item.gathering.gatheringId }
  48.             })
  49.            
  50.             // --- Get all gatherings, max 20 concurrent requests
  51.             //
  52.             Promise.map( gatheringQueries, getGathering, { concurrency: 20 })
  53.             .then( res =>
  54.             {
  55.                 res.forEach( items =>
  56.                 {
  57.                     // --- Each 'items' is an array of scientificNames found in a specific gathering
  58.                     //
  59.                     items.forEach( item =>
  60.                     {
  61.                         if ( item.unit && item.unit.linkings && item.unit.linkings.taxon )
  62.                         {
  63.                             let speciesName = item.unit.linkings.taxon.scientificName
  64.              
  65.                             // --- Collect all other names which are not equal to the name
  66.                             //     given as the parameter for the query,
  67.                             //     and accept only taxons in the same order
  68.                             //
  69.                             if ( speciesName !== scientificName && speciesData[speciesName] )
  70.                             {
  71.                                 if ( !spData[speciesName] )
  72.                                 {
  73.                                     let dataObj = { count: 1 }
  74.                                     spData[speciesName] = dataObj
  75.                                 }
  76.                                 else
  77.                                 {
  78.                                     let dataObj = spData[speciesName]  
  79.                                     dataObj.count++
  80.                                 }                          
  81.                             }
  82.                         }
  83.                     })
  84.                 })
  85.  
  86.                 // --- Convert spData into an array
  87.                 //
  88.                 let list = []
  89.                 let properties = Object.getOwnPropertyNames( spData )
  90.  
  91.                 let i
  92.                 for ( i = 0; i<properties.length; i++ )
  93.                 {
  94.                     let ljSpeciesName = properties[i]
  95.                     let ljNbr = speciesData[ljSpeciesName].speciesNbr
  96.  
  97.                     if ( spData[ljSpeciesName].count > 1 )
  98.                     {
  99.                         list.push( { speciesName:ljSpeciesName, speciesNbr:ljNbr,
  100.                                      count:spData[ljSpeciesName].count } )
  101.                     }
  102.                 }
  103.                  
  104.                  // --- Sort the list
  105.                  //
  106.                 let sortedList = list.sort( function ( a, b )
  107.                 {
  108.                     return a.speciesNbr - b.speciesNbr
  109.                 })
  110.  
  111.                 // -- The final list of associated species
  112.                 //
  113.                 console.log( sortedList )
  114.             })
  115.         })
  116. }
  117.  
  118. function getGathering( query )
  119. {
  120.     return request
  121.     .get('https://api.laji.fi/v0/warehouse/query/list' )
  122.     .query({
  123.         gatheringId: query.gatheringId,
  124.         sourceId: settings.sources,
  125.         countryId: settings.country,          
  126.         selected: 'unit.linkings.taxon.scientificName',
  127.         pageSize: pageSize,        
  128.         access_token: settings.lajifi_accessToken })
  129.     .then( res =>
  130.     {
  131.         return res.body.results        
  132.     })
  133. }
  134.  
  135. // --- An example result
  136. //
  137. 347 gatherings for Cicindela campestris
  138.  
  139. [ { speciesName: 'Cicindela sylvatica', speciesNbr: 191, count: 5 },
  140.   { speciesName: 'Carabus nemoralis', speciesNbr: 201, count: 2 },
  141.   { speciesName: 'Carabus nitens', speciesNbr: 205, count: 4 },
  142.   { speciesName: 'Calathus melanocephalus', speciesNbr: 442,  count: 2 },
  143.   { speciesName: 'Agonum sexpunctatum', speciesNbr: 464, count: 3 },
  144.   { speciesName: 'Phosphuga atrata', speciesNbr: 624, count: 2 },
  145.   { speciesName: 'Cetonia aurata', speciesNbr: 1959, count: 2 },
  146.   { speciesName: 'Protaetia cuprea', speciesNbr: 1961, count: 2 },
  147.   { speciesName: 'Dermestes lardarius', speciesNbr: 2205, count: 2 },
  148.   { speciesName: 'Propylea quatuordecimpunctata', speciesNbr: 2633, count: 2 },
  149.   { speciesName: 'Calvia quatuordecimguttata', speciesNbr: 2635, count: 2 },
  150.   { speciesName: 'Hippodamia tredecimpunctata', speciesNbr: 2639, count: 2 },
  151.   { speciesName: 'Coccinella septempunctata', speciesNbr: 2646, count: 4 },
  152.   { speciesName: 'Lagria hirta', speciesNbr: 2895, count: 2 },
  153.   { speciesName: 'Hylobius abietis', speciesNbr: 3411, count: 2 } ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement