
##   2013.04.04 - adpated to Biopython 1.6 using the SeqIO module, specifically the SeqIO.index() function.
##   find a seq with the exact ID

## modified 2018.02.01 for python 3

import sys, os

from Bio import SeqIO


if len(sys.argv) <= 1:  #input file name if not suppied as argument
    ifile = input("input FASTA filename: ")
else:
    ifile = sys.argv[1]
 
if not (os.path.isfile(ifile)):
    print ("The FASTA file " + ifile + " you specified cannot be found.")
    sys.exit()

seq_dict = SeqIO.index(ifile, "fasta")   #making a dic of id:seq

print ("Found %i records" % len(seq_dict))

seq_id = input("ID of the sequence to be extracted, i.e. first word following >: ")  #ID of the sequence, can be partial

start = int(input ("start position: "))
end= int(input ("end position: "))
ofile = input ("output filename: ")    # file to store the retrieved seq
of = open (ofile, "w")
of.write (">" + seq_dict[seq_id].id + " Range:" + str(start) + " : "+ str(end)+ "\n")
of.write (str(seq_dict[seq_id].seq[start:end]))


of.close()
print ("done")
   


