delete_seq_following_pattern_within_multiFASTA.py
script¶If you'd like an active Jupyter session to run this notebook, launch one by clicking here, and then upload this notebook to the session that starts up.
Otherwise, the static version is rendered more nicely via here.
If you haven't used one of these notebooks before, they're basically web pages in which you can write, edit, and run live code. They're meant to encourage experimentation, so don't feel nervous. Just try running a few cells and see what happens!.
Some tips:
You'll need the current version of the script to run this notebook, and the next cell will get that. (Remember if you want to make things more reproducible when you use the script with your own data, you'll want to edit calls such as this to fetch a specific version of the script. How to do this is touched upon in the comment below here.
!curl -O https://raw.githubusercontent.com/fomightez/sequencework/master/AdjustFASTA_or_FASTQ/delete_seq_following_pattern_within_multiFASTA.py
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 17560 100 17560 0 0 68862 0 --:--:-- --:--:-- --:--:-- 68862
%run delete_seq_following_pattern_within_multiFASTA.py -h
usage: delete_seq_following_pattern_within_multiFASTA.py [-h] [-ld] [-os OUTPUT_SUFFIX] SEQUENCE_FILE RECORD_ID PATTERN delete_seq_following_pattern_within_multiFASTA.py takes a sequence pattern string, a sequence file (FASTA-format), and a record id, and deletes any sequence following the sequence pattern. In other words it trims the specified sequence, to make the first match to the pattern the new end. (The FASTA- formatted sequence file is assumed by default to be a multi-FASTA, i.e., multiple sequences in the provided file, although it definitely doesn't have to be. In case it is only a single sequence, the record id becomes moot, see below.) Nothing will be returned; however a copy of the FASTA sequence file with the truncated sequence specified will be produced. **** Script by Wayne Decatur (fomightez @ github) *** positional arguments: SEQUENCE_FILE Name of sequence file to use as input. Must be FASTA format. Can be a multi-FASTA file, i.e., multiple sequences in FASTA format in one file. RECORD_ID Specific identifier of sequence entry in sequence file to modify. If the provided sequence file only contains one sequence, then that sequence will be altered, and whatever is provided for this parameter will be ignored. In other words, if the sequence file is not a multi-FASTA file, you don't need to determine the identifier and can instead just enter `blahblah` or any other nonsensical string in this spot. PATTERN Sequence or sequence pattern to use to locate site after which to delete in the specified sequence. Regular expressions are accepted here; however any information about case will be ignored as the provided sequence pattern and sequence will both be converted to lower case to check for a match. optional arguments: -h, --help show this help message and exit -ld, --leave_dashes Add this flag when calling the script in order to be able to use gaps (represented as dashes) in the pattern required to match. I.E., for matching with an aligned FASTA file. (***ATYPICAL.***) -os OUTPUT_SUFFIX, --output_suffix OUTPUT_SUFFIX OPTIONAL: Set a suffix for including in file name of output. If none provided, '_clipped' will be used.
#write example FASTA to file
s = '''>evoli
atctgatctggggcgaaatgagactgatctgatctggtctgtggcg
>smer
atctgaatctgagactatatgagactgatctgatctgctctgaagc
'''
!echo "{s}" > sequence.fa
%%bash
python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer tCtgAGact
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
Note that cell above illustrates that the comparison is insensitive to case.
In the above cell and elsewhere in this notebook, %%bash
cell magic is used to send this to the shell to run as if on the command line.
You could simply run something like python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer tgAtct
if you are working on the command line directly. In fact, the terminal is available from the Jupyter dashboard (or from the JupyterLab launcher) and you can feel free to try running the command below in a terminal in this Jupyter session if you'd like.
python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer tCt
We'll use a shorter way to send the commad to the shell in the next cell.
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaatctgagact
(The cell above uses another Jupyter notebook/ IPython trick to send a command to the command line. Namely that anything on a line after an exclamation point !
will be executed on the system command line. However, using that style I saw no advanced display formatting of the stderr when I tried using the exclamation point, e.g., !python delete_seq_following_pattern_within_multiFASTA.py smer tCt
vs. using the %%bash
cell magic. Hence, I used %%bash
in the demo when calling the script.)
Although it is good practice to keep original versions of files, if you absolutely need to replace the original file, you can rename the ouput file to replace the original with a command similar to this:
!mv sequence_clipped.fa sequence.fa
Remember you can dispense with providing an actual record id if there is only one record.
#write example FASTA-formatted with one sequence to file
s = '''>evoli
atctgatctggggcgaaatgagactgatctgatctggtctgtggcg
'''
!echo "{s}" > single_sequence.fa
You still have to provide something for record identifier, but it can be any string. In the example, below moot
is used. Completely irrelevant but the 'placeholder' makes the command have all the parts needed.
%%bash
python delete_seq_following_pattern_within_multiFASTA.py single_sequence.fa moot tCtgA
Single sequence with id of 'evoli' provided in the sequence file. It will be used to search for the provided sequence pattern and delete the residues after it. 2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'evoli', and saved within a modified version of 'single_sequence.fa' as the output file 'single_sequence_clipped.fa'. *****************DONE**************************
If you are used to using Jupyter notebooks, you can use %run
instead of python delete_seq_following_pattern_within_multiFASTA sequence.fa smer tct
to get the same result, as shown in the next call.
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer tct --output_suffix _diff_clipped
5 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_diff_clipped.fa'. *****************DONE**************************
!head sequence_diff_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atct
Very useful for when using this in a Jupyter notebook to build into a pipeline or workflow.
Prepare first by importing the main function from the script into the notbeook environment.
from delete_seq_following_pattern_within_multiFASTA import delete_seq_following_pattern_within_multiFASTA
(That call will look redundant; however, it actually means from the file delete_seq_following_pattern_within_multiFASTA.py
import the delete_seq_following_pattern_within_multiFASTA()
function.)
Then call that function and provide the needed arguments in the call. The needed arguments are the sequence file
, record id
of the specific sequence to search for the pattern within (can be gibberish if there is only one sequence provided inside sequence file), sequence pattern to search for
, and number of residues
to get after the sequence.
The function will produce a file as output if there is a match.
delete_seq_following_pattern_within_multiFASTA("sequence.fa", "evoli", "GATCTGGGGCGA")
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'evoli', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli CLIPPED atctgatctggggcga >smer atctgaatctgagactatatgagactgatctgatctgctctgaagc
The equivalent to using the --output_suffix
option on the command line can also be done when calling the function; however, the syntax is slightly different because the way functions work in Python differs than ways you use things on the command line.
delete_seq_following_pattern_within_multiFASTA("sequence.fa", "evoli", "GATCT", suffix_for_saving = "_clipped_fun")
3 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'evoli', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped_fun.fa'. *****************DONE**************************
!head sequence_clipped_fun.fa
>evoli CLIPPED atctgatct >smer atctgaatctgagactatatgagactgatctgatctgctctgaagc
Remember you can dispense with providing an actual, real record id if there is only one record.
You just need to supply something in that spot as a 'placeholder'.
delete_seq_following_pattern_within_multiFASTA("single_sequence.fa", "evoli", "GATCT", suffix_for_saving = "_clipped")
Single sequence with id of 'evoli' provided in the sequence file. It will be used to search for the provided sequence pattern and delete the residues after it. 3 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'evoli', and saved within a modified version of 'single_sequence.fa' as the output file 'single_sequence_clipped.fa'. *****************DONE**************************
!head single_sequence_clipped.fa
>evoli CLIPPED atctgatct
Providing sequence patterns to search for can accomodate regular expression search terms (see Appendix 2 of Haddock and Dunn's Practical Computing for Biologists). However, it can be tricky to input some of the symbols and special characters that regular expression search terms tend to use and get them interpreted exactly as expected. Especially in light of the many ways one can call this script or the associated function in a Jupyter notebook.
I illustrate some of the things I found to work here.
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer a{{2,}}
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
That regular expression search term demonstrated above is equivalent to a{2,}
and searches for two or more matches to a
in a row (or A
in row because I make comparison case insensitive beyond input expression). Note that the brackets have to be doubled up to get read in from IPython to ultimately Python as single brackets. (Single brackets got converted to parantheses for some reason.) I worked this out by testing input from command by printing what I had right before search and luckily tried what I had learned from here for dealing with brackets and .format()
.
This is probably the best route to use regular expressions.
delete_seq_following_pattern_within_multiFASTA("sequence.fa", "smer", "a{2,}", suffix_for_saving = "_clipped_viafun")
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped_viafun.fa'. *****************DONE**************************
!head sequence_clipped_viafun.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
!head sequence.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer atctgaatctgagactatatgagactgatctgatctgctctgaagc
delete_seq_following_pattern_within_multiFASTA("sequence.fa", "smer", "atc*", suffix_for_saving = "_asterisk_demo")
6 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_asterisk_demo.fa'. *****************DONE**************************
!head sequence_asterisk_demo.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atc
#write example with blocks of unknown nucleotides in FASTA to file
s = '''>smar
atNNctgatNNNNNNNNNNNNNNNNNNNNNNNtgatctggtctgtggcg
>colc
atNNctgaatctgagactatatNNNNNNNNNNNNNNtctgctctgaagc
'''
!echo "{s}" > sequencewn.fa
delete_seq_following_pattern_within_multiFASTA("sequencewn.fa", "colc", "N{5,}")
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'colc', and saved within a modified version of 'sequencewn.fa' as the output file 'sequencewn_clipped.fa'. *****************DONE**************************
!head sequencewn_clipped.fa
>smar atNNctgatNNNNNNNNNNNNNNNNNNNNNNNtgatctggtctgtggcg >colc CLIPPED atNNctgaatctgagactatatNNNNNNNNNNNNNN
Despite that method of calling the function with the regular expression provided as an argument being the most direct and easiest way to use them, I can imagine it won't cover all cases, and so I am going to detail my additional findings in this section.
Interestingly, a different approach to escaping the brackets is necessary when using the %%bash
cell magic.
%%bash
python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer a\{2,\}
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
Yet, if you add in quotes you can get away without escaping the brackets.
%%bash
python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer "a{2,}"
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
The cell below shows it works when using the exclamation mark way to send commands to shell, too.
!python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer a{{2,}}
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
!python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer a\{2,\}
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
!python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer "a{{2,}}"
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
As below shows, other complex regular expression search terms work when %run
method used sometimes both with and without quotes around the pattern producing the same results.
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer ..... --output_suffix dot_expl
9 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequencedot_expl.fa'. *****************DONE**************************
!head sequencedot_expl.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctg
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer "....." --output_suffix dot_explwq
9 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequencedot_explwq.fa'. *****************DONE**************************
!head sequencedot_explwq.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctg
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer "a{{2,}}"
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atctgaa
Use of an asterisk in the regular expression search term with the %run
approach seems to be allowed if handled like in the %%bash
approach.
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer \atc\*
6 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atc
%%bash
python delete_seq_following_pattern_within_multiFASTA.py sequence.fa smer \atc\*
6 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
!head sequence_clipped.fa
>evoli atctgatctggggcgaaatgagactgatctgatctggtctgtggcg >smer CLIPPED atc
The default behaviour of the script is to remove gaps represented by dashes from any sequence pattern provided. The idea is that many use cases will involve searhcing for sequence patterns that have gaps because the sequence text was copied from a sequence alignment, and it seems like a waste of processing to have the user clean the sequences ahead of time. Plus, most people will be searching sequences that don't have gaps.
However, with the addition of the --leave_dashes
option in the command line tool or setting the filter_dashes
variable to False
when calling the main function, the user can ovveride this typical behavior and still use the script. For example, with an aligned FASTA file format. The caveat is that number of residues to get will then be counting the gaps / dashes too.
First, show it goes from working to failing when that setting added in current example data.
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa evoli GATCTGGG------GCGA
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'evoli', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa evoli "GATCTGGG------GCGA"
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'evoli', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
%run delete_seq_following_pattern_within_multiFASTA.py sequence.fa evoli GATCTGGG------GCGA --leave_dashes
*****************DONE************************** ***NO MATCHES FOUND. NO CHANGES MADE.***** **** ERROR?!?!?** *****************DONE**************************
delete_seq_following_pattern_within_multiFASTA("sequence.fa", "evoli", "GATCTGGG------GCGA")
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'evoli', and saved within a modified version of 'sequence.fa' as the output file 'sequence_clipped.fa'. *****************DONE**************************
delete_seq_following_pattern_within_multiFASTA("sequence.fa", "evoli", "GATCTGGG------GCGA", filter_dashes=False)
*****************DONE************************** ***NO MATCHES FOUND. NO CHANGES MADE.***** **** ERROR?!?!?** *****************DONE**************************
To demonstrate the setting works.
#write example aligned FASTA file format
s = '''>evoli
atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaagcgaaaaacaaa
>smer
atct--gaatcg----atctggg------gcgaagactgatctgatctgctctgaagc--gcgaaaaaaaaa
'''
!echo "{s}" > gapped_sequence.fa
delete_seq_following_pattern_within_multiFASTA("gapped_sequence.fa", "smer", "-{5,}GCGA", filter_dashes=False)
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
%run delete_seq_following_pattern_within_multiFASTA.py gapped_sequence.fa smer "\-\-\-GCGA" --leave_dashes
!head gapped_sequence_clipped.fa
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
%run delete_seq_following_pattern_within_multiFASTA.py gapped_sequence.fa smer "\-{{5,}}GCGA" --leave_dashes
!head gapped_sequence_clipped.fa
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
When using on the command line, the dashes need to be escaped with a backslash. The next two cells demonstrate that.
%%bash
python delete_seq_following_pattern_within_multiFASTA.py gapped_sequence.fa smer "---GCGA" --leave_dashes
usage: delete_seq_following_pattern_within_multiFASTA.py [-h] [-ld] [-os OUTPUT_SUFFIX] SEQUENCE_FILE RECORD_ID PATTERN delete_seq_following_pattern_within_multiFASTA.py: error: the following arguments are required: PATTERN
%%bash
python delete_seq_following_pattern_within_multiFASTA.py gapped_sequence.fa smer "\-\-\-GCGA" --leave_dashes
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
%%bash
python delete_seq_following_pattern_within_multiFASTA.py gapped_sequence.fa smer "\-{5,}GCGA" --leave_dashes
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
%%bash
python delete_seq_following_pattern_within_multiFASTA.py gapped_sequence.fa smer "\-{5,}GCGA" --leave_dashes
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
Thus, as with regular expression search terms in general, the wisest choice is probably using delete_seq_following_pattern_within_multiFASTA()
function when dealing with a complex search pattern.
delete_seq_following_pattern_within_multiFASTA("gapped_sequence.fa", "smer", "-{5,}GCGA", filter_dashes=False)
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
delete_seq_following_pattern_within_multiFASTA("gapped_sequence.fa", "smer", "--GCGA", filter_dashes=False)
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
Two matches for above, but only processes after first.
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
delete_seq_following_pattern_within_multiFASTA("gapped_sequence.fa", "smer", "---GCGA", filter_dashes=False)
*****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
delete_seq_following_pattern_within_multiFASTA("gapped_sequence.fa", "smer", "-{2,}GCGA", filter_dashes=False)
2 matches to the sequence found in the specified sequence. The sequence that follows the match encountered first has been deleted. *****************DONE************************** Sequence after the match to the provided pattern deleted from 'smer', and saved within a modified version of 'gapped_sequence.fa' as the output file 'gapped_sequence_clipped.fa'. *****************DONE**************************
!head gapped_sequence_clipped.fa
>evoli atct--gatctgggggatctggg------gcgactgatctgatctggtctgtggcggcaa gcgaaaaacaaa >smer CLIPPED atct--gaatcg----atctggg------gcga
Enjoy!
Upload your own sequence files to any running Jupyter session and adapt the commands in this notebook to search wihin them. Edit the notebook or copy the necessary cells to make the script work with your own data.
If editing the script (*ATYPICAL*) and using import of the main function to test changes here in this Jupyter notebook, you'll need to run the following code in order to specifically trigger import of the updated version of the code for the function subsequent to any edit. Otherwise, without a restart of the kernel, the notebook environment will see any call to import the function and essentially ignore it as it considers that function already imported into the notebook environment.
# Run this to have new code reflected in the version of the function in memory within the notebook namespace
import importlib
import delete_seq_following_pattern_within_multiFASTA; importlib.reload( delete_seq_following_pattern_within_multiFASTA ); from delete_seq_following_pattern_within_multiFASTA import delete_seq_following_pattern_within_multiFASTA
# above line from https://stackoverflow.com/a/11724154/8508004