<p>For example, the fasta are like these:</p>

<pre><code>gene.fasta:

&gt;Hsc_gene_9627.t1
ATGGCACGCATTTTCCTCATTCTTTTATTGCTGCACA
ACATCTGCTGTGCAGCCGCTTCA
&gt;Hsc_gene_9627.t2
GGGGTTTTCCCC
&gt;Hsc_gene_962.t1
AGTCGTCAGTCAGTAGTCGC

seq.txt:

HSC_gene_9627
HSC_gene_9734 
HSC_gene_996|HSC_gene_9734
</code></pre>

<p>We use a few modules, read in the fasta:</p>

<pre><code>import re
from Bio import SeqIO                                                                 
records = list(SeqIO.parse("gene.fasta","fasta"))
</code></pre>

<p>Read in the list:</p>

<pre><code>seq = [i.rstrip().lower() for i in open('seq.txt').readlines()]
</code></pre>

<p>Here I use two functions, one to write fasta files, and the other to format the header for comparison:</p>

<pre><code>def fasta_out(rec):
    return("&gt;"+str(rec.description)+"\n"+str(rec.seq))

def format(rec):
    return re.sub("[.][^ ]*$","",rec.description).lower()
</code></pre>

<p>Define the records to keep:</p>

<pre><code>keep = [fasta_out(i) for i in records if format(i) in seq]
</code></pre>

<p>Write them out:</p>

<pre><code>f=open('matched.fasta','w')
f.writelines("\n".join(keep))
f.close()
</code></pre>

<p>We can check:</p>

<pre><code>open('matched.fasta').readlines()                                                          
Out[50]: 
['&gt;Hsc_gene_9627.t1\n',
 'ATGGCACGCATTTTCCTCATTCTTTTATTGCTGCACAACATCTGCTGTGCAGCCGCTTCA\n',
 '&gt;Hsc_gene_9627.t2\n',
 'GGGGTTTTCCCC']
</code></pre>

<p>Most likely there are some simple while, for loops that does not require modules..</p>

CC BY-SA 4.0
2020-06-10T12:06:22.403Z
Thank you very much @StupidWolf
Samer Habash
CC BY-SA 4.0
2020-06-03T20:30:58.073Z
2020-06-03T20:30:58.073Z
StupidWolf
<p>I have a fasta file (gene.fasta)with sequences with names patterns:  </p>

<pre><code>&gt;Hsc_gene_9627.t1
ATGGCACGCATTTTCCTCATTCTTTTATTGCTGCACAACATCTGCTGTGCAGCCGCTTCA
TTGCTCATTTTGAATGCCGTTACATTGGAGAAGGATGCTAATGATTATGCCGTTGGCGAT
</code></pre>

<p>and I have the Ids in text file (seq.txt) which are not the exact as in the fasta file:</p>

<pre><code>HSC_gene_996
HSC_gene_9734
and some of the names came as 
HSC_gene_996|HSC_gene_9734 
</code></pre>

<p>How can extract the sequences?
I am not experienced in python so please use python for dummies language :)
Thank you in advance </p>

CC BY-SA 4.0
2020-06-03T11:47:07.217Z
Does it have to be python? There are far easier ways using only bash, etc.
NatWH
CC BY-SA 4.0
2020-06-03T12:37:54.477Z
I just have python in my computer, if there is please help me, I tried to find some online tools but it didn't work
Samer Habash
CC BY-SA 4.0
2020-06-03T14:07:18.720Z
You may want to try Joe Healey's bioinfo-tools repository. [This script](https://github.com/jrjhealey/bioinfo-tools/blob/master/fastafetcher.py) looks like the one to use.
Ram RS
CC BY-SA 4.0
2020-06-03T15:08:56.437Z
To second @RamRS, that script should work indeed - provided your IDs are exact full or substring matches (excluding the `>`). It's been a while since I used it, so if it could be tweaked to be more useful, I'm happy to take suggestions. Whilst I love `bash` etc., I'd probably disagree with NatWH that it is _easier_ than using something like `biopython` which is purpose built for this.
Joe Healey
CC BY-SA 4.0
2020-06-03T15:24:56.223Z
OP's problem seems to be their platform. Python excels in being a cross-platform solution, hence OP looking for Python based solutions makes sense.
Ram RS
CC BY-SA 4.0
2020-06-03T19:05:47.333Z
Hi @SamerHabash, i can write it in python. Can you give an example where it is supposed to hit (i suppose Hsc_gene_9627.t1 and Hsc_gene_9627) and instances where it doesn't?
StupidWolf
CC BY-SA 4.0
2020-06-03T19:22:56.737Z
Hi @StupidWolf, that would be great thank you. All my target sequences IDs are like (HSC_gene_996) in a text file which are missing the (.t1 and .t2 in some cases). in the fasta file they are all ending with ( .t1 or some cases .t2- like this one :  >Hsc_gene_9627.t1 )
Samer Habash
<python><fasta>
What is the full script to extract sequences from fasta file by using Ids in text file in python 3 and pycharm?
# What is the full script to extract sequences from fasta file by using Ids in text file in python 3 and pycharm?

