bashloopsfastaagrep

Writing a script that uses agrep to loop through lines in a document one by one against lines in another document and getting a result


I am trying to write a script that uses agrep to loop through files in one document and match them against another document. I believe this might use a nested loop however, I am not completely sure. In the template document, I need for it to take one string and match it against other strings in another document then move to the next string and match it again

enter image description here

If unable to see images for some odd reason I have included the links at the bottom here as well. Also If you need me to explain more just let me know. This is my first post so I am not sure how this will be perceived or if I used the correct terminologies :)

Template agrep/highlighted- https://imgur.com/kJvySbW
Matching strings not highlighted- https://imgur.com/NHBlB2R

I have already looked on various websites regarding loops

#!/bin/bash
#agrep script
echo ${BASH_VERSION}


TemplateSpacers="/Users/kj/Documents/Research/Dr. Gage 
Research/Thesis/FastA files for AGREP 
test/Template/TA21_spacers.fasta"
MatchingSpacers="/Users/kj/Documents/Research/Dr. Gage 
Research/Thesis/FastA files for AGREP test/Matching/TA26_spacers.fasta"

for * in filename 

do 

agrep -3 * to file im comparing to  

#potentially may need to use nested loop but not sure 

Solution

  • Ok, I get it now, I think. This should get you started.

    #!/bin/bash
    
    document="documentToSearchIn.txt"
    
    grep -v spacer fileWithSearchStrings.txt | while read srchstr ; do
       echo "Searching for $srchstr in $document"
       echo agrep -3 "$srchstr" "$document"
    done
    

    If that looks correct, remove the echo before agrep and run again.


    If, as you say in the comments, you want to store the script somewhere else, say in $HOME/bin, you can do this:

    mkdir $HOME/bin
    

    Save the script above as $HOME/bin/search. Now make it executable (only necessary one time) with:

    chmod +x $HOME/bin/search
    

    Now add $HOME/bin to your PATH. So, find the line starting:

    export PATH=...
    

    in your login profile, and change it to include the new directory:

    export PATH=$PATH:$HOME/bin
    

    Then start a new Terminal and you should be able to just run:

    search
    

    If you want to be able to specify the name of the strings file and the document to search in, you can change the code to this:

    #!/bin/bash
    
    # Pick up parameters, if supplied
    #   1st param is name of file with strings to search for
    #   2nd param is name of document to search in
    str=${1:-""}
    doc=${2:-""}
    
    # Ensure name of strings file is valid
    while : ; do
       [ -f "$str" ] && break
       read -p "Enter strings filename:" str
    done
    
    # Ensure name of document file is valid
    while : ; do
       [ -f "$doc" ] && break
       read -p "Enter document name:" doc
    done
    
    echo "Search for strings from: $str, searching in document: $doc"
    
    grep -v spacer "$str" | while read srchstr ; do
       echo "Searching for $str in $doc"
       echo agrep -3 "$str" "$doc"
    done
    

    Then you can run:

    search path/to/file/with/strings path/to/document/to/search/in
    

    or, if you run like this:

    search
    

    it will ask you for the 2 filenames.