Skip to main content

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.
Keep this python script also in the same directory.
The path shown in bold  need to be replaced with the proper path of the directory.
$python column_interchange_script.py command shoulf carry out column exchange for all the files in the directory.



One problem I was facing was the script was reading itself also and collapsing with an error. So I added exception using "!=" returning result for it with else statement.


One precaution: if you modify the script using editor, then it creates a temprary file column_interchange_script.py~ sometimes. This could create problem. script reads this file also and collapses. So either modify it in terminal or delete this file.

Why I needed this?
The Umbrella Sampling calculations yield files with values in two columns. For plotting individual histograms corresponding to each file we need to exchange columns. Number of such files might be large enough to motivate you to write a script to do so.
I have few things in mind to improvise on it, but since my task is fulfilled with this script, I might not modify it any further
 

Comments

Popular posts from this blog

Material Studio - selective molecule movement

So, working with Material Studio is awesome. You get to handle bigger, complicated and even unimaginable system. But I am facing problems in very minute functioning. I recently faced two problems, but managed to resolve on my own. Somehow, tutorials doesn't help in resolving them and you end up doing some exercise on your own to get the solution. Among these, the problem of bringing two molecular structures in one window and then adjusting one molecule by moving it around, while other structure remains untouched, as it is. Actually, this should not be a problem to do. There is a simple way of doing this. Select the molecule to be moved and then there is a symbol (button) to select, which allows you to selectively move that molecule only. This is the same way it can be done in Material studio too.  But among all the dozens of symbols, the new person handling MS, kind of gets lost. So, what he does is check the tutorials, for any help. But the tutorials don't talk about suc...

Conditions to use different Schrodinger Equation forms

Every molecular system has an energy associated with it. This energy might show a shift depending on the surrounding environment or internal variations. Magnetic dipoles present in a molecular system in the form of electrons and the nuclei is one such reason for the energy shift. But the effect of these magnetic dipoles on the energy shifts will be much less which can be accounted by considering perturbation theory. Chemical binding energies on the other hand are responsible for the major shifts in energy of a molecular system. Thus the focus is shifted towards the valence electrons responsible for most of the chemical activity of a molecule. The valence electrons are separated from the core electrons. The core electrons are treated approximately. This reduces the efforts of considering the relativistic effects affecting the core electrons, and thus can now be ignored. What are relativistic effect? It has been nicely explained by Ermler and Pitzer in the introduction of th...

Generating a structure PDB file using Gabedit

If Gabedit is installed and functioning on your system, type gabedit  in terminal. Gabedit will open and you can find an option "Geometry" in the tool bar. Select "Geometry"--> "Draw" After some trials and playing you will finally learn how to generate a structure. Once your structure is ready, you need to set the atom types and charges. For this click on "M" button in the top left of the "Draw Geometry" window and select options as: M-->"Set"--> "Atom Type & charge using PDB template" Here if you use "Atom Types using connection types", it helps. You can check if the atom type and charge has been set according to the amber requirements by an indirect method. Try to run a quick MM calculation. Run MM calculations by clicking: "M"-->Molecular Mechanics-->optimization It may sometimes end up with a pop up window that suggests that either atom type or charge is not...