Shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripting
# 1  
Old 11-06-2010
Shell scripting

The two shell scripts (t1prog and t2prog) are given below they are working fine. The input for the first program is 't1.det' and for second program is 't1.rnaml'. These two input files are in 'dir1' folder. I am executing the shell like 'sh t1prog > t1out' and 'sh t2prog > t2out' from this directory only. Then I am executing a java program 'java RNA'; for this, t1out and t2out are input files used in the program and I am getting the final output on screen.
The input files 't1.det' and 't1.rnaml' are in different folders with same name and with different values. Each folder specifies one gene sequence input files.
In mfold directory there are 5 directors and each directory contains these input files as shown below
Code:
 
cd mfold
dir1  dir2  dir3  dir4  dir5
 
cd dir1
t1.det t1.rnaml
cd ..
------
cd dir5
t1.det t1.rnaml

I need a shell script to automate this process like t1prog and t2prog first take the inputs from dir1 folder, run the shell and redirect the output to t1out and t2out and after executing the java program with statment 'java RNA' the output will be redirected to a file like 'RNAout1'. Then takes the inputs from dir2 folder, same process again, and redirect the output to 'RNAout2'. Like this for all inputs values from 5 folders. Modify the two shell scripts given below, to accept the inputs in that way (i think last line of each script to be modified).

shell 1: t1prog
Code:
1. #!/bin/bash
2. PrintVal() { C=${C#*(}; C=${C%)*}; echo $C | sed 's/).*( /-/'; } 
3. while IFS=':=' read A B C
4. do
5. case $(echo $A) in
6. Initial*)
7. ((NotFirst)) && { echo; echo; } || NotFirst=1
8. echo ${B#* }; echo
9. Hairpins=0
10. ;;
11. 
12. Hairpin*)
13. ((Hairpins==0)) && echo "$A:"
14. PrintVal
15. ((Hairpins++))
16. ;;
17. Multi-loop)
18. echo "$A:"
19. PrintVal
20. ;;
21. esac
22. done <t1.det

shell 2: t2prog
Code:
#!/bin/sh
i=1
while read line
do
freecount=`echo "${line}" | grep -i "free" | wc -l`
poscount=`echo "${line}" | grep -i "position" | wc -l`
if [ $freecount -eq 1 ]
then
rangeoutput=0
else
rangeoutput=1
fi
if [ $rangeoutput -eq 0 ]
then
value=`echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p'`
echo $value
else
if [ $poscount -eq 1 ]
then
pvalue[$i]=`echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p'`
if [ $i -eq 2 ]
then
echo "${pvalue[1]} - ${pvalue[2]}"
i=0
fi
i=`expr $i + 1`
fi
fi
done <t1.rnaml

for one input set in 'dir1' i am executing like this
Code:
sh t1prog > t1out
sh t2prog > t2out
java RNA

for inputs in different directories and executing these and redirecting the final ouput after executing 'java RNA' statement to a file is needed.
WRITING A SHELL SCRIPT FOR THIS IS HIGHLY APPRCIATED. THANKS IN ADVANCE.

Moderator's Comments:
Mod Comment Use code tags, please...

Last edited by Scott; 11-07-2010 at 06:01 AM..
# 2  
Old 11-07-2010
Please, use code tags to make the code more readable.
Howto:
  1. Select the text corresponding to code
  2. Click on the 'code' button in the editor's toolbarImage
The t2prog can really be optimized. Here is my proposition for the same functionality:

