Linux实践

计算生物学实验2: Linux实践

    • @[TOC]( 计算生物学实验2: Linux实践)
      • 1. 实验目的
      • 2. 实验准备
        • 2.1 实验平台
        • 2.2 数据简述
        • 2.3 软件配置
      • 3. 实验内容
        • 3.1 Linux基本命令练习
          • 3.1.1 conversation with Linux – simple command examples
          • 3.1.2 basic operations on directories
          • 3.1.3 basic operations on files
          • 3.1.4 basic operations on text files
          • 3.1.5 advanced processing of text files(ZmB73_5b_FGS.gff)
        • 3.2 screen命令和scp命令的练习
          • 3.2.1 using “screen” to create a persistent session
          • 3.2.2 using scp to transfer files between Linux machines
        • 3.3 bash脚本以及多线程操作
          • 3.3.1 simple bash script
          • 3.3.2 running BLAST on two CPUs
          • 3.3.3 Multiple independent tasks run sequentially
          • 3.3.4 Multiple independent tasks run in parallel
      • 4. 实验总结
        • 4.1 实验结论
        • 4.2 实验收获
        • 4.3 其它心得

1. 实验目的

通过本实验,熟悉生物信息学/计算生物学常用的 操作系统(Linux),对Linux操作系统上的各种命令进行学习使用,例如文件、目录的新建、删除、复制等基本操作。为了提高效率,还在本实验中涉及到了如何通过screen命令实现多窗口的对话,以及如何编写bash脚本,如何运用多线程。本实验也会通过scp/wget命令学习如何从网络上下载所需的数据。

2. 实验准备

2.1 实验平台

Linux JSvr02 3.10.0-1160.80.1.el7.x86_64 #1 SMP Tue Nov 8 15:48:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

2.2 数据简述
数据来源
examples .tgz/work/cb-data/Linux_workshop
dir2transferscp命令从cbsufsrv7.biohpc.cornell.edu下载
databases/work/cb-data/Linux_workshop
auxfiles/work/cb-data/Linux_workshop
2.3 软件配置
软件版本
vim7.4
BLAST2.6.0+

3. 实验内容

本实验分三个部分,

  • 第一部分对Linux操作系统的各种简单命令进行练习
  • 第二部分主要练习screen命令和scp命令
  • 第三部分学习编写bash脚本以及多线程操作
3.1 Linux基本命令练习
3.1.1 conversation with Linux – simple command examples

该部分通过使用Linux的一些基本命令,完成对Linux系统的初步熟悉。

(a) Who else is logged in to this machine?

who

(b) What is the directory you “are” currently in?

pwd

(c ) List the contents of the directory

ls -al
  • -a显示所有文件及目录(. 开头的隐藏文件也会列出)
  • -l 以长格式显示文件和目录信息,包括权限、所有者、大小、创建时间等(每行只展示一个)
    在这里插入图片描述
  • 第一列共10位,第1位表示文档类型,d表示目录,-表示文件; 后9位,依次对应三种身份所拥有的权限,身份顺序为:owner、group、others,权限顺序为:readable(r)、writable(w)、executable(x)。例如:-r-xr-x---的含义为当前文档是一个文件,拥有者可读、可执行,同一个群组下的用户,可读、可执行,其他人没有任何权限。
    在这里插入图片描述
  • 第二列表示链接数,表示有多少个文件链接到inode号码。
  • 第三列表示拥有者
  • 第四列表示所属群组
  • 第五列表示文档容量大小,单位字节
  • 第六列表示文档最后修改时间,注意不是文档的创建时间哦
  • 第七列表示文档名称。以点(.)开头的是隐藏文档

(d)How much disk space does my directory take?

du –hs .
  • -h(–human-readable)以K,M,G显示大小,提高可读性
  • -s ( --summarize) 显示目录或文件的总大小

(e)Find summary information about the storage available on the machine

df -h #此处-h也是(--human-readable)

