英文:
Loop through every 'even' line in a file
问题
我有一个具有以下结构的fasta文件。为了背景信息,fasta文件只是一个以'>'标识的标题的文本文件,下面是文本内容。我想要创建一个for循环,它可以迭代遍历这个fasta文件的每一行。
文件名是chicken_topmotifs.fasta
>gene8
ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
>gene12
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
>gene18
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
>gene21
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA
for sequence in chicken_topmotifs.fasta;
do
echo $sequence
done
英文:
I have a fasta file with the following structure. For context, a fasta file is simply a text file with a header denoted by '>' and below it is the text. I want to create a for-loop that can iterate through every even line of this fasta file.
The name of the file is chicken_topmotifs.fasta
>gene8
ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
>gene12
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
>gene18
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
>gene21
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA
for sequence in chicken_topmotifs.fasta;
do
echo $sequence
done
答案1
得分: 1
只需每次循环时进行两次读取。第一次读取获取奇数行,第二次获取其后的偶数行。
while read -r gene; do
read -r sequence
# 使用$sequence执行操作
done < chicken_topmotifs.fasta
英文:
Just do two reads each time through the loop. The first read gets the odd line, the second one gets the even line after it.
while read -r gene; do
read -r sequence
# do stuff with $sequence
done < chicken_topmotifs.fasta
答案2
得分: 0
以下是翻译好的部分:
假设:
- 忽略标题(
>
)行 - 忽略空行
一个bash
的想法:
while read -r sequence
do
echo "$sequence"
done < <(grep '^[ATGC]' chicken_topmotifs.fasta)
如果不必担心空行:
while read -r sequence
do
echo "$sequence"
done < <(grep -v '^>' chicken_topmotifs.fasta)
这两个生成:
ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA
英文:
Assumptions:
- ignore header (
>
) lines - ignore blank lines
One bash
idea:
while read -r sequence
do
echo "$sequence"
done < <(grep '^[ATGC]' chicken_topmotifs.fasta)
If we don't have to worry about blank lines:
while read -r sequence
do
echo "$sequence"
done < <(grep -v '^>' chicken_topmotifs.fasta)
Both of these generate:
ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论