Make Utility In Unix

                  Compiling a program that consists of more than one source
file involves two steps: compiling each module into its respective object
file and then linking the object files into the executable program. Object
files are intermediate code files created by the compiler which by 
themselves are not executable, but when linked together, are. They are 
easily identified by their .o extension.Object files are created with the
-c option on the compiler. For example, the commands

cc -c prog.c
cc -c func.c

will compile the source files prog.c and func.c into the object files 
prog.o and func.o.An additional command to the compiler,

cc prog.o func.o

will compile prog.o and func.o into a working a.out file which you can execute.
The process of putting together .o files is called "linking." These are the 
basic steps for compiling a program with more than one source file.So compiling 
source code files can be very tedious when it contains several source files and
we want to type compile command every time.
               MAKE allows you to automate this by specifying the compilation 
commands in a file which MAKE uses, called the "Makefile".The basic idea 
behind creation of a Makefile is to tell MAKE what you want to compile and how
to compile it.
              A Makefile is a normal text file that may be created with any Unix
editor,such as vi. The usual form of a Makefile entry is:

target: dependency ...
(tab)command
(tab)command
   ....
MAKE is very particular about how targets, dependencies, and commands are 
arranged in Makefile entries. The target should be at the leftmost margin
with a colon immediately after it. There should be at least one space or
tab between the colon and the first dependency. Dependencies should be 
separated by spaces or tabs.The command should be on the next immediate 
line and spaced from the left margin by ONE tab. 
        In general, the Makefile tells MAKE how to do each step of the 
compilation process. For each .c file, MAKE creates a corresponding .o file.
Then when all the .o files have been compiled, MAKE links them together into
an executable.
         Let us continue with an example. We have two source files, prog.c and 
func.c.The prog.c file has an #include "head.h" statement, the func.c file has
no includes,and the executable program is to be named "myprog". One way to 
construct a Makefile would be as follows:

myprog: prog.o func.o
	 cc prog.o func.o -o myprog
prog.o: prog.c head.h
	 cc -c prog.c
func.o: func.c
	 cc -c func.c

This Makefile says that myprog is dependent on prog.o and func.o. That is,
prog.o and func.o are required to exist and be up-to-date in order to put 
together the program myprog. Likewise, prog.o is dependent on prog.c and 
head.h. Finally func.o is dependent on func.c.We will now use MAKE to 
create prog.o, func.o, and myprog with the command:

	% make myprog

          MAKE looks for a file specifically called "Makefile". If it does 
not find one,MAKE will not work.MAKE starts at the first target, "myprog".
It notes that there is a dependency on prog.o and so proceeds to the target
"prog.o". Now MAKE sees that prog.o in turn is dependent upon prog.c and 
head.h. MAKE then checks to see if prog.c is current. Since there is no 
prog.c target in the Makefile and prog.c exists,MAKE is satisfied that prog.c
is up-to-date. Likewise for head.h. MAKE returns to the business of assuring
that prog.o is up-to-date. Since prog.o does not exist,MAKE invokes the 
command "cc -c prog.c" to make a current prog.o. MAKE returns to "myprog" 
and continues to the next dependency, func.o. MAKE goes through a sequence
of steps to create func.o similar to those described for prog.o. Returning
to "myprog", MAKE sees that there are no more dependencies to check. Since
"myprog" does not exist, MAKE invokes the command "cc prog.o func.o -o myprog"
and the job is done.
Advertisements

An Introduction To AWK

              The awk language is a small, C-style language designed 
for the processing of regularly formatted text. This usually includes
database dumps and system log files.The AWK utility is a data extraction
and reporting tool that uses a data-driven scripting language consisting
of a set of actions to be taken against textual data either in files or 
data streams for the purpose of producing formatted reports.Awk's funny 
name comes from the names of its original authors, Alfred V. Aho, 

Brian W. Kernighan and Peter J. Weinberger.

Basic structure
                 AWK is a language for processing files of text. A file is
treated as a sequence of records, and by default each line is a record. Each 
line is broken up into a sequence of fields, so we can think of the first word
in a line as the first field, the second word as the second field, and so on.

               An AWK program is of a sequence of pattern-action statements.
AWK reads the input a line at a time. A line is scanned for each pattern in 
the program, and for each pattern that matches, the associated action is 
executed.

An AWK program is a series of pattern action pairs.

condition {action}

Here the condition is typically an expression and action is a series of 
commands. The input is split into records, where by default records are 
separated by newline characters so that the input is split into lines. 
The program tests each record  against each of the conditions in turn, and 
executes the action for each expression that is true. Either the condition
or the action may be omitted. The condition defaults to matching every record.
The default action is to print the record.

           Another important pattern is specified by the keywords "BEGIN" and
"END" .These two words specify actions to be taken before any lines are read, 
and after the last line is read. The AWK program below:

BEGIN { print "START" }
      { print         }
END   { print "STOP"  }

This will adds one line before and one line after the input file.
             The general form of the awk command is 
awk <pattern> '{print <stuff>}' <file>

In this case, stuff is going to be some combination of text, special 
variables that represent each word in the input line, and perhaps a 
mathematical operator or two. As awk processes each line of the input
file, each word on the line is assigned to variables named $1 (the first
word), $2 (the second word), and so on.The variable $0 contains the 
entire line.
     Let's start with a file, words.data, that contains these lines:

nail hammer wood
pedal foot car
clown pie circus

Now we'll use the print function in awk like this:

awk '{print "Hit the",$1,"with your",$2}' words.data
Hit the nail with your hammer
Hit the pedal with your foot
Hit the clown with your pie

We can also put some numeric data in the input file as in the grades.data
file:

Rogers 87 100 95
Lambchop 66 89 76
Barney 12 36 27

Then we can perform some calculations like this:

awk '{print "Avg for",$1,"is",($2+$3+$4)/3}' grades.data
Avg for Rogers is 94
Avg for Lambchop is 77
Avg for Barney is 25
Also if we want to exclude lines from being processed, we can enter 
something like this:

awk /^clown/'{print "See the",$1,"at the",$3}' words.data
See the clown at the circus

Here, we told awk to consider only the input lines that start with clown.
Note that there is no space between the pattern and the print specifier.

Starting With Shell Script

What is a shell script?

A shell script is a command which would have been directly typed into a shell.

It can be a sequence of commands that you type regularly.By putting them into

a script we can reduce them to a single command.Also there is no difference in

syntax between interactive command line use and placing the commands in a

file.But there are some commands line history which we can only type in

interactive command line.

Why use shell scripts?

Shell scripts are used to combine lengthy and repetitive sequence of commands

into a single and simple command.We can create new commands using combinations

of utilities.It can also be used to generalize operations on a single set of data, into a

procedure that can be applied to any similar set of data.

Writing the script

First use one of the text editors like vi and write this inside it:

#!/bin/sh

clear

echo “Hello world”

and save the file using .sh extension,say hello.sh.

Making it executable

To do execute,tell linux that this is an executable file.

chmod +x hello.sh

Running the script

To run the script just go to the directory where the file is saved and type the following

command:

./hello.sh

The output will be:

 

 

Recursion in Python

                   Recursion is a method where solution to a problem depends on solutions to the

smaller instances of the same problem.One of the most magical thing that a program can do

is to define a function that can call itself. Here is an example:

def countdown(n):

if n==0: return 0

else: return countdown(n-1)+ n

print countdown(3)

When we execute this program ,the output obtained will be 3.

We use a stack diagram to represent the state of a program during function call.Every time

a function gets called , a  new function frame containing local variables and parameters

are created by the Python.The following is the stack diagram for countdown() with n=3.

For a recursive function ,there might be more than one frame on the stack at the same time.

The top of the stack is the frame for __main__.The four countdown frames have different

values for the parameter n.The bottom of the stack ,where n=0,is called the base case.

When the base case is reached ,no more recursive calls are made,hence no more frames

are created.

If a recursion never reaches a base case, it goes on making recursive calls forever, and

the program never terminates.This is known as infinite recursion. Here is an example of

a minimal program with infinite recursion:

 

def recurse():

recurse()

One of the dangers of the code that calls itself is that,it will get into an endless loop and

so there will be a limit to the number of levels deep we can go.In Python that defaults to 1000.

But we can increase the limit of depth using the setrecursionlimit method of the sys class.

The following is an example code that uses the setrecursionlimit method to change the

depth level of the recursion:

import sys

sys.setrecursionlimit(1500)

def countdown(n):

if n==0: return 0

else: return countdown(n-1)+n

print countdown(1200)

When we run the code without that extra line,”RuntimeError: maximum recursion depth

exceeded” we will get the ouput 720600.

Heap Sort

                               

                         A heap is a specialized tree-based data structure that satisfies

the heap property: if B is a child node of A, then key(A) ≥ key(B). This

implies that an element with the greatest key is always in the root node,

and so such a heap is sometimes called a max-heap. Alternatively, if the

