raphael mrode - start [ilri research...

42
Partner Logo Partner Logo Introduction to Unix and Fortran Raphael Mrode Training in quantitative genetics and genomics 30 May – 10 June 2016 ILRI , Nairobi

Upload: ngotruc

Post on 13-Mar-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Partner Logo Partner

Logo

Introduction to Unix and Fortran

Raphael Mrode

Training in quantitative genetics and genomics

30 May – 10 June 2016

ILRI , Nairobi

Page 2: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Introduction to unix commands

• Unix is an operating system (the suite of programs which make the

computer work) which was first developed in the 1960s.

• Many different versions of UNIX.The most popular varieties are:

Sun Solaris, GNU/Linux, and MacOS X

• The kernel of UNIX is the hub of the operating system:

it allocates time and memory to programs

handles the file storage and communications in response to system calls

Page 3: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Introduction to unix commands

• The shell acts as an interface between the user and the kernel.

• The shell is a command line interpreter (CLI). It interprets the commands

the user types in and arranges for them to be carried out.

Page 4: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Directory Structure

Directory structure

/home/user MAIN directory

Sub-directories

Data Programs Scripts

Cattle Goat Chicken Sub-sub-directories

Page 5: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Some useful commands

Command Meaning

ls list files and directories

ls -a list all files and directories

mkdir make a directory

cd directory change to named directory

cd change to home-directory

cd ~ change to home-directory

cd .. change to parent or previous directory

pwd display the path of the current directory

Page 6: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Some Useful commands

Command Meaning

cp file1 file2 copy file1 and call it file2

mv file1 file2 move or rename file1 to file2

rm file remove a file

rmdir directory remove a directory

cat file display a file

less file display a file a page at a time

head file display the first few lines of a

file

tail file display the last few lines of a

file

grep 'keyword'

file search a file for keywords

wc file count number of

lines/words/characters in fil

Page 7: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Somer Useful commands

Command Meaning

command > file redirect standard output to a file

command >> file append standard output to a file

command < file redirect standard input from a file

command1 | command2 pipe the output of command1 to the

input of command2

cat file1 file2 > file0 concatenate file1 and file2 to file0

sort sort data

who list users currently logged in

Page 8: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

The vi editor

• The editor that comes with the UNIX operating system is called vi (visual

editor).

• The UNIX vi editor is a full screen editor and has two modes of operation:

Command mode commands which cause action to be taken on the file. Every character typed is a command that does something to the text file being edited

Insert mode in which entered text is inserted into the file. every character typed is added to the text in the file; pressing the <Esc> (Escape) key turns off the Insert mode.

NOTE: Both UNIX and vi are case-sensitive

Page 9: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Creating a file using vi editor

• To Start vi

• To use vi on a file: type vi filename.

If the file named filename exists, then the first page (or screen) of the file will be displayed

If the file does not exist, then an empty file and screen are created into which you may enter text.

Page 10: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Inserting or Adding Text

The following commands allow you to insert and add text.

Each of these commands puts the vi editor into insert mode; thus, the <Esc>

key must be pressed to terminate the entry of text and to put the vi editor

back into command mode.

Page 11: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Inserting text

i insert text before cursor, until <Esc> hit

I insert text at beginning of current line, until <Esc> hit

a append text after cursor, until <Esc> hit

A append text to end of current line, until <Esc> hit

o

open and put text in a new line below current line,

until <Esc> hit

O

open and put text in a new line above current line,

until <Esc> hit

Page 12: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

To Exit vi

• When you have finished creating you file, and not in insert

mode:

• Save your work and exit by typing ZZ or (shift zz)

• However, it is also possible to quit vi without saving the file:

• :w filename1 <Return> quit vi, writing modified file to file named filename1

• :q!<Return> quit vi without saving the changes made

Note: The cursor moves to bottom of screen whenever a colon (:) is typed and

the command is completed by hitting the <Return>

Page 13: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Moving up and down the file

• Use the arrow keys to move up and down the file and along

a line.

• Following is useful

: nG move cursor to line n

:G move cursor to last line in file

Page 14: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Adding, Changing, and Deleting Text

An important command is the one that allows you to back up and undo your

last action.

u undo what you have just done

The following commands allow you to delete text.

x delete single character under cursor

Nx delete N characters, starting with character under cursor

Page 15: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Adding, Changing, and Deleting Text

dw delete the single word beginning with character under cursor

D delete the remainder of the line, starting with current cursor

position

dd delete entire current line

Ndd delete N lines, beginning with the current line;

Page 16: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Adding, Changing, and Deleting Text

The following commands allow you to modify text.

r replace single character under cursor (no <Esc> needed)

R replace characters, starting with current cursor position, until <Esc> hit

Page 17: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Cutting and Pasting Text

The following commands allow you to copy and paste text.

Yy copy (yank, cut) the current line into the buffer

Nyy copy (yank, cut) the next N lines, including the current

line, into the buffer

P put (paste) the line(s) in the buffer into the text after

the current line

Page 18: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Searching Text

• To locate instances of particular sets of characters (or

strings), use the following commands

/ search forward for occurrence of string

?

search backward for occurrence of string in text

n move to next occurrence of search string

N move to next occurrence of search string in opposite direction

Page 19: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Determining Line Numbers

• How to determine the line number of the current line or the

total number of lines in the file being edited

:. = returns line number of current line at bottom of screen

:= returns the total number of lines at bottom of screen

^g

provides the current line number, along with the total

number of lines, in the file at the bottom of the screen

Page 20: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Introduction to programming in Fortran

; Planning

• Writing a program is systematic task. Prior to code writing one has to go

through certain stages:

• Analyse and specify requirements.

• Design the solution.

• Code the solution using the chosen programming language.

• At the end of coding, verification, testing and maintenance are also

require

Page 21: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

General structure of programs

• Comments about the function of the program

• Program name

• Define the variables

• Open files

• Read in data

• Manipulate data

• Write out solution

• Terminate the program

• Check results

Page 22: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Example of a very simple program

! display a message

PROGRAM hello

WRITE(*,*) 'Hello World!'

END PROGRAM hello

Page 23: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Basic information on programming Fortrand90

• The basic unit of information in a computer is a bit: which has

only two possible states, usually represented the binary digits

0 and 1

• A byte is the amount of computer memory required for one

character, and is eight bits long.

• Since each bit in a byte can be in two possible states, this

gives 2 8, i.e. 256, different combinations.

Page 24: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Data Types

• A data type is a means of denoting data values.

• The Fortran 90 standard requires five intrinsic data types,

which can be divided into two classes.

• The numeric types are: integer, real and complex.

• The non-numeric types: character and logical.

• Associated with each data type are various kinds.

• This is a basically the number of bits available for storage, so

that, for example, there might be two kinds of integer: short

and long.

Page 25: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Data types

• Fortran had what was called an implicit type rule.

• Variables starting with the letters I to N inclusive were

automatically specified with integer type

• Other variables starting with any other letter were

automatically specified real.

• The implicit type rule can lead to serious programming errors.

To guard against such errors it is strongly recommended you

start your program with this statement: Implicit none.

Page 26: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Defining Variables

• Programs make use of objects whose value is allowed to change while it is

running, these are known as variables.

• A variable must have an associated data type, such as REAL or INTEGER or

CHARACTER

• When using implicit type rule, variables must be declared at beginning of

the program before they are used: Examples

• REAL :: milk , eggs , bodyweight

• INTEGER :: pens, herds

Page 27: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Defining Variables

The variable declaration statement may also be used to assign an initial value

to variables as they are declared.

If an initial value is not assigned to a variable it should not be assumed to

have any value until one is assigned

REAL :: sum=0., meanage=34.6

INTEGER :: pens=10, icount=0

Page 28: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Defining Variables

• The general form of a variable declaration is:

• TYPE [attr] :: variable list

• Where attribute has several meanings.

• For real and integer variables, the attribute kind is used to define the

precision with much variables are stored.

• The default depends on the compiler but is 4 (4 bytes). Higher precision

can be assigned as follows: Real :: (kind=8) ; Integer :: (kind=8)

• For character variables, the attribute length is used to define the length of

the character. Eg. Character (len = 3) :: name, implies name is a charcater

variable with maximum length of 3 characters

Page 29: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Parameters in Fortran

The term parameter in Fortran refers to a value which will be constant

throughout the program

Eg: Programmer may want the value of pi to be unaltered during a program.

Therefore pi may be defined as

REAL, PARAMETER :: pi=3.141592

The word REAL defines the type of pi and the word PARAMETER is an

attribute of the REAL object which is known as pi and has the value

3.141592.

Page 30: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Parameters in Fortran

Parameters may also be defined for other data types, for example:

INTEGER, PARAMETER :: maxvalue=1024

INTEGER, PARAMETER :: repeatcount=1000

The objects declared to be parameters may not be altered in the program.

Integer parameters are useful in defining array sizes in fortan

Page 31: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Reading data from files and writing results

to a file

OPEN( 1, FILE = 'DATA', status=’old’ )

OPEN( 2, FILE = 'OUTPUT' ,status=’unknown)

READ(1, *) n, B, C

D = B+C

WRITE(2,*)n,D

End

The OPEN statement connects the unit number (1) to the external file DATA

and the input data is then read in. Similarly the results are written in the file

connected to unit number (2)

The status ‘old’, implies file already exists and ‘unknown’ will create a new file if

needed, or connect to an existing one

Page 32: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Formatted reads and writes

• The format statement controls exactly how data is read in from the data

file or written to the output file

• In the previous slide, the use of * in the read and write statements implies

reading and writing with free format (data columns separated by space)

• However are data structure might be complex and a format is needed

• Format for Integer values are of the form is Iw

• where w specifies the is width and the value is right justified

• Eg. 123 is written under I5 as 123

Page 33: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Formatted reads and writes

• Format for Real values are of the form the form Fw.d,

• where w defines the total field width (including a possible sign and the decimal

point),

• d defines the number of digits after the decimal point

• E.g. –12.345 is printed under F8.2 as -12.35.

Character values are controlled by the A edit descriptor in one of two forms :

A or Aw.

Logical values are controlled by the L edit descriptor in the form Lw.

Page 34: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Some Intrinsic functions

• ABS(X): ABS absolute value of integer, real or complex X.

• EXP(X): EXP value of the exponential function of X , where X may be real or complex.

• INT(X ): INT converts integer, real or complex X to integer type truncating toward zero, e.g. INT(3.9)

returns 3,

• LOG(X): LOG natural logarithm of real or complex X. Note that an integer argument will cause an error.

LOG10(X): LOG10 base 10 logarithm of X.

• MAX(X1, X2[, X3, ...]): MAX maximum of two or more integer or real arguments.

• MIN(X1, X2[, X3, ...]): MIN minimum of two or more integer or real arguments.

• MOD(K, L): MOD remainder when K is divided by L. Arguments must be both integer or both real.

• NINT(X ): NINT nearest integer to X, e.g. NINT(3.9) returns 4,

• REAL(X ): REAL function converts integer, real or complex X to real type, e.g. REAL(2)/4 returns 0.5,

whereas REAL(2/4) returns 0.0.

• SQRT(X): SQRT square root of real or complex X.

Page 35: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Arithmetic operations

Type Operator

Arithmetic

(square) **

(multiplication) * (Division) /

(addition) + (subtraction) -

Relational

<

(less

than)

<=

(greater than

or equal to)

>

(greater

than)

>=

Greater than

or equal to)

==

(equal

to)

/=

(not

equal

to)

Logical

.NOT.

.AND.

.OR.

Page 36: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Some tools for processing data

After reading data, the data can be manipulated using several features

The Do loop : In a simple form, it can be written as:

DO I = J, K

Statements

END DO

where I, J and K are integer . The statements are executed repeatedly; the values of J and K

determine how many repeats are made.

On the first loop, I takes the value of J, and is then increased by 1 at the end of each loop

(including the last).

Looping stops once I has reached the value of K, and execution proceeds with the statement

after END DO.

I will have the value K+1 after completion of the loop .

Page 37: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Some tools for processing data

IF-THEN-ELSE

It is used such that if a conditional statement is true, certain statements are

executed otherwise another set of statements are executed

IF condition THEN

block1

ELSE

blockE

END IF

where condition is a logical expression having a value of either true or false block1 and blockE are blocks of

statements.

If the condition is true, block1 is executed otherwise blockE is executed

Execution continues in the normal sequential way with the next statement after END IF.

Page 38: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Some tools for processing data

The condition may be formed from numeric expressions with the relational operators,

such as <, <=, ==(equals) and /=(not equals), and from other logical expressions with

the logical operators , such as .NOT., .AND. and .OR..

For example, let us assume we have read the value of age for animals from a file and we can classify age into 3

