Loading 2023-Spring/2023-01-30-session-02/reg_expressions.ipynbdeleted 100644 → 0 +0 −105 Original line number Diff line number Diff line %% Cell type:markdown id:4823b8fa-f087-4a5f-8247-b0c0879c9f60 tags: # reg_expressions Presented by Cristian E B. %% Cell type:markdown id:d735e17e-13b5-482c-9704-f19515dbc923 tags: Import `re` short for the regular expression module. (it should be present by default. Otherwise use `pip` or `pip3` to install. %% Cell type:code id:7d0b628c-ebbb-4c7d-b429-120c7e2887c9 tags: ``` python import re ``` %% Cell type:markdown id:78a8c47f-21e1-4861-ac3b-36516ed1b9ab tags: Create function to open fasta files specifically %% Cell type:code id:92db34b7-e9d3-4d0e-a3b4-45ed0c2d85fd tags: ``` python def open_fasta(file_name): """Open Fasta sequence files. Parameters ---------- file_name : Str File name. Returns ------- fasta_seq : Str Seqeunce data contained in the Fasta file. """ # open file seq_file = open(file_name, 'r') data = seq_file.read() seq_file.close() #index of first \n character index = data.find('\n') # get nucleotide sequence after first \n fasta_seq = data[index+1:].replace('\n', '') fasta_seq = fasta_seq.upper() return fasta_seq ``` %% Cell type:markdown id:5611118f-ccb4-454d-9466-f8ca1d87b4c0 tags: Define function to acquire regular expression and search in selected fasta file. %% Cell type:code id:b9350e9f-e482-4a71-8b28-4fae40a7ee98 tags: ``` python def find_sequence(expression, sequence): """Find regular expression in sequence. Parameters ---------- expression : Str Regular expression. sequence : Str String where the search is performed. Returns ------- None. """ hit_list = re.finditer(expression, sequence) print('{:<10s} {:<10s} {:s}'. format('start', 'end', 'match')) for hit in hit_list: match = hit.group(0) start, end = hit.span() print('{:<10d} {:<10d} {:s}'. format(start+1, end, match)) ``` %% Cell type:code id:91a9e266-2f32-4132-8bac-2925ff6d0aa9 tags: ``` python Run the command. Assumes that the fasta file has been opened. #Y =open_fasta('chrY.fna') s = 'ACCTGAACGAGTGTGAGTAGGCCA' find_sequence('GT', s) ``` Loading
2023-Spring/2023-01-30-session-02/reg_expressions.ipynbdeleted 100644 → 0 +0 −105 Original line number Diff line number Diff line %% Cell type:markdown id:4823b8fa-f087-4a5f-8247-b0c0879c9f60 tags: # reg_expressions Presented by Cristian E B. %% Cell type:markdown id:d735e17e-13b5-482c-9704-f19515dbc923 tags: Import `re` short for the regular expression module. (it should be present by default. Otherwise use `pip` or `pip3` to install. %% Cell type:code id:7d0b628c-ebbb-4c7d-b429-120c7e2887c9 tags: ``` python import re ``` %% Cell type:markdown id:78a8c47f-21e1-4861-ac3b-36516ed1b9ab tags: Create function to open fasta files specifically %% Cell type:code id:92db34b7-e9d3-4d0e-a3b4-45ed0c2d85fd tags: ``` python def open_fasta(file_name): """Open Fasta sequence files. Parameters ---------- file_name : Str File name. Returns ------- fasta_seq : Str Seqeunce data contained in the Fasta file. """ # open file seq_file = open(file_name, 'r') data = seq_file.read() seq_file.close() #index of first \n character index = data.find('\n') # get nucleotide sequence after first \n fasta_seq = data[index+1:].replace('\n', '') fasta_seq = fasta_seq.upper() return fasta_seq ``` %% Cell type:markdown id:5611118f-ccb4-454d-9466-f8ca1d87b4c0 tags: Define function to acquire regular expression and search in selected fasta file. %% Cell type:code id:b9350e9f-e482-4a71-8b28-4fae40a7ee98 tags: ``` python def find_sequence(expression, sequence): """Find regular expression in sequence. Parameters ---------- expression : Str Regular expression. sequence : Str String where the search is performed. Returns ------- None. """ hit_list = re.finditer(expression, sequence) print('{:<10s} {:<10s} {:s}'. format('start', 'end', 'match')) for hit in hit_list: match = hit.group(0) start, end = hit.span() print('{:<10d} {:<10d} {:s}'. format(start+1, end, match)) ``` %% Cell type:code id:91a9e266-2f32-4132-8bac-2925ff6d0aa9 tags: ``` python Run the command. Assumes that the fasta file has been opened. #Y =open_fasta('chrY.fna') s = 'ACCTGAACGAGTGTGAGTAGGCCA' find_sequence('GT', s) ```