如何在Python中跳过读取输入文件的第一行

时间:2015-07-02 21:29:03

标签: python scripting biopython

我正在尝试编写一个脚本,它将自动处理包含DNA序列的文件并将其转换为蛋白质序列。到目前为止我唯一的打嗝是为了让我的脚本处理infile我需要以某种方式省略第一行。

我的代码如下所示:

#!/usr/bin/python

### This script will translate in all three frames ###

import glob
from Bio.Seq import *
from Bio.Alphabet import generic_dna

print "Please drag in the directory to be processed: "
folder = raw_input().replace(" ","")
file = glob.glob(str(folder) + "/" + '*.seq')

for i in file:

        with open (i, "r") as myfile:

                ### Need to somehow remove / read over the first line of the input...###

                seq = myfile.read().replace(" ", "").replace("\n", "")

        x = 0

        output = open(i + ".tran", "w+")

        while x < 3:
                cd = Seq(seq[x:], generic_dna)
                qes = seq[::-1]
                cdr = Seq(qes[x:], generic_dna)
                error = cd.translate().count('*')
                reverror = cdr.translate().count('*')
                output.write(str(cd.translate()) + "\nStops: " + str(error) + "\n\n")
                output.write("Reverse\n" + str(cdr.translate()) + "\nStops: " + str(reverror) + "\n\n")
                x += 1

我的输入文件可能看起来像这样

>G2-pBAD-Forward_A11.ab1
NNNNNNNNNNCNNNCNGNNGCTTTTTATCGCAACTCTCTACTGTTTCTCCATACCCGTTTTTTTGGGCTAGCGAATTCGA
GCTCGAAATAATTTTGTTTAACTTTAAGAAGGAGATATACATATGATTGTAATGAAACGAGTTATTACCCTGTTTGCTGT
ACTGCTGATGGGCTGGTCGGTAAATGCCTGGTCAAGCTTGGCTGTTTTGGCGGATGAGAGAAGATTTTCAGCCTGATACA
GATTAAATCAGAACGCAGAAGCGGTCTGATAAAACAGAATTTGCCTGGCGGCAGTAGCGCGGTGGTCCCACCTGACCCCA
TGCCGAACTCAGAAGTGAAACGCCGTAGCGCCGATGGTAGTGTGGGGTCTCCCCATGCGAGAGTAGGGAACTGCCAGGCA
TCAAATAAAACGAAAGGCTCAGTCGAAAGACTGGGCCTTTCGTTTTATCTGTTGTTTGTCGGTGAACGCTCTCCTGAGTA
GGACAAATCCGCCGGGAGCGGATTTGAACGTTGCGAAGCAACGGCCCGGAGGGTGGCGGGCAGGACGCCCGCCATAAACT
GCCAGGCATCAAATTAAGCAGAAGGCCATCCTGACGGATGGCCTTTTTGCGTTTCTACAAACTCTTTTGTTTATTTTTCT
AAATACATTCAAATATGTATCCGCTCATGAGACAATAACCCTGATAAATGCTTCAATAATATTGAAAAAGGAAGAGTATG
AGTATTCAACATTTCCGTGTCGCCCTTATTCCCTTTTTTGCGGCATTTTGCCTTCCTGTTTTTGCTCACCCAGAAACGCT
GGTGAAAGTAAAAGATGCTGAAGATCAGTTGGGTGCAGCAAACTATTAACTGGCGAACTACTTACTCTAGCTTCCCGGCA
ACAATTAATAGACTGGATGGAGGCGGATAAAGTTGCAGGACCACTTCTGCGCTCGGCCCTTCCGGCTGGCTGGGTTTATT
GCTGATAAATCTGGAGCCGGTGAGCGTGNTCTCGCGGTATCATTGCAGCACTGGGGCCAGATGGTAAGCCCTTCCCGTAT
CGNANTTNNCTACACGAN

我需要的是一种优雅的方法来删除包含'&gt;'的第一行。

3 个答案:

答案 0 :(得分:3)

您可以使用SeqIO

with open(i) as myfile:
    for record in SeqIO.parse(myfile, "fasta"):
       print record.id
       print record.seq  

record.seq将为您提供序列,record.id会为您提供ID,如果您只有或者想要一个序列,您可以在下一个调用:

with open(i) as myfile:
    print(next(SeqIO.parse(myfile, "fasta"))).seq

我没有在输入中看到任何空格,因此我不确定替换是如何工作的,这会将序列输出为单个字符串。

输出:

