def __init__(self, name = '', cas = '', inchi = '', inchikey = '', csid = ''): #first define the SOAP service for searching searchurl = 'http://www.chemspider.com/Search.asmx?WSDL' try: self.searchclient = Client(searchurl) except Exception as e: print(e) #define the soap service for inchi-conversion inchiurl = 'http://www.chemspider.com/InChI.asmx?WSDL' try: self.inchiclient = Client(inchiurl) except Exception as e: print(e) #set all properties to the ones from initiating call self.cas = cas self.inchi = inchi self.inchikey = inchikey self.name = name self.csid = csid #no transaction id for now self.transaction_id = '' #how quickly should we ask for results? in seconds self.timetick = 0.2 self.maxtime = 15 #read chemspider token from config file 'chemspider_token' try: f = hl.openfile('chemspider_token.txt') except IOError: raise IOError self.token = f.readline()
# the chemspider_token.txt should only contain the token (available online for free) # using the argparse module to make use of command line options parser = argparse.ArgumentParser( description="Read a list of chemicals and output the same list completed with InChI and Chemspider IDs (tab separated)" ) parser.add_argument("--version", action="version", version="r1") parser.add_argument("filename", help="Specify a filename to read.") # parse it args = parser.parse_args() try: liste = hl.openfile(args.filename) except IOError: sys.exit("Could not read file") try: outputliste = open(args.filename + "_output.txt", "w") except: sys.exit("Could not open output file") for line in liste: line = line.rstrip("\r\n") print("Searching for: " + line) try: compound = ChemicalObject(name=line) except IOError: sys.exit("No chemspider token given. Aborting.")
elif args.folder is not None: #adjust file path in case we're running on f*****g windows args.folder = os.path.normcase(args.folder) #lets go through that folder and add every filename to the filelist caldirlist = os.listdir(args.folder) for file in caldirlist: filelist.append([os.path.join(args.folder, file), file]) elif args.filelist is not None: #in this we have to read the list of filenames in a file f = hl.openfile(args.filelist) for line in f: #we split the array by whitespaces or tabs (split tries both) line = line.strip('\r\n').split() #first argument should be the filename + path, therefore appending it to a temp array together with the filename filelist.append(line[0], os.path.basename(line[0])) #let's walk our filelist for file in filelist: #this variable is set to false if we encounter a non-readable file usefulfile = True try: data = hl.readfile(file[0]) except IOError: