% Created 2026-09-17 Thu 12:57
% Intended LaTeX compiler: lualatex
\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{fontspec}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{minted}
\author{samuel.ortion@universite-paris-saclay.fr}
\date{2026-09}
\title{TP n°2 Unix}
\hypersetup{
 pdfauthor={samuel.ortion@universite-paris-saclay.fr},
 pdftitle={TP n°2 Unix},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={},
 pdflang={English}}
\usepackage{biblatex}

\begin{document}

\maketitle
\section*{Get the data files}
\label{sec:org0018d72}

\begin{enumerate}
\item Download the \texttt{data.tar.gz} archive from \url{https://s.ortion.fr/w/course/mssb-computer-refresher/unix/tp/tp2/data.tar.gz}.
\item Extract the content of the archive into a folder \texttt{data}.
\item List the content of this folder.
\end{enumerate}
\section*{The \texttt{csv\_fields} bash function}
\label{sec:org2763b4c}

\begin{enumerate}
\item Create a bash function named \texttt{csv\_fields} that takes as argument the path to a CSV file and outputs in two columns: (1) the column index and (2) the column name.

Your function should be called as follows:

\begin{minted}[]{bash}
csv_fields table.csv
\end{minted}

\item Improve this function by adding an optional argument to allow the function user to specify the field separator character in function of the format of the file (comma, tabulation, etc.). By default the field separator should be the comma ','.

\item You can add your function \texttt{csv\_fields} to your \texttt{\textasciitilde{}/.bashrc} file in your home folder. Then, when you will launch a new bash session, or by typing \texttt{source \textasciitilde{}/.bashrc} you will load the function in the current shell environment.
\end{enumerate}
\subsection*{Hints}
\label{sec:org4c215b7}

For bash functions as well as shell scripts, to get the value of an argument, we use ``\$1'' for the first argument, ``\$2'' for the second and so on.

To define a function in bash, the syntax is the following:

\begin{minted}[]{bash}
my_function() {
    first_argument="$1"
    # enter the code of the function here
}
\end{minted}

To add an argument with a default value if the argument is not given, we can use the following syntax:

\begin{minted}[]{bash}
MY_VARIABLE=${VARIABLE_VALUE:-$DEFAULT_ARGUMENT_VARIABLE}
\end{minted}
With this syntax, the variable \texttt{MY\_VARIABLE} will take the value of the variable \texttt{DEFAULT\_VARIABLE\_VALUE}, when the variable \texttt{VARIABLE\_VALUE} is not defined.
\section*{Sort and join}
\label{sec:org94d8f99}

In the archive \texttt{data.tar.gz}, there are two files that contains information on the number of chromosomes that exists in some example model organisms.

The file \texttt{model\_names.dat} contains the name of the organism model species.
The file \texttt{model\_chromosomes.dat} contains the number of chromosomes each species contains.

Both files are formatted as tab-separated values file.

An identifier is set in the first column of both files.

Merge the files with sort and join.

Check the content of the merged file.

Source: \url{https://en.wikipedia.org/wiki/List\_of\_organisms\_by\_chromosome\_count}.
\section*{AWK on an EMBL file}
\label{sec:orga9b395f}

In the \texttt{data.tar.gz} archive you can find the file \texttt{./data/CP116886.1.embl} which is an EMBL file containing the genome of the \emph{Thermococcus kodakarensis}, a thermophile archeae.

AWK is a programming language dedicated to advanced file filtering on Unix.

The EMBL format is a fixed-width columnar format. To deal with such a file with AWK, we can use the special variable \texttt{FIELDWIDTH} to define the width of the columns of the file (\href{https://www.gnu.org/software/gawk/manual/html\_node/Fixed-width-data.html}{ref}).

\begin{enumerate}
\item Study the content of the EMBL file to get the proper column width and how to use the \texttt{FIELDWIDTH} variable in AWK.
\item Create a \texttt{awk} script to count the number of \texttt{gene} feature of an EMBL file.
\item Create a \texttt{awk} script to extract a table mapping the gene identifier (\texttt{/locus\_tag}) to the length of the gene sequence from an EMBL file.
\item Create an awk script to get the size of the genome recorded in the an EMBL.
\end{enumerate}
\subsection*{Hints}
\label{sec:orgbf531ed}

To use a script with \texttt{awk}, we use the option \texttt{-f}. A valid shebang for \texttt{awk} is \texttt{\#!/usr/bin/env -S awk -f}.
\end{document}
