Bioinformatics Tools

Pages

Showing posts with label tricks. Show all posts
Showing posts with label tricks. Show all posts

Thursday, February 27, 2014

Short Simple Tips and Techniques for use in Bioinformatics

Here I am compiling some of the short simple tips and tricks for use to ease out some of Bioinformatics work, I would also be thankful if you can contribute to it, if you can come across some. Write them as comment and I will update them.

 #####################################

Running single or multiple commands on all files or selected files in a directory.


For running a command on number of files in a directory, use this:

CODE:

for i in *; do command; done;

Wherein * will select all the files to act upon, you can use *.fasta to work only with the fasta files, etc.
command is the linux command you wish to act on all the selected files, where in each selected file is represented by or recalled by "$i" and can also be used to generate output with the filename added to it.

Example:

for i in *.fasta; do grep ">gi" "$i" > "$i".output; done;

Similarly to copy a file from anywhere (previous directory in this example) in multiple directories

for i in *; do cp ../program.pl "$i" ; done;

##################################################

For working on multiples files, like removing, copying etc. on every directory inside a directory or multiple directories do the following: 

To remove all the fasta files from the folder or sub folders

find /pathtofolder/ -type f -iname "*.fasta" -exec rm {} \;

This will find all the files
find /pathtofolder/ -type f -iname "*.fasta"

and this will remove these files
 -exec rm {} \;

##################################################

To remove files containing a particular text in them from various folders inside the directory: 

For example you have downloaded all genomes *.fna from NCBI Genomes for Bacterial Genomes and you just wish to use *.fna from the bacterial genomes but not from the plasmids which you wish to remove.


grep -lrIZ plasmid . | xargs -0 rm -f --

This will remove all files containing plasmid in it.

##################################################

To grep the count (say fasta) in various files inside a directory: 

grep -c ">" */*/*.fasta > reads_count

This will count the number of reads from each file in those directories along with the file name and that can be redirected to a file.

NOTE: Change */*/*.fasta depending upon the number of directories inside i.e. */*.fasta will work if the files are in one directory below the present directory.

##################################################

 Calculating Mean Length of FASTA Sequences

 Here's a shot simple code for calculating the mean length of a FASTA sequence.

Code:


awk '{/>/&&++a||b+=length()}END{print b/a}'  sequence.fasta > average

Alternate faster methods are available, read here BIOSTARS

##################################################

Convert FASTQ to FASTA:

Code:

sed -n ' 1~4s/^@/>/p; 2~4p ' file.fastq > file.fasta


sed 's/\ /\_/g' seqfile > seqfile_ed (to make unique ids in flash assembled sequences)


##################################################

Schedule one job after another

Run one script after another in such a way that second script starts after finishing first one. Without using Pipe | or ampercent && i.e. the first process is already running and you want second one to start after the first one finishes. And this can be done in different folder in case the output of second script will affect the output of first script. So run this on any folder you wish to.

Code:

while ps -p $PID; do sleep 1; done; script2

Explanation and Example:

while ps -p 4437; do sleep 1; done; echo "It Works"

Where $PID is the process id of the already running job (add PID number)

script2 is your script you wish to run after first script ends

sleep 1 is sleep for one second (SUFFIX may be ‘s’ for seconds (the default), ‘m’ for minutes, ‘h’ for hours or ‘d’ for days, read man sleep)

##################################################

Sample command for formatdb and blastall

FORMAT_DB:
nohup time /home/sanjiv/Softwares/blast-2.2.26/bin/formatdb -p T -i input.database &

BLAST_ALL:

Protein BLAST (blastp)

nohup /home/sanjiv/Softwares/blast-2.2.22/bin/blastall -p blastp -d location_of_database.fa -i location_of_input_file.fasta -o  location_of_output_file.blastall.out -a 30 -e 0.000001 &

Nucleotide BLAST (blastx)

