def download(path, root=None, skip=True): skip = '-s' if skip else '-f' print(path, path.parts) base = path.parts[0] print(f'At {base} from {root}...', end=' ') query = [] if root is None: root = 'root' query.append(f"'{root}' in parents") if base != '*': if '*' in base: query.append(f"name contains '{base.strip('*')}'") else: query.append(f"name = '{base}'") query = ' and '.join(query) if len(path.parts) == 1: print('Downloading...') process = Popen(f'gdrive download {skip} query " {query} "', shell=True) process.wait() else: print('Recursing...') child = PurePath(os.path.join(*path.parts[1:])) process = Popen(f'gdrive list --query " {query} "', stdout=PIPE, shell=True).stdout process.readline() for line in process: download(child, line.decode().split()[0])
class SAMReader: def __init__(self, fileName, isBam=False): if (isBam == False and fileName[-4:] == ".sam"): self.f = open(fileName) elif (isBam == True and fileName[-4:] == ".bam"): args = "samtools view " + fileName print args.split() self.f = Popen(args.split(), stdout=PIPE).stdout else: print("your suffix and does not match value self.isBam") sys.exit(1) self.header = list() # store SAM header self.lineNo = 0 # store current read line number self.line = "" # store current read line signal.signal(signal.SIGPIPE, signal.SIG_DFL) def __iter__(self): return self def next(self): self.line = self.f.readline() self.lineNo += 1 while (self.line != '' and self.line[0] == '@'): self.header.append(self.line) self.line = self.f.readline() self.lineNo += 1 if (self.line == ''): self.f.close() raise StopIteration fields = self.line.split('\t') record = dict() record["QNAME"] = fields[0] record["FLAG"] = int(fields[1]) record["RNAME"] = fields[2] record["POS"] = int(fields[3]) record["MAPQ"] = int(fields[4]) record["CIGAR"] = fields[5] record["MRNM"] = fields[6] record["MPOS"] = int(fields[7]) record["ISIZE"] = int(fields[8]) record["SEQ"] = fields[9] record["QUAL"] = fields[10] record["TAGS"] = fields[11:] # we don't care the optional tags unless necessary # if (len(fields) > 11): # for i in fields[11:]: # (tag, vtype, value) = i.split(":") # if (vtype=="i"): # record[tag] = int(value) # elif(vtype=="f"): # record[tag] = float(value) # else: # record[tag] = value return record def dump(self): print self.line
def iter_processes(): pipe = Popen(["ps", "-Af"], stdout=PIPE).stdout pipe.readline() # Skip header for line in pipe: fields = line.split(None, 7) pid = int(fields[1]) command = fields[-1] name = basename(command.split()[0]) yield pid, name
def call_fh(self): print(self.path) out = Popen('fasthenry ' + self.path) #out = os.popen('ls') while 1: if out.readline() == "": break print(out.readline()) f = open(join(self.dir, 'Zc.mat'), 'r') result = out.read() return out, result
def run_command(command, shell=False): if DEBUG: print str(command) + "\n" return sys.__stdout__.write(str(command) + "\n") stdout = Popen(command, stdout=PIPE, shell=True).stdout line = stdout.readline() count = 1 if line else 0 while line: sys.__stdout__.write(line) line = stdout.readline() count += 1 return count
def getNetworkParams(): """Return a dictionary of network parameters. Values include: ssid Current SSID bssid Current Base Station ID """ from subprocess import Popen, PIPE networkParams = {} airportCmd = "%s -I " % binaries["airport"] debug("Running %s" % airportCmd) pipe = Popen(airportCmd, shell=True, stdout=PIPE).stdout while True: line = pipe.readline() if len(line) == 0: break components = line.split() if components[0] == "SSID:": networkParams["ssid"] = components[1] continue if components[0] == "BSSID:": networkParams["bssid"] = components[1] continue pipe.close() # Query interface, which one depends on wether we're using the airport # or not if networkParams.has_key("SSID"): interface = airportIF else: interface = ethernetIF ifconfigCmd = "%s %s" % (binaries["ifconfig"], interface) debug("Running %s" % ifconfigCmd) pipe = Popen(ifconfigCmd, shell=True, stdout=PIPE).stdout while True: line = pipe.readline() if len(line) == 0: break components = line.split() if components[0] == "inet": networkParams["IPaddress"] = components[1] pipe.close() return networkParams
def get_surface(lemma_morph, pos): """ Given a lemma+morph in RASP format, returns a tuple containing (surface, lemma). Uses morphg to generate the surface, or returns 2 copies of the input if morphg was not provided. """ global morphg_file parts = lemma_morph.rsplit("+", 1) if len(parts) == 1 or lemma_morph == "+": # No inflection lemma = surface = lemma_morph elif len(parts) == 2 and "+" not in parts[0]: # Standard inflected unit lemma = parts[0] if morphg_file is not None: lemma_morph = lemma_morph.replace("\"", "\\\"") cmd = "echo \"%s_%s\" | ${morphg_res:-./%s -t}" % \ ( lemma_morph, pos, morphg_file ) p = Popen(cmd, shell=True, stdout=PIPE).stdout #generates the surface form using morphg surface = unicode(p.readline(), 'utf-8').split("_")[0] p.close() else: surface = lemma else: # the token contains one or several '+' lemma = surface = parts[0] return (surface, lemma)
def runMatch(self,playerOne, playerTwo,gameSeed) : try : inline= Popen("./QtSpimbot -file "+playerOne+" -file2 "+playerTwo + " -randomseed "+gameSeed+ " -randommap -tournament -run -exit_when_done -maponly -quiet ",\ stdout=PIPE, shell=True).stdout string = "not" while(not (string == '')) : string = inline.readline() if string[:7] == "winner:" : return string[8:-1] print "\nerror, What? This should not be so? Did you quit qtSpim?" return self.manual_override(playerOne,playerTwo,gameSeed) except KeyboardInterrupt: return self.manual_override(playerOne,playerTwo,gameSeed) except Alarm: print "timeOut" killerror= Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout print killerror.read() time.sleep(1) return "#fail#"
def _get_surface(self, lemma_morph, pos, ctxinfo): """ Given a lemma+affix in RASP format, returns a tuple containing (surface, lemma, affix). Uses morphg to generate the surface, or returns 2 copies of the input if morphg was not provided. """ affix = "" parts = lemma_morph.rsplit("+", 1) if len( parts ) == 1 or lemma_morph == "+": # No inflection, e.g. lemma_morph="+" lemma = surface = lemma_morph elif len(parts) == 2 and "+" not in parts[ 0]: # Standard inflected unit, e.g. lemma_morph="be+s" lemma, affix = parts if self.morphg_file is not None: lemma_morph = lemma_morph.replace("\"", "\\\"") cmd = "echo \"%s_%s\" | ${morphg_res:-./%s -t}" % \ ( lemma_morph, pos, self.morphg_file ) p = Popen(cmd, shell=True, stdout=PIPE).stdout #generates the surface form using morphg surface = str(p.readline(), self.encoding).split("_")[0] p.close() else: ctxinfo.warn_once( "Not using morphg, using lemma+affix instead of surface") surface = lemma_morph else: # the token contains one or several '+', e.g. lemma_morph="C+++" lemma = surface = parts[0] affix = parts[1] return (surface, lemma, affix)
def get_surface( lemma_morph, pos ) : """ Given a lemma+morph in RASP format, returns a tuple containing (surface, lemma). Uses morphg to generate the surface, or returns 2 copies of the input if morphg was not provided. """ global morphg_file parts = lemma_morph.rsplit("+",1) if len(parts) == 1 or lemma_morph == "+": # No inflection lemma = surface = lemma_morph elif len(parts) == 2 and "+" not in parts[0]: # Standard inflected unit lemma = parts[0] if morphg_file is not None : lemma_morph = lemma_morph.replace("\"","\\\"") cmd = "echo \"%s_%s\" | ${morphg_res:-./%s -t}" % \ ( lemma_morph, pos, morphg_file ) p = Popen(cmd, shell=True, stdout=PIPE).stdout #generates the surface form using morphg surface = unicode(p.readline(), 'utf-8').split("_")[ 0 ] p.close() else: surface = lemma else: # the token contains one or several '+' lemma = surface = parts[0] return ( surface, lemma )
def runTwoPlayers(seed_list, rand): if not type(rand) == str: rand = "-randomseed " + str(rand(1355029990, 1355039990)) + " -randommap" print rand try: inline = Popen("./QtSpimbot -file spimbot.s -file2 spimbot2.s -run " + rand + " -exit_when_done -maponly -quiet ", stdout=PIPE, shell=True).stdout string = "not" out_come = [] while (not (string == '')): string = inline.readline() if string[:7] == "cycles:": out_come.append(string[7:-1]) if string[:7] == "winner:": out_come.append(string) return out_come return ["error, What? This should not be so?"] except Alarm: print "your bot is too slow" killerror = Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout print killerror.read() time.sleep(1) seed_list.append(rand[12:-11]) return ["fail"]
def getuser(opts): user="" if opts.user is not None: user=opts.user else: f=Popen(('whoami'),stdout=PIPE).stdout user=f.readline().strip() return user
def getuser(opts): user = "" if opts.user is not None: user = opts.user else: f = Popen(('whoami'), stdout=PIPE).stdout user = f.readline().strip() return user
def testRESP(respfile): TransFuncsid = "B053F03" Respinid = "B053F05" cmdtrans = "cat " + respfile + " | grep " + TransFuncsid + " |awk '{print $5 }' " cmdResp = "cat " + respfile + " | grep " + Respinid + " |awk '{print $6 }' " t = Popen(cmdtrans, shell=True, close_fds=True, stdout=PIPE).stdout r = Popen(cmdResp, shell=True, close_fds=True, stdout=PIPE).stdout trans = t.readline( )[:-1] # We are checking just in the first line with the pattern Resp = r.readline()[:-1] t.close() r.close() return (trans, Resp)
def testRESP(respfile): TransFuncsid = "B053F03" Respinid= "B053F05" cmdtrans = "cat " + respfile + " | grep " + TransFuncsid + " |awk '{print $5 }' " cmdResp = "cat " + respfile + " | grep " + Respinid + " |awk '{print $6 }' " t = Popen(cmdtrans, shell=True,close_fds=True, stdout=PIPE).stdout r = Popen(cmdResp , shell=True,close_fds=True, stdout=PIPE).stdout trans = t.readline()[:-1] # We are checking just in the first line with the pattern Resp = r.readline()[:-1] t.close() r.close() return (trans, Resp)
class PoSlave: def __init__(self): self.slave = Popen(['coqtop.opt', '-ideslave'], shell=True, stdin=PIPE, stdout=PIPE) def write(self, line): self.slave.writeline(line) def read(self): return self.slave.readline()
def factor_sig(n): pipe = Popen(["factor", str(n)], shell=False, stdout=PIPE).stdout l = pipe.readline() pipe.close() sig = {} factors_s = re.sub("^[^:]*:", "", l) for x in re.findall("[0-9]+", factors_s): if x not in sig: sig[x] = 0 sig[x] += 1 return sorted(sig.values())
def svn_list(ns, url): cmd = "svn ls {}".format(url) pipe = Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout # you cannot do the "assign and set" trick that you commonly do in other language , typically the C #because assignment in python is a "Statement" not a expression. # check How to assign a variable in IF, and then return it. (Python): http://stackoverflow.com/a/1551223 # for the iteration recipe,check "reading text files: http://effbot.org/zone/readline-performance.htm" while 1: line = pipe.readline() if line: yield line
def factor_sig(n): pipe = Popen(['factor', str(n)], shell=False, stdout=PIPE).stdout l = pipe.readline() pipe.close() sig = {} factors_s = re.sub('^[^:]*:', '', l) for x in re.findall('[0-9]+', factors_s): if not x in sig: sig[x] = 0 sig[x] += 1 return sorted(sig.values())
def factor_sig(n): pipe = Popen(['factor', str(n)], shell=False, stdout=PIPE).stdout l = pipe.readline() pipe.close() sig = {} factors_s = re.sub('^[^:]*:', '', l) for x in re.findall('[0-9]+', factors_s): if x not in sig: sig[x] = 0 sig[x] += 1 return sorted(sig.values())
def test_popen(): f = Popen(('uname', '-a'), stdout=PIPE).stdout data = f.readline() f.close() print data print '-'*30 f = Popen('who', stdout=PIPE).stdout data = [eachLine.strip() for eachLine in f] f.close() for eachLine in data: print eachLine
def executeCmd(cmd): from subprocess import Popen, PIPE, STDOUT fromCmd = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, close_fds=True).stdout while True: line = fromCmd.readline() if len(line) == 0: break debug(line) fromCmd.close()
def run(self): """Invokes exiftran on the command line, and reads each line of its shell output until it's finished execution. This process effectively halts further progress of the program, which is why it's contained in a thread. Continuously updates the value of files_processed_count.""" self.prepare_filenames() cmd = 'exiftran -aip %s' % self.filenames_str p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).stderr while 1: line = p.readline() if not line: break # Unfortunately, exiftran dumps progress info as well as # error and other messages to stderr. So, we just inspect # each line manually: if it begins with 'processing ', # it's the latest progress update; otherwise, it's an error # or some other message, and we ignore it. if line.startswith('processing '): self.files_processed_count += 1
def run(self): """Xplot file is opened (and normally doesn't print anything to stdout). The modified version prints by pressing 'c' the begin and end time of the current view. This view is then written to a new file. """ xplot = Popen(["xplot", self.args.xpl_file], bufsize=0, stdout=PIPE, shell=False).stdout while True: line = xplot.readline() if not line: break print line begin, end = re.match("<time_begin:time_end> = "\ "<(-?[\d.]+):(-?[\d.]+)>", line).group(1, 2) self.cut(float(begin), float(end))
def run(self): """Invokes exiftran on the command line, and reads each line of its shell output until it's finished execution. This process effectively halts further progress of the program, which is why it's contained in a thread. Continuously updates the value of files_processed_count.""" self.prepare_filenames() cmd = "exiftran -aip %s" % self.filenames_str p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).stderr while 1: line = p.readline() if not line: break # Unfortunately, exiftran dumps progress info as well as # error and other messages to stderr. So, we just inspect # each line manually: if it begins with 'processing ', # it's the latest progress update; otherwise, it's an error # or some other message, and we ignore it. if line.startswith("processing "): self.files_processed_count += 1
def command_with_status(cmd): wsock = request.environ.get('wsgi.websocket') if not wsock: abort(400, 'Expected WebSocket request.') opts = wsock.receive() args = shlex.split("%s %s" % (cmd, opts)) try: pipe = Popen(args, stdout=PIPE).stdout while True: line = pipe.readline() if line: wsock.send(line) else: break except WebSocketError: pass
def _runParmchk(self, ident, tempDir): import os, os.path, sys from subprocess import Popen, STDOUT, PIPE from chimera import replyobj parmDir = os.path.dirname(__file__) if not parmDir: parmDir = os.getcwd() parmchkIn = os.path.join(tempDir, "parmchk.in.%d" % ident) self._writeParmchk(parmchkIn) self.frcmod = os.path.join(tempDir, "frcmod.%d" % ident) chimeraRoot = os.environ["CHIMERA"] anteHome = os.path.join(chimeraRoot, "bin", "antechamber") command = [ os.path.join(anteHome, "exe", "parmchk"), "-i", parmchkIn, "-f", "mol2", "-o", self.frcmod, "-p", os.path.join(parmDir, "parm", "gaff.dat") ] replyobj.status("Running PARMCHK for %s\n" % self.chimeraMolecule.name, log=True) replyobj.info("command: %s\n" % " ".join(command)) p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=False, env={ "ACHOME": anteHome }, bufsize=1).stdout while True: line = p.readline() if not line: break replyobj.info("(parmchk) %s\n" % line.rstrip()) if not os.path.exists(self.frcmod): from chimera import LimitationError raise LimitationError("Unable to compute partial " "charges: PARMCHK failed.\n" "Check reply log for details.\n") self.frcmod = None replyobj.status("Finished PARMCHK for %s\n" % self.chimeraMolecule.name, log=True)
def numa_structure(): lscpu_pipe = Popen("lscpu", stdout=PIPE).stdout number_of_numa_nodes = 0 cpus_per_node = 0 not_ready = True outputString = "" coresNodeList = [] while not_ready: line = lscpu_pipe.readline() if line: matchObject = re.search("NUMA node\d CPU\(s\):(.*)", line, re.M) if matchObject: number_of_numa_nodes = number_of_numa_nodes + 1 cpusString = matchObject.group(1).strip() rangeMatchObject = re.search("(\d+)-(\d+)", cpusString, re.M) coreListString = "" if rangeMatchObject: start = int(rangeMatchObject.group(1).strip()) end = int(rangeMatchObject.group(2).strip()) cpus_per_node = end + 1 coreList = [] for i in range(start, end + 1): coreList.append(str(i)) coreListString = ",".join(coreList) else: cpus_per_node = len(cpusString.split(",")) coreListString = re.sub(r' ', "", cpusString) coresNodeList.append(coreListString) else: not_ready = False #Hack because of bug in cpuinfo for bulldozer if socket.gethostname() == "bulldozer": newCoresNodeList = [] for i in range(0, number_of_numa_nodes, 2): newCoresNodeList.append(coresNodeList[i] + "," + coresNodeList[i + 1]) coresNodeList = newCoresNodeList number_of_numa_nodes = number_of_numa_nodes / 2 cpus_per_node = cpus_per_node * 2 lscpu_pipe.close() coresNodeList = ["{" + x + "}" for x in coresNodeList] numaStructure = "{" + ",".join(coresNodeList) + "}" return (number_of_numa_nodes, cpus_per_node, numaStructure)
def main(): parser = OptionParser(usage="usage: %prog [options]") parser.add_option("-d", action="store_true", dest="offline", help="offline mode") parser.add_option("-o", "--output", action="store", dest="output", default=f_log, help="output file") (opt, argv) = parser.parse_args() adb = "adb" log = "logcat" pkg = "org.umd.logging" opts = [] if opt.offline: opts.append("-d") opts.extend([pkg + ":I", "*:S"]) if not opt.offline: call([adb, log, "-c"]) a = Popen([adb, log] + opts, stdout=PIPE).stdout f = open(f_log, "w") indent = -2 while True: try: line = a.readline() if len(line) == 0: break # EOF line = line.rstrip("\r\n") if not pkg in line: continue msg = "".join(re.split("(>|<)", line)[1:]) if msg[0] == ">": indent += 2 print "%*s%s" % (indent, "", msg) f.write("%*s%s\n" % (indent, "", msg)) elif msg[0] == "<": print "%*s%s" % (indent, "", msg) f.write("%*s%s\n" % (indent, "", msg)) indent -= 2 except KeyboardInterrupt: break f.close()
def runGame( seed_list, rand) : if not type(rand) == str : rand = "-randomseed "+str(rand(1355029990,1355039990))+" -randommap" print rand try : inline= Popen("./QtSpimbot -file spimbot.s -run "+rand+ " -exit_when_done -maponly -quiet ", stdout=PIPE, shell=True).stdout string = "not" while(not (string == '')) : string = inline.readline() if string[:7] == "cycles:" : return [string[7:-1]] return ["error, What? This should not be so?"] except Alarm: print "your bot is too slow" killerror= Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout print killerror.read() time.sleep(1) seed_list.append(rand[12:-11]) return ["fail"]
def main(): parser = OptionParser(usage="usage: %prog [options]") parser.add_option("-d", action="store_true", dest="offline", help="offline mode") parser.add_option("-o", "--output", action="store", dest="output", default=f_log, help="output file") (opt, argv) = parser.parse_args() adb = "adb" log = "logcat" pkg = "org.umd.logging" opts = [] if opt.offline: opts.append("-d") opts.extend([pkg + ":I", "*:S"]) if not opt.offline: call([adb, log, "-c"]) a = Popen([adb, log] + opts, stdout=PIPE).stdout f = open(f_log, 'w') indent = -2 while True: try: line = a.readline() if len(line) == 0: break # EOF line = line.rstrip("\r\n") if not pkg in line: continue msg = ''.join(re.split("(>|<)", line)[1:]) if msg[0] == '>': indent += 2 print "%*s%s" % (indent, "", msg) f.write("%*s%s\n" % (indent, "", msg)) elif msg[0] == '<': print "%*s%s" % (indent, "", msg) f.write("%*s%s\n" % (indent, "", msg)) indent -= 2 except KeyboardInterrupt: break f.close()
def main(args): if "-h" in args: print >> sys.stderr, __doc__.strip() return 0 os.environ["TERM"] = "dumb" cmd = "prstat -c %s" % " ".join(args) pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout lines = [] while True: while True: line = pipe.readline() if not lines: now = datetime.datetime.now() lines.append("\n%79s\n" % now.strftime("%H:%M:%S")) lines.append(line.replace("\015", "")) if "Total:" in line: sys.stdout.writelines(lines) sys.stdout.flush() lines[:] = []
def graptokens(rand) : if not type(rand) == str : rand = "-randomseed "+str(rand(1355029990,1355039990))+" -randommap" try : inline= Popen("./QtSpimbot -file done.s "+rand+ " -maponly -debug -run -exit_when_done", stdout=PIPE, shell=True).stdout string = "not" pointList =[] while(not (string == '')) : string = inline.readline() if string[:6] == "TOKEN:" : nums=string[7:].split(" ") pointList.append( (int(nums[0]),int(nums[1]))) return pointList except Alarm: killerror= Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout print "error" return []
def _runParmchk(self, ident, tempDir): import os, os.path, sys from subprocess import Popen, STDOUT, PIPE from chimera import replyobj parmDir = os.path.dirname(__file__) if not parmDir: parmDir = os.getcwd() parmchkIn = os.path.join(tempDir, "parmchk.in.%d" % ident) self._writeParmchk(parmchkIn) self.frcmod = os.path.join(tempDir, "frcmod.%d" % ident) chimeraRoot = os.environ["CHIMERA"] anteHome = os.path.join(chimeraRoot, "bin", "antechamber") command = [ os.path.join(anteHome, "exe", "parmchk"), "-i", parmchkIn, "-f", "mol2", "-o", self.frcmod, "-p", os.path.join(parmDir, "parm", "gaff.dat") ] replyobj.status("Running PARMCHK for %s\n" % self.chimeraMolecule.name, log=True) replyobj.info("command: %s\n" % " ".join(command)) p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=False, env={"ACHOME": anteHome}, bufsize=1).stdout while True: line = p.readline() if not line: break replyobj.info("(parmchk) %s\n" % line.rstrip()) if not os.path.exists(self.frcmod): from chimera import LimitationError raise LimitationError("Unable to compute partial " "charges: PARMCHK failed.\n" "Check reply log for details.\n") self.frcmod = None replyobj.status("Finished PARMCHK for %s\n" % self.chimeraMolecule.name, log=True)
def ictclas( self,text ): text = text.replace('\'',' ') # 去除单引号,以免对分词操作造成影响 cmd = "./ictclas '%s'" % text.encode('gbk','ignore') std_out = Popen(cmd, shell = True,stdout = PIPE).stdout # 提取指纹 finger = std_out.readline() # 仅记录指纹没有出现过的--去重 if finger in self.fingers:return None self.fingers.append( finger ) word_nums = [] for line in std_out: line=line.strip() if line: word,num = line.split() word = word.decode('gbk').encode('utf-8','ignore') # 1.统计词频 if word.find(':')!=-1:continue # 过滤含':'的词 self.add_word(word,int(num) ) # 2.形成doc word_nums.append( "%s:%s" % (word,num) ) pass pass page_words = ','.join( word_nums ) return page_words
def graptokens(rand): if not type(rand) == str: rand = "-randomseed " + str(rand(1355029990, 1355039990)) + " -randommap" try: inline = Popen("./QtSpimbot -file done.s " + rand + " -maponly -debug -run -exit_when_done", stdout=PIPE, shell=True).stdout string = "not" pointList = [] while (not (string == '')): string = inline.readline() if string[:6] == "TOKEN:": nums = string[7:].split(" ") pointList.append((int(nums[0]), int(nums[1]))) return pointList except Alarm: killerror = Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout print "error" return []
A detailed description of prod. """ __author__ = "[email protected] (Gary Boland)" import serial import os import feedparser import time from subprocess import Popen, PIPE myserial = serial.Serial("/dev/ttyACM0", 9600) while True: result = Popen(("/usr/local/symlinks/prodcertstatus"), stdout=PIPE).stdout data = result.readline() result.close() data1 = data.strip() data2 = data1.split() data3 = data2[2:] data4 = " ".join(data3) myserial.write(data4) myserial.flushOutput() time.sleep(10) def main(argv): pass if __name__ == "__main__":
# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. # Please see the License for the specific language governing rights and # limitations under the License. # # @APPLE_OSREFERENCE_LICENSE_HEADER_END@ ## #!/usr/bin/env python import sys from subprocess import call, Popen, PIPE kexts = [] pipe = Popen("/usr/sbin/kextfind \( -l -and -x -and -arch i386 \)", shell=True, stdout=PIPE).stdout line = pipe.readline() while line: kexts.append(line.strip()) line = pipe.readline() NULL = open("/dev/null") for kext in kexts: try: print "Processing", kext #cmd = "/sbin/kextload -ns /tmp/syms \"%s\"" % kext cmd = "/sbin/kextload \"%s\"" % kext kextload = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) for i in range(20): kextload.stdin.write("0x1000\n"); retcode = kextload.wait()
from subprocess import Popen, PIPE from sys import argv file1 = argv[1] file2 = argv[2] out = argv[3] f1 = Popen('zcat %s'%file1, shell=True, stdout=PIPE).stdout f2 = Popen('zcat %s'%file2, shell=True, stdout=PIPE).stdout o = Popen('gzip - > %s'%out, shell=True, stdin=PIPE).stdin l = f1.readline() while l: o.write('%s'%l) for i in range(3): o.write(f1.readline()) for i in range(4): o.write(f2.readline()) l = f1.readline() #next read? f1.close() f2.close() o.close()
def writePrmtop(m, topfile, parmset, unchargedAtoms=None): import os import chimera from chimera import replyobj from WriteMol2 import writeMol2 from tempfile import mkdtemp status = replyobj.status if unchargedAtoms and parmset.lower().endswith("ua"): # united atom replyobj.warning("Some uncharged/untyped protons expected due" " to use of united-atom force field.\n") unchargedHeavy = [] skip = [] for uncharged in unchargedAtoms.values(): for uc in uncharged: if uc.element.number == 1: skip.append(uc) else: unchargedHeavy.append(uc) unchargedAtoms = unchargedHeavy else: skip = [] if unchargedAtoms: if chimera.nogui: raise ValueError("Some atoms don't have charges/types") from chimera.baseDialog import AskYesNoDialog d = AskYesNoDialog("Some atoms don't have charges/types" " assigned. Write prmtop anyway?") if d.run(chimera.tkgui.app) == "no": return tempDir = mkdtemp() def _clean(): for fn in os.listdir(tempDir): os.unlink(os.path.join(tempDir, fn)) os.rmdir(tempDir) sleapIn = os.path.join(tempDir, "sleap.in.mol2") writeMol2([m], sleapIn, status=status, gaffType=True, skip=skip) leaprc = os.path.join(tempDir, "solvate.cmd") writeLeaprc(tempDir, topfile, parmset, leaprc) chimeraRoot = os.environ["CHIMERA"] amberHome = os.path.join(chimeraRoot, "bin", "amber10") acHome = os.path.join(chimeraRoot, "bin", "antechamber") command = [os.path.join(amberHome, "exe", "sleap"), "-f", leaprc] print 'command: ', command if status: status("Running sleap" ) from subprocess import Popen, STDOUT, PIPE # For some reason on Windows, if shell==False then antechamber # cannot run bondtype via system(). import sys if sys.platform == "win32": shell = True else: shell = False replyobj.info("Running sleap command: %s\n" % " ".join(command)) import os os.environ["AMBERHOME"]=amberHome os.environ["ACHOME"]=acHome sleapMessages = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=shell, bufsize=1).stdout while True: line = sleapMessages.readline() if not line: break replyobj.status("(writeprmtop) %s" % line, log=True) if not os.path.exists(topfile): _clean() from chimera import NonChimeraError raise NonChimeraError("Failure running sleap \n" "Check reply log for details\n") else: replyobj.status("Wrote parmtop file %s\n" % topfile, log=True)
import json import os import os.path import psrchive from subprocess import Popen, PIPE data = [] os.environ["TERM"] = "dumb" for n in range(0, 58): for i in range(0, 12): fname = "n" + str(n) + "b" + str(i).zfill( 2) + '_2020_07_16_12:45:47.fil.pazi' if os.path.isfile(fname): log = "n" + str(n) + "_run_summary.json" with open(log) as json_file: data = json.load(json_file) #cmd = "psrstat -q -c '{$on:max-$off:avg}' -j DFTp " + fname cmd = "psrstat -q -c snr -j DFTp " + fname pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout value = pipe.readline().strip("\n").strip(" ").strip("snr=") ra = data['beams']["list"][i]["ra_hms"] dec = data['beams']["list"][i]["dec_dms"] #print fname[0:3]+"b"+str(i),ra,dec,value print ra, dec, value
def spectral_filtering_detections(tid, db_path_T='template_db_1/', db_path_M='matched_filter_1/', db_path=autodet.cfg.dbpath, SNR_thres=5., WAVEFORMS=None, normRMS=True, best=True): from subprocess import Popen, PIPE from obspy import Stream, Trace #----------------------------------------------------------------------------------------------- T = autodet.db_h5py.read_template('template{:d}'.format(tid), db_path=db_path+db_path_T) #----------------------------------------------------------------------------------------------- print("Looking for {}{:d}_*".format(db_path + db_path_M + '*multiplets', tid)) files_list = Popen('ls '+db_path+db_path_M+'*multiplets{:d}_*'.format(tid), stdout=PIPE, shell=True).stdout line = files_list.readline()[:-1] files = [] while len(line) != 0: files.append(line.decode('utf-8')) line = files_list.readline()[:-1] i = 0 Nsamp = 0 ns = 0 S = Stream() #------------- retrieve metadata --------------- while True: try: wav = files[i][-len('wav.h5'):] == 'wav.h5' if wav: if Nsamp == 0: with h5.File(files[i], mode='r') as fwav0: Nsamp = fwav0['waveforms'][:,:,:,:].shape[-1] i += 1 else: with h5.File(files[i], mode='r') as fm0: if len(fm0['origin_times']) == 0: i += 1 continue else: i += 1 nc = len(fm0['components'][()]) ns = len(fm0['stations'][()]) S.stations = fm0['stations'][()].astype('U').tolist() S.components = fm0['components'][()].astype('U').tolist() S.latitude = T.metadata['latitude'] S.longitude = T.metadata['longitude'] S.depth = T.metadata['depth'] S.template_ID = tid if ns != 0 and Nsamp != 0: break except IndexError: print("None multiplet for template {:d} !! Return None".format(tid)) return None #---------------------------------------------- if WAVEFORMS is None: CC = np.zeros(0, dtype=np.float32) if best: for file in files: if file[-len('meta.h5'):] != 'meta.h5': continue with h5.File(file, mode='r') as fm: if len(fm['correlation_coefficients']) == 0: continue else: CC = np.hstack((CC, fm['correlation_coefficients'][:])) CC = np.sort(CC) #CC_thres = np.sort(CC)[-min(5, len(CC))] if len(CC) > 300: CC_thres = CC[-101] elif len(CC) > 70: CC_thres = CC[int(7./10.*len(CC))] # the best 30% elif len(CC) > 30: CC_thres = np.median(CC) # the best 50% elif len(CC) > 10: CC_thres = np.percentile(CC, 33.) # the best 66% detections else: CC_thres = 0. Nstack = np.zeros((ns, nc), dtype=np.float32) WAVEFORMS = np.zeros((0,ns,nc,Nsamp), dtype=np.float32) Nmulti = 0 for file in files: if file[-len('wav.h5'):] != 'wav.h5': continue with h5.File(file, mode='r') as fw: if len(fw['waveforms']) == 0: continue else: if best: with h5.File(file[:-len('wav.h5')]+'meta.h5', mode='r') as fm: selection = np.where(fm['correlation_coefficients'][:] > CC_thres)[0] if selection.size == 0: continue waves = np.zeros((selection.size, ns, nc, Nsamp), dtype=np.float32) waves[:,:,:,:] = fw['waveforms'][selection,:,:,:] else: waves = fw['waveforms'][:,:,:,:] Nmulti += waves.shape[0] for m in range(waves.shape[0]): for s in range(ns): for c in range(nc): if normRMS: norm = np.sqrt(np.var(waves[m,s,c,:])) else: norm =1. if norm != 0.: waves[m,s,c,:] /= norm WAVEFORMS = np.vstack((WAVEFORMS, waves)) elif normRMS: for m in range(WAVEFORMS.shape[0]): for s in range(ns): for c in range(nc): norm = np.sqrt(np.var(WAVEFORMS[m,s,c,:])) if norm != 0.: WAVEFORMS[m,s,c,:] /= norm else: pass filtered_waveforms = np.zeros((ns, nc, Nsamp), dtype=np.float32) for s in range(ns): for c in range(nc): filtered_waveforms[s, c, :] = np.sum(spectral_filtering(WAVEFORMS[:, s, c, :], SNR_thres=SNR_thres), axis=0) return filtered_waveforms
def SVDWF_multiplets_test(template_id, db_path=autodet.cfg.dbpath, db_path_M='matched_filter_2/', db_path_T='template_db_2/', WAVEFORMS=None, normRMS=True, Nsing_values=5, max_freq=autodet.cfg.max_freq, attach_raw_data=False): from subprocess import Popen, PIPE from obspy import Stream, Trace from scipy.linalg import svd from scipy.signal import wiener #----------------------------------------------------------------------------------------------- T = autodet.db_h5py.read_template('template{:d}'.format(template_id), db_path=db_path+db_path_T) #----------------------------------------------------------------------------------------------- print("Looking for {}{:d}_*".format(db_path + db_path_M + '*multiplets', template_id)) files_list = Popen('ls '+db_path+db_path_M+'*multiplets{:d}_*'.format(template_id), stdout=PIPE, shell=True).stdout line = files_list.readline()[:-1] files = [] while len(line) != 0: files.append(line.decode('utf-8')) line = files_list.readline()[:-1] i = 0 Nsamp = 0 ns = 0 S = Stream() #------------- retrieve metadata --------------- while True: try: wav = files[i][-len('wav.h5'):] == 'wav.h5' if wav: if Nsamp == 0: with h5.File(files[i], mode='r') as fwav0: Nsamp = fwav0['waveforms'][:,:,:,:].shape[-1] i += 1 else: with h5.File(files[i], mode='r') as fm0: if len(fm0['origin_times']) == 0: i += 1 continue else: i += 1 nc = len(fm0['components'][()]) ns = len(fm0['stations'][()]) S.stations = fm0['stations'][()].astype('U').tolist() S.components = fm0['components'][()].astype('U').tolist() S.latitude = T.metadata['latitude'] S.longitude = T.metadata['longitude'] S.depth = T.metadata['depth'] S.template_id = template_id if ns != 0 and Nsamp != 0: break except IndexError: print("None multiplet for template {:d} !! Return None".format(template_id)) return None #---------------------------------------------- catalog = autodet.db_h5py.read_catalog_multiplets('multiplets{:d}'.format(template_id), db_path_M=db_path_M, db_path=db_path) CC = catalog['correlation_coefficients'] best_detection_indexes = np.argsort(CC)[::-1] if CC.size > 300: best_detection_indexes = best_detection_indexes[:100] # the best 100 detections elif CC.size > 100: best_detection_indexes = best_detection_indexes[:int(30./100. * CC.size)] # the best 30% elif CC.size > 50: best_detection_indexes = best_detection_indexes[:int(50./100. * CC.size)] # the best 50% elif CC>size > 10: best_detection_indexes = best_detection_indexes[:int(66./100. * CC.size)] # the best 66% else: pass # keep all detections # reorder by chronological order best_detection_indexes = best_detection_indexes[np.argsort(catalog['origin_times'][best_detection_indexes])] # get the waveforms n_events = best_detection_indexes.size WAVEFORMS = np.zeros((n_events, ns, nc, Nsamp), dtype=np.float32) filename0 = db_path + db_path_M + catalog['filenames'][best_detection_indexes[0]].decode('utf-8') f = h5.File(filename0 + 'wav.h5', mode='r') for n in range(n_events): filename = db_path + db_path_M + catalog['filenames'][best_detection_indexes[n]].decode('utf-8') if filename == filename0: pass else: f.close() f = h5.File(filename + 'wav.h5', mode='r') WAVEFORMS[n, :, :, :] = f['waveforms'][catalog['indices'][best_detection_indexes[n]], :, :, :] # normalization for s in range(ns): for c in range(nc): if normRMS: norm = np.std(WAVEFORMS[n, s, c, :]) else: norm = np.abs(WAVEFORMS[n, s, c, :]).max() if norm != 0.: WAVEFORMS[n, s, c, :] /= norm filtered_data = np.zeros((n_events, ns, nc, Nsamp), dtype=np.float32) for s in range(ns): for c in range(nc): filtered_data[:,s,c,:] = SVDWF(WAVEFORMS[:,s,c,:], Nsing_values, max_freq=max_freq) #filtered_data[:,s,c,:] = spectral_filtering(WAVEFORMS[:,s,c,:], SNR_thres=5., max_freq=max_freq) mean = np.mean(filtered_data[:,s,c,:], axis=0) mean /= np.abs(mean).max() S += Trace(data=mean) S[-1].stats.station = S.stations[s] S[-1].stats.channel = S.components[c] S[-1].stats.sampling_rate = autodet.cfg.sampling_rate S.data = filtered_data if attach_raw_data: S.raw_data = WAVEFORMS S.Nmulti = best_detection_indexes.size return S
for i in range(11): a = s.readline() if ' smearing contrib.' in a: break # correct for finite temperature entropy term # in case of finite temp. smearing if a[:22] == ' smearing contrib.': energy -= 0.5 * float(a.split()[-2]) * rydberg return energy p = Popen('grep -a -n Giannozzi ' + argv[1] + ' 2>/dev/null | tail -1', shell=True, stdout=PIPE).stdout try: n = int(p.readline().split()[0].strip(':')) except: print('No valid pw-log at ' + argv[1] + ' found.', file=stderr) p.close() exit(2) p.close() s = open(argv[1], 'r') # skip over previous runs in log in case the current log has been # appended to old ones for i in range(n): s.readline() a = s.readline() while a[:11] != ' celldm': a = s.readline()
#!/usr/bin/env python # encoding: UTF-8 """Test demo 14.3 for chapter 14.""" from subprocess import call, Popen, PIPE import os hosts = call(('cat', '/etc/hosts')) print hosts print '' f = Popen(('uname', '-a'), stdout=PIPE).stdout data = f.readline() f.close() print data, print '' f = Popen('who', stdout=PIPE).stdout data = [eachLine.strip() for eachLine in f] f.close() for eachLine in data: print eachLine os.sys.exit(1)
def _nonStdCharge(residues, netCharge, method, gaffType, status, showCharges): r = residues[0] if status: status("Copying residue %s\n" % r.type) # create a fake Molecule that we can write to a Mol2 file nm = chimera.Molecule() nm.name = r.type # write out the residue's atoms first, since those are the # ones we will be caring about nr = nm.newResidue(r.type, ' ', 1, ' ') from chimera.molEdit import addAtom atomMap = {} atomNames = set() ratoms = r.atoms # use same ordering of atoms as they had in input, to improve # consistency of antechamber charges ratoms.sort(lambda a1, a2: cmp(a1.coordIndex, a2.coordIndex)) for a in ratoms: atomMap[a] = addAtom(a.name, a.element, nr, a.coord()) atomNames.add(a.name) # add the intraresidue bonds and remember the interresidue ones nearby = set() for a in ratoms: na = atomMap[a] for n in a.neighbors: if n.residue != r: nearby.add(n) continue nn = atomMap[n] if nn in na.bondsMap: continue nm.newBond(na, nn) from chimera.idatm import typeInfo extras = set() while nearby: nb = nearby.pop() aname = _getAName(str(nb.element), atomNames) na = addAtom(aname, nb.element, nr, nb.coord()) atomMap[nb] = na extras.add(na) for nbn in nb.neighbors: if nbn in atomMap: nm.newBond(na, atomMap[nbn]) else: try: ti = typeInfo[nbn.idatmType] except KeyError: fc = 0 geom = 4 else: fc = estimateNetCharge([nbn]) geom = ti.geometry if fc or geom != 4: nearby.add(nbn) else: extras.update(_methylate(na, nbn, atomNames)) totalNetCharge = netCharge + estimateNetCharge(extras) from tempfile import mkdtemp import os, os.path tempDir = mkdtemp() def _clean(): for fn in os.listdir(tempDir): os.unlink(os.path.join(tempDir, fn)) os.rmdir(tempDir) from WriteMol2 import writeMol2 anteIn = os.path.join(tempDir, "ante.in.mol2") writeMol2([nm], anteIn, status=status) chimeraRoot = os.environ["CHIMERA"] anteHome = os.path.join(chimeraRoot, 'bin', 'antechamber') anteOut = os.path.join(tempDir, "ante.out.mol2") if method.lower().startswith("am1"): mth = "bcc" elif method.lower().startswith("gas"): mth = "gas" else: _clean() raise ValueError("Unknown charge method: %s" % method) command = [ os.path.join(anteHome, "exe", "antechamber"), "-i", anteIn, "-fi", "mol2", "-o", anteOut, "-fo", "mol2", "-c", mth, "-nc", str(totalNetCharge), "-df", "0", "-j", "5", "-s", "2" ] if status: status("Running ANTECHAMBER for residue %s\n" % r.type) from subprocess import Popen, STDOUT, PIPE # For some reason on Windows, if shell==False then antechamber # cannot run bondtype via system(). import sys if sys.platform == "win32": shell = True else: shell = False replyobj.info("Running ANTECHAMBER command: %s\n" % " ".join(command)) import os os.environ['ACHOME'] = anteHome anteMessages = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=shell, bufsize=1).stdout while True: line = anteMessages.readline() if not line: break replyobj.status("(%s) %s" % (r.type, line), log=True) if not os.path.exists(anteOut): _clean() raise ChargeError("Failure running ANTECHAMBER for residue %s\n" "Check reply log for details\n" % r.type) if status: status("Reading ANTECHAMBER output for residue %s\n" % r.type) from chimera import Mol2io, defaultMol2ioHelper mol2io = Mol2io(defaultMol2ioHelper) mols = mol2io.readMol2file(anteOut) if not mol2io.ok(): _clean() raise IOError(mol2io.error()) if not mols: _clean() raise RuntimeError("No molecules in ANTECHAMBER output for" " residue %s" % r.type) chargedAtoms = mols[0].atoms if len(chargedAtoms) != len(nm.atoms): _clean() raise RuntimeError("Wrong number of atoms (%d, should be %d)" " in ANTECHAMBER output for residue %s" % (r.type, len(chargedAtoms), len(nm.atoms))) if status: status("Assigning charges for residue %s\n" % r.type) # put charges in template templateAtoms = nm.atoms # can't rely on order... templateAtoms.sort(lambda a1, a2: cmp(a1.serialNumber, a2.serialNumber)) nonZero = False addedChargeSum = 0.0 _totalCharge = 0.0 for ta, ca in zip(templateAtoms, chargedAtoms): _totalCharge += ca.charge if ta in extras: addedChargeSum += ca.charge continue if ca.charge: nonZero = True if not nonZero: _clean() raise ChargeError("Failure running ANTECHAMBER for residue %s\n" "Check reply log for details\n" % r.type) # adjust charges to compensate for added atoms... adjustment = (addedChargeSum - (totalNetCharge - netCharge)) / (len(templateAtoms) - len(extras)) for ta, ca in zip(templateAtoms, chargedAtoms): if ta in extras: continue ta.charge = ca.charge + adjustment if gaffType: ta.gaffType = ca.mol2type # map template charges onto first residue track = chimera.TrackChanges.get() for fa, ta in atomMap.items(): if ta in extras: continue fa.charge = ta.charge if showCharges: fa.label = "%+g" % fa.charge track.addModified(fa, ATTR_SET) if gaffType: fa.gaffType = ta.gaffType # map charges onto remaining residues for rr in residues[1:]: for fa, ra in zip(r.oslChildren(), rr.oslChildren()): ra.charge = fa.charge if showCharges: ra.label = "%+g" % ra.charge track.addModified(ra, ATTR_SET) if gaffType: ra.gaffType = fa.gaffType _clean() if status: status("Charges for residue %s determined\n" % r.type) replyobj.info("Charges for residue %s determined\n" % r.type) return True
def initiateSolvate(models, method, solvent, extent, center, status): import os import chimera from chimera import replyobj from chimera.molEdit import addAtom from WriteMol2 import writeMol2 from tempfile import mkdtemp for m in models: tempDir = mkdtemp() print 'tempDir: ', tempDir def _clean(): for fn in os.listdir(tempDir): os.unlink(os.path.join(tempDir, fn)) os.rmdir(tempDir) sleapIn = os.path.join(tempDir, "sleap.in.mol2") sleapOut= os.path.join(tempDir, "sleap.out.mol2") writeMol2([m], sleapIn, status=status) leaprc = os.path.join(tempDir, "solvate.cmd") writeLeaprc(tempDir, method, solvent, extent, center, leaprc) chimeraRoot = os.environ["CHIMERA"] amberHome = os.path.join(chimeraRoot, "bin", "amber10") command = [os.path.join(amberHome, "exe", "sleap"), "-f", leaprc] print 'command: ', command if status: status("Running sleap" ) from subprocess import Popen, STDOUT, PIPE # For some reason on Windows, if shell==False then antechamber # cannot run bondtype via system(). import sys if sys.platform == "win32": shell = True else: shell = False replyobj.info("Running sleap command: %s\n" % " ".join(command)) import os os.environ["AMBERHOME"]=amberHome sleapMessages = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=shell, bufsize=1).stdout while True: line = sleapMessages.readline() if not line: break replyobj.status("(solvate) %s" % line, log=True) if not os.path.exists(sleapOut): _clean() from chimera import NonChimeraError raise NonChimeraError("Failure running sleap \n" "Check reply log for details\n") if status: status("Reading sleap output\n") from chimera import Mol2io, defaultMol2ioHelper mol2io = Mol2io(defaultMol2ioHelper) mols = mol2io.readMol2file(sleapOut) if not mol2io.ok(): _clean() raise IOError(mol2io.error()) if not mols: _clean() raise RuntimeError("No molecules in sleap output") assert len(mols)==1 outm = mols[0] natom = len(m.atoms) nresd = len(m.residues) inAtoms = m.atoms outAtoms = outm.atoms # sort in coordIndex (i.e. input) order # (due to deletions, coordIndex values need _not_ be consecutive) serialSort = lambda a1, a2: cmp(a1.coordIndex, a2.coordIndex) inAtoms.sort(serialSort) outAtoms.sort(serialSort) if status: status("Translating %d atoms" % len(inAtoms)) for inA, outA in zip(inAtoms, outAtoms[:len(inAtoms)]): inA.setCoord(outA.coord()) # added solvent hydrogens may not have been categorized yet, so use # this less obvious way of gathering solvent atoms... existingSolvent = set() from chimera.elements import metals, alkaliMetals nonAlkaliMetals = metals - alkaliMetals for r in m.residues: if len(r.atoms) == 1 and r.atoms[0].element in nonAlkaliMetals: continue for a in r.atoms: if a.surfaceCategory in ["solvent", "ions"]: existingSolvent.update(r.atoms) break # copy mol2 comment which contain the info of the solvent: shape, size, etc if hasattr( outm, "mol2comments" ) and len(outm.mol2comments) > 0: m.solventInfo = outm.mol2comments[0] print "solvent info: ", m.solventInfo if existingSolvent: solventCharges = {} for r in outm.residues[nresd:]: solventNum = r.id.position - nresd if status: status("Creating solvent residue %d " % solventNum ) atomMap = {} nr = m.newResidue(r.type, ' ', solventNum, ' ') # mark residue for exclusion by AddCharge... nr._solvateCharged = True for a in r.atoms: na = addAtom(a.name, a.element, nr, a.coord(), serialNumber=a.serialNumber) na.charge = a.charge na.gaffType = a.mol2type atomMap[a] = na if a.name[0]=="H": na.element = 1 if a.name[0]=="C": na.element = 6 if a.name[0]=="N": na.element = 7 if a.name[0]=="O": na.element = 8 if a.name[0]=="P": na.element = 15 if a.name[0]=="S": na.element = 16 if a.name[0:2]=="Cl": na.element = 17 if existingSolvent: solventCharges[(r.type, a.name)] = a.charge if r.type == "WAT": solventCharges[ ("HOH", a.name)] = a.charge for a in r.atoms: na = atomMap[a] for n in a.neighbors: assert n.residue == r nn = atomMap[n] if nn in na.bondsMap: continue m.newBond(na, nn) if existingSolvent: unknowns = set() for sa in existingSolvent: key = (sa.residue.type, sa.name) try: sa.charge = solventCharges[key] except KeyError: unknowns.add(key) sa.residue._solvateCharged = True if unknowns: replyobj.warning("Could not determine charges for" " pre-existing solvent/ions from added solvent" "/ions for: " + ", ".join([" ".join(x) for x in unknowns])) _clean() from Midas import window window(models)
print "error while downloading schema" sys.exit(1) schema.close() timestampfile = open(maindir + '/schemainfo.txt', 'w') timestampfile.write(mdate + '\n' + str(size) + '\n') timestampfile.close() pipe = Popen("/bin/tar -zxOf " + maindir + "/schema_" + asctime + ".tgz svn.log | /bin/grep 'revision='", shell=True, bufsize=512, stdout=PIPE).stdout revpattern = re.compile("\"[0-9]+\"") version = re.findall(revpattern, pipe.readline())[0] pipe.close() version = version.replace('"', '') os.unlink(maindir + "/schema_" + asctime + ".tgz") emailtxt = "From: " + confvar['emailfrom'] + " \n\ To: " + confvar['emailto'] + " \n\ Subject: [schemamonitor] New schema version " + version + " from " + mdate + " \n\n\n\ You can download it from here:\n\ ftp://172.30.73.133/nsmdiff_and_stuff/schema/schema_" + version + ".tgz\n\ The file size is: " + str(size) + "\n\ The release notes information can be probably downloaded from here:\n\ http://kb.juniper.net/library/CUSTOMERSERVICE/GLOBAL_JTAC/technotes/DMI_Schema_v" + version + ".pdf\n\n\ --\nThis email was created by a very intelligent script\nAll flames/complaints will go to /dev/null\n"
from subprocess import Popen, PIPE if sys.version_info > (3,): long = int BASE = 1004535809 # Tuple is (n,k) where n is the sum and k is the count. cache = {(0,0):1,(1,1):1,(1,0):0,(0,1):1} pipe = Popen("primes 2", shell=True, stdout=PIPE).stdout last = 1 counts = [] for i in range(0,20001): n = int(pipe.readline()) counts.append(n-last) last = n pipe.close() MAX = 20000 # p = prev. def next_row(k,p): if k == 0: return counts return [ sum([counts[i] * p[n-i] for i in range(0,n+1)]) % BASE for n in range(0, MAX+1) ]
def initiateAddions(models, iontype, numion, status): import os import chimera from chimera import replyobj from chimera.molEdit import addAtom from WriteMol2 import writeMol2 from tempfile import mkdtemp for m in models: tempDir = mkdtemp() def _clean(): for fn in os.listdir(tempDir): os.unlink(os.path.join(tempDir, fn)) os.rmdir(tempDir) sleapIn = os.path.join(tempDir, "sleap.in.mol2") sleapOut = os.path.join(tempDir, "sleap.out.mol2") writeMol2([m], sleapIn, status=status, gaffType=True) leaprc = os.path.join(tempDir, "solvate.cmd") writeLeaprc(tempDir, iontype, numion, leaprc) chimeraRoot = os.environ["CHIMERA"] amberHome = os.path.join(chimeraRoot, "bin", "amber10") command = [os.path.join(amberHome, "exe", "sleap"), "-f", leaprc] if status: status("Running sleap") from subprocess import Popen, STDOUT, PIPE # For some reason on Windows, if shell==False then antechamber # cannot run bondtype via system(). import sys if sys.platform == "win32": shell = True else: shell = False replyobj.info("Running sleap command: %s\n" % " ".join(command)) import os os.environ["AMBERHOME"] = amberHome sleapMessages = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=shell, bufsize=1).stdout while True: line = sleapMessages.readline() if not line: break replyobj.status("(addions) %s" % line, log=True) if not os.path.exists(sleapOut): _clean() from chimera import NonChimeraError raise NonChimeraError("Failure running sleap \n" "Check reply log for details\n") if status: status("Reading sleap output\n") from chimera import Mol2io, defaultMol2ioHelper mol2io = Mol2io(defaultMol2ioHelper) mols = mol2io.readMol2file(sleapOut) if not mol2io.ok(): _clean() raise IOError(mol2io.error()) if not mols: _clean() raise RuntimeError("No molecules in sleap output") assert len(mols) == 1 outm = mols[0] solute_nresd = get_solute_nresd(m) print "total, solute, solvent: ", len( m.residues), solute_nresd, len(m.residues) - solute_nresd if status: status("Deleting old solvents") while len(m.residues) > solute_nresd: m.deleteResidue(m.residues[solute_nresd]) inAtoms = m.atoms outAtoms = outm.atoms # sort in coordIndex (i.e. input) order # (due to deletions, coordIndex values need _not_ be consecutive) serialSort = lambda a1, a2: cmp(a1.coordIndex, a2.coordIndex) inAtoms.sort(serialSort) outAtoms.sort(serialSort) # sleap repositions solute... if status: status("Translating %d atoms" % len(inAtoms)) for inA, outA in zip(inAtoms, outAtoms[:len(inAtoms)]): inA.setCoord(outA.coord()) for r in outm.residues[solute_nresd:]: if status: status("Creating ions/solvent residue %d " % (r.id.position - solute_nresd)) atomMap = {} nr = m.newResidue(r.type, ' ', 1, ' ') for a in r.atoms: na = addAtom(a.name, a.element, nr, a.coord(), serialNumber=a.serialNumber) na.charge = a.charge na.gaffType = a.mol2type if len(a.neighbors) == 0: na.drawMode = chimera.Atom.Sphere atomMap[a] = na if a.name[0:2] == "Br": na.element = 35 elif a.name[0:2] == "Cl": na.element = 17 elif a.name[0:2] == "Cs": na.element = 47 elif a.name[0:2] == "Mg": na.element = 12 elif a.name[0:2] == "Na": na.element = 11 elif a.name[0:2] == "Rb": na.element = 48 elif a.name[0] == 'F': na.element = 9 elif a.name[0] == 'I': na.element = 53 elif a.name[0] == 'K': na.element = 19 elif a.name[0] == "H": na.element = 1 elif a.name[0] == "C": na.element = 6 elif a.name[0] == "N": na.element = 7 elif a.name[0] == "O": na.element = 8 elif a.name[0] == "P": na.element = 15 elif a.name[0] == "S": na.element = 16 for a in r.atoms: na = atomMap[a] for n in a.neighbors: assert n.residue == r nn = atomMap[n] if nn in na.bondsMap: continue m.newBond(na, nn) _clean()
def _nonStdCharge(residues, netCharge, method, gaffType, status, showCharges): r = residues[0] if status: status("Copying residue %s\n" % r.type) # create a fake Molecule that we can write to a Mol2 file nm = chimera.Molecule() nm.name = r.type # write out the residue's atoms first, since those are the # ones we will be caring about nr = nm.newResidue(r.type, ' ', 1, ' ') from chimera.molEdit import addAtom atomMap = {} atomNames = set() ratoms = r.atoms # use same ordering of atoms as they had in input, to improve # consistency of antechamber charges ratoms.sort(lambda a1, a2: cmp(a1.coordIndex, a2.coordIndex)) for a in ratoms: atomMap[a] = addAtom(a.name, a.element, nr, a.coord()) atomNames.add(a.name) # add the intraresidue bonds and remember the interresidue ones nearby = set() for a in ratoms: na = atomMap[a] for n in a.neighbors: if n.residue != r: nearby.add(n) continue nn = atomMap[n] if nn in na.bondsMap: continue nm.newBond(na, nn) from chimera.idatm import typeInfo extras = set() while nearby: nb = nearby.pop() aname = _getAName(str(nb.element), atomNames) na = addAtom(aname, nb.element, nr, nb.coord()) atomMap[nb] = na extras.add(na) for nbn in nb.neighbors: if nbn in atomMap: nm.newBond(na, atomMap[nbn]) else: try: ti = typeInfo[nbn.idatmType] except KeyError: fc = 0 geom = 4 else: fc = estimateNetCharge([nbn]) geom = ti.geometry if fc or geom != 4: nearby.add(nbn) else: extras.update( _methylate(na, nbn, atomNames)) totalNetCharge = netCharge + estimateNetCharge(extras) from tempfile import mkdtemp import os, os.path tempDir = mkdtemp() def _clean(): for fn in os.listdir(tempDir): os.unlink(os.path.join(tempDir, fn)) os.rmdir(tempDir) from WriteMol2 import writeMol2 anteIn = os.path.join(tempDir, "ante.in.mol2") writeMol2([nm], anteIn, status=status) chimeraRoot = os.environ["CHIMERA"] anteHome = os.path.join(chimeraRoot, 'bin', 'antechamber') anteOut = os.path.join(tempDir, "ante.out.mol2") if method.lower().startswith("am1"): mth = "bcc" elif method.lower().startswith("gas"): mth = "gas" else: _clean() raise ValueError("Unknown charge method: %s" % method) command = [os.path.join(anteHome, "exe", "antechamber"), "-i", anteIn, "-fi", "mol2", "-o", anteOut, "-fo", "mol2", "-c", mth, "-nc", str(totalNetCharge), "-df", "0", "-j", "5", "-s", "2"] if status: status("Running ANTECHAMBER for residue %s\n" % r.type) from subprocess import Popen, STDOUT, PIPE # For some reason on Windows, if shell==False then antechamber # cannot run bondtype via system(). import sys if sys.platform == "win32": shell = True else: shell = False replyobj.info("Running ANTECHAMBER command: %s\n" % " ".join(command)) import os os.environ['ACHOME'] = anteHome anteMessages = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=shell, bufsize=1).stdout while True: line = anteMessages.readline() if not line: break replyobj.status("(%s) %s" % (r.type, line), log=True) if not os.path.exists(anteOut): _clean() raise ChargeError("Failure running ANTECHAMBER for residue %s\n" "Check reply log for details\n" % r.type) if status: status("Reading ANTECHAMBER output for residue %s\n" % r.type) from chimera import Mol2io, defaultMol2ioHelper mol2io = Mol2io(defaultMol2ioHelper) mols = mol2io.readMol2file(anteOut) if not mol2io.ok(): _clean() raise IOError(mol2io.error()) if not mols: _clean() raise RuntimeError("No molecules in ANTECHAMBER output for" " residue %s" % r.type) chargedAtoms = mols[0].atoms if len(chargedAtoms) != len(nm.atoms): _clean() raise RuntimeError("Wrong number of atoms (%d, should be %d)" " in ANTECHAMBER output for residue %s" % (r.type, len(chargedAtoms), len(nm.atoms))) if status: status("Assigning charges for residue %s\n" % r.type) # put charges in template templateAtoms = nm.atoms # can't rely on order... templateAtoms.sort(lambda a1, a2: cmp(a1.serialNumber, a2.serialNumber)) nonZero = False addedChargeSum = 0.0 _totalCharge = 0.0 for ta, ca in zip(templateAtoms, chargedAtoms): _totalCharge += ca.charge if ta in extras: addedChargeSum += ca.charge continue if ca.charge: nonZero = True if not nonZero: _clean() raise ChargeError("Failure running ANTECHAMBER for residue %s\n" "Check reply log for details\n" % r.type) # adjust charges to compensate for added atoms... adjustment = (addedChargeSum - (totalNetCharge - netCharge)) / ( len(templateAtoms) - len(extras)) for ta, ca in zip(templateAtoms, chargedAtoms): if ta in extras: continue ta.charge = ca.charge + adjustment if gaffType: ta.gaffType = ca.mol2type # map template charges onto first residue track = chimera.TrackChanges.get() for fa, ta in atomMap.items(): if ta in extras: continue fa.charge = ta.charge if showCharges: fa.label = "%+g" % fa.charge track.addModified(fa, ATTR_SET) if gaffType: fa.gaffType = ta.gaffType # map charges onto remaining residues for rr in residues[1:]: for fa, ra in zip(r.oslChildren(), rr.oslChildren()): ra.charge = fa.charge if showCharges: ra.label = "%+g" % ra.charge track.addModified(ra, ATTR_SET) if gaffType: ra.gaffType = fa.gaffType _clean() if status: status("Charges for residue %s determined\n" % r.type) replyobj.info("Charges for residue %s determined\n" % r.type) return True
# limitations under the License. # # @APPLE_OSREFERENCE_LICENSE_HEADER_END@ ## #!/usr/bin/env python import sys from subprocess import call, Popen, PIPE kexts = [] pipe = Popen("/usr/sbin/kextfind \( -l -and -x -and -arch i386 \)", shell=True, stdout=PIPE).stdout line = pipe.readline() while line: kexts.append(line.strip()) line = pipe.readline() NULL = open("/dev/null") for kext in kexts: try: print "Processing", kext #cmd = "/sbin/kextload -ns /tmp/syms \"%s\"" % kext cmd = "/sbin/kextload \"%s\"" % kext kextload = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) for i in range(20): kextload.stdin.write("0x1000\n") retcode = kextload.wait()
def initiateAddions(models, iontype, numion, status): import os import chimera from chimera import replyobj from chimera.molEdit import addAtom from WriteMol2 import writeMol2 from tempfile import mkdtemp for m in models: tempDir = mkdtemp() def _clean(): for fn in os.listdir(tempDir): os.unlink(os.path.join(tempDir, fn)) os.rmdir(tempDir) sleapIn = os.path.join(tempDir, "sleap.in.mol2") sleapOut= os.path.join(tempDir, "sleap.out.mol2") writeMol2([m], sleapIn, status=status, gaffType=True) leaprc = os.path.join(tempDir, "solvate.cmd") writeLeaprc(tempDir, iontype, numion, leaprc) chimeraRoot = os.environ["CHIMERA"] amberHome = os.path.join(chimeraRoot, "bin", "amber10") command = [os.path.join(amberHome, "exe", "sleap"), "-f", leaprc] if status: status("Running sleap" ) from subprocess import Popen, STDOUT, PIPE # For some reason on Windows, if shell==False then antechamber # cannot run bondtype via system(). import sys if sys.platform == "win32": shell = True else: shell = False replyobj.info("Running sleap command: %s\n" % " ".join(command)) import os os.environ["AMBERHOME"]=amberHome sleapMessages = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=tempDir, shell=shell, bufsize=1).stdout while True: line = sleapMessages.readline() if not line: break replyobj.status("(addions) %s" % line, log=True) if not os.path.exists(sleapOut): _clean() from chimera import NonChimeraError raise NonChimeraError("Failure running sleap \n" "Check reply log for details\n") if status: status("Reading sleap output\n") from chimera import Mol2io, defaultMol2ioHelper mol2io = Mol2io(defaultMol2ioHelper) mols = mol2io.readMol2file(sleapOut) if not mol2io.ok(): _clean() raise IOError(mol2io.error()) if not mols: _clean() raise RuntimeError("No molecules in sleap output") assert len(mols)==1 outm = mols[0] solute_nresd = get_solute_nresd(m) print "total, solute, solvent: ", len(m.residues), solute_nresd, len(m.residues)-solute_nresd if status: status( "Deleting old solvents" ) while len(m.residues) > solute_nresd: m.deleteResidue( m.residues[solute_nresd] ) inAtoms = m.atoms outAtoms = outm.atoms # sort in coordIndex (i.e. input) order # (due to deletions, coordIndex values need _not_ be consecutive) serialSort = lambda a1, a2: cmp(a1.coordIndex, a2.coordIndex) inAtoms.sort(serialSort) outAtoms.sort(serialSort) # sleap repositions solute... if status: status("Translating %d atoms" % len(inAtoms)) for inA, outA in zip(inAtoms, outAtoms[:len(inAtoms)]): inA.setCoord(outA.coord()) for r in outm.residues[solute_nresd:]: if status: status("Creating ions/solvent residue %d " % (r.id.position-solute_nresd) ) atomMap = {} nr = m.newResidue(r.type, ' ', 1, ' ') for a in r.atoms: na = addAtom(a.name, a.element, nr, a.coord(), serialNumber=a.serialNumber) na.charge = a.charge na.gaffType = a.mol2type if len(a.neighbors)==0: na.drawMode = chimera.Atom.Sphere atomMap[a] = na if a.name[0:2]=="Br": na.element = 35 elif a.name[0:2]=="Cl": na.element = 17 elif a.name[0:2]=="Cs": na.element = 47 elif a.name[0:2]=="Mg": na.element = 12 elif a.name[0:2]=="Na": na.element = 11 elif a.name[0:2]=="Rb": na.element = 48 elif a.name[0]=='F': na.element = 9 elif a.name[0]=='I': na.element = 53 elif a.name[0]=='K': na.element = 19 elif a.name[0]=="H": na.element = 1 elif a.name[0]=="C": na.element = 6 elif a.name[0]=="N": na.element = 7 elif a.name[0]=="O": na.element = 8 elif a.name[0]=="P": na.element = 15 elif a.name[0]=="S": na.element = 16 for a in r.atoms: na = atomMap[a] for n in a.neighbors: assert n.residue == r nn = atomMap[n] if nn in na.bondsMap: continue m.newBond(na, nn) _clean()