1
$\begingroup$

I have a fasta file with 18 sequences inside. I want to extract the length of each sequence with header using the len function. My file looks like:

>DR-1
ATGTATGAGGGCAAGAAGACGAAGAACATGTTCCTGACCCGGGCCCTGGAGAAGATTCTG
GCCGACAAGGAAGTGAAGAAGGCGCATCACTCCCAGCTGCGCAAAGCGTGCGAGGTGGCC
CTAGAGGAAATAAAAGTTGAAACTGAGAAACAAAGCCCTCCTCATGGAGAAGCAAAAGCT
GGATCAGGCACCCTTCCACCAGTGAAATCAAAGACAAATTTTATTGAAGCAGACAAGTAC
TTTTTACCCTTTGAGTTGGCATGCCAGTCCAAATGTCCCCGCATAGTTAGTACATCTCTA
GATTGTTTGCAGAAACTTATTGCTTATGGGCACTTGACTGGCAGGGCTCCAGATAGTACG
ACACCAGGCAAAAAATTAATTGATAGAATTATTGAAACAATATGTGGCTGCTTCCAAGGT
CCTCAAACAGATGAAGGAGTTCAGTTGCAGATAATAAAGGCTTTACTTACCGCAGTGACA
TCACAACACATAGAAATCCATGAAGGAACTGTGCTGCAAGCTGTGAGGACATGCTACAAT
ATCTATTTAGCCAGCAAAAATCTCATCAATCAGACAACAGCCAAAGCTACTCTTACTCAA
ATGCTGAACGTTATCTTTGCACGCATGGAAAACCAAGCATTACAGGAGGCCAAACAAATG
GAAAGAGAAAGACATCGGCAGCAGCAGCATTTGTTACAGTCTCCAGTAAGCCATCATGAG
CCCGAGTCACCTCATCTTAGATATTTGCCACCTCAGACTGTTGATCATATAAACCAAGAG
CATGAAGGGGATCTTGAGCCCCAAACACATGATGTGGATAAAAGTCTCCAAGATGATACA
GAGCCTGAAAACGGATCTGATATTTCCAGTGCAGAAAATGAACAGACAGAAGCTGATCAG
GCAACTGCAGCTGAAACATTATCTAAAAATGATATCTTGTATGATGGAGACTATGAAGAA
AAGCCACTTGATATTGTCCAGAGCATTGTAGAAGAAATGGTGAACATCATTGTTGGAGAC
ATGGGAGAAGGGATGGCTATCAGTGCAAGTACAGAAGGCAACACTGGAACTGTAGAAGAT
GGTAGTGACAGTGAAAACATCCAAGCAAATGGAATCCCAGGAACACCCATTTCTGTTGCA
TATACACCATCGTTACCTGACGACAGGTTATCTGTCTCCTCCAATGATACTCAGGAATCT
GGAAATTCTTCAGGACCTTCACCTGGTGCTAAGTTTTCCCACATTTTACAAAAGGATGCC
>SK-1
TTTCTAGTGTTCAGGTCATTGTGTAAGCTGTCAATGAAACCACTGTCAGATGGACCACCA
GATCCAAAGTCTCATGAGTTACGGTCCAAGATTCTTTCATTGCAGTTGCTTCTGTCCATT
CTGCAGAATGCAGGACCTGTTTTCAGGACAAATGAGATGTTTATTAATGCTATCAAGCAG
TATCTATGTGTTGCACTCTCAAAAAACGGAGTCTCATCTGTTCCAGAGGTTTTTGAGCTT
TCTCTTTCCATATTTCTTACTTTGTTGTCAAACTTCAAGACACATCTGAAGATGCAAATT
GAGGTATTCTTTAAAGAAATCTTCCTGTACATTTTGGAAACGTCTACAAGCTCATTTGAC
CATAAGTGGATGGTTATTCAGACACTGACAAGGATCTGTGCAGATGCTCAGAGTGTGGTG
GACATTTATGTAAATTATGACTGTGACCTAAATGCAGCAAATATATTTGAAAGACTAGTA
AATGACCTGTCGAAAATTGCCCAAGGAAGGGGCAGCCAAGAACTTGGTATGAGTAATGTT
CAGGAATTGAGCCTGCGAAAAAAGGGGTTAGAATGTTTAGTTTCAATTTTGAAATGTATG
GTTGAATGGAGCAAGGATCAGTATGTGAACCCCAACTCTCAAACTACCCTTGGTCAGGAA
AAACCCTCAGAGCAAGAGATAAGTGAAGTTAAACACCCAGAGACTATAAACAGATATGGA
AGTTTAAACTCCTTAGAATCCACATCATCATCAGGAATTGGCAGCTACAGTACACAGATG
TCTGGCACTGATAATCCAGAACAATTTGAGGTCCTAAAGCAGCAAAAAGAAATAATAGAA
CAAGGGATTGATTTATTTAATAAGAAACCAAAGAGAGGAATACAATACCTCCAAGAACAA
GGGATGCTTGGCACCACACCCGAAGATATAGCTCAGTTCTTACATCAAGAAGAAAGATTA
GACTCTACTCAGGTGGGTGAGTTCCTGGGAGATAATGATAAATTCAATAAAGAAGTCATG
>SC-1
TATGCATATGTAGACCAACATGACTTTTCAGGAAAAGACTTTGTTTCAGCTCTTCGTCTA
TTTCTGGAAGGATTCCGGCTTCCAGGAGAAGCTCAGAAAATTGATCGATTAATGGAAAAA
TTTGCTGCAAGATACCTAGAGTGCAACCAAGGACAAACCCTTTTTGCTAGTGCAGATACT
GCTTATGTTTTGGCCTACTCAATTATCATGTTGACTACAGACCTTCACAGTCCACAGGTT
AAAAACAAAATGACAAAAGAACAGTACATTAAAATGAATCGTGGTATCAATGACAGTAAA
GATCTCCCTGAAGAGTATCTATCAGCCATTTATAACGAAATAGCTGGGAAAAAAATATCA
ATGAAAGAAACAAAAGAACTAACAATCCCTACAAAGTCAACTAAACAGAATGTAGCCAGT
GAAAAGCAAAGACGACTTCTGTATAACTTAGAAATGGAACAGATGGCCAAGACGGCTAAA
GCACTCATGGAAGCTGTGAGCCATGTTCAGGCACCTTTTACAAGTGCAACACACTTGGAG
CACGTGAGGCCGATGTTTAAGTTGGCTTGGACACCTTTTCTGGCTGCATTCAGTGTGGGT
CTCCAAGACTGTGATGATACTGAAGTAGCCTCTCTTTGCCTAGAAGGGATAAGATGTGCT
ATCAGGATTGCATGCATTTTCAGCATTCAGCTGGAAAGAGATGCATATGTCCAAGCACTA
GCCAGATTTACCTTACTTACAGTGAGTTCTGGTATTACTGAAATGAAACAGAAGAATATC
GACACAATAAAAACACTCATCACGGTGGCTCACACTGATGGAAATTATTTAGGAAATTCG
TGGCATGAGATCCTGAAGTGCATCAGTCAGTTAGAGCTGGCACAACTTATAGGAACTGGA
GTGAAGCCTCGGTACATTTCTGGGACTGTGAGAGGCAGAGAAGGGTCTCTCACTGGGACA
AAAGATCAGGCTCCAGATGAATTTGTTGGTCTAGGGCTAGTTGGAGGAAATGTGGACTGG
AAGCAGATAGCCAGTATTCAGGAGTCTATTGGAGAAACCAGCTCTCAAAGTGTGGTAGTT
GCTGTGGATAGAATATTCACCGGATCCACAAGGCTAGATGGAAATGCTATTGTTGATTTT
GTCCGTTGGCTATGTGCTGTGTCTATGGATGAATTGCTTTCTACTACACACCCAAGAATG
TTTAGTCTACAAAAAATAGTAGAAATATCATACTATAACATGGGAAGAATAAGACTGCAG
TGGTCTCGGATTTGGGAAGTTATTGGAGATCATTTTAATAAGGTTGGTTGCAATCCTAAT
GAAGATGTAGCCATATTTGCAGTAGACTCTCTGAGGCAGCTATCAATGAAGTTTTTAGAG
>SC-2
AAAGGAGAGCTTGCAAACTTCAGATTCCAAAAGGATTTCTTACGACCTTTTGAACATATA
ATGAAACGAAACAGGTCTCCAACAATTCGAGATATGGTTGTACGGTGTATAGCACAAATG
GTTAATTCTCAGGCTGCAAATATTCGTTCAGGATGGAAGAACATTTTCTCAGTATTCCAT
CTAGCTGCATCAGACCAAGATGAAAGCATAGTAGAACTTGCATTTCAGACAACAGGGCAC
ATTGTCACTCTTGTATTTGAAAAACACTTTCCGGCCACCATTGATTCTTTCCAGGATGCA
GTGAAGTGTTTGTCTGAATTCGCATGTAATGCAGCTTTCCCAGACACAAGTATGGAAGCA
ATCCGACTTATTCGCCATTGTGCAAAATATGTGTCTGATAGACCGCAGGCTTTCAAGGAG
TACACAAGTGATGACATGAGCGTGGCACCTGAAGACAGGGTGTGGGTGAGAGGGTGGTTC
CCGATCCTCTTTGAGTTGTCTTGTATCATCAATAGATGCAAATTAGATGTAAGAACCAGG
GGCTTAACAGTAATGTTTGAAATAATGAAAACATATGGCCACACTTATGAAAAACATTGG
TGGCAGGACTTATTCAGAATTGTTTTCAGAATCTTTGACAACATGAAATTGCCAGAACAG
CAGACAGAGAAAGCTGAATGGATGACAACAACTTGTAACCATGCCCTTTATGCAATATGT
GATGTATTCACCCAGTATTTAGAAGTTCTCAGCGATGTACTTTTGGATGATATTTTTGCC
CAGCTTTACTGGTGTGTACAACAAGACAATGAGCAGTTAGCACGATCTGGTACAAACTGC
TTAGAAAACGTGGTTATTCTGAATGGTGAAAAGTTTACTCTTGAAATCTGGGATAAAACG
TGTAACTGCACCCTGGATATCTTCAAAACCACAATTCCTCACGCGCTTCTGACTTGGCGT
>TA-1
CCCACCTCTGGCGAGGCCGAACCCCCATCTCCATCAGCTGTGAGTGAAAAGCCGTTGGAT
GCAATATCACAAAAATCTGTAGACATACATGATTCTATTCAACCAAGATCTTCAGATAAC
AGACAGCAAGCACCACTGGTTTCTGTTTCTACTGTGAGTGAAGAAGTTAGCAAAGTGAAA
TCCACAGCAAAATTCCCAGAACAAAAATTGTTTGCTGCCCTGTTGATTAAATGTGTTGTG
CAGCTGGAACTCATCCAGACCATCGACAACATTGTCTTCTTCCCAGCCACAAGCAAGAAG
GAGGATGCAGAGAACTTAGCTGCAGCCCAGAGAGATGCAGTAGACTTCGATGTTCGAGTT
GATACTCAAGACCAAGGAATGTACCGCTTTTTAACATCACAACAACTGTTCAAGCTACTG
GACTGCTTATTAGAGTCGCATAGATTTGCAAAAGCATTTAATTCCAACAATGAACAGAGG
ACTGCTCTGTGGAAAGCAGGCTTCAAAGGCAAGTCCAAGCCCAATCTTCTAAAACAGGAG
ACAAGCAGCCTGGCGTGTGGGCTGCGCATTCTCTTCCGGATGTACATGGACGAGAGCAGA
GTCAGTGCCTGGGAAGAAGTCCAGCAGAGGCTTTTAAACGTCTGCAGGGAAGCGCTGAGC
TACTTCCTCACTCTAACATCCGAAAGTCACCGGGAAGCCTGGACTAACTTACTGTTACTG
TTCCTCACTAAAGTTCTGAAGATCAGTGACAGCAGGTTTAAAGCTCACGCATCATTCTAC
TACCCTCTCTTATGTGAAATTATGCAATTTGACTTGATTCCGGAACTGCGTGCTGTTCTT
AGAAGATTTTTCCTGCGAATTGGAATAGTATTTCAAATATCACAACCACCTGAACAGGAA
CTTGGGATAAACAGGCAATGA
>DR-2
ATGCAGCCACCACCTCAGGCAGTCCCGTCTGGGGTGGCTGGACCACCCCCAGCTGGGAAT
CCTCGGAGCATGTTCTGGGCCAACAGCCCTTACAGGAAGCCAGCCAATAATGCACCAGTG
GCTCCCATAACACGCCCATTGCAGCCAGTAACGGATCCGTTTGCTTTTAATAGACAGACT
CTTCAAAACACACCAGTAGGAAGTTCATCCAAAAGCAGCCTGCCCAATTTGCCTGGCCCA
GCCCTCTCTGTTTTTTCTCAGTGGCCTGGTTTGCCTGTGACTCCCACAAATGCTGGGGAT
AGCTCCACAGGATTGCATGAGCCTCTGTCAGGAACTCTGTCACAGCCTAGAGCAGATGCC
AGTCTATTTCCTCCTGCATCTACCCCTTCTTCACTCCCTGGGCTGGAGGTGAGCAGGAAT
GCTGAGGCTGATCCCAGCTCAGGACATGAAGTCCAGATGCTGCCACATTCAGCTCACTAC
ATTCCAGGAGTGGGTCCTGAGCAGCCTCTCGGGGGCCAAATGAATGACAGTGGGTCCGGG
CCTGACCAACCCATGAATAGGCATGCCCCCCATGATGGTGCCGTGACCCACGCAGCATCT
CCTTTCCTCCCTCAGCCACAGATGCCAGGTCAGTGGGGGCCTGCACAAGGAGGCCCTCAG
CCCTCCTATCAGCATCACTCACCCTACCTGGAGGGACCTGTTCAGAACATGGGGCTCCAG
GCTGCCAGCTTGCCCCATTTTCCCCCTCCATCTAGTCTACACCAGGGACCTGGCCATGAG
TCTCATGCCCCACAAACATTTACTCCAGCATCCTTGGCCAGTGGTGAAGGGAATGAGATA
GTTCACCAGCAAAGTAAAAATCACCCATTGAGTAGCTTCCCCCCTAAACACACTTTTGAG
CAGAATTCCAGAATTGGGAATATGTGGGCAAGCCCAGAGTTGAAGCAGAATCCGGGAGTG
AATAAAGAGCATCTGCTAGACCCTGCCCATGTCAATCCCTTCACTCAGGGAAATAGTCCT
GAAAACCAGGCGCATCATCCCCCAGTGGCAGCAACTAACCATGCCCTGCAAGAAGCAGCC
TCTGGAGCTCTCTCAATGTTTTTTCAAGGGGAAGAGACAGAGAATGAGGAAAATCTCTCA
TCTGAAAAAGCAGGCCTCGATAAGAGATTGAACTTGGATAGCTTTTCTTCTACCTCCAGA
CTGGGCCATCCACCACCACCTGGAGCTAGTGGTGTCT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CTACCAACTCCAGCCAGGCAGCTGTGCGGTCAGAGCAGCTGTGGCTACACCCACCA
CCTCCTAATACTTTTGGCCCAGCACCTCAAGACTTGGCATCCTACTACTATTACAGACCC
CTGTATGATGCCTACCAGTCTCAGTACCCTTCACCGTACCCGTCAGATCCTGGGACAGCC
TCGCTGTACTACCAGGATATGTATGGCCTGTATGAGCCTCGTTACAGACCCTATGACAGC
TCAGCATCTGCTTATGCCGAGAACCACCGCTATTCTGAGCCTGAGCGGCCCAGCTCCCGT
GCAAGTCACTATTCCGACCAGCTTGCTCCCAGGCAAGGATATCCTGAAGGATACTATAAT
TCCAAGAGTGGATGGAGCAGTCACAGTGATTACTATGCAAATTATTACTCTGGCCAGTAT
GATTATGGAGACCCCAGCCGCTGGGATCGTTACTATGGGTCTCGCCTTAGGGATCCTCGC
ACCTGGGACCGGAGGTACTGGTATGACTCTGAACATGACCCATACAGGAAGGACCACTAT
GCTTACAGTGACAGGCCTGAGAAATGTGATGATCACTGGAGGTATGACCCTCGCTTCACT
GGGAGCTTCGACGATGACGCTGAGATCCACAGGGACCCCTATGGAGAAGAAGCAGACAGA
CGCAGCATCCACAGTGAGCACTCGGCACGGAGCCTGCGCAGCACTCACAGTCTGCCCAGC
CGCCGCAGCAGCCTCAGCTCCCATTCACACCAGAGTCAGATTTACAGAAGCCACCATGTG
ACTGGCGGTTCCTTTGAGGCCCCTCATGCCCCAGGCTCATTTCATGGTGATTATGCCTAT
GGCACATATGCCAGCAATTTCAGCGGTGCCCATGGTTTTCCAGAGTACAGCTACCCTGCA
GACACCTCCTGGCCTGCTGTGGAGCAAGTTCCATCAAGACCGACCTCTCCTGAGAAGTTT
...
...
...
...