classes: >21 months = class 1, 21-40 months = class 2 and > 40 = class 3

If (age > 21 ) then

Class = 1

Else if age > 20 .and. age <= 40)then

Class = 2

If (age > 40 ) then

Class = 3

End if

Page 39: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Some tools for processing data

The IF statement

IF (condition) statement

In this case only a single statement is executed if the condition is true.

Nothing happens if it is false.

Eg. Assume you want to fined the mean of only females calves

If (gender == ‘F’) then

sum = sum + body weight

n = n + 1

end if

At the end , you find mean as mean = sum/n

Page 40: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

Matrix manipulation

• There are two intrinsic functions, which return a dot (scalar) product and a

matrix product:

• DOT_PRODUCT( X, Y ) , where X and Y are row and column vectors of

right dimensions. Example : a = dot_product (x,y)

• MATMUL( A, B ) , where A and B are matrices with dimensions Anxm and

Bmxn Example: d = matmul(a,b)

Transpose

W = transpose(C)

Matrix addition

D = M + P , given that M and P are conformable

Page 41: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

The presentation has a Creative Commons licence. You are free to re-use or distribute this work, provided credit is given to ILRI.

better lives through livestock

ilri.org

Page 42: Raphael Mrode - start [ILRI Research Computing]hpc.ilri.cgiar.org/beca/training/AQGG_2016/materials/Mrode/BEca... · Raphael Mrode Training in ... cat file1 file2 > file0 concatenate

CRP and CG logos