编写函数
get_parser
这样地:
def get_parser():
parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
parser.add_argument('-l', required = False, type = str, help='Name of input file when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format
return parser
main
功能如下:
def main():
parser = get_parser()
args = parser.parse_args() # check arguments
if args.s and args.g:
with open(args.g, "rU") as genome: #open(args.c + '.gbk','w') as outgbk:
for record in SeqIO.parse(genome, "genbank"):
if args.s == record.id:
#with open(args.c + '.gbk','w') as outgbk:
SeqIO.write([record], open(record.id + ".gbk", "w"), "genbank")
print record.id + ".gbk created."
else:
parser.print_help()