This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Run BLASTN command line using string rather than file

Hello,

I am running Blastn from terminal to search for a sequence. The parameter -query takes a file; would it be possible to use a string instead? If I have the sequence in a variable Q. what would be the flag for blastn ... -? $Q? Thank you

blastn terminal input format

1 answer

Take the following with a grain of salt until you test it out yourself. Much to my surprise blastn seems to work with a variable containing just sequence as input.

I made a test database with one sequence in it (truncated for brevity).

$ more test.fa
>Z18649.1 L.obscurus gene for large subunit rRNA
GGCACTGCCTGCCCAGTGACAATCGTTAAACGGCCGCGGTATCTTGACCGTGCAAAGGTAGCATAATCAC
TTGTTCTCTAAATAAGGACTTGTATGAATGGCCACACGAGGGTTTTACTGTCTCTTACTCTTAATCAGTG

Defined a variable with sequence that wrapped around on multiple lines.

$ export C="GGCACTGCCTGCCCAGTGACAATCGTTAAACGGCCGCGGTATCTTGACCGTGCAAAGGTAGCATAATCAC
TTGTTCTCTAAATAAGGACTTGTATGAATGGCCACACGAGGGTTTTACTGTCTCTTACTCTTAATCAGTG
AAATTGACCTCCCCGTGAAGAGGCGGGGATAACACAATAAGACGAGAAGACCCTATGGAGCTTTAATTAA"

Echo the sequence out (it still has new line gaps, which you can remove).

$ echo $C
GGCACTGCCTGCCCAGTGACAATCGTTAAACGGCCGCGGTATCTTGACCGTGCAAAGGTAGCATAATCAC TTGTTCTCTAAATAAGGACTTGTATGAATGGCCACACGAGGGTTTTACTGTCTCTTACTCTTAATCAGTG AAATTGACCTCCCCGTGAAGAGGCGGGGATAACACAATAAGACGAGAAGACCCTATGGAGCTTTAATTAA

Run blast search.

$ blastn -db ./junk -task blastn -query <(echo $C) -out blasting

Get the search results (truncated).

Database: test.fa
           1 sequences; 505 total letters

Query= 
Length=210
                                                                      Score        E
Sequences producing significant alignments:                          (Bits)     Value

Z18649.1 L.obscurus gene for large subunit rRNA                       388        8e-113

>Z18649.1 L.obscurus gene for large subunit rRNA
Length=505

 Score = 388 bits (210),  Expect = 8e-113
 Identities = 210/210 (100%), Gaps = 0/210 (0%)
 Strand=Plus/Plus

Query  1    GGCACTGCCTGCCCAGTGACAATCGTTAAACGGCCGCGGTATCTTGACCGTGCAAAGGTA  60
            ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sbjct  1    GGCACTGCCTGCCCAGTGACAATCGTTAAACGGCCGCGGTATCTTGACCGTGCAAAGGTA  60

Just a small note, the OS will make it appear as a file to blast

ls <(echo $C) will give an output like /dev/fd/63

Blast can parse fasta files as direct query, then it would output the regular sequence name. See this very old thread:

Local Blast: Querying A Single Sequence Without Input File. Possible ?

seq=">SeqName\nGGCACTGCCTGCCCAGTGACAATCGTTAAACGGCCGCGGTATCTTGACCGTGCAAAGGTAGCATAATCACTTGTTCTCTAAATAAGGACTTGTATGAATGGCCACACGAGGGTTTTACTGTCTCTTACTCTTAATCAGTGAAATTGACCTCCCCGTGAAGAGGCGGGGATAACACAATAAGACGAGAAGACCCTATGGAGCTTTAATTAA"

blastn -db ./junk -task blastn -query <(echo -e $seq) -out blasting

looks like it is working indeed: I obtained a file with the output. Thank you.

I would argue the answer to this question is simply “no”. Just creating an ad hoc file input from a variable isn’t really the same thing as passing a string.

Log in to answer this question.