reading information from a table and apply a command on multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading information from a table and apply a command on multiple files
# 1  
Old 10-02-2012
MySQL reading information from a table and apply a command on multiple files

Hey gyuz,

I wanna calculate the number of mapped reads of a bam file in a region of interest. I used this code to do so :

Code:
samtools view input.bam chrname:region1 > region1.txt

This will store all the reads from given bam file within the region of interest in region1.txt

Now I have several bam files and several regions for each which are different. (regions are not the same in all bam files)

I prepared a list of them like below:

bam1 region1
bam1 region2
bam1 region3
bam2 region4
bam2 region4
bam3 region5

I want the script to read from the table and apply the function on proper bam file and regions and report the output for each region in a single file. region1.txt, region2.txt, region3.txt,.....

If you can help me with this you saved my time so much!!

Thanks in advance.

Last edited by @man; 10-02-2012 at 12:44 PM..
# 2  
Old 10-02-2012
Try this:
Code:
while read BAM REGION; do samtools view $BAM chrname:$REGION >> $REGION.txt; done <file

remove the region*.txt files before use.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-02-2012
Thanks RudiC. It seems it's working just fine.
Now I have many files which I cannot open them. I believe it's because of their name:

one example:
gi|16799079|ref|NC_003212.1|:2769071-2769101.txt

How can I rename them all by replacing pipes and colons by underscore for example to have something like this:

gi_16799079_ref_NC_003212.1__2769071-2769101.txt

I appreciate your help!
# 4  
Old 10-02-2012
to make it easy and quick loop through while or for loop in that directory and mv the files with the below command...

Code:
 
echo "gi|16799079|ref|NC_003212.1|:2769071-2769101.txt"|sed -e 's/[:|]/_/g'


Last edited by vidyadhar85; 10-02-2012 at 11:56 AM..
# 5  
Old 10-02-2012
Try
Code:
for i in gi*; do echo mv \"$i\" ${i//[|:]/_}; done

The ${i//[|:]/_} is a bash Pattern substitution, so don't expect it to work with other shells.

If happy with the result, remove the echo cmd. You may need to play around with the double quotes and their escapes.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-02-2012
Thanks RudiC once more.
It works just fine with this code:
Code:
for i in gi*.txt; do mv $i ${i//[|:]/_}; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to apply the update statement in multiple servers on multiple dbs at a time .?

Hi , Can any please help the below requirement on all multiple servers and multiple dbs. update configuration set value='yes' ;1) the above statement apply on 31 Databases at a time on different Ip address eg : 10.104.1.12 (unix ip address ) the above ip box contains 4 db's eg : db... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

Apply command to all files in folder

Hi all! I have this command grep -E '^\To: |^\Date: |^\Subject: ' fileA.txt > fileA_1.txt && grep -v '^\To: |^\Date: |^\Subject: ' fileA.txt >> fileA_1.txt && rm fileA.txt && sed -i -e 's/\(Date: \|Subject: \|To: \)//g' fileA_1.txtHow do I apply it to all the files in the folder (each file has a... (7 Replies)
Discussion started by: guilliber
7 Replies

3. UNIX for Dummies Questions & Answers

Load multiple files into a table

Hi, I need to load data from two files to a single table. My requirement is that I get two files in which a few column data are manadatory. These files are identified based on the file name. For example, I have two files ABCFile and BCDFile. ABCFile has mandatory data in column 3 and 4... (0 Replies)
Discussion started by: reshma15193
0 Replies

4. Shell Programming and Scripting

search information in multiple files and save in new files

hi everyone, im stuck in here with shell :) can you help me?? i have a directory with alot files (genbank files ... all ended in .gbk ) more than 1000 for sure ... and i want to read each one of them and search for some information and if i found the right one i save in new file with new... (6 Replies)
Discussion started by: andreia
6 Replies

5. UNIX for Dummies Questions & Answers

Reading Table name from a list of files in a Directory

Hi , I have searched through the forum but not able to find out any help :( i have a directory having lot of files which contains sql statemtns eg : file 1 contains select from table_name1 where ..................... select from table_name2 where .......... select from ... (3 Replies)
Discussion started by: Trendz
3 Replies

6. Shell Programming and Scripting

extracting information from multiple files

Hello there, I am trying to extract (string) information ( a list words) from 4 files and then put the results into 1 file. Currently I am doing this using grep -f list.txt file1 . and repeat the process for the other 3 files. The reasons i am doing that (a) I do know how to code (b) each file... (4 Replies)
Discussion started by: houkto
4 Replies

7. Shell Programming and Scripting

apply record separator to multiple files within a directory using awk

Hi, I have a bunch of records within a directory where each one has this form: (example file1) 1 2 50 90 80 90 43512 98 0909 79869 -9 7878 33222 8787 9090 89898 7878 8989 7878 6767 89 89 78676 9898 000 7878 5656 5454 5454 and i want for all of these files to be... (3 Replies)
Discussion started by: amarn
3 Replies

8. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

9. Shell Programming and Scripting

How to Pull out multiple files from DB table and redirect all those files to a differetn directory?

Hi everyone!! I have a database table, which has file_name as one of its fields. Example: File_ID File_Name Directory Size 0001 UNO_1232 /apps/opt 234 0002 UNO_1234 /apps/opt 788 0003 UNO_1235 /apps/opt 897 0004 UNO_1236 /apps/opt 568 I have to... (3 Replies)
Discussion started by: ss3944
3 Replies

10. Shell Programming and Scripting

Apply `chmod` for multiple files through FTP

Hi all, Can you please help me in this aspect. I devoloped a FTP script to copy a directory to remote server. Now i got stuck-up in changing the file permissions for all the files in directory. I tried to change the permissions of single file and I did it but failed in changing... (3 Replies)
Discussion started by: Chanakya.m
3 Replies
Login or Register to Ask a Question