Search code examples
pythonstringpython-2.7fasta

How can I remove first line from fasta file?


Structure of fasta file is like this:

>gi|568815364|ref|NT_077402.3| Homo sapiens chromosome 1 genomic scaffold, GRCh38 Primary Assembly HSCHR1_CTG1
TAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAAC
CCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCAACCCTAACCCTAACCCTAACCCTAACCCTAA
CCCTAACCCCTAACCCTAACCCTAACCCTAACCCTAACCTAACCCTAACCCTAACCCTAACCCTAACCCT
AACCCTAACCCTAACCCTAACCCCTAACCCTAACCCTAAACCCTAAACCCTAACCCTAACCCTAACCCTA
ACCCTAACCCCAACCCCAACCCCAACCCCAACCCCAACCCCAACCCTAACCCCTAACCCTAACCCTAACC

The first line is some information about the content of file and rest lines are strand of DNA, RNA or amino acid. To do some work with this kind of file I need to remove first line of file. How can I do this using python? I tried this code, but its not suitable:

My_string=open("SimpleFastaFile.fa", "r").read()
def line_remove(str):
    if str.isalnum()==False:
        str=str[1:]
        line_remove(str)

line_remove(My_string)

Solution

  • If you need the whole file's content, why not read all lines at once and immediately slice away the first line?

    with open('path','r') as f:
        content = f.readlines()[1:]
    output="".join(content)