- the first function adds extra rows (--) after every pull. Also to translate into pulling fastq files, if I change to -A 3, it does not do the grep properly.
- seqkit produces no output when i run it
How can I use an file of a list of FASTA headers to pull out reads from a FASTA file (header + sequence)?
My IDs.txt file looks like this:
>NR::NT:L28960.1:NS500126:798:HWTLTAFXX:2:21207:14062:16380/1
>NR::NT::NS500126:798:HWTLTAFXX:2:21207:14062:16380/2
>NR::NT::NS500126:798:HWTLTAFXX:2:21207:20748:13870/1
My file.fasta looks like this:
>NR::NT:L28960.1:NS500126:798:HWTLTAFXX:2:21207:14062:16380/1
CCCCACTTTCCTTTACAGTACTTGTTCACTATCGGTCTTTGGTTGTATTTAGCCTTAGGTAAACTCATATCACCTATATTCATACTGCACTACCAAACAGTACTACTCAAATTTAGAGAGTATAATATCGATAACGAGACTATAACTCTC
>NR::NT::NS500126:798:HWTLTAFXX:2:21207:14062:16380/2
CTTAGATTTATTCTAAGTGTTGTATAGGGTAGTCACAAACAAATACACTAAAAATGTGACCATAGAGAGTTATAGTCTCGTTATCGATATTATACTCTCTAAATTTGAGTAGTACTGTTTGGTAGTGCAGTATGAATATAGGTGATATGA
>NR::NT::NS500126:798:HWTLTAFXX:2:21207:20748:13870/1
GTCGTATTCACACTTACAACAGGTAATGACTAACTTCCCAGCTTAGAGGCCGTCAGCTGTATCCCAGAGTTACGCCCTAAAGTCACTAGCAATAGCTGCACCTGCTAACCAAGACTTAGGTCTCCCACCCACAGTAGCTCTATAACCGCC
>NR::NT::NS500126:798:HWTLTAFXX:2:21207:20748:13870/2
CGTCACTACAAGTGCTAGCGTAACGTTAGTGTTTGTGTACGGCTAGCTGGGGCTTAGGTTGAAGACCTGTGGGGCGGTTATAGAGCTACTGTGGGTGGGAGACCTAAGTCTTGGTTAGCAGGTGCAGCTATTGCTAGTGACTTTAGGGCG
I've tried this command cut -c 2- IDs.txt | xargs -n 1 samtools faidx file.fasta but it breaks the sequence up, producing multiple lines uneccessarily.
I've also tried this command cat IDs.txt | awk '{gsub("_","\\_",$0);$0="(?s)^>"$0".*?(?=\\n(\\z|>))"}1' | pcregrep -oM -f - file.fasta but it produce an output.
Does anyone know a solution to this problem?
3 answers
Add --no-group-separator to grep command to avoid getting --------- after each record.
Try filterbyname.sh from BBTools / BBMap:
filterbyname.sh -Xmx100m threads=1 in=file.fasta names=IDs.txt ths=t out=out.fasta fastawrap=0
- The resulting out.fasta file is the same as the file.fasta when I run this command.
I tested with your example - deleting the second read name - and it worked as intended. Are there any differences between the example you provided and the actual files you intend to parse?
I solved my problem by:
- Deleting the > on the ID list.
seqtk subseq file.fa IDs.txt > out.fasta
You should have done the same with seqkit as mentioned in my post kratnasiri. As per extra ---, i think you are grepping against multiple files. In addition, OP is for fasta files, not for fastq. Moderate this post to reply to original comment.
Log in to answer this question.
Please search in future, this is the number 1 biostars question.