在这里插入图片描述
(f)Find summary information about RAM memory available on the machine

free

(g)Fid more information about the du command

man du

(h)Repeat one of the previous commands without re-typing it

history
!50 #执行第50条命令
3.1.2 basic operations on directories

(a)Create your temporary directory in the scratch file system

mkdir ./workdir

(b)create a subdirectory (of that new directory), called mytmp.

mkdir ./workdir/mytmp

(c )Verify the subdirectory mytmp has been created

ls ./workdir

(d)list contents of mytmp

ls -al ./workdir/mytmp

(e)remove mytmp

rm -r (-f) mytmp
  • -r表示删除目录
  • -f是强制执行(删除时不要询问)
3.1.3 basic operations on files

(a)create directory /workdir/your_id (replace your_id by your real userID).

mkdir ./workdir/s20223282034

(b)Copy the file examples.tgz located in /work/cb-data/Linux_workshop to your temporary directory

cd ./workdir/s20223282034
cp /work/cb-data/Linux_workshop/examples.tgz .
  • . 代表当前目录

(c )Unpack the file examples.tgz and list the resulting files and directories

tar –xzvf examples.tgz
  • -z: 表示要使用 gzip 解压归档文件
  • -x: 解压归档文件
  • -v: 显示详细输出,列出被解压的文件
  • -f: 指定要解压的归档文件的名称

(d)Check the type of each file

