#!/usr/bin/python # # Rajarshi Guha # 23/02/2005 # import string,sys,getopt,os def usage(): print """ Splits an SDF file containing multiple structures into individual SDF files. Usage: splitsdf.py [OPTIONS] -b,--base Basename of resultant files. -f,--file SDF to split -h,--help This message -v,--verbose Verbose output If basename is given as XXX, then the resultant files will called XXX?.sdf where ? will be a zero padded integer. The default value is the basname of the input file. The resultant files are placed in the current working directory """ if __name__ == '__main__': inputfile = '' base = '' verbose = 0 try: opt,args = getopt.getopt(sys.argv[1:], 'b:f:hv',\ ['base=','file=','help=','verbose']) except getopt.GetoptError: usage() sys.exit(0) for o,a in opt: if o in ('-h','--help'): usage() sys.exit(0) if o in ('-f','--file'): inputfile = a if o in ('-b','--base'): base = a if o in ('-v','--verbose'): verbose = 1 if inputfile == '': usage() sys.exit(0) cnt = 1 sdffile = open(inputfile,'r') if base == '': base = string.split(inputfile,".")[0] outfile = open('%s%05d.sdf' % (base,cnt) ,'w') for line in sdffile: if string.find(line,'$$$$') == 0: if verbose: print 'Done with molecule %d' % (cnt) outfile.close() #sdffile.readline() cnt = cnt + 1 outfile = open('%s%05d.sdf' % (base,cnt) ,'w') else: outfile.write(line) os.system('rm %s%05d.sdf' % (base,cnt)) if verbose: print 'Done'