I want the output to look like :

>DR-1
1260 
>SK-1
1020
>SC-1
1380
>SC-2
960
>TA-1
921
>DR-2
2400
TA-2
>1980
...
...
...
...

I am new to learning python for molecular analysis, so am having trouble with the logic and syntax behind it. I'm looking forward to a warm response from you. Thanks again.

$\endgroup$

3 Answers 3

3
$\begingroup$

Replace "string protein sequences.fa" with "your_fasta_file.fa" and your file should be at the same path that you created your code.

import Bio.SeqIO as IO
record_dict = IO.to_dict(IO.parse("string_protein_sequences.fa", "fasta"))
for key in record_dict.items():
    print(key[0],"\n ",len(key[1].seq))

Output:

9606.ENSP00000354718 
  2817
9606.ENSP00000383118 
  191
9606.ENSP00000275364 
  381
9606.ENSP00000265563 
  404
9606.ENSP00000265717 
  418
9606.ENSP00000348461 
  211
9606.ENSP00000249071 
  192
9606.ENSP00000304283 
  192
9606.ENSP00000400175 
  193
9606.ENSP00000285735 
  193
9606.ENSP00000339467 
  191

I hope that's what you need, good luck

$\endgroup$
2
  • $\begingroup$ Thanks for you quick respone. Unfortunately i didnt have biopython in my windows and also in Linux env. I could not make it run. Can you please give the code to run with general python code? $\endgroup$ Commented Jun 21, 2021 at 7:06
  • 1
    $\begingroup$ You can just install it with this command: pip install biopython, if not try the code that is in the second answer $\endgroup$
    – dachi
    Commented Jun 21, 2021 at 14:23
