<jupyter_start><jupyter_text>Introduction to RBP packageThis notebook includes a demonstration of `rbp` modules' functionality. RandomThe `random` module contains functions for random genomic coordinates and sequences permutation.<jupyter_code>%load_ext autoreload
%autoreload 2

from rbp.random import random_genomic_position, random_genomic_interval, seq_permutation, gen_random_intervals
import pandas as pd<jupyter_output><empty_output><jupyter_text>PermutationsFor a sequence, the package provides `seq_permutation` function to do a random shuffling of characters:<jupyter_code>seq_permutation('ALPHABET')<jupyter_output><empty_output><jupyter_text>Optionally, we might want not to permute individual characters but k-mers. In the example below, note that `ALP`, `HAB` and `ET` k-mers are untouched.<jupyter_code>seq_permutation('ALPHABET', k=3)<jupyter_output><empty_output><jupyter_text>Random positions and intervals With `random_genomic_position` function, it is easy to get a random position in a human genome (GRCh38). Note that all coordinates are 1-based.<jupyter_code>random_genomic_position()<jupyter_output><empty_output><jupyter_text>We can, of course, use any other genome by providing its chromosomes' lengths (either a dictionary or pandas.Series object).<jupyter_code>MINI_GENOME = {'1': 5, '2': 3}
random_genomic_position(MINI_GENOME)<jupyter_output><empty_output><jupyter_text>Instead of one position, we can generate a random interval as follows:<jupyter_code>random_genomic_interval(interval_length=200)<jupyter_output><empty_output><jupyter_text>Generate random genomic intervals with `gen_random_intervals`, with reproducibility (seed).<jupyter_code>gen_random_intervals(sample_size=10, interval_size=25, reference='hg19', seed=10)<jupyter_output><empty_output><jupyter_text>And there is a straightforward way how to generate not one, but many genomic positions or intervals, e.g.<jupyter_code>N = 1000
SEQ_LENGTH = 200
many_intervals = [random_genomic_interval(SEQ_LENGTH) for _ in range(N)]
many_intervals_df = pd.DataFrame(many_intervals, columns=['chr', 'start', 'end'])
many_intervals_df.head()<jupyter_output><empty_output><jupyter_text>generate a random set of nucleotide sequences with `random_nucleotides`.<jupyter_code>from rbp.random.random_sequence import random_nucleotides

random_nucleotides(sample_size=10, seq_length=20)<jupyter_output><empty_output><jupyter_text>Encoding / DecodingEncoding is a process of conversion from genomic sequences to numerical values. Decoding is the opposite, from numerical values to genomic sequences.Currently, only one-hot encoding is implemented. We use boolean representation.<jupyter_code>from rbp.encoding import one_hot_encoding, one_hot_decoding
import matplotlib.pyplot as plt
import pandas as pd

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Convert sequences to one hot encoding with `one_hot_encoding`.Note the resulting array is three-dimensional. The shape of the array is:    (number of sequences x length of one sequence x size of the alphabet )<jupyter_code>X = one_hot_encoding(['AACT', 'CCTG'])
print('array shape is:', X.shape, '\n', sep=' ')
#print('one hot encoded sequences are:', X.astype(int), sep='\n')<jupyter_output>array shape is: (2, 4, 4)<jupyter_text>With `one_hot_decoding`, we can easily get back the original sequences.<jupyter_code>Xd = one_hot_decoding(X)
print('decoded array is:', Xd)<jupyter_output>decoded array is: ['AACT', 'CCTG']<jupyter_text>A generic dot matrix of sequence vs sequence complementarity can be generated with `dot_matrix`.Note the resulting array is three-dimensional. The shape of the array is:    ( number of sequence pairs x length of seq_X x length of seq_Y x 1 )<jupyter_code>from rbp.random.random_sequence import random_nucleotides
from rbp.encoding import dot_matrix

import pandas as pd
# create dataframe
df = pd.DataFrame( {"seq_X" : random_nucleotides(sample_size=10, seq_length=20),
                    "seq_Y" : random_nucleotides(sample_size=10, seq_length=20)})

array_ohe = dot_matrix(df)
print('array shape is:',array_ohe.shape)<jupyter_output>array shape is: (10, 20, 20, 1)
{'fraction_non_alphanumeric': 0.08924264351181863, 'fraction_numerical': 0.0120598166907863, 'mean_word_length': 7.880085653104925, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '219466', 'license': 'MIT', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1277, 'n_words': 376, 'path': 'notebooks/Intro_To_RBP.ipynb', 'repo_name': 'simecek/rbp', 'size': '4146'}