file * #查看当前目录下的文件的类型
file * scripts/* #查看当前目录和子目录scripts下的文件类型
  • *为通配符,代表该目录下所有文件
    关于通配符,不只有这一个用法,比如: ikun.*表示该目录下名为ikun的所有文件,* 代表的任意后缀;再比如: *.ikun 表示该目录下后缀为ikun的所有文件…

(e)Create a new directory in /workdir/your_id, called sequences

mkdir sequences

(f)Move the files flygenome.fa and short_reads.fastq to directory sequences

mv flygenome.fa short_reads.fastq sequences

(g)Create a new directory in /workdir/your_id, called shellscripts

mkdir shellscripts

(h)Move all shell scripts (i.e., all files with names ending with “.sh”) from directory scripts to the newly created directory shellscripts

mv scripts/*.sh shellscripts

(i)Remove the directory scripts

rm –rf scripts
3.1.4 basic operations on text files

(a)Page through a file using less

less ZmB73_5b_FGS.gff

(b)Display the first 10 and the last 10 lines of the fastq file

cd /workdir/userID/sequences
head -10 short_reads.fastq
tail -10 short_reads.fastq

©Save lines 1000 through 2000 of the fastq file above into another file

head -2000 short_reads.fastq | tail -1000 > middle_lines.fastq
  • |为管道,用于将前面表达式结果传给后面的表达式
  • >用于将原本打印在屏幕上的内容转存于后面的文件中,便于查看。值得注意的是,单个>会覆盖该文件原有的内容,而>>表示追加在该文件的后面。

那此处就是先取该文件的前2000行,再取这2000行的后1000行,就是1000~2000行,最后将这个结果存到middle-lines.fastq文件中。

(d)Count the lines/words/characters in a fastq file. How many reads does this file contain?

wc short_reads.fastq

在这里插入图片描述
其中,3 个数字分别表示testfile文件的行数、单词数,以及该文件的字节数。
对于fastq格式的数据,每4行为1个reads,所以这个文件里共有1000000个reads。

(e)Look for a string in a file and number of lines the string occurs in ‘AATCGT’

grep AATTCGT short_reads.fastq
grep AATTCGT short_reads.fastq | wc -l
  • grep 常与正则表达式搭配使用,例如:如果想查找的是“AATTCGT”和“CATTCGT”的行,如何用1条语句来表示:
grep [AC]ATTCGT short_reads.fastq

此处,[AC]是[A|C]的缩写,即第一位可以是A,也可以是C。

(f)Note the size of the file . The compress the file using gzip. What is the gain from compression?

ls -al short_reads.fastq
gzip short_reads.fastq
ls -al short_reads.fastq.gz

在这里插入图片描述
在这里插入图片描述
通过对比,不难发现,压缩后的文件占据更小的硬盘空间

3.1.5 advanced processing of text files(ZmB73_5b_FGS.gff)

(a)Look into the file to examine its structure (use more,cat or a text editor)

less  ZmB73_5b_FGS.gff

(b)Create a new file, containing only gene features, with columns 9, 1, 4, and 5 (in this order)

grep -P "\tgene\t" ZmB73_5b_FGS.gff > tmp_gene
cut -f 9 tmp_gene > tmp_gene_attr
cut -f 1,4,5 tmp_gene | paste tmp_gene_attr - > final_file
  • grep -P 即以Perl脚本的格式来解释
  • cut -f 指定显示哪个区域
  • paste指令会把每个文件以列对列的方式,一列列地加以合并。此处的-表示管道前面cut的结果
    在这里插入图片描述
    那么,这样的代码,为什么不直接9,1,4,5,而是要如此麻烦的先提取第9列,再提取1,4,5列合并呢:其实是cut命令会对参数进行排序后再提取,即无论输入的时候是什么顺序,都会按1,4,5,9的方式提取。

(c )Sort this new file over Chromosome and End position

sort –k 2,2 –k 4,4n final_file > final_file_sorted
  • -k 2,2:这个选项指定了排序的依据。-k--key的缩写,用于指定一个或多个排序键。这里的2,2表示排序将基于每一行的第二个字段,并且只考虑第二个字段。

  • -k 4,4n:这是第二个排序键,它指定了如果第一个键(第二个字段)相等,则使用第四个字段作为排序的依据。4,4表示第四个字段,而n--numeric的缩写,意味着排序是基于数字值而不是字符串值进行的。
    在这里插入图片描述

(d)Examine the sorted file in a text editor

vi final_file_sorted
3.2 screen命令和scp命令的练习

该部分通过对screen命令和scp命令的练习,以实现多窗口的对话,以及从网络上下载和上传数据。

3.2.1 using “screen” to create a persistent session

(a)In the terminal window, type screen and hit Enter. You just opened the first window in your screen session.Type Ctrl-a c open two more screen windows.
(b)switch to the next window pressing Ctrl-a n,Switch to previous window using Ctrl-a p.

screen -list

在这里插入图片描述
(c )closing the Xshell terminal window, type screen –list. You should see the screen session you left behind.
(d)Type screen –d –r. This will re-connect you to your screen session.

screen -d -r 86639

在这里插入图片描述
(e)Detach your screen session using Ctrl-a d (all your windows will keep running).
在这里插入图片描述
(f)Terminate your screen session by hitting Ctrl-d in each window, Doing it in the last window will **terminate the screen session.**Your main Xshell terminal will keep running.

3.2.2 using scp to transfer files between Linux machines

(a) On server cbsufsrv7.biohpc.cornell.edu there is a directory /data1/Workshop/dir2transfer
with some test files in it – all readable to you.

scp –r s20223282034@cbsufsrv7.biohpc.cornell.edu:/data1/Workshop/dir2transfer .
wget ftp://cbsufsrv7.biohpc.cornell.edu:/data1/Workshop/dir2transfer #同样效果
  • 此处-r是递归复制整个目录

(b) create a text file called myid_file (replace myid with your actual userid) containing string “This is my test file”. Use your favorite text editor to create this file. Copy the newly created file onto cbsufsrv7.biohpc.cornell.edu into the directory /data1/Workshop

scp s20223282034_file s20223282034@cbsufsrv7.biohpc.cornell.edu:/data1/Workshop
3.3 bash脚本以及多线程操作
3.3.1 simple bash script

(a)Write a bash script (call it blast_script.sh) to launch a BLAST search using the input data downloaded previously and the command from the lecture

>blast_script.sh #创建文件
vi blast_script.sh #用vim打开
#!/bin/bash  #用bash解释该脚本
blastx -db ./databases/swissprot -query seq_tst.fa –out hits.txt >& blast.out
  • vim是一个无鼠标的文本编辑器,在此处按i进入插入模式,输入脚本后,按Esc退出插入模式,输入:wq!保存退出。
    在这里插入图片描述
  • >& 是将标准输出和标准错误输出同时重定向到blast.out文件中。

(b) Make sure the script is executable
©In another terminal, start the top program (top -u myID)
(d)Run the script . Observe the top output in the other window. How much CPU and memory is the run taking?

chmod u+x blast_script.sh
top -u s20223282034
sh blast_script.sh
  • chmod命令是控制用户对文件的权限的命令,只有文件所有者和超级用户可以修改文件或目录的权限。可以使用绝对模式(八进制数字模式)符号模式指定文件的权限。 上面的命令是符号模式,可读性较强,但书写略微麻烦。另一种写法是绝对模式,chmod 777 file,绝对模式需要了解1~7分别表示什么权限。

在这里插入图片描述

八进制权限rwx
7读 + 写 + 执行rwx
6读 + 写rw-
5读 + 执行r-x
4只读r–
3写 + 执行-wx
2只写-w-
1只执行–x
0
  • top -u 表示只实时监控特定的用户,而不是全部用户。

执行脚本,占用1个cpu(99.7%),占用0.1%的内存。在这里插入图片描述

3.3.2 running BLAST on two CPUs

(a) Modify the script blast_script.sh you created previously to

  1. run BLAST using two CPU threads(hint: use the –num_threads option)
  2. accept the name of the query FASTA file to be processed as an argument
vi blast_script.sh
#!/bin/bash
MYQUERY=$1
blastx –num_threads 2 -db ./databases/swissprot -query $MYQUERY -o ${MYQUERY}.out >& ${MYQUERY}.log
#退出vim
sh blast_script.sh seq_tst.fa & # &用于将命令放于后台执行,不中断对话

(b)Observe the top output in the other window. How much CPU and memory is the run taking now?
在这里插入图片描述
**执行脚本,占用2个cpu(199.7%),占用0.2%的内存。**相比于单线程,会占用更多更多的资源,但效率会更高。

3.3.3 Multiple independent tasks run sequentially
  1. Copy compressed files AAA_1.gz, AAA_2.gz, AAA_3.gz from /shared_data/Linux_workshop/auxfiles
  2. Write a bash script (call it gunzip_script.sh) to un-compress the three files using gunzip tool.
  3. Run gunzip_script.sh. Observe the top output in the other terminal window. How many running process do you see?
>gunzip_script.sh
vi gunzip_script.sh
#!/bin/bash
for i in {1..3}
do
    gunzip AAA_${i}.gz
done
#退出vim
sh gunzip_script.sh

在这里插入图片描述

  • 不加&会依次执行几个独立的任务。
3.3.4 Multiple independent tasks run in parallel
>gzip_script.sh
vi gzip_script.sh
#!/bin/bash
for i in {1..3}
do
   gzip BBB_${i} &
done
#退出vim
sh gzip_script.sh

在这里插入图片描述

  • &会并行执行几个独立的任务。

4. 实验总结

4.1 实验结论

Linux 是一种自由和开放源码的类 UNIX 操作系统。Linux 能运行主要的 UNIX 工具软件、应用程序和网络协议。它支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

4.2 实验收获

从上述实验中,进行了Linux的简单交互命令,目录操作,文件操作的学习,还对多视窗管理、文件的下载/上传、bash脚本的编写,多线程的运用进行了学习和练习,达到了初步运用Linux的目的。

4.3 其它心得

可以使用一个在线文档或者如菜鸟教程的网页多进行查询并多加练习,以致熟能生巧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值