nohup /home/sanjiv/Softwares/blast-2.2.22/bin/blastall -p blastx -d location_of_database.fa -i location_of_input_file.fasta -o  location_of_output_file.blastall.out -a 30 -e 0.000001 &

##################################################

Fetch FASTA sequences using identifier from a file.

CODE:

grep -A1 -f list mainfile > fastaofidsfromlist

Explanation:

To fetch selected fasta sequence from huge file. Keep your identifiers or fasta headers in 'list' (i.e. like Rv ids). 'mainfile' is the original file from where you have to fetch the fasta. 'fastaofidsfromlist' is the your file with fasta sequences of the ids you gave as list. Issue the following command.

CAUTION: For this to work properly, your mainfile should have sequences in one line only, without line breaks. Check output file before use.

##################################################

Convert multi-line fasta to single line fasta

awk 'BEGIN{RS=">"}NR>1{sub("\n","\t"); gsub("\n",""); print RS$0}'  file

 ##################################################

Using BLAST+

Make blast database

makeblastdb -in proteases.faa -dbtype 'prot' -out CJProteases

BLAST

blastp -query proteins.fasta -db DATABASE -out OUTPUT_File -evalue 1e-30 -outfmt 7

Wednesday, January 29, 2014

LINUX BASICS FOR BEGINNERS AND ADVANCE USERS

Since long I was trying to learn Linux. I realized it is not as difficult as it seems. I have compiled some of the basic commands that are mandatory to learn to step into this beautiful world. They are not all, I have skipped grep, awk and sed knowingly, as I presume they deserve bit more attention. I will keep updating the list as soon as I can. Do look around too, some good video tutorials are also available that aid in learning. Also, I have made this available as a simple printable PDF. 

LINUX BASICS FOR ADVANCE USERS       - Download Here

Basic commands: On the terminal type these commands at-least two-three times to practice. Also, it is advisable to read man (help) pages of all these commands at least once.

####################################################

Command

Function
man <command>
:
Displays manual or help page for a commend, always read it full at least once
cd
:
Change directory, to navigate from one directory to another
cd -
:
Toggle between previous directory and current directory
ls
:
List files or folders, with several arguments give detail information about the files and folders, try: ls -ltrh
date
:
Displays current date
ssh
:
login to remote host
passwd
:
Change password
bc
:
Opens calculator
cp
:
Copies files or directories, usage: cp file.txt /directory
mv
:
Moves file, rename file, usage: mv file1.txt file2.txt (renames file1.txt to file2.txt)
pwd
:
Present working directory
mkdir
:
Make a new directory, usage: mkdir dir1 ; creates directory dir1
rmdir
:
Removes empty directory
cal
:
Opens calendar
echo
:
Displays message, usage: echo "hi" ; displays hi
printf
:
Alternative to echo, displays message
script
:
Records your session
uname
:
Know about your machine, usage: uname -a
tty
:
Know your terminal
stty
:
Display and setting terminal characteristics
gzip
:
Compressing files
gunzip
:
Decompress files
tar
:
Archival program
head <filename>
:
Displays beginning of a file
tail <filename>
:
Displays end of a file
exit
:
Exits terminal
who
:
Who is logged into the system
whoami
:
Username
hostname
:
Machine name
rm -r
:
Removes directory including its content recursively (be careful)
ctrl+w
:
Cuts last work with keyboard while typing on terminal
ctrl+y
:
 Paste with keyboard
