Python: How to output the FASTA header or chromosome index figure according to the location?
Question: I have the code which help me to move the window of size 5 when it moves from
left to right. The file is in fasta format with header >chromosome for example
followed by the index of the chromosome. I would like to output the header
index number according to the exact index. Can anyone help me?

**Code**

    
    
    from Bio import SeqIO
    
    with open("test1_out.fasta","w") as f:
            for seq_record in SeqIO.parse("test1.fasta", "fasta"):
                for i in range(len(seq_record.seq) - 4) :
                   f.write(">" + str(seq_record.id) + "\n")
                   f.write(str(seq_record.seq[i:i+5]) + "\n")
    

**test1.fasta**

    
    
    >chr1:1-8 
    ATCGCGTC
    >chr2:1-10
    ATTTTCGCGA
    

**Actual output**

    
    
    >chr1:1-8
    ATCGC
    >chr1:1-8
    TCGCG
    >chr1:1-8
    CGCGT
    >chr1:1-8
    GCGTC
    >chr2:1-10
    ATTTT
    >chr2:1-10
    TTTTC
    >chr2:1-10
    TTTCG
    >chr2:1-10
    TTCGC
    >chr2:1-10
    TCGCG
    >chr2:1-10
    CGCGA
    

**Desired output**

    
    
    >chr1:1-5
    ATCGC
    >chr1:2-6
    TCGCG
    >chr1:3-7
    CGCGT
    >chr1:4-8
    GCGTC
    >chr2:1-5
    ATTTT
    >chr2:2-6
    TTTTC
    >chr2:3-7
    TTTCG
    >chr2:4-8
    TTCGC
    >chr2:5-9
    TCGCG
    >chr2:6-10
    CGCGA
    


Answer: You just have to modify the heading writing:

    
    
    seq_name = seq_record.id.split(":")[0]  # Get the "chr1"
    for i in range(len(seq_record.seq) - 4):        
        seq_coords = "{}-{}".format(i + 1, i + 5)  # Make the coordinates
    
        f.write(">" + seq_name + ":" + seq_coords + "\n")  # Print them both
    

