循环遍历文件中的每一行中的“偶数”行。

huangapple go评论46阅读模式
英文:

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 &lt; chicken_topmotifs.fasta

答案2

得分: 0

以下是翻译好的部分:

假设:

  • 忽略标题(&gt;)行
  • 忽略空行

一个bash的想法:

while read -r sequence
do
      echo "$sequence"
done < <(grep '^[ATGC]' chicken_topmotifs.fasta)

如果不必担心空行:

while read -r sequence
do
      echo "$sequence"
done < <(grep -v '^&gt;' chicken_topmotifs.fasta)

这两个生成:

ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA
英文:

Assumptions:

  • ignore header (&gt;) lines
  • ignore blank lines

One bash idea:

while read -r sequence
do
      echo &quot;$sequence&quot;
done &lt; &lt;(grep &#39;^[ATGC]&#39; chicken_topmotifs.fasta)

If we don't have to worry about blank lines:

while read -r sequence
do
      echo &quot;$sequence&quot;
done &lt; &lt;(grep -v &#39;^&gt;&#39; chicken_topmotifs.fasta)

Both of these generate:

ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA

huangapple
  • 本文由 发表于 2023年2月10日 07:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定