Skip to main content

Posts

Showing posts with the label python

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   ...

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.

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...