<p>you may use simple R commands, using the 'seqinr' package</p>

<pre><code>#load package
library(seqinr)
#load file containing sequences
data&lt;-read.fasta("filename.fa")
#create a vector containing species names: these are the last part of the string
vec.names&lt;-unlist(lapply(strsplit(names(data), ";"), function(x)x[length(x)]))
#find names to keep: indices which are not in the species to remove
species.to.remove&lt;-c("species_name1", "species_name2", "species_name4")
vec.tokeep&lt;-which(! vec.names %in%  species.to.remove)
#write the final output
write.fasta(sequences=data[vec.tokeep], names=names(data)[vec.tokeep], file.out="output.fa")
</code></pre>

CC BY-SA 4.0
2020-04-29T09:19:11.780Z
2020-04-29T09:29:18.733Z
2020-04-29T09:29:18.733Z
thomas duge de bernonville
<p>I have a file with 16S sequences. some headers contain species information. For my purposes I would like to exclude a number of species from the file, therefore I would like to do a pattern matching within a 16S header (that contains other characters beyond the species name) to then remove that header and the following sequence.</p>

<p>So, I have:</p>

<pre><code>&gt;NCBI_ID1;high_rank_taxonomy;...;species_name1
ATCCTT
&gt;NCBI_ID2;high_rank_taxonomy;...;species_name2
ATTTTCCC
&gt;NCBI_ID3;high_rank_taxonomy;...;species_name3
TTTCCCCAAAA
&gt;NCBI_ID4;high_rank_taxonomy;...;species_name4
CCCTTTAAA
</code></pre>

<p>I would like to exclude, say, species 1,2 and 4 to retain:</p>

<pre><code>&gt;NCBI_ID3;high_rank_taxonomy;...;species_name3
TTTCCCCAAAA
</code></pre>

<p>I found this solution in an adjacent question: </p>

<pre><code>awk 'BEGIN{while((getline&lt;"ids.txt")&gt;0)l["&gt;"$1]=1}/^&gt;/{f=!l[$1]}f' seq.fa
</code></pre>

<p>where I have my species names in the file ids.txt and sequences in file seq.fa</p>

<p>but that would work if the whole of my header was just the species name (contents of each line in ids.txt), which is not the case.
I thought I could be smart and use <code>*species_name1*</code> instead of <code>species_name1</code> as contents of ids.txt but that apparently doesn't work.
Could someone help me with modifying the current solution to accommodate the existence of additinal strings in the headers?</p>

<p>Many thanks!
A</p>

CC BY-SA 4.0
2020-04-28T16:01:05.863Z
my awk is really bad.. would doing it in R or python help?
StupidWolf
CC BY-SA 4.0
2020-04-28T20:32:15.127Z
Say yes to that, @SW's Python is really good. The other way in is a Perl one-liner.
M__
CC BY-SA 4.0
2020-04-29T01:58:53.507Z
Or, you know, Heng Li's bioawk would be a good middle ground.
Ram RS
CC BY-SA 4.0
2020-04-29T11:00:03.590Z
@StupidWolf sure thing, I am very new to bioinformatics, so I am happy to operate anything that does the job and learn it in the process : ) R would be preferable at the moment
Irrussional
CC BY-SA 4.0
2020-04-29T11:05:06.340Z
Hi @Irrussional, see whether the R solution posted below works? I think it should be ok
StupidWolf
CC BY-SA 4.0
2020-04-29T11:33:14.757Z
yes, on it currently! thanks
Irrussional
<fasta><bash><shell><awk>
remove sequences from fasta file matching a string in the header
# remove sequences from fasta file matching a string in the header

(Asked by: username_0 on 2020-04-28)

I have a file with 16S sequences. some headers contain species information. For my purposes I would like to exclude a number of species from the file, therefore I would like to do a pattern matching within a 16S header (that contains other characters beyond the species name) to then remove that header and the following sequence.
So, I have:
>NCBI_ID1;high_rank_taxonomy;...;species_name1
ATCCTT
>NCBI_ID2;high_rank_taxonomy;...;species_name2
ATTTTCCC
>NCBI_ID3;high_rank_taxonomy;...;species_name3
TTTCCCCAAAA
>NCBI_ID4;high_rank_taxonomy;...;species_name4
CCCTTTAAA

I would like to exclude, say, species 1,2 and 4 to retain:
>NCBI_ID3;high_rank_taxonomy;...;species_name3
TTTCCCCAAAA

I found this solution in an adjacent question: 
awk 'BEGIN{while((getline<"ids.txt")>0)l[">"$1]=1}/^>/{f=!l[$1]}f' seq.fa

where I have my species names in the file ids.txt and sequences in file seq.fa
but that would work if the whole of my header was just the species name (contents of each line in ids.txt), which is not the case.
I thought I could be smart and use *species_name1* instead of species_name1 as contents of ids.txt but that apparently doesn't work.
Could someone help me with modifying the current solution to accommodate the existence of additinal strings in the headers?
Many thanks!
A

**Tags:** fasta, bash, shell, awk
> **Comments:**
> **username_1**: my awk is really bad.. would doing it in R or python help?  
> **username_2**: Say yes to that, @SW's Python is really good. The other way in is a Perl one-liner.  
> **username_3**: Or, you know, Heng Li's bioawk would be a good middle ground.  
> **username_0**: @StupidWolf sure thing, I am very new to bioinformatics, so I am happy to operate anything that does the job and learn it in the process : ) R would be preferable at the moment  
> **username_1**: Hi @Irrussional, see whether the R solution posted below works? I think it should be ok  
> **username_0**: yes, on it currently! thanks  

---

## 1 answers

### Answer by username_4 on 2020-04-29. Score: 2

you may use simple R commands, using the 'seqinr' package
#load package
library(seqinr)
#load file containing sequences
data<-read.fasta("filename.fa")
#create a vector containing species names: these are the last part of the string
vec.names<-unlist(lapply(strsplit(names(data), ";"), function(x)x[length(x)]))
#find names to keep: indices which are not in the species to remove
species.to.remove<-c("species_name1", "species_name2", "species_name4")
vec.tokeep<-which(! vec.names %in%  species.to.remove)
#write the final output
write.fasta(sequences=data[vec.tokeep], names=names(data)[vec.tokeep], file.out="output.fa")


---

