Skip to content

Commit

Permalink
Add Accession Lookup find by first word
Browse files Browse the repository at this point in the history
  • Loading branch information
lczech committed Nov 26, 2024
1 parent 8457df4 commit b64a75a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
45 changes: 35 additions & 10 deletions lib/genesis/taxonomy/accession_lookup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include "genesis/taxonomy/taxonomy.hpp"
#include "genesis/taxonomy/taxon.hpp"
#include "genesis/utils/text/char.hpp"
#include "genesis/utils/text/string.hpp"

#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -115,9 +117,12 @@ class AccessionLookup
* If not found, either a `nullptr` is returned, or an exception is thrown, depending on
* @p throw_if_not_found.
*/
inline Taxon* get( std::string const& accession, bool throw_if_not_found = false ) const
{
auto it = map_.find( accession );
inline Taxon* get(
std::string const& accession,
bool also_look_up_first_word = true,
bool throw_if_not_found = false
) const {
auto const it = find_( accession, also_look_up_first_word );
if( it == map_.end() ) {
if( throw_if_not_found ) {
throw std::invalid_argument(
Expand All @@ -135,17 +140,22 @@ class AccessionLookup
*
* Same as get(), but returns the Taxon as a const pointer.
*/
inline Taxon const* cget( std::string const& accession, bool throw_if_not_found = false ) const
{
return get( accession, throw_if_not_found );
inline Taxon const* cget(
std::string const& accession,
bool also_look_up_first_word = true,
bool throw_if_not_found = false
) const {
return get( accession, also_look_up_first_word, throw_if_not_found );
}

/**
* @brief Check if the map contains a specific key
* @brief Check if the map contains a specific key.
*/
inline bool contains( std::string const& accession ) const
{
return map_.find(accession) != map_.end();
inline bool contains(
std::string const& accession,
bool also_look_up_first_word = true
) const {
return find_( accession, also_look_up_first_word ) != map_.end();
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -251,6 +261,21 @@ class AccessionLookup
}
}

inline ConstIterator find_(
std::string const& accession,
bool also_look_up_first_word
) const {
auto it = map_.find( accession );
if( it != map_.end() ) {
return it;
}
if( also_look_up_first_word ) {
auto const acc_first = utils::split( accession, "\t " )[0];
return map_.find( acc_first );
}
return map_.end();
}

// -------------------------------------------------------------------------
// Data Members
// -------------------------------------------------------------------------
Expand Down
64 changes: 64 additions & 0 deletions test/src/taxonomy/accession_lookup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Genesis - A toolkit for working with phylogenetic data.
Copyright (C) 2014-2024 Lucas Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact:
Lucas Czech <[email protected]>
University of Copenhagen, Globe Institute, Section for GeoGenetics
Oster Voldgade 5-7, 1350 Copenhagen K, Denmark
*/

/**
* @brief
*
* @file
* @ingroup test
*/

#include "src/common.hpp"

#include "genesis/taxonomy/accession_lookup.hpp"
#include "genesis/taxonomy/taxon.hpp"
#include "genesis/taxonomy/taxonomy.hpp"
#include "genesis/taxonomy/formats/taxonomy_reader.hpp"
#include "genesis/taxonomy/functions/taxonomy.hpp"

using namespace genesis;
using namespace genesis::taxonomy;

TEST( Taxonomy, AccessionLookup )
{
// Skip test if no data availabe.
NEEDS_TEST_DATA;

// Read the test taxonomy
std::string const infile = environment->data_dir + "taxonomy/tax_slv_ssu_123.1.clean";
auto tax = TaxonomyReader().read( utils::from_file( infile ));

// Get some taxon from the taxonomy
auto* taxon = find_taxon_by_name( tax, "Acidilobus" );
ASSERT_TRUE( taxon != nullptr );

// Create a lookup for the taxonomy
auto lookup = AccessionLookup();

Check failure on line 56 in test/src/taxonomy/accession_lookup.cpp

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04, gcc-7)

cannot deduce template arguments for ‘AccessionLookup’ from ()

Check failure on line 56 in test/src/taxonomy/accession_lookup.cpp

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04, gcc-8)

cannot deduce template arguments for ‘AccessionLookup’ from ()

Check failure on line 56 in test/src/taxonomy/accession_lookup.cpp

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, llvm-8)

use of class template 'AccessionLookup' requires template arguments

Check failure on line 56 in test/src/taxonomy/accession_lookup.cpp

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, llvm-7)

use of class template 'AccessionLookup' requires template arguments

Check failure on line 56 in test/src/taxonomy/accession_lookup.cpp

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, llvm-6)

cannot refer to class template 'AccessionLookup' without a template argument list

Check failure on line 56 in test/src/taxonomy/accession_lookup.cpp

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, gcc, 11)

missing template arguments before ‘(’ token

Check failure on line 56 in test/src/taxonomy/accession_lookup.cpp

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, llvm, 11)

use of class template 'AccessionLookup' requires template arguments
lookup.add( "ABCD.1", taxon );

// Perform a lookup by full name
EXPECT_TRUE( lookup.get( "ABCD.1 Acidilobaceae Acidilobus" ) != nullptr );

// Perform a lookup by first word
EXPECT_TRUE( lookup.get( "ABCD.1" ) != nullptr );
}

0 comments on commit b64a75a

Please sign in to comment.