Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. from tempfile import NamedTemporaryFile
  2.  
  3. class Pose:
  4.     def assign(self, other):
  5.         pass
  6.  
  7. def compute_similarity(pose, filespec):
  8.     pose_copy = Pose()
  9.     pose_copy.assign(pose)
  10.  
  11.     #Setup the 9-mer fragment (9-mer is better than 3-mer for this analysis)
  12.     fragset = ConstantLengthFragSet(9)
  13.     fragset.read_fragment_file(filespec)
  14.  
  15.     movemap = MoveMap()
  16.     movemap.set_bb(True)
  17.  
  18.     #Setup and apply the fragment inserting mover
  19.     mover = ClassicFragmentMover(fragset, movemap)
  20.     mover.apply(pose_copy, count)
  21.  
  22.     #Measure the RMSD difference between the original pose and the new changed pose (the copy)
  23.     difference = rosetta.core.scoring.CA_rmsd(pose , pose_copy)
  24.     return difference
  25.  
  26. def get_frag_suffix(position, fragment):
  27.     """Return a string matching the end of fragment lines for this pos/frag"""
  28.     return 'P{:3d} F{:3d}'.format(position, fragment)
  29.  
  30. def process_fragment(pose, tempfile, position, outfile):
  31.     """Process the fragment data in tempfile, set up for next fragment"""
  32.     tempfile.close()
  33.     diff = compute_similarity(pose, tempfile.name)
  34.     print(diff, position, sep='\t', file=outfile)
  35.  
  36. def RMSD(pose):
  37.     # Isolate fragments
  38.         rmsd = NamedTemporaryFile(mode='w', prefix='RMSD')
  39.         temp_fragfile = None
  40.  
  41.         with open('aat000_09_05.200_v1_3' , 'r') as frag:
  42.             for line in frag:
  43.                 if line.startswith(' position: '):
  44.                     # Start of a new position block
  45.  
  46.                     if temp_fragfile is not None:
  47.                         # Process end of prior position
  48.                         process_fragment(pose, temp_fragfile, position, rmsd)
  49.  
  50.                     pos_line = line
  51.                     position = int(line.split()[1])
  52.                     fragment = 1
  53.                     frag_suffix = get_frag_suffix(position, fragment)
  54.  
  55.                 elif line.strip() == '':
  56.                     continue
  57.  
  58.                 elif line.endswith(frag_suffix):
  59.                     print(line, file=temp_fragfile)
  60.  
  61.                 else:
  62.                     # End of previous fragment, start of a new one.
  63.                     process_fragment(pose, temp_fragfile, position, rmsd)
  64.  
  65.  
  66.     count = 0
  67.     for x in range (int(size)):
  68.         count +=1
  69.         fragnum = 0
  70.         for x in range(200):
  71.             frag = open('aat000_09_05.200_v1_3' , 'r')
  72.             temp = open('temp' , 'w')
  73.             for line in frag:
  74.                 if line.startswith(' position:{:13d}'.format(count)):
  75.                     temp.write(line)
  76.                     #print(line)
  77.             temp.write('\n')
  78.             fragnum += 1
  79.             frag = open('aat000_09_05.200_v1_3' , 'r')
  80.             for line in frag:
  81.                 if line.endswith('P{:3d} F{:3d}'.format(count , fragnum) + '\n'):
  82.                     temp.write(line)
  83.                     #print(line)
  84.                     sys.stdout.flush()
  85.             temp.write('\n')
  86.             temp.close()
  87.             frag.close()
  88.                         rmsd_diff = compute_similarity(pose, temp_fragfile.name)
  89.                         #print(RMSD , '\t' , count , '\t' , fragnum)
  90.                         rmsd.write(str(RMSD) + '\t' + str(count) + '\n')
  91.             #Reset the copy pose to original pose
  92.             pose_copy.assign(pose)
  93.             os.remove('temp')
  94.     rmsd.close()
  95.     #Analyse the RMSD file to get the lowest RMSD for each position
  96.     data = open('RMSDvsPosition.dat' , 'w')
  97.     lowest = {}                         #Mapping group number -> lowest value found
  98.     for line in open('RMSD.temp'):
  99.         parts = line.split()
  100.         if len(parts) != 2:             #Only lines with two items on it
  101.             continue
  102.         first = float(parts[0])
  103.         second = int(parts[1])
  104.         if first == 0:                  #Skip line with 0.0 RMSD (this is an error from the 9-mer fragment file). I don't know why it happens
  105.             continue
  106.         if second not in lowest:
  107.             lowest[second] = first
  108.         else:
  109.             if first < lowest[second]:
  110.                 lowest[second] = first
  111.     for position, rmsd in lowest.items():
  112.         print(str(rmsd) + '\t' + str(position))
  113.         data.write(str(position) + '\t' + str(rmsd) + '\n')
  114.     data.close()
  115.     os.remove('RMSD.temp')
  116.     gnuplot = open('gnuplot_sets' , 'w')
  117.     gnuplot.write("""set terminal postscript
  118. set output './plot.pdf'
  119. set encoding iso_8859_1
  120. set term post eps enh color
  121. set xlabel 'Position'
  122. set ylabel 'RMSD (\\305)'
  123. set yrange [0:]
  124. set xrange [0:]
  125. set xtics 1
  126. set title 'Fragment Quality'
  127. set key off
  128. set boxwidth 0.5
  129. set style fill solid
  130. plot 'RMSDvsPosition.dat' with boxes
  131. exit
  132. """)
  133.     gnuplot.close()
  134.     os.system('gnuplot < gnuplot_sets')
  135.     os.remove('gnuplot_sets')
  136. #----------------------------------------------------------------------
  137. pose = pose_from_pdb('structure.pdb')
  138. RMSD(pose)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement