Package: text2term
text2termontology mapperA tool for mapping free-text descriptions of (biomedical) entities to controlled terms in ontologies.InstallationInstall package usingpip:pip install text2termExamplesProgrammaticimporttext2termimportpandasdf1=text2term.map_terms("test/unstruct_terms.txt","http://www.ebi.ac.uk/efo/efo.owl")df2=text2term.map_terms(["asthma","acute bronchitis"],"http://www.ebi.ac.uk/efo/efo.owl")df3=text2term.map_terms({"asthma":"disease","acute bronchitis":["disease","lungs"]},"http://www.ebi.ac.uk/efo/efo.owl")Below is an example of caching, assuming the same imports as above:text2term.cache_ontology("http://www.ebi.ac.uk/efo/efo.owl","EFO")df1=text2term.map_terms("test/unstruct_terms.txt","EFO",use_cache=True)df2=text2term.map_terms(["asthma","acute bronchitis"],"EFO",use_cache=True)text2term.clear_cache("EFO")Command LineThe basic use of the tool requires asourcefile containing a list of terms to map to the giventargetontology:python text2term -s test/unstruct_terms.txt -t http://www.ebi.ac.uk/efo/efo.owlSpecify an output file where the mappings should be saved using-o:python text2term -s test/unstruct_terms.txt -t efo.owl -o /Documents/my-mappings.csvSet the minimum acceptable similarity score for mapping each given term to an ontology term using-min:python text2term -s test/unstruct_terms.txt -t efo.owl -min 0.8The mapped terms returned will have been determined to be 0.8 similar to their source terms in a 0-1 scale.Exclude deprecated ontology terms (declared as such viaowl:deprecated true) using-d:python text2term -s test/unstruct_terms.txt -t efo.owl -dLimit search to only terms whose IRIs start with any IRI given in a list specified using-iris:python text2term.py -s test/unstruct_terms.txt -t efo.owl -iris http://www.ebi.ac.uk/efo/EFO,http://purl.obolibrary.org/obo/HPHere, because EFO reuses terms from other ontologies such as HP and GO, the HP terms would be included but the GO terms would be excluded.Use the cache on the command line, first by flagging it, then in the future using the acronym:python text2term -s test/unstruct_terms.txt -t http://www.ebi.ac.uk/efo/efo.owl -c EFOThen, after running this, the following command is equivalent:python text2term -s test/unstruct_terms.txt -t EFOProgrammatic UsageThe tool can be executed in Python with themap_termsfunction:text2term.map_terms(source_terms,target_ontology='http://some.ontology/v1.owl',base_iris=(),csv_columns=(),excl_deprecated=False,max_mappings=3,mapper=Mapper.TFIDF,min_score=0.3,output_file='',save_graphs=False,save_mappings=False,separator=',',use_cache=False,term_type=OntologyTermType.CLASS,incl_unmapped=False)NOTE: As of 3.0.0, the former three functions (map_file,map_terms,map_tagged_terms) have been condensed into one function. Users can now change the name of any function in old code tomap_termsand it reads the input context to maintain the functionality of each one.ArgumentsFormap_terms, the first argument can be any of the following: 1) a string that specifies a path to a file containing the terms to be mapped, 2) a list of the terms to be mapped, or 3) a dictionary where the keys are the terms to be mapped, and values can be a list of tags or a list of TaggedTerm objects (see below).
Currently, the tags do not affect the mapping in any way, but they are added to the output dataframe at the end of the process. The exception is the Ignore tag, which causes the term to not be mapped at all, but still be outputted in the results if the incl_unmapped argument is True (see below).All other arguments are the same, and have the same functionality:target_ontology: str
Path or URL or acronym of 'target' ontology to map the source terms to. When the chosen mapper is BioPortal or Zooma,
provide a comma-separated list of ontology acronyms (eg 'EFO,HPO') or write 'all' to search all ontologies. When the target ontology has been previously cached, provide the ontology name that was used to cache it.
As of version 2.3.0, it is possible to specify ontology acronyms as thetarget_ontology(eg "EFO" or "CL"), which is achieved usingbioregistryto retrieve URLs for those acronyms.base_iris: tuple
Map only to ontology terms whose IRIs start with one of the strings given in this tuple, for example:
('http://www.ebi.ac.uk/efo','http://purl.obolibrary.org/obo/HP')csv_column: tuple
Allows the user to specify a column to map if a csv is passed in as the input file. Ignored if the input is not a file path.source_terms_ids: tuple
Collection of identifiers for the given source terms
WARNING: While this is still available for the tagged term function, it is worth noting that dictionaries do not necessarily preserve order, so it is not recommended. If using the TaggedTerm object, the source terms can be attached there to guarantee order.excl_deprecated: bool
Exclude ontology terms stated as deprecated viaowl:deprecated truemapper: mapper.Mapper
Method used to compare source terms with ontology terms. One of: levenshtein, jaro, jarowinkler, jaccard, fuzzy, tfidf, zooma, bioportal
These can be initialized by invoking mapper.Mapper e.g.mapper.Mapper.TFIDFmax_mappings: int
Maximum number of top-ranked mappings returned per source termmin_score: float
Minimum similarity score [0,1] for the mappings (1=exact match)output_file: str
Path to desired output file for the mappingssave_graphs: bool
Save vis.js graphs representing the neighborhood of each ontology termsave_mappings: bool
Save the generated mappings to a file (specified byoutput_file)seperator: str
Character that separates the source term values if a file input is given. Ignored if the input is not a file path.use_cache: bool
Use the cache for the ontology. More details are below.term_type: term.OntologyTermType
Specifies whether to map to ontology classes, properties or any of the two. Possible values are ['class', 'property', 'any'].incl_unmapped: bool
Include all unmapped terms in the output. If something has been tagged 'Ignore' (see below) or falls below themin_scorethreshold, it is included without a mapped term at the end of the output data frame.All default values, if they exist, can be seen above.Return ValueBoth functions return the same value:df: Data frame containing the generated ontology mappingsOntology CachingAs of version 1.1.0, users can cache ontologies that they want to use regularly or quickly. Programmatically, there are two steps to using the cache: creating the cache, then accessing it. First, the user can cache ontologies using either of two functions:cache_ontology(ontology_url,ontology_acronym="",base_iris=())cache_ontology_set(ontology_registry_path)The first of these will cache a single ontology from a URL or file path, with it being referenced by an acronym that will be used to reference it later. If no acronym is given, it will use the URL as the cache name. An example can be found above.
The second function allows the user to cache several ontologies at once by referencing a CSV file of the format:acronym,version,url. An example is provided inresources/ontologies.csvOnce an ontology has been cached by either function, it is stored in a cache folder locally, and thus can be referenced even in different Python instances.
As of version 2.3.0, thecache_ontologyfunction also returns an object that can be used to call any of themapfunctions, as well asclear_cacheandcache_exists. These have the same arguments, exceptontology_targetis not specified and there is nouse_cacheoption, as it is always True.NOTE: Due to how ontologies are processed in memory,cache_ontology_setmust be used to cache multiple ontologies in a single Python instance. Ifcache_ontologyis used multiple times in one instance, the behavior is undefined and may cause visible or invisible errors.After an ontology is cached, the user can access the cache by using the assigned acronym in the place oftarget_ontologyand setting theuse_cacheflag toTrue.
To clear the cache, one can call:clear_cache(ontology_acronym='')If no arguments are specified, the entire cache will be cleared. Otherwise, only the ontology with the given acronym will be cleared.
Finally,cache_exists(ontology_acronym='')is a simple function that returnsTrueif the given acronym exists in the cache, andFalseotherwise. It is worth noting that while ontology URLs can repeat, acronyms must be distinct in a given environment.Input PreprocessingAs of version 1.2.0, text2term includes regex-based preprocessing functionality for input terms. Specifically, these functions take the input terms and a collection of (user-defined) regular expressions, then match each term to each regular expression to simplify the input term.Like the "map" functions above, the two functions differ on whether the input is a file or a list of strings:preprocess_terms(terms,template_path,output_file='',blocklist_path='',blocklist_char='',rem_duplicates=False)preprocess_tagged_terms(file_path,template_path='',blocklist_path='',blocklist_char='',rem_duplicates=False,separator=';:;')In all cases, the regex templates and blocklist must be stored in a newline-separated file. If an output file is specified, the preprocessed strings are written to that file and the list of preprocessed strings is returned.The blocklist functionality allows the user to specify another regex file. If any terms match any regex in blocklist, they are removed from the terms, or, if a blocklist character is specified, replaced with that character for placeholding.
NOTE: As of version 2.1.0, the arguments were changed to "blocklist" from "blacklist". Backwards compatibility is currently supported, but will likely be discontinued at the next major release.The Remove Duplicatesrem_duplicatesfunctionality will remove all duplicate terms after processing, if set toTrue.
WARNING: Removing duplicates at any point does not guarantee which original term is kept. This is particularly important if original terms have different tags, so user caution is advised.The functionpreprocess_terms()returns a dictionary where the keys are the original terms and the values are the preprocessed terms.
Thepreprocess_tagged_terms()function returns a list of TaggedTerm items with the following function contracts:def__init__(self,term=None,tags=[],original_term=None,source_term_id=None)defadd_tags(self,new_tags)defupdate_term(self,term)defupdate_source_term_id(self,source_term_id)defget_original_term(self)defget_term(self)defget_tags(self)defget_source_term_id(self)As mentioned in the mapping section above, this can then be passed directly tomap_terms, allowing for easy programmatic usage. Note that this allows multiple of the same preprocessed term with different tags.Note on NA values in input: As of v2.0.3, when the input to text2term is a table file, any rows that containNAvalues in the specified term column, or in the term ID column (if provided), will be ignored.Tag UsageAs of 3.0.0, some tags have additional functionality that is added when attached to a term:IGNORE:
If an ignore tag is added to a term, that term will not be mapped to any terms in the ontology. It will only be included in the output if theincl_unmappedargument is True. Here are the following values that count as ignore tags:IGNORE_TAGS=["ignore","Ignore","ignore ","Ignore "]Command Line UsageAfter installation, execute the tool from a command line as follows:python text2term -s SOURCE -t TARGET [-o OUTPUT] [-m MAPPER] [-csv CSV_INPUT] [-top TOP_MAPPINGS] [-min MIN_SCORE] [-iris BASE_IRIS] [-d EXCL_DEPRECATED] [-g SAVE_TERM_GRAPHS]To display a help message with descriptions of tool arguments do:python text2term -horpython text2term --helpRequired arguments-s SOURCEInput file containing 'source' terms to map to ontology terms (list of terms or CSV file).-t TARGETPath or URL of 'target' ontology to map source terms to. When the chosen mapper is BioPortal or Zooma, provide a comma-separated list of acronyms (eg 'EFO,HPO') or write'all'to search all ontologies.Optional arguments-o OUTPUTPath to desired output file for the mappings.-m MAPPERMethod used to compare source terms with ontology terms. One of:levenshtein, jaro, jarowinkler, jaccard, indel, fuzzy, tfidf, zooma, bioportal.-csv CSV_INPUTIndicates a CSV format input—follow with the name of the column containing terms to map, optionally followed by the name of the column containing identifiers for the terms (eg 'my terms,my term ids').-top TOP_MAPPINGSMaximum number of top-ranked mappings returned per source term.-min MIN_SCOREMinimum similarity score [0,1] for the mappings (1=exact match).-iris BASE_IRISMap only to ontology terms whose IRIs start with a value given in this comma-separated list (eg 'http://www.ebi.ac.uk/efo,http://purl.obolibrary.org/obo/HP)').-d EXCL_DEPRECATEDExclude ontology terms stated as deprecated viaowl:deprecated true.-g SAVE_TERM_GRAPHSSavevis.jsgraphs representing the neighborhood of each ontology term.-c STORE_IN_CACHEUsing this flag followed by the acronym the ontology should be stored as, the program will same the target ontology to the cache. After that, referencing the acronym intargetwill reference the cache. Examples are above.