Package: biotext
BiotextThe biotext library offers resources for natural language processing based on
bioinformatics tools. With biotext, it is possible to use native bioinformatics
methods to create text mining strategies. The biotext process starts by
encoding natural language texts in a format based on biological sequences,
which are usable in alignment and vectorization approaches.FeaturesAMINOcode (biotext.aminocode)biotext.aminocode.encode_string: Encodes a string using AMINOcode.biotext.aminocode.encode_list: Encodes all strings in a list using
AMINOcode.biotext.aminocode.decode_string: Decodes a string encoded with AMINOcode.biotext.aminocode.decode_list: Decodes all strings in a list encoded with
AMINOcode.DNAbits (biotext.dnabits)biotext.dnabits.encode_string: Encodes a string using DNAbits.biotext.dnabits.decode_string: Decodes a string encoded with DNAbits.biotext.dnabits.encode_list: Encodes all strings in a list using DNAbits.biotext.dnabits.decode_list: Decodes all strings in a list encoded with
DNAbits.FASTA Tools (biotext.fastatools)biotext.fastatools.import_fasta: Imports a FASTA file.biotext.fastatools.export_fasta: Creates a FASTA file from a list of
sequences.biotext.fastatools.create_seqrecord_list: Creates a list of SeqRecord
objects from a list of sequences.biotext.fastatools.run_clustalo: Performs multiple sequence alignment using
Clustal Omega.biotext.fastatools.get_consensus: Retrieves the consensus sequence from a
set of sequences.biotext.fastatools.get_header: Retrieves the headers from a list of
SeqRecord objects.biotext.fastatools.get_seq: Retrieves the sequences from a list of
SeqRecord objects.biotext.fastatools.fasta_to_mat: Converts FASTA sequences to a vectorial
representation.Word Embedding Tools (biotext.wordembtools)biotext.wordembtools.WordEmbedding: A class for generating word embeddings
from a collection of texts.InstallationYou can install BioText using pip:pip install biotextFunctionsAMINOcode (aminocode)biotext.aminocode.encode_stringEncodes a string with AMINOcode.Parametersinput_string: strNatural language text string to be encoded.detail: strSet details in coding.'d' for details in digits; 'p' for details on the punctuation;
'dp' or 'pd' for both.Default is 'dp'.Returnsencoded_string: stringEncoded text.ExampleEncode a string.importbiotextasbtinput_string="Hello world!"encoded_string=bt.aminocode.encode_string(input_string,'dp')print(encoded_string)# HYELLYQYSYWYQRLDYPWbiotext.aminocode.encode_listEncodes all strings in a list with AMINOcode.Parametersstring_list: listList of string to be encoded.detail: strSet details in coding.'d' for details in digits; 'p' for details
on the punctuation; 'dp' or 'pd' for both.Default is 'dp'.verbose: boolIf True displays progress.Returnsencoded_list: listList with all encoded text in string format.ExampleEncode the strings in a list and view the result of the first item.importbiotextasbtstring_list=['Hello','world','!']encoded_list=bt.aminocode.encode_list(string_list,detail='dp')print(encoded_list)# ['HYELLYQ', 'YWYQRLD', 'YPW']biotext.aminocode.decode_stringDecodes a string with AMINOcode reverse.Parametersinput_string: strText string encoded with AMINOcode.detail: strSet details in coding. 'd' for details in digits; 'p' for details on
the punctuation; 'dp' or 'pd' for both.Default is 'dp'.Returnsdecoded_string: strDecoded text.ExampleDeconde a string.importbiotextasbtencoded_string="HYELLYQYSYWYQRLDYPW"decoded_string=bt.aminocode.decode_string(encoded_string,'dp')print(decoded_string)# hello world!biotext.aminocode.decode_listDecodes all strings in a list with reverse AMINOcode.Parametersstring_list: listList of string encoded with aminocode.detail: strSet details in coding. 'd' for details in digits; 'p' for details on
the punctuation; 'dp' or 'pd' for both.Default is 'dp'.verbose: boolIf True displays progress.Returnsdecoded_list: list of stringList with all decoded text.ExampleDescode the strings in a list and view the result with a loop.importbiotextasbtencoded_list=['HYELLYQ','YWYQRLD','YPW']decoded_list=bt.aminocode.decode_list(encoded_list,detail='dp')print(decoded_list)# ['hello', 'world', '!']DNAbits (dnabits)biotext.dnabits.encode_stringEncodes a string with DNAbits.Parametersinput_string: stringNatural language text string to be encoded.Returnsencoded_string: stringEncoded text.ExampleEncode a string.importbiotextasbtinput_string="Hello world!"encoded_string=bt.dnabits.encode_string(input_string)print(encoded_string)# AGACCCGCATGCATGCTTGCAAGATCTCTTGCGATCATGCACGCCAGAbiotext.dnabits.decode_stringDecodes a string with DNAbits reverse.Parametersinput_string: stringText string encoded with DNAbits.Returnsdecoded_string: stringDecoded text.ExampleDecode a string.importbiotextasbtencoded_string="AGACCCGCATGCATGCTTGCAAGATCTCTTGCGATCATGCACGCCAGA"decoded_string=bt.dnabits.decode_string(encoded_string)print(decoded_string)# Hello world!biotext.dnabits.encode_listEncodes all strings in a list with DNAbits.Parametersstring_list: listList of string to be encoded.verbose: boolIf True displays progress.Returnsencoded_list: listList with all encoded text in string format.ExampleEncode the strings in a list and view the result of the first item.importbiotextasbtstring_list=['Hello','world','!']encoded_list=bt.dnabits.encode_list(string_list)print(encoded_list)# ['AGACCCGCATGCATGCTTGC', 'TCTCTTGCGATCATGCACGC', 'CAGA']biotext.dnabits.decode_listDecodes all strings in a list with reverse DNAbits.Parametersstring_list: listList of string encoded with DNAbits.verbose: boolIf True displays progress.Returnsdecoded_list: list of string
- List with all decoded text.ExampleDecode the strings in a list and view the result with a loop.importbiotextasbtencoded_list=['AGACCCGCATGCATGCTTGC','TCTCTTGCGATCATGCACGC','CAGA']decoded_list=bt.dnabits.decode_list(encoded_list)print(decoded_list)# ['Hello', 'world', '!']FASTA Tools (fastatools)biotext.fastatools.import_fastaUses biopython to import a FASTA file.Parametersinput_file_name: string (valid file name)Input fasta file name.Returnsseqrecord_list: list of SeqRecordList of SeqRecord imported from file.ExampleImport a FASTA file named 'sequences.fasta'.importbiotextasbtinput_file='sequences.fasta'fasta=bt.fastatools.import_fasta(input_file)# print first sequence in input fileprint(fasta[0])# ID: 1# Name: 1# Description: 1# Number of features: 0# Seq('HYELLYQYSYWYQRLD')biotext.fastatools.export_fastaCreate a file using a SeqRecord (Biopython object) list.Parametersoutput_file_name: stringOutput fasta file name.seqrecord_list: list of SeqRecordList of SeqRecord.ExampleExport a SeqRecord list as FASTA file named 'sequences.fasta'.importbiotextasbtseq_list=['ACTG','GTCA']seqrecord_list=bt.fastatools.create_seqrecord_list(seq_list)bt.fastatools.export_fasta(seqrecord_list,'sequences.fasta')biotext.fastatools.create_seqrecord_listCreate a list of SeqRecord (Biopython object) with a string list.Parametersseq_list: list of stringList of biological sequences in string format.header: list of stringList of headers in string format, if set to 'None' the headers will
be automatically defined with numbers in increasing order.Returnsseqrecord_list: list of SeqRecordList of SeqRecord.ExampleDecode a string.importbiotextasbtseq_list=['ACTG','GTCA']seqrecord_list=bt.fastatools.create_seqrecord_list(seq_list)foriinseqrecord_list:print(i)# ID: 1# Name: <unknown name># Description: 1# Number of features: 0# Seq('ACTG')# ID: 2# Name: <unknown name># Description: 2# Number of features: 0# Seq('GTCA')biotext.fastatools.run_clustaloRun Clustal Omega multiple sequence alignment on the input file.Parametersinput_file_name: strPath to the input file containing the sequences to align.args: str, optionalAdditional arguments to pass to Clustal Omega. Defaults to an empty string.Returnsalign: Bio.Align.MultipleSeqAlignmentAligned sequences in the Clustal format.ExamplePerform multiple sequence alignment using Clustal Omega:importbiotextasbtinput_file='sequences.fasta'alignment=bt.fastatools.run_clustalo(input_file)print(alignment)# Alignment with 3 rows and 16 columns# HYELLYQYSYWYQRLD 1# HYELLYQ--------- 2# ---------YWYQRLD 3biotext.fastatools.get_consensusGet the consensus sequence from a list of sequences.Parametersseq_list: listList of sequences in SeqRecord object format or as strings.preserve_gap: bool, optionalIf True, the consensus sequence may contain gaps ("-") based
on the majority characters and the gaps present in the aligned sequences.If False, the consensus sequence is determined without gaps
by considering only the majority characters.Returnsconsensus: strConsensus sequence based on the majority characters, with or without
gaps ("-") depending on thepreserve_gapparameter.align: listList of aligned sequences.ExampleCalculate the consensus sequence from a list of sequences:importbiotextasbtseq_list=['ACTG','ACTC','ACCC','ACC']consensus,align=bt.fastatools.get_consensus(seq_list)print('Consensus: ',consensus)print('Alignment: ',align)# Consensus:  ACCC# Alignment:  ['ACTG', 'ACTC', 'ACCC', 'ACC-']biotext.fastatools.get_headerGet the header from all items in a list of SeqRecord (Biopython object).Parametersseqrecord_list: list of SeqRecordList of SeqRecord.Returnsheader_list: list of stringList of all headers extracted from input.ExampleCreate seqrecord_list, extract headers and print it.importbiotextasbtseq_list=['ACTG','GTCA']seqrecord_list=bt.fastatools.create_seqrecord_list(seq_list)extracted_header_list=bt.fastatools.get_header(seqrecord_list)print(extracted_header_list)# ['1', '2']biotext.fastatools.get_seqGet the sequences from all items in a list of SeqRecord (Biopython object).Parametersseqrecord_list: list of SeqRecordList of SeqRecord.Returnsseq_list: list of stringList of all sequences extracted from input.ExampleCreate seqrecord_list, extract sequences and print it.importbiotextasbtseq_list=['ACTG','GTCA']seqrecord_list=bt.fastatools.create_seqrecord_list(seq_list)extracted_seq_list=bt.fastatools.get_seq(seqrecord_list)print(extracted_seq_list)# ['ACTG', 'GTCA']biotext.fastatools.fasta_to_matConvert FASTA sequences to a matrix representation using SWeeP method.Parametersfasta: listList of sequences in SeqRecord object format or as strings.mask: list, optionalA list specifying the mask values. Defaults to [2, 1, 2].**kwargs: dict, optionalAdditional keyword arguments to pass to thefas2sweepfunction.Returnsmat: numpy.ndarray or scipy.sparse.lil_matrixMatrix representation of the sequences.ExampleConvert FASTA sequences to a matrix representation:importbiotextasbtseq_list=['HYELLYQYSYWYQRLD','HYELLYQ','YWYQRLD']matrix=bt.fastatools.fasta_to_mat(seq_list)print(matrix.shape)# (3, 600)Word Embedding Tools (wordembtools)wordembtoolsA class for generating word embeddings from a collection of texts.Parametersdata_set: list or pandas.SeriesThe collection of texts to generate embeddings.word_set: list, optionalA pre-defined set of words to use for the embedding. Defaults to None.remove_stopwords: bool, optionalWhether to remove stop words from the texts. Defaults to False.stopwords_list: list, optionalCustom list of stop words to remove from the texts. Defaults to None.min_occ_to_use: int, optionalThe minimum number of occurrences of a word in the collection of texts
to include it in the embedding. Defaults to 100.max_words: int, optionalThe maximum number of words to include in the word set. Defaults to
10,000.word_tokenizer_fun: function, optionalCustom function for tokenizing words in each text. Defaults to None.sweep_projection_mat: numpy.ndarray, optionalThe projection matrix for SWeeP vectorization. Defaults to None.sweep_mask: list, optionalThe mask to apply during SWeeP vectorization. Defaults to [2, 1, 2].sweep_dtype: dtype, optionalThe data type for SWeeP vectorization. Defaults to None.sweep_composition: str, optionalThe composition mode for SWeeP vectorization. Defaults to 'binary'.preserve_data_set_splited: bool, optionalWhether to preserve the split data set object. Defaults to False.preserve_data_set_sweeped: bool, optionalWhether to preserve the swept data set object. Defaults to False.n_jobs: int, optionalThe number of jobs to use for parallelization. Defaults to 1.chunk_size: int, optionalThe size of each chunk for parallelization. Defaults to 1000.sweep_n_jobs: int, optionalThe number of jobs to use for SWeeP vectorization. Defaults to None.If None, it receives the value of n_jobs.sweep_chunk_size: int, optionalThe size of each chunk for SWeeP vectorization. Defaults to None.If None, it receives the value of chunk_size.verbose: bool, optionalWhether to print progress messages. Defaults to True.Attributesword_set: listSet of unique words.word_embedding: numpy.ndarrayWord embeddings for the words in the word_set.elapsed_time: listElapsed time for each step of the process.Exampleimportbiotextasbttexts=[]withopen('texts.txt','r')asfile:forlineinfile:texts.append(line)we=bt.wordembtools.WordEmbedding(data_set=texts)embeddings=we.word_embeddingUsage ExamplesEncoding with AMINOcodeimportbiotextasbtinput_string="Hello world!"encoded_string=bt.aminocode.encode_string(input_string,'dp')print(encoded_string)# HYELLYQYSYWYQRLDYPWstring_list=['Hello','world','!']encoded_list=bt.aminocode.encode_list(string_list,detail='dp')print(encoded_list)# ['HYELLYQ', 'YWYQRLD', 'YPW']Decoding with AMINOcodeimportbiotextasbtencoded_string="HYELLYQYSYWYQRLDYPW"decoded_string=bt.aminocode.decode_string(encoded_string,'dp')print(decoded_string)# hello world!encoded_list=['HYELLYQ','YWYQRLD','YPW']decoded_list=bt.aminocode.decode_list(encoded_list,detail='dp')print(decoded_list)# ['hello', 'world', '!']Importing and Exporting FASTA Filesimportbiotextasbtinput_file='sequences.fasta'fasta=bt.fastatools.import_fasta(input_file)print(fasta[0])# Print the first sequence in the input fileseq_list=['ACTG','GTCA']seqrecord_list=bt.fastatools.create_seqrecord_list(seq_list)bt.fastatools.export_fasta(seqrecord_list,'sequences.fasta')Generating Word Embeddingsimportbiotextasbttexts=['This is the first text.','This is the second text.','And this is the third text.']we=bt.wordembtools.WordEmbedding(data_set=texts,min_occ_to_use=0)embeddings=we.word_embeddingprint(embeddings)Vectorizing FASTA Sequenceimportbiotextasbtseq_list=['HYELLYQ','YWYQRLD','YPW']matrix=bt.fastatools.fasta_to_mat(seq_list)print(matrix.shape)# (3, 600)Encoding Text with AMINOcode and Vectorizingimportbiotextasbttexts=['This is the first text.','This is the second text.','And this is the third text.']encoded_texts=bt.aminocode.encode_list(texts,'dp')matrix=bt.fastatools.fasta_to_mat([encoded_texts])print(matrix.shape)# (3, 600)