NNNNNNNNNNCNNNCNGNNGCTTTTTATCGCAACTCTCTACTGTTTCTCCATACCCGTTTTTTTGGGCTAGCGAATTCGAGCTCGAAATAATTTTGTTTAACTTTAAGAAGGAGATATACATATGATTGTAATGAAACGAGTTATTACCCTGTTTGCTGTACTGCTGATGGGCTGGTCGGTAAATGCCTGGTCAAGCTTGGCTGTTTTGGCGGATGAGAGAAGATTTTCAGCCTGATACAGATTAAATCAGAACGCAGAAGCGGTCTGATAAAACAGAATTTGCCTGGCGGCAGTAGCGCGGTGGTCCCACCTGACCCCATGCCGAACTCAGAAGTGAAACGCCGTAGCGCCGATGGTAGTGTGGGGTCTCCCCATGCGAGAGTAGGGAACTGCCAGGCATCAAATAAAACGAAAGGCTCAGTCGAAAGACTGGGCCTTTCGTTTTATCTGTTGTTTGTCGGTGAACGCTCTCCTGAGTAGGACAAATCCGCCGGGAGCGGATTTGAACGTTGCGAAGCAACGGCCCGGAGGGTGGCGGGCAGGACGCCCGCCATAAACTGCCAGGCATCAAATTAAGCAGAAGGCCATCCTGACGGATGGCCTTTTTGCGTTTCTACAAACTCTTTTGTTTATTTTTCTAAATACATTCAAATATGTATCCGCTCATGAGACAATAACCCTGATAAATGCTTCAATAATATTGAAAAAGGAAGAGTATGAGTATTCAACATTTCCGTGTCGCCCTTATTCCCTTTTTTGCGGCATTTTGCCTTCCTGTTTTTGCTCACCCAGAAACGCTGGTGAAAGTAAAAGATGCTGAAGATCAGTTGGGTGCAGCAAACTATTAACTGGCGAACTACTTACTCTAGCTTCCCGGCAACAATTAATAGACTGGATGGAGGCGGATAAAGTTGCAGGACCACTTCTGCGCTCGGCCCTTCCGGCTGGCTGGGTTTATTGCTGATAAATCTGGAGCCGGTGAGCGTGNTCTCGCGGTATCATTGCAGCACTGGGGCCAGATGGTAAGCCCTTCCCGTATCGNANTTNNCTACACGAN

你也可以使用range而不是while循环,并将字母表传递给SeQIO:

for record in SeqIO.parse(myfile, "fasta",generic_dna)
    ...
for x in range(3):
       ....

这应该更接近你想要的东西:

from Bio import SeqIO
from Bio.Alphabet import generic_dna
from Bio.Seq import Seq


folder = raw_input().replace(" ","")
files = glob.glob(folder + "/" + '*.seq')

for i in files:
    with open(i) as myfile:
            seq = next(SeqIO.parse(myfile, "fasta", generic_dna)).seq
            qes = seq[::-1]
            with open("{}.tran".format(i), "w+") as output:
                 for x in range(3):
                     cd = seq[x:]
                     cdr = qes[x:]
                     error = seq.translate().count('*')
                     reverror = cdr.translate().count('*')
                     output.write("{}\nstops: {}\n\n".format(cd.translate(), error))
                     output.write("Reverse: {}\nStops: {}\n\n ".format(cdr.translate(), reverror))

哪个输出:

XXXXXXXFLSQLSTVSPYPFFWASEFELEIILFNFKKEIYI*L**NELLPCLLYC*WAGR*MPGQAWLFWRMREDFQPDTD*IRTQKRSDKTEFAWRQ*RGGPT*PHAELRSETP*RRW*CGVSPCESRELPGIK*NERLSRKTGPFVLSVVCR*TLS*VGQIRRERI*TLRSNGPEGGGQDARHKLPGIKLSRRPS*RMAFLRFYKLFCLFF*IHSNMYPLMRQ*P**MLQ*Y*KRKSMSIQHFRVALIPFFAAFCLPVFAHPETLVKVKDAEDQLGAANY*LANYLL*LPGNN**TGWRRIKLQDHFCARPFRLAGFIADKSGAGERXLAVSLQHWGQMVSPSRIXXXYT
stops: 25

Reverse: XHIXXXYALPEW*TGVTTLLWRSXASGRGLNSRYLGRSAFPARVFTRTLK*AEVGQIINNGPSISFIKRSIIKRRGLTRSRRK*KWSQRPTRFCPSVLRRFFPYSRCAFTTYEYEKEKVIITS*IVPITEYSPMYKLT*IFLFVFSNIFAFFR*AVLPEDELNYGPSNTARRTGGGRPGNEALQV*ARAA*TG*VLSQVAVCCLFCFPGQKADSESKINYGPSRDESVPLWGVMVAAMPQSEDSSRTPVHPGGAMTAVRLRQNSLAKTQD*IRHSPTFRRE*AVLSVRTGP*MAGRVVVMSFVPLLSKVMLVYI*RKNFNLF**SSSLSDRVFLPIPLCHLSTLFFXXXXXX
Stops: 15

 XXXXXXAFYRNSLLFLHTRFFGLANSSSK*FCLTLRRRYTYDCNETSYYPVCCTADGLVGKCLVKLGCFGG*EKIFSLIQIKSERRSGLIKQNLPGGSSAVVPPDPMPNSEVKRRSADGSVGSPHARVGNCQASNKTKGSVERLGLSFYLLFVGERSPE*DKSAGSGFERCEATARRVAGRTPAINCQASN*AEGHPDGWPFCVSTNSFVYFSKYIQICIRS*DNNPDKCFNNIEKGRV*VFNISVSPLFPFLRHFAFLFLLTQKRW*K*KMLKISWVQQTINWRTTYSSFPATINRLD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WSVNGWSGSRHVVCPIIEQSNVSIHIEEEFQFVLIKLELKRSGFFAHTSLSSLNAIFRXXXXXX
Stops: 14

虽然有警告,因为您的序列不是三的倍数

答案 1 :(得分:1)

starting = True
lines = []
with open (i, "r") as myfile:
    for line in myfile:
        if starting:
            starting = False
            continue
        lines.append(line)

# now you have all the lines except the first one in "lines"     

答案 2 :(得分:0)

当您去阅读文件时,请检查该行的第一个字符是否为“&gt;”。如果它只是阅读该行并跳过它。与上面提到的注释一样,您可能希望每次读取一行时检查这一点,以便知道您是在处理新序列还是相同序列,因为您的文件可能有多个序列。

此链接将为您提供有关在python中读取文件的所有文档。 https://docs.python.org/2/tutorial/inputoutput.html