Thursday, June 25, 2015

Batch script to automate simulations in MATLAB

Many of us use MATLAB to perform simulations. Often such simulations requires multiple runs of the same program. Suppose we are simulating an ODE -based model for a cell signaling pathway. We have created a main function (say Main.m) that execute the simulation while calling and using other functions. We want to simulate the same model with large number of different values of a particular parameter. One way of doing it, is to modify the code itself to run multiple simulations with different sets of parameter values. But, I would prefer not to tamper with the code. The other option is to change the parameter value and run the simulations one after another. That's bit cumbersome and unprofessional. Can't we automate the process?

Yes, we can. Don't hard code the parameters in the main code. Let it read the parameter values from a separate file. That can be a text or m-file. Make multiple folders having all the codes and parameter file. Change the values in the parameter files in these folders. Now use a script to run the simulation from each of these folder, one after another. Note that each of the sub-folder should have all the codes to run the simulations independently.

Even, you can use this strategy to, sequentially, run completely different simulations from different sub-folders. The only requirement is that you have to use same name (Main.m) for all the main functions. 

The batch script written bellow does exactly that. My PhD student Vimalathithan wrote the script. If you find it useful, do give him the credit.

This may not be the only way or best way to automate multiple runs. Please do write back, with your take on this issue.

batch_script.bat
:: -The script automates sequential repeated run of same Matlab code. 
:: -The user should enter the location of the parent folder which contains the Sub models
:: -The user should enter the location of matlab.exe file
:: -Every event is recorded in a LogFile.txt in the parent directory
:: -The scripts does the following job 
::  1.Creates a text file holding the name of the sub folders present in the parent directory
::  2.Executes each sub folder by invoking the main function (Main.m)
::  3. If the function is not found, the error message is recorded and the loop continues with the next sub folder.
::  4.If matlab throws any error, the error message is recorded and the loop continues with the next sub folder
::-------------------------------------------------------------------------------------------------------------------------
::Enter the location of the Model 
set ModelDir=D:\Dropbox\modeling_tool\MATLAB_vimal\ODEmodeling\ODE1

::Enter the location of the Matlab.exe file
set MatlabDir=C:\matlab\bin\matlab.exe
::-------------------------------------------------------------------------------------------------------------------------
::Do not make any changes in the code below
::-------------------------------------------------------------------------------------------------------------------------
cd %ModelDir%
Echo Start time is %date%_%time%>LogFile.txt
::Lists the folders in the current directory and writes them in a text file
dir /A:D /O:N /B>List.txt
setlocal enableextensions enabledelayedexpansion
Echo off
::Counts the number of lines in the text file
for /F %%A in ('type "List.txt"^|find "" /V /C') do (
 set /A count=%%A
)
set /A num=1
::Executes the matlab code present in each sub folder
for /F "tokens=*" %%A in  (List.txt) do (
 Echo.>>%ModelDir%\LogFile.txt
 Echo Executing !num!-%%A of %count%
 cd %ModelDir%\%%A
 IF EXIST ErrorFile.txt (
  del ErrorFile.txt
 )
 IF EXIST %ModelDir%\%%A\Main.m (
  "%MatlabDir%" -nodisplay -nosplash -nodesktop -wait -r "run('Main.m');exit;"
  IF EXIST %ModelDir%\%%A\ErrorFile.txt (
   Echo %%A is not executed>>%ModelDir%\LogFile.txt
   Echo WARNING. Matlab has logged an error file in the location %ModelDir%\%%A>>%ModelDir%\LogFile.txt
  )
  IF NOT EXIST %ModelDir%\%%A\ErrorFile.txt (
  Echo %%A is executed successfully>>%ModelDir%\LogFile.txt
  )
 ) 
 IF NOT EXIST %ModelDir%\%%A\Main.m (
  Echo %%A is not executed>>%ModelDir%\LogFile.txt
  Echo WARNING. Main function is not found in the location %ModelDir%\%%A>>%ModelDir%\LogFile.txt
 )
 set /A num+=1
 cd %ModelDir%
)
endlocal
Echo.>>%ModelDir%\LogFile.txt
Echo End time is %date%_%time%>>LogFile.txt
::Deletes the List.txt file
del List.txt
Echo on

Monday, January 26, 2015

How to stay updated with latest literature

When I was doing Ph. D, online publishing of scientific journals had just learned walking. We were more dependent on hard copies of journals in libraries. For many of us, it used to be a ritual to regularly visit the library and flip through the pages of our favorite journals, to catch the new and relevant to our work. But, the Internet has changed the way we access journals now. It has democratized access to knowledge and also overwhelmed us with information. Hundreds of papers get published every day. Though you may have access to most of those, it’s difficult to keep track of papers of interest. Thankfully, there are tools that can help you to keep you informed about the latest literature in your field. 

E-mail alert by PubMed: NCBI PubMed has changed the way we do biomedical research. You must be searching this database at least once every day. Do you know that you can make an personal account with NCBI to save your searches? Visit MyNCBI page to make an account. Log in your account. Perform a search using a keyword suitable for your research. This time you will get a link for "Save Search", near the top of the search results page. Click it. It will take you to a page to save your search result with option to activate email alert for new results on this key word. You can select the frequency of such update. I select to get update daily, so that whenever there is a new paper, I get informed immediately. You can anytime come back to your MyNCBI page and edit your saved searches. The key of this email alert is the key word(s) used for the search. Therefore, you have to be very careful in choosing them. Use different key words and create saved searches for those. Subsequently, based on the email alerts, you may change the keywords. 