0
$\begingroup$

Don't forget to change the file name and write the valid path

with open("string_protein_sequences.fa") as file_content:
    liste=[]
    j=-1
    for seqs in file_content:
        if seqs.startswith('>'):
            j+=1
            print(seqs,lis[j])
            some=0
            i=0
        else:
            i+=1
            some+=len(seqs) 
        liste.append(some-i)
    lis=[]
    for j in range(1,len(liste)):
        if liste[j]==0:
            lis.append(liste[j-1])
    lis.append(liste[-1])

Output:

 >9606.ENSP00000354718 (AKAP13)
  2817
 >9606.ENSP00000383118 (CDC42)
  191
 >9606.ENSP00000275364 (GNA12)
  381
 >9606.ENSP00000265563 (PRKAR2A)
  404
 >9606.ENSP00000265717 (PRKAR2B)
  418
 >9606.ENSP00000348461 (RAC1)
  211
 >9606.ENSP00000249071 (RAC2)
  192
 >9606.ENSP00000304283 (RAC3)
  192
 >9606.ENSP00000400175 (RHOA)
  193
 >9606.ENSP00000285735 (RHOC)
  193
 >9606.ENSP00000339467 (RHOG)
  191
$\endgroup$
0
$\begingroup$

you can use a dictionary as well:

fasta = {}
with open("test.fasta", "r") as file:
    for line in file:
        line = line.strip()
        if line[0] == ">":
            header = line
            fasta[header] = ""
        else:
            data = line
            fasta[header] += data

for k in fasta.keys():
    print(f"{k}\n{len(fasta[k])}")
$\endgroup$
2
  • $\begingroup$ It works great! Thanks. The len information has generated on the bottom row of the header. Can i put this information with the seq id using some name like: >seq1 "len=424" AATT.........................ATTT $\endgroup$ Commented Sep 17, 2021 at 15:12
  • $\begingroup$ In that case you should use fasta.items() to iterate through the dictionary: for k, v in fasta.items(): print(f'{k} "len={len(fasta[k])}"\n{v}') $\endgroup$
    – zorbax
    Commented Sep 17, 2021 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.