(Asked by: username_0 on 2020-06-03)

I have a fasta file (gene.fasta)with sequences with names patterns:  
>Hsc_gene_9627.t1
ATGGCACGCATTTTCCTCATTCTTTTATTGCTGCACAACATCTGCTGTGCAGCCGCTTCA
TTGCTCATTTTGAATGCCGTTACATTGGAGAAGGATGCTAATGATTATGCCGTTGGCGAT

and I have the Ids in text file (seq.txt) which are not the exact as in the fasta file:
HSC_gene_996
HSC_gene_9734
and some of the names came as 
HSC_gene_996|HSC_gene_9734 

How can extract the sequences?
I am not experienced in python so please use python for dummies language :)
Thank you in advance

**Tags:** python, fasta
> **Comments:**
> **username_1**: Does it have to be python? There are far easier ways using only bash, etc.  
> **username_0**: I just have python in my computer, if there is please help me, I tried to find some online tools but it didn't work  
> **username_2**: You may want to try Joe Healey's bioinfo-tools repository. [This script](https://github.com/jrjhealey/bioinfo-tools/blob/master/fastafetcher.py) looks like the one to use.  
> **username_3**: To second @RamRS, that script should work indeed - provided your IDs are exact full or substring matches (excluding the `>`). It's been a while since I used it, so if it could be tweaked to be more useful, I'm happy to take suggestions. Whilst I love `bash` etc., I'd probably disagree with NatWH that it is _easier_ than using something like `biopython` which is purpose built for this.  
> **username_2**: OP's problem seems to be their platform. Python excels in being a cross-platform solution, hence OP looking for Python based solutions makes sense.  
> **username_4**: Hi @SamerHabash, i can write it in python. Can you give an example where it is supposed to hit (i suppose Hsc_gene_9627.t1 and Hsc_gene_9627) and instances where it doesn't?  
> **username_0**: Hi @StupidWolf, that would be great thank you. All my target sequences IDs are like (HSC_gene_996) in a text file which are missing the (.t1 and .t2 in some cases). in the fasta file they are all ending with ( .t1 or some cases .t2- like this one :  >Hsc_gene_9627.t1 )  

---

## 1 answers

### Answer by username_4 on 2020-06-03. Score: 1

For example, the fasta are like these:
gene.fasta:

>Hsc_gene_9627.t1
ATGGCACGCATTTTCCTCATTCTTTTATTGCTGCACA
ACATCTGCTGTGCAGCCGCTTCA
>Hsc_gene_9627.t2
GGGGTTTTCCCC
>Hsc_gene_962.t1
AGTCGTCAGTCAGTAGTCGC

seq.txt:

HSC_gene_9627
HSC_gene_9734 
HSC_gene_996|HSC_gene_9734

We use a few modules, read in the fasta:
import re
from Bio import SeqIO                                                                 
records = list(SeqIO.parse("gene.fasta","fasta"))

Read in the list:
seq = [i.rstrip().lower() for i in open('seq.txt').readlines()]

Here I use two functions, one to write fasta files, and the other to format the header for comparison:
def fasta_out(rec):
    return(">"+str(rec.description)+"\n"+str(rec.seq))

def format(rec):
    return re.sub("[.][^ ]*$","",rec.description).lower()

Define the records to keep:
keep = [fasta_out(i) for i in records if format(i) in seq]

Write them out:
f=open('matched.fasta','w')
f.writelines("\n".join(keep))
f.close()

We can check:
open('matched.fasta').readlines()                                                          
Out[50]: 
['>Hsc_gene_9627.t1\n',
 'ATGGCACGCATTTTCCTCATTCTTTTATTGCTGCACAACATCTGCTGTGCAGCCGCTTCA\n',
 '>Hsc_gene_9627.t2\n',
 'GGGGTTTTCCCC']

Most likely there are some simple while, for loops that does not require modules..

> **Comments:**
> **username_0**: Thank you very much @StupidWolf  

---