bash code:
  1. #!/bin/bash
  2. i=1
  3. while read line
  4. do
  5.    if echo "${line}" | grep -i "free"
  6.    then
  7.       value=$(echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p')
  8.       echo $value
  9.    elif echo "${line}" | grep -i "position"
  10.    then
  11.       pvalue&#91;$i]=$(echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p')
  12.       ((i==2)) && { echo "${pvalue[1]} - ${pvalue[2]}"; i=0; }
  13.       ((i++))
  14.    fi
  15. done <t1.rnaml
For the script that would wrap the whole process, that's my proposition:
Code:
#!/bin/bash
cd mfold
for Dir in Dir*
do
   cd $Dir
   t1.det >t1out && t1.rnaml>t2out
   java RNA
   cd ..
done

If You want just one script to perform that, use functions and the script would look like:
Code:
#!/bin/bash
ParseT1()   { 
   # PASTE THE CODE OF FIRST SCRIPT
}
ParseT2()   {
   # PASTE THE CODE OF SECOND SCRIPT
}
cd mfold
for Dir in dir* # or just * if there are only subdirectories
do
   cd $Dir
   ParseT1 >t1out  && ParseT2 >t2out
   java RNA
   cd ..
done


Last edited by frans; 11-07-2010 at 04:04 AM.. Reason: forgot java RNA
This User Gave Thanks to frans For This Post:
# 3  
Old 11-08-2010
shell scripting

Dear Sir

I used the second code using Parse T1 and Parse T2. After execution of that code output files 't1out' and 't2out' are generated in sub directories 'dir1' and 'dir2' but final output is not coming and giving the following errors. I am giving Java code also for information. . When executing t1prog, t2prog followed by JAVA, I am getting output for a single data set like 'sh t1prog > t1out; sh t2prog > t2out; java RNA'. For this, it is giving errors, I am in confusion.


Code:
 
Exception in thread "main" java.lang.NoClassDefFoundError: RNA
Caused by: java.lang.ClassNotFoundException: RNA
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

The above set of errors are coming for each 'dir'. If two direcotries are there 'dir1' and 'dir2'; two times the same set of errors coming and finally getting the $ prompt.


Code:
 
import java.io.*;
import java.util.*;
public class RNA
{
public static final String inputFile1 = "t1out";
public static final String inputFile2 = "t2out";
 
private Hashtable rangeTable = new Hashtable();
private ArrayList multiloopList = new ArrayList();
private ArrayList hairPinList = new ArrayList();
private int STARTRANGE = -1;
private int DEFAULTSTARTRANGE = 40;
private static final boolean DEBUG = false;
 
public RNA()
{
}
public RNA(int STARTRANGE)
{
this.STARTRANGE = STARTRANGE;
if(STARTRANGE == -1) this.STARTRANGE = DEFAULTSTARTRANGE;
}
 
private void init() throws IOException
{
FileReader fileReaderObj = new FileReader(inputFile2);
BufferedReader bufferReaderObj = new BufferedReader(fileReaderObj);
String line = null;
line = bufferReaderObj.readLine();
String saveLine = null;
boolean advanced = false;
while(line != null)
{
advanced = false;
if (line.trim().startsWith("-"))
{
saveLine = line.trim();
String multiloop = new String();
if(DEBUG) System.out.println(saveLine);
while (((line = bufferReaderObj.readLine()) != null) && !line.trim().startsWith("Hairpin loop:"))
{
if (line.trim().equals("Multi-loop:"))
{
multiloop = bufferReaderObj.readLine();
multiloopList = new ArrayList();
multiloopList.add("Multi-loop:");
multiloopList.add(multiloop);
if(DEBUG) System.out.println("multiloop--->"+multiloop);
}
}
if(line.trim().equals("Hairpin loop:"))
{
multiloopList.add("Hairpin loop:");
while (((line = bufferReaderObj.readLine()) != null) && !line.trim().startsWith("-"))
{
advanced = true;
if(line.trim().length() > 0)multiloopList.add(line);
if(DEBUG) System.out.println("Hairpin loop--->"+line);
 
}
if(DEBUG) System.out.println("Hairpin loop size--->"+multiloopList.size());
}
 
}
rangeTable.put(saveLine,multiloopList);
if(!advanced )
{
line = bufferReaderObj.readLine();
//System.out.println("advance-->"+line);
}
}
}
 
public void compare() throws IOException
{
FileReader fileReaderObj = new FileReader(inputFile1);
BufferedReader bufferReaderObj = new BufferedReader(fileReaderObj);
boolean advanced = false;
String line = null;
line = bufferReaderObj.readLine();
while(line != null)
{
advanced = false;
System.out.println(line);
if(line.trim().startsWith("-"))
{
List list = (ArrayList)rangeTable.get(line.trim());
while(((line = bufferReaderObj.readLine()) != null) && !line.trim().startsWith("-"))
{
String []result = line.trim().split("-");
int left = Integer.parseInt(result[0].trim());
int right = Integer.parseInt(result[1].trim());
if(left > STARTRANGE && (right - left) > 20)
{
String multiLoopRange = (String)list.get(1);
String []result1 = multiLoopRange.trim().split("-");
int multiLoopLeft = Integer.parseInt(result1[0].trim());
int multiLoopRight = Integer.parseInt(result1[1].trim());
if(left > multiLoopLeft)
{
for(int i = 3; i < list.size(); i++)
{
String hairPinValue = (String)list.get(i);
String []hairPinresult = hairPinValue.trim().split("-");
int hairPinleft = Integer.parseInt(hairPinresult[0].trim());
int hairPinRight = Integer.parseInt(hairPinresult[1].trim());
if(left < hairPinleft && right > hairPinRight)
{
int microRNAleft = left + 21;
if (microRNAleft <= hairPinleft)
{
System.out.println("MIRNA--"+ left +":"+microRNAleft);
}
int microRNAright = hairPinRight + 1 + 21;
if(microRNAright <= right)
{
System.out.println("MIRNA--"+ (hairPinRight+1)+":"+microRNAright);
}
}
}
}
 
}
advanced = true;
}
}
if(!advanced) line = bufferReaderObj.readLine();
}
}
public static void main(String []r) throws Exception
{
System.out.println("Enter the Starting Micro RNA range to be computed...");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String sInputRange = in.readLine();
int inputRange = -1;
try
{
inputRange = Integer.parseInt(sInputRange);
}
catch(Exception e)
{
System.out.println("Invalid input...computing with DEFAULT Micro RNA Range 40.");
inputRange = -1;
}
RNA rna = new RNA(inputRange);
rna.init();
rna.compare();
}
}

[/CODE]
Quote:
Originally Posted by frans
Please, use code tags to make the code more readable.
Howto:
  1. Select the text corresponding to code
  2. Click on the 'code' button in the editor's toolbarImage
The t2prog can really be optimized. Here is my proposition for the same functionality:

bash code:
  1. #!/bin/bash
  2. i=1
  3. while read line
  4. do
  5. if echo "${line}" | grep -i "free"
  6. then
  7. value=$(echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p')
  8. echo $value
  9. elif echo "${line}" | grep -i "position"
  10. then
  11. pvalue&#91;$i]=$(echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p')
  12. ((i==2)) && { echo "${pvalue[1]} - ${pvalue[2]}"; i=0; }
  13. ((i++))
  14. fi
  15. done <t1.rnaml
For the script that would wrap the whole process, that's my proposition:
Code:
#!/bin/bash
cd mfold
for Dir in Dir*
do
   cd $Dir
   t1.det >t1out && t1.rnaml>t2out
   java RNA
   cd ..
done

If You want just one script to perform that, use functions and the script would look like:
Code:
#!/bin/bash
ParseT1()   { 
   # PASTE THE CODE OF FIRST SCRIPT
}
ParseT2()   {
   # PASTE THE CODE OF SECOND SCRIPT
}
cd mfold
for Dir in dir* # or just * if there are only subdirectories
do
   cd $Dir
   ParseT1 >t1out  && ParseT2 >t2out
   java RNA
   cd ..
done


Last edited by kswapnadevi; 11-08-2010 at 09:48 PM.. Reason: modified
# 4  
Old 11-18-2010
shell scripting

The optimized code t2prog is executed. When I am working with real datasets it consists of additional data values in the tag like <secondary-structure-display> .....</secondary-structure-dispaly> that are also coming in output. For each sequence of data, these secondary structure is also there. This secondary structure not taking into the consideration for output sir. I am giving the additional part of the data part in input file that is to be ommitted. The given shell script 't2prog' to be modified sir.

Code:
 
<secondary-structure-display>
<ss-base-coord>
<base-id>
<position>1</position>
</base-id>
<coordinates>367.39 547.83</coordinates>
</ss-base-coord>
<ss-base-coord>
<base-id>
<position>2</position>
</base-id>
<coordinates>364.6 563.78</coordinates>
</ss-base-coord>
<ss-base-coord>
<base-id>
<position>3</position>
</base-id>
<coordinates>361.82 579.74</coordinates>
</ss-base-coord>
<ss-base-coord>
<base-id>
<position>4</position>
</base-id>
<coordinates>359.04 595.69</coordinates>
</ss-base-coord>
<ss-base-coord>
<base-id>
<position>5</position>
</base-id>
<coordinates>356.25 611.64</coordinates>
<ss-base-coord>
<base-id>
<position>141</position>
</base-id>
<coordinates>372.23 519.3</coordinates>
</ss-base-coord>
</secondary-structure-display>



Quote:
Originally Posted by frans
Please, use code tags to make the code more readable.
Howto:
  1. Select the text corresponding to code
  2. Click on the 'code' button in the editor's toolbarImage
The t2prog can really be optimized. Here is my proposition for the same functionality:

bash code:
  1. #!/bin/bash
  2. i=1
  3. while read line
  4. do
  5. if echo "${line}" | grep -i "free"
  6. then
  7. value=$(echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p')
  8. echo $value
  9. elif echo "${line}" | grep -i "position"
  10. then
  11. pvalue&#91;$i]=$(echo "${line}" | sed -n 's/<.*>\(.*\)<.*>/\1/p')
  12. ((i==2)) && { echo "${pvalue[1]} - ${pvalue[2]}"; i=0; }
  13. ((i++))
  14. fi
  15. done <t1.rnaml
For the script that would wrap the whole process, that's my proposition:
Code:
#!/bin/bash
cd mfold
for Dir in Dir*
do
   cd $Dir
   t1.det >t1out && t1.rnaml>t2out
   java RNA
   cd ..
done

If You want just one script to perform that, use functions and the script would look like:
Code:
#!/bin/bash
ParseT1()   { 
   # PASTE THE CODE OF FIRST SCRIPT
}
ParseT2()   {
   # PASTE THE CODE OF SECOND SCRIPT
}
cd mfold
for Dir in dir* # or just * if there are only subdirectories
do
   cd $Dir
   ParseT1 >t1out  && ParseT2 >t2out
   java RNA
   cd ..
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Shell Programming and Scripting

help me in Shell Scripting

Hi there please have a look at the code..i want to create Using a named pipe. Run a find in the background starting in the working directory While this is happening wait for input from the user to ask him which file to find. If the user does not enter any data in 10 seconds ask the user again.... (1 Reply)
Discussion started by: kattak1511
1 Replies

3. Shell Programming and Scripting

Shell scripting

Hi, if in a network there are lots of PCs connected with either windows or linux as operating system.Then what will be the shell script for the same and also if the PC has linux in it then we have to find if it is occupied or unoccupied. If the PC has windows in it then we have to find if it is... (6 Replies)
Discussion started by: akansha singh
6 Replies

4. UNIX for Dummies Questions & Answers

Shell Scripting

Hey I have a data in the file named as outputFile.txt. The data is in the format 123456,12345678912345,400,09/09/09,INACTIVE. I want this output without commas ie 12345612345678912345400090909INACTIVE. Please tell me what to do and clear explain all the terms, as I am new to it. (6 Replies)
Discussion started by: sampandey31
6 Replies

5. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

6. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

7. Android

Android Scripting Environment: Shell Scripting and Android

I just upgraded to Android 2.2 from 2.1. The GPS issue that was troublesome in 2.1 seems to have been fixed. Some of web browsing seems faster, but it could just be my connection is better today ;) Flash works in some browsers but not very good and it is too slow for Flash apps designed for... (0 Replies)
Discussion started by: Neo
0 Replies

8. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

9. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question