Twitter: Do you Tweet. No am not asking you to use Twitter to add spice to your social life. You can use it as a powerful tool to keep updated with what's happening in your field of study. Most of the reputed journals use social media, including Twitter, to spread news about important papers published by those. Even, scientific associations use Twitter to announce breakthrough in specific fields. Even many scientists use regularly Tweet about latest papers published by them and their peers. So join Twitter. Find out Journals and organizations of your interests in Twitter. Start following them. See if any of your peers is there in Twitter. Follow them too. Then make a habit to scroll through the tweets once daily. And don’t be just an observer. Reciprocate. Tweet about your new publication. Tweet about a recent paper that you found interesting. Build a social network around your science. 

Faculty of 1000: This is what you have always dream of. A bunch of experts reading and flagging latest literature for you. Obviously, this does come with a cost. Subscribe to F1000Prime. Choose particular topics of your interest and even experts to follow from the list. F1000 will send you email alerts whenever your experts review a brilliant paper of your interest.  One day, maybe your paper will also get reviewed by F1000 and get the prestigious badge of F1000 in the PubMed. Till then enjoy reading.

Saturday, November 01, 2014

Invisible co-passengers in your life

You think it's your body. You touch it, see it,  feel it and appreciate it. Looking at the anatomical pictures of  internal parts, you feel amazed about the complexity of your body. But is everything in your body yours? Count the cells. You will realise that majority of the cells, you carry, are not yours. Those are diverse microorganisms, mostly bacteria. According to a rough estimate, a person, having 40 lakh crore cells, caries 400 lakh crore microbes. In other words, we are ten percent human. Almost three percent of our weight comes from these tiny creatures. They are present on our skin, in cavities, even in internal organs like lung and gut. The womb that was once considered to be a sterile zone, also harbours microorganisms. All this microorganisms, in our body, as a whole constitute human microbiome. A hidden world within us.

It has long been known that our gut have bacteria that help us in various ways, from providing micronutrients like vitamins to chasing out bad microbes. They are commonly called commensals. Many of these bacteria are different types of lactobacillus, similar to the one that we use to make curd. In fact, consumption of curd is an way to supplement the existing microbes within your gut. Prolonged use of antibiotics destroys microbes in the gut, even the helpful ones. That's why patients are advised to consume probiotics as supplement. Probiotics are live microorganisms that are helpful for our health. 

Our microbiome is like a cocktail. It’s  difficult to identify and study individual microbes in it. Now, technologies exist to identify large number of different microorganisms in a mixture, in short time, using their genetic fingerprints. Such technologies have invigorated research in this field. There is also a philosophical shift among biologists. The time of over-emphasis on genes of an individual is over. Now biologists realise that we need to take the environment  into consideration to understand complexities of human physiology and diseases. The Microbiome is our immediate environment. The way we manipulate the nature around us, these microorganisms also manipulate us. And we need to understand their actions on us to maintain our health.

Take the problem of obesity. Its becoming an epidemic, even in our country. Your physician, friendly experts and the advertisements of fitness clinics must have now convinced you that our food habit and lifestyle are making us obese. Thats correct, to a large extent. But those microbes in your gut also play a role in this. Many microbes in our gut help in harvesting energy from food and affect metabolism. Therefore, variation in the microbial ecology of our gut can affect our body weight. Studies have shown that the microbiome of obese individuals is different from that of lean. In fact, one study has even shown that response to a weight-loss program has relation to individuals gut microbes. Animal studies have proved that by manipulating gut microbiome we may control obesity. One way to achieve that is to transplant a right cocktail of microorganisms in our gut. Even selective foods or antibiotics can be used to manipulate the microbial ecology. However, such works are still experimental and you have to wait some more time to add a cocktail of microbes in your fiber-rich weight-loss diet.  

But experiments are not limited to obesity. Transplantation of microbes is helping in fight against a dreaded infection. Clostridium difficile is a bacteria that causes severe diarrhea, particularly in patients using antibiotics. In many cases treatment with antibiotics does not help. Loss of friendly microbes in the gut aggravate this. Infusion, made out of stool from a healthy donor, given like an enema helps in reconstructing the lost microbial world in the gut and that contains this drug-resistant disease. Such stool transplantation has been experimentally used even in other diseases like irritable bowel syndrome.  Everyday, scientists are discovering connections between microbiome and other diverse diseases like asthma in children, autism, arthritis and even cardiovascular diseases. In fact many people believe that improved sanitation in the West is reducing childhood exposure to microorganisms, tweaking their microbiome and making them more susceptible to various diseases.

Our journey with microorganisms starts in the mother’s womb. The process of birth also adds, as microbes present in the vagina get transfered.  In fact, microbiome of children born by cesarean delivery differs from those born by normal way. After the birth, a child's microbiome is shaped by the microbes present in mothers milk. Subsequently, it keeps changing with our food habits, environment, use of antibiotics, interactions with people and pets. The hidden world within us is dynamic and keeps interacting with us. Just like the ecosystem outside, our technology and lifestyle also affects the microbial ecology within.  The biology of this ecosystem is little known. For long we have been self-centered and have written our own travelogues. Now we are learning to recognize and appreciate these invisible co-passengers of our life. Surely you will love them as much you love the mole on your cheek.