comparison is reversed, the smallest element is always in the root node,

which results in a min-heap. The heap is an efficient implementation of an

abstract data type called a priority queue. Heaps are crucial in several

efficient graph algorithms such as Dijkstra’s algorithm, and in the sorting

algorithm heap sort.

Heap sort is an implementation of a binary heap. Once we

have implemented the heap,we will easily deduce heap sort.The data

structure of the heap sort algorithm is a heap. The data sequence to be

sorted is stored as the labels of the binary tree.For each element at index i

in the tree:its left child is at index 2i+1,its right child is at index 2i+2 and its

parent  is at (i-1)/2.If the provided  list is not empty,it will be heapified and new

elements can be inserted to the heap.A new element   will be first put at the

end of the tree,then pulled up until the tree satisfies the heap priority.

To add an element to a heap we must perform an up-heap operation also

known as bubble-up in order to restore the heap property. We can do this

in O(logn) time, using a binary heap, by following this algorithm:


  1. Add the element to the bottom level of the heap.
  2. Compare the added element with its parent; if they are in the correct order, stop.
  3. If not, swap the element with its parent and return to the previous step.

 

An almost complete binary tree with n vertices has a depth of at most log(n).

This approach requires O(n.logn) time because each insertion takes

O(n.logn) time and there are n elements.Thus,the time complexity of

heap sort is T (n)=O(n·log(n)).

Python Classes and objects

             Python is fully object-oriented: we can define our own classes, inherit from our own or built-in classes,
 and instantiate the classes we have defined.
A Python class starts with the reserved word class, followed by the class name. 

A Simple Python Class

class Point(object):
	pass

1. The name of this class is Point, and it doesn't inherit from any other class. 

2. This class doesn't define any methods or attributes, but syntactically, there needs to be something in the 
   definition, so you use pass.

3. Everything in a class is indented, just like the code within a function, if statement, for loop, and while loop. 

Here the pass statement in Python is like an empty set of braces ({}) in Java or C.

A class efinition can also like this:

class Point(object):
    """represents a point in 2-D space"""

Here the body is a docstring that explains what the class is for.
Let us see what will be the output when we will print the calss Point.

>>> print Point
<class '__main__.Point'>

Since the class Point is defined at the top level, its “full name” is __main__.Point.

To create a Point,call Point as if it were a function.

>>> blank = Point()
>>> print blank
<__main__.Point object at 0xb7785a6c>

The output will be a return value,that is a reference to a Point object, which we assign to blank. Creating a new object is
 called instantiation, and the object is an instance of the class.

We can also assign values to an instance using dot notation.For example:

>>> blank.x = 3.0
>>> blank.y = 4.0

To read the value of an attribute we will use the following syntax:

>>> print blank.y
4.0
>>> x = blank.x
>>> print x
3.0

We can also define functions inside the class as same as the attributes.
ex:-

class Rectangle(object):
    """represent a rectangle.
       attributes: width, height, corner.
    """
To represent a rectangle, you have to instantiate a Rectangle object and assign values to the attributes:

box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0

The figure shows the state of this object:

An object that is an attribute of another object is embedded.

Here find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle:

def find_center(box):
    p = Point()
    p.x = box.corner.x + box.width/2.0
    p.y = box.corner.y + box.height/2.0
    return p

Here is an example that passes box as an argument and assigns the resulting Point to center:

>>> center = find_center(box)
>>> print_point(center)
(50.0, 100.0)

Shell command to print the number of occurrences of chromosomes

  The following is a text file containing human genetics data.

http://svn.software-carpentry.org/swc/data/1000gp.vcf 

Here is the command to print the number of 
occurrences of chromosomes from the above data file:
grep -v "^#" 1000gp.vcf| cut -f 1 | sort | uniq -c

Here a pipeline symbol '|' is used to combine 
several commands into a single one. A pipe uses
output of the command on its left side as the
input to its right side.

Now let's see the use of each keyword in the command. 
grep -v "^#" is used to print the lines other than
header files,i.e lines which are not starting with 
"#" from the given file.cut -f 1 is used to select 
only the first field from its input.sort is used to
sort the lines in the ascending order.uniq -c is 
used to print the number of occurrences by omitting 
the repeated value.
Thus the final result will be the list of unique 
chromosomes ,each of with their number of occurrences.

Ramya

Hi friends,

I am Ramya, doing MCA in GEC Thrissur. I am here to share my ideas about the projects that i am going to do in my course. I am expecting your valuable feedbacks and as a beginner it will help me to improve a lot .

Thank you