从fasta文件中提取指定位置的序列

基因组的fasta文件很大,只想要指定位置的序列,用linux脚本从fasta文件中提取指定位置的sequences。

#!/bin/bash
# 从fasta文件中提取指定位置的序列并写入新文件
 
# 参数:输入文件路径,起始位置,结束位置
input_file=$1
start_pos=$2
end_pos=$3
output_file=$4
 
# 从输入文件中提取序列
grep -v '^>' "$input_file" | awk -v start=$start_pos -v end=$end_pos '{
    line = $0;
    if (start <= length(line) && end >= start) {
        print substr(line, start - 1, end - start + 1)
    }
}' > "$output_file"
 
# 提取序列对应的ID并添加到新文件的序列头部
seq_id=$(grep '^>' "$input_file" | awk -v start_line=1 -v end_line=2 'NR == start_line || NR == end_line {print}')
paste -d '\n' <(echo "$seq_id") <(cat "$output_file") > "${output_file}.tmp" && mv "${output_file}.tmp" "$output_file"
 
# 给序列头部添加'>'符号
#sed -i 's/^/>/' "$output_file"
 
# 打印提取的序列
cat "$output_file"

#usage
#./extractSeqWithPos.sh input_file start_pos end_pos output_file

##Tips: postion位置可能需要往后+1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容