Skip to main content

Posts

Using Dictionary to edit ptraj input file

Recently I have been working on Linear combination of Distance (LCOD). One has to extract a set of structures (may vary from 100 to 1000 to any bigger number depending on requirement, accuracy) from a very long trajectory. If one is looking for very specific orientation, then it is necessary to go through the whole trajectory and choose the required frames and select that geometry, which is very tedious and time taking job. But more often a random selection from the trajectory is done to take care of all the diversity in the trajectory. In Amber, one can use ptraj to select a frame from trajectory and created its restart file to launch LCOD calculation. To launch such 100 or 1000 calculation one need to modify or create 100 or 1000 such input files to feed to ptraj with the frame number and the name of the restart file. I am using a dictionary in python to do so. Beginning with 2 lists: one having frame numbers to be selected another with name of the restart files correspondin...

Column subtractor - Python

I am still struggling with the after effects of Linux reinstallation on my system. Two major problems: 1) Firefox and openoffice is still not in English. Its giving me hard time. 2) The functions in openoffice  version of excel is not working. I am yet to sort that out. In the mean time, I desperately needed it to do simple task. subtracting values of one column from one in another column and write the difference in next column. So, I had input in this form: ------------------ 0    2.179139    0.951816 1    2.314531    1.006480 2    2.191692    0.951938 3    2.079704    0.952747 4    2.092761    0.939545 5    2.362056    0.984791 6    2.153595    0.957934 7    2.213173    0.951768 8    2.175687   ...
For some reason our administrator decided to update OS of my desktop. He installed latest version Ubuntu 11.10. My first impression was not very good. I did not find it userfriendly. Main problem is accessing different windows you open. Say, you open a firefox window with many work related tabs opned in it. This window will minimize to the left sidebar under firefox icon that also is the desktop icon to access firefox. Now, if you want open another firefox window which should have pages not work related but say personal like gmail or youtube, you click on the same icon. Instead of what you wish, the same firefox window with work related pages which you minimized few moments back reappears. So, to distinguish work related pages from personal pages is not so stright forward. One option is you open your personal page in one of the tabs in work related firefox window and then drag and drop that tab out of the window. Then it allows you to have another firefox window. But why is needs...

Gnuplot to plot histogram

GNUPLOT can be used to generate histograms. The script below allows one to plot variations in angle from 0° to 180°. Each histogram box width correspond to 3° (covered here under bw option). To execute this script; type this in the terminal: gnuplot histogram.p You should be in the folder where below script is saved in a file name "histogram.p" and a dat file (here, it is N---H-O.dat) should be present. histogram.p ------------------ set term gif set output "angle_N---H-O.gif" set xrange [0.0:180.0] set boxwidth 2.0 absolute set style fill solid 1.0 border -1 set xtics 10 set title "molecule 3.0A Angle N---H-O" set ylabel "occurance" set xlabel "angle" bw = 3   # substitute what you want bin(x,width)=width*floor(x/width) plot 'N---H-O.dat' using (bin($2,bw)):(1.0) smooth freq with boxes ------------------ The dat file is an input file with data in the format: 0    23.680344 1    73.766251 2    67.4...

Editing multiple files simultanoeusly

I needed to delete all the lines in a set of files except for for the first forty. Since the number of files was big, I used this script in python to do the job: -------------------------------- !/usr/bin/python #script to read files and delete lines import os listing=os.listdir(" /home/kjoshi/Documents/PY/useful/file_edit ") for infile in listing:  print infile  if infile == "files_line_delete.py":    print "This is NOT a good file"  else:   fi=open(infile).readlines()   fo=open(infile +'_output','w')   del fi[40: ]   fo.writelines(fi) --------------------------------- Task is simple. Keep all the files needed to be edited in one folder along with this script. Substitute the path shown in bold with proper path of your folder. Using command $python files_line_delete.py you get output files with each file having first forty lines.

Creating multiple folders and subfolders

The task was to create a set of folders say 1,2,3,4 and in each of these four folders create four sub folders. Thus 16 folders were to be created and in each of these 16 folders a common set of files were to be copied. I used c shell scripting to do the job. I needed two lists. One list for primary folders list1: 1 2 3 4 and another list for the subfolders. list2: 5 6 7 8 It worked as: ------------------- bash-3.2$ csh r410comp2% r410comp2%foreach f (`cat ../list1`) foreach? echo $f foreach? mkdir "$f" foreach? cd "$f" foreach? foreach g (`cat ../../list2`) foreach? echo $g foreach? mkdir "$f"_"$g" foreach? cd "$f"_"$g" foreach? cp /home/kjoshi/W/work/complex/2_ion/umbrella/6/6_75/u_6_75.in . foreach? cp /home/kjoshi/W/work/complex/2_ion/umbrella/6/6_75/amber11_intel_ref_md . foreach?cp /home/kjoshi/W/work/complex/2_ion/umbrella/6/6_75/2_ion.inpcrd . foreach?cp /home/kjoshi/W/work/complex/2_io...

Column exchange script in Python

This script allows one to exchange two columns in a file.And it do so for all the files in a directory. The python script is : ---------------------------- #script to read and execute column exchange for all files in the directory import os listing=os.listdir(" /home/K/Documents/PY/column_exchange ") for infile in listing:  print infile  if infile !="column_interchange_script.py":   fi=open(infile).readlines()   fo=open(infile +'_output','w')   space="   "   for line in fi:    first,second=line.split()    fo.write('%s%s%s\n'%(second,space,first))  else:    print "This is not interesting file"     ----------------------------------------------------    Some expert might simplify it a lot more, but for new python user like me, this is a big leap. So, how does it work: Create a directory where in all the files for which column exchange need to be done are stored. Kee...