def CHECK(self,Q,t,P): #/ -------------------------------------------------------------------------- x1 = Q[iX1] y1 = Q[iY1] x2 = Q[iX2] y2 = Q[iY2] x3 = Q[iX3] y3 = Q[iY3] x4 = Q[iX4] y4 = Q[iY4] rad1 = P[jR1] rad2 = P[jR2] rad3 = P[jR3] rad4 = P[jR4] r12 = rad1 + rad2 r13 = rad1 + rad3 r14 = rad1 + rad4 r23 = rad2 + rad3 r24 = rad2 + rad4 r34 = rad3 + rad4 dx12 = x1 - x2 dy12 = y1 - y2 dx13 = x1 - x3 dy13 = y1 - y3 dx14 = x1 - x4 dy14 = y1 - y4 dx23 = x2 - x3 dy23 = y2 - y3 dx24 = x2 - x4 dy24 = y2 - y4 dx34 = x3 - x4 dy34 = y3 - y4 if (t > 21912800.0): v2 = (Q[iVX4]*Q[iVX4]) + (Q[iVY4]*Q[iVY4]) if (v2 < 1.0e+9): cc = math.sqrt(v2) Q[iVX4] += Acc*Q[iVX4]/cc Q[iVY4] += Acc*Q[iVY4]/cc self.iCount += 1 self.tCount += AccInc print fpformat.sci(Q[iX4],5), fpformat.sci(Q[iY4],5), fpformat.sci(Q[iVX4],5), fpformat.sci(Q[iVY4],5), fpformat.sci(Acc,5), fpformat.sci(cc,5) rv = 0 if ((r12*r12) > ((dx12*dx12)+(dy12*dy12))): rv = 1 elif ((r13*r13) > ((dx13*dx13)+(dy13*dy13))): rv = 2 elif ((r14*r14) > ((dx14*dx14)+(dy14*dy14))): rv = 3 elif ((r23*r23) > ((dx23*dx23)+(dy23*dy23))): rv = 4 elif ((r24*r24) > ((dx24*dx24)+(dy24*dy24))): rv = 5 elif ((r34*r34) > ((dx34*dx34)+(dy34*dy34))): rv = 6 return rv
def test_failing_values(self): # Now for 'unreasonable n and d' self.assertEquals(fix(1.0, 1000), '1.' + ('0' * 1000)) self.assertEquals(sci("1" + ('0' * 1000), 0), '1e+1000') # This behavior is inconsistent. sci raises an exception; fix doesn't. yacht = "Throatwobbler Mangrove" self.assertEquals(fix(yacht, 10), yacht) try: sci(yacht, 10) except NotANumber: pass else: self.fail("No exception on non-numeric sci")
def test_failing_values(self): # Now for 'unreasonable n and d' self.assertEquals(fix(1.0, 1000), '1.'+('0'*1000)) self.assertEquals(sci("1"+('0'*1000), 0), '1e+1000') # This behavior is inconsistent. sci raises an exception; fix doesn't. yacht = "Throatwobbler Mangrove" self.assertEquals(fix(yacht, 10), yacht) try: sci(yacht, 10) except NotANumber: pass else: self.fail("No exception on non-numeric sci")
def outputStdDataFile(file,tags,data,stepRanges): """ This function takes the contents of tags and data, and outputs them to a std format *prn file. This is for testing purposes. """ filename = "test_" + file print "\nCreating std format output file: " + filename output = open(filename,'w') tagstring = "Index " numvars = len(tags) for i in range(numvars): tagstring += tags[i] + " " tagstring += "\n" output.write(tagstring) sizeTmp = len(stepRanges) size = stepRanges[sizeTmp-1]+1 for i in range(size): numberstring = str(i) + " " for j in range(numvars): numberstring += str(fpformat.sci(data[i,j],8)) + " " numberstring += "\n" output.write(numberstring) finalstring = "End of Xyce(TM) Simulation\n" output.write(finalstring) return
def checkSci(self, n, digits): result = sci(n, digits) if isinstance(n, StringType): n = repr(n) expected = "%.*e" % (digits, float(n)) # add the extra 0 if needed num, exp = expected.split("e") if len(exp) < 4: exp = exp[0] + "0" + exp[1:] expected = "%se%s" % (num, exp) self.assertEquals(result, expected)
def parseLatLong(latlong): """ Parse Lat or Long in METAR notation into float values. N and E are +, S and W are -. Expects one positional string and returns one float value. """ # I know, I could invert this if and put # the rest of the function into its block, # but I find it to be more readable this way if latlong is None: return None s = latlong.upper().strip() elms = s.split('-') ud = elms[-1][-1] elms[-1] = elms[-1][:-1] elms = [int(i) for i in elms] coords = 0.0 elen = len(elms) if elen > 2: coords = coords + float(elms[2]) / 3600.0 if elen > 1: coords = coords + float(elms[1]) / 60.0 coords = coords + float(elms[0]) if ud in ('W', 'S'): coords = -1.0 * coords f, i = math.modf(coords) if elen > 2: f = float(fpformat.sci(f, 4)) elif elen > 1: f = float(fpformat.sci(f, 2)) else: f = 0.0 return f + i
def parseLatLong(latlong): """ Parse Lat or Long in METAR notation into float values. N and E are +, S and W are -. Expects one positional string and returns one float value. """ # I know, I could invert this if and put # the rest of the function into its block, # but I find it to be more readable this way if latlong is None: return None s = latlong.upper().strip() elms = s.split('-') ud = elms[-1][-1] elms[-1] = elms[-1][:-1] elms = [int(i) for i in elms] coords = 0.0 elen = len(elms) if elen > 2: coords = coords + float(elms[2])/3600.0 if elen > 1: coords = coords + float(elms[1])/60.0 coords = coords + float(elms[0]) if ud in ('W', 'S'): coords = -1.0*coords f, i = math.modf(coords) if elen > 2: f = float(fpformat.sci(f, 4)) elif elen > 1: f = float(fpformat.sci(f, 2)) else: f = 0.0 return f+i
def parseLatLong(self, latlong): """ Parse Lat or Long in METAR notation into float values. N and E are +, S and W are -. Expects one positional string and returns one float value. """ # I know, I could invert this if and put # the rest of the function into its block, # but I find this to be more readable if latlong is None: return None s = string.strip(string.upper(latlong)) elms = string.split(s, "-") ud = elms[-1][-1] elms[-1] = elms[-1][:-1] elms = map(string.atoi, elms) coords = 0.0 elen = len(elms) if elen > 2: coords = coords + float(elms[2]) / 3600.0 if elen > 1: coords = coords + float(elms[1]) / 60.0 coords = coords + float(elms[0]) if ud in ("W", "S"): coords = -1.0 * coords f, i = math.modf(coords) if elen > 2: f = float(fpformat.sci(f, 4)) elif elen > 1: f = float(fpformat.sci(f, 2)) else: f = 0.0 return f + i
def test_basic_cases(self): self.assertEquals(fix(100.0 / 3, 3), '33.333') self.assertEquals(sci(100.0 / 3, 3), '3.333e+001')
def mapPeptides2STRING(blastDir, organism, fastafilename, id_pos_res, id_seq, number_of_processes, datadir, fast=False, leave_intermediates=False): sys.stderr.write("Mapping using blast\n") incoming2string = {} string2incoming = {} # Speedup, only blast sequences with site specified tempfile.tempdir = '/output' blast_tmpfile = tempfile.NamedTemporaryFile() #f = open('/output/blast_tmp', 'wb') if id_pos_res == {}: for id in id_seq: wr = '>' + id + '\n' + id_seq[id] + '\n' blast_tmpfile.write(wr) else: for id in id_pos_res: try: wr = '>' + id + '\n' + id_seq[id] + '\n' blast_tmpfile.write(wr) #print(wr) #f.write(wr) except: sys.stderr.write("No sequence available for '%s'\n" % id) blast_tmpfile.flush() blastDB = os.path.join(datadir, "%s.protein.sequences.fa" % (organism)).replace( ' ', '\\ ') # print("blastdb path is" + blastDB) # Check if blast database is actually initialized, if not: do it if not os.path.isfile(blastDB + '.pin'): #print("Is this it??") command = "%s/formatdb -i %s" % (blastDir.rsplit("/", 1)[0], blastDB) sys.stderr.write( "Looks like blast database is not initialized, trying to run:\n%s\n" % command) myPopen(command) command = "%s -a %s -p blastp -e 1e-10 -m 8 -d %s -i %s | sort -k12nr" % ( blastDir, number_of_processes, blastDB, blast_tmpfile.name) #print(blastDir, blastDB, '/output/blast_tmp') #if os.path.isfile(blast_tmpfile.name): #print('File exists') # to save time - jhkim if fast and os.path.isfile(fn_blast_output): #print('fast') blast_out = ReadLines(fn_blast_output) else: #print('mopen') blast_out = myPopen(command) if leave_intermediates: WriteString(fn_blast_output, "".join(blast_out)) #print(blast_out) for line in blast_out: tokens = line.split('\t') incoming = tokens[0] if incoming not in incoming2string: # get rid of organism prefix string = tokens[1].replace("%s." % organism, "") identity = float(tokens[2]) evalue = float(tokens[-2]) if string in string2incoming: sys.stderr.write('Best hit for ' + incoming + ' is not reciprocal\n') if identity < 90: sys.stderr.write('Best hit for ' + incoming + ' has only ' + fpformat.fix(identity, 2) + ' %identity\n') if evalue > 1e-40: sys.stderr.write('Best hit for ' + incoming + ' has high E-value ' + fpformat.sci(evalue, 2) + ' \n') if incoming in incoming2string: incoming2string[incoming][string] = True else: incoming2string[incoming] = {string: True} if string in string2incoming: string2incoming[string][incoming] = True else: string2incoming[string] = {incoming: True} else: pass return incoming2string, string2incoming
'''
def mapPeptides2STRING(blastDir, organism, fastafilename, id_pos_res, id_seq, number_of_processes, datadir, fast = True, leave_intermediates = False): sys.stderr.write("Mapping using blast\nLength id_seq: %s\n"%len(id_seq)) incoming2string = {} string2incoming = {} #for id in id_seq: # sys.stderr.write("ID_Seq: " + id + '\n') # Speedup, only blast sequences with site specified sys.stderr.write("tempfile: %s"%tempfile) #import pdb; pdb.set_trace() blast_tmpfile = tempfile.NamedTemporaryFile() #blast_tmpfile = "/home/gary/NetwoKINtmp/tmpXIao_unzip.txt" if id_pos_res == {}: for id in id_seq: blast_tmpfile.write('>'+id+'\n'+id_seq[id]+'\n') else: for id in id_pos_res: try: blast_tmpfile.write('>'+id+'\n'+id_seq[id]+'\n') except: sys.stderr.write("No sequence available for '%s'\n"%id) blast_tmpfile.flush() # blastDB = os.path.join(datadir, "%s.protein.sequences.fa"%(organism)).replace(' ', '\\ ') #Changed blastDB = "/home/gary/Documents/20180925_NetworKIN_Hornberger_RapMIC_Redo/NetworKIN_release3.0/MouseProteome_20150814_noIsoforms_HumanEnsembl74_noNAs.fasta" #sys.stderr.write("blastDB is initialized: %s"%os.path.isfile(blastDB+'.pin')) # Check if blast database is actually initialized, if not: do it if not os.path.isfile(blastDB+'.pin'): command = "%s/formatdb -i %s"%(blastDir.rsplit("/", 1)[0], blastDB) sys.stderr.write("Looks like blast database is not initialized, trying to run:\n%s\n"%command) myPopen(command) savefile = True if savefile: command = "%s -a %s -p blastp -e 1e-10 -m 8 -d %s -i %s | sort -k12nr -> blast_out_Ensembl74.txt"%(blastDir, number_of_processes, blastDB, blast_tmpfile.name) else: command = "%s -a %s -p blastp -e 1e-10 -m 8 -d %s -i %s | sort -k12nr"%(blastDir, number_of_processes, blastDB, blast_tmpfile.name) sys.stderr.write("\nBlast command: %s\n"%command) # to save time - jhkim # TRUE if you want to use the existing Blast output file if True and os.path.isfile("/home/gary/Documents/20180925_NetworKIN_Hornberger_RapMIC_Redo/NetworKIN_release3.0/blast_out_Ensembl74.txt"): sys.stderr.write("Reading Blast Output\n") blast_out = ReadLines("/home/gary/Documents/20180925_NetworKIN_Hornberger_RapMIC_Redo/NetworKIN_release3.0/blast_out_Ensembl74.txt") else: sys.stderr.write("Performing blast search\n") sys.stderr.write("fn_blast_output: %s"%fn_blast_output) blast_out = myPopen(command) if leave_intermediates: WriteString(fn_blast_output, "".join(blast_out)) sys.stderr.write("Length blast_out: %s\n"%len(blast_out)) for line in blast_out: #sys.stderr.write(":%s\n"%line) if not line: sys.stderr.write("Empty line detected\n") continue tokens = line.split('\t') if not len(tokens) > 5: continue incoming = tokens[0] if incoming not in incoming2string: # get rid of organism prefix string = tokens[1].replace("%s."%organism, "") identity = float(tokens[2]) evalue = float(tokens[-2]) if string in string2incoming: sys.stderr.write('Best hit for '+incoming+' is not reciprocal\n') if identity < 90: sys.stderr.write('Best hit for '+incoming+' has only '+fpformat.fix(identity, 2)+' %identity\n') if evalue > 1e-40: sys.stderr.write('Best hit for '+incoming+' has high E-value '+fpformat.sci(evalue, 2)+' \n') if incoming in incoming2string: incoming2string[incoming][string] = True else: incoming2string[incoming] = { string: True } if string in string2incoming: string2incoming[string][incoming] = True else: string2incoming[string] = { incoming: True } else: pass sys.stderr.write("Length incoming2string: %s\n"%len(incoming2string)) sys.stderr.write("Length string2incoming: %s\n"%len(string2incoming)) return incoming2string, string2incoming
def test_basic_cases(self): self.assertEquals(fix(100.0/3, 3), '33.333') self.assertEquals(sci(100.0/3, 3), '3.333e+001')