ctrl+a
:
Cursor at the beginning of the line while typing on terminal
ctrl+e
:
End of the line while typing on terminal
ctrl+k
:
Cuts to the end of the line while typing on terminal
ctra+c
:
Kills the process currently running
history
:
History of all the commands on terminal
~
:
Home directory, you ca do [ cd ~ ] to go to the home directory
wc
:
Word count, can be combined (piped through) various other commands (it gives results as lines, words and characters)
wc -c
:
Counts the number of bytes
wc -w
:
Counts the number of words
wc -l
:
Counts the number of lines
free -m
:
Free memory (ram) in mb
free -g
:
Free memory (ram) in gb
df
:
Disk space
du - sch <dir>
:
Disk space of the current directory
du -sch *
:
Disk space of individual files or directories
du -sch * | sort -nr
:
Disk space of individual files or directories sorted by file size
w
:
Who is logged onto the system and what are they doing
ps
:
Processes running by the users
ps -e
:
All the processes running in the system, also used with argument -a, -x, read man ps
ps -o %t -p <pid>
:
How long the process was running
ctrl+z
:
Suspend or sleep the current running process in foreground
bg
:
Run process in background
fg
:
Bring processes foreground, those running in background
kill <pid>
:
Kills a process with the given process id
kill -9 <pid>
:
Violently kills a given process id giving it no time to cleanup
kill -l <pid>
:
List all signals that can be sent to a process
kill -s sigstop <pid>
:
Suspends or sleep a process
kill -s sigcont <pid>
:
Resumes or wakes up a process
renice -n <value> <pid>
:
Gives a priority value to the process id, ranges from 1-19, higher the value lower the priority, default is 10, it is said to be how nice the process is i.e. How much ram it leaves for other process to run
cmp <file1> <file2>
:
Compares two given files
diff <file1> <file2>
:
Find the differences between two files, compares two files, argument -w ignores whitespace
mount
:
Mounts drive, usage: mount /media/usb
umount
:
Unmounts drive, usage: /media/usb
eject
:
Ejects cd rom, usage: /media/cdrom
join
:
Combines lines from two files on a common field, usage: join file1.txt file2.txt
tr a-z A-Z < file.txt
:
Transfers case to another i.e. Lowercase to uppercase
tr A-Z a-z < file.txt
:
Transfers case to another i.e. Uppercase to lowercase
xargs
:
Takes output of one command and passes it as an argument to another command usage: cat urllist.txt | xargs wget -c
sort
:
Sorts the lines of a text files in ascending order, usage: sort file.txt ; argument -r sorts in descending order, usage: sort -r file.txt ; argument -t is delimiter (colon, space, comma, tab etc.), usage: sort -t: file.txt ; argument -k sorts on a particular field, I recommend to read more on this, important and very helpful, used in combination with other commands [ man sort ]
uniq
:
Removes duplicate entries from sorted files, therefore mostly used in combination with sort, in order for uniq to work, all the duplicate entries should be in adjacent lines, usage: sort names.txt | uniq ; sort -u names.txt ; to count duplicate lines: sort file.txt | uniq -c ;  to display duplicate entries: sort file.txt | uniq -cd
cut
:
Used to display only specific columns from a text file or other command outputs i.e. To display first field from a colon delimited file : cut -d: -f 1 file.txt ; to display first and third field from colon delimited file : cut -d: -f 1, 3 file.txt
find
:
Find files, usage: find [pathname] [condition], arguments it takes are -name (for names of the files), -type (for type of file) -size (for size of file i.e. +100m, -mtime (modified time in days i.e. +60 or -2), -exec
stat
:
Used either to check the status or properties of single file or file system, usage: stat /etc/file.txt ; argument -f i.e. Stat -f / (displays the status of the file system i.e. Size/total/free/available
ac -d
:
Displays the statics about the user connect time, argument -d breaks the output for individual days, usage: ac -d username
&
:
At the end of the command executes job in the background but if you logout the job will be killed
nohup
:
At the beginning of the command executes job in background with (ampercent &), usage: nohup ./script.sh & ; the job will run in background even if you logout
screen
:
Once you logout the same session will not be connected, to do that, end with screen and attach it later by screen to get back as it was when you logged out
at
:
Schedule job i.e. At -f backup.sh 10am tomorrow
watch
:
To execute command continuously at certain intervals, usage: watch df -h
split
:
Splits the given file (big file) as per the requirement with respective arguments, i.e. File size, number of lines, etc.