Category: BADAS
Regular Expressions
A regular expression (aka regex) is a sequence of characters that describe or match a pattern of text. For example, the string aabb12 could be described as aabb12, two a’s, two’bs, then 1, then 2, or four letters followed by two numbers.
Keep in mind that regular expressions differ slightly per language but the theort and application remain the same. We will start with Python.
Metacharacters
Metacharacters are characters that have an alternate meaning rather than a literal meaning. There are many of these and they can be found on the Python regular expression documentation.
For example, we’ll take the following characters to search a text file:
– ‘A’
– ‘.’
– ‘$’
import re
text = 'Alan is the coolest man. Ever!$$$'
# compiling a regular expression allows you to reuse the regular expression.
A = re.compile('A')
dot = re.compile('.')
dollar = re.compile('$')
# Any pattern that is matches will be presented as an item within a list.
A.findall(text)
['A']
It matches ‘A’! No surprise here. Now let’s see what the ‘.’ does:
dot.findall(text)
['A',
'l',
'a',
'n',
' ',
'i',
's',
' ',
't',
'h',
'e',
' ',
'c',
'o',
'o',
'l',
'e',
's',
't',
' ',
'm',
'a',
'n',
'.',
' ',
'E',
'v',
'e',
'r',
'!',
'$',
'$',
'$']
It matched everything?! That’s because the ‘.’ is a special character that we will get into later.
Now let’s see what the dollar sign finds us:
dollar.findall(text)
['']
Nope, not a bug: ‘$’ did match something but it is also a special character.
Position Metacharacters
This type of regular expression is used to match characters based on where they are located as opposed to what the character means.
Let’s take the $
for example. I told you that it matched something, but nothing was returned in the output. That is because $
is a special character that matches the end of a line. Let’s try an example:
text = 'Alan is the coolest man ever, man'
# In regular expressions, backslashes tell the engine to interpret metacharacters as literals.
pattern = re.compile('man')
pattern.findall(text)
['man', 'man']
This is no surprise, it found both instances of ‘man’. Now, let’s say we only want to capture ‘man’ at the end of a string:
pattern = re.compile('man$')
pattern.findall(text)
['man']
Trust me, this is grabbing the last man. Don’t believe me?
text = 'Alan is the coolest man ever'
pattern.findall(text)
[]
Told ya. Now, what happens if we actually do want to match a dollar sign? Well, we can use an escape character, \
(backslash), before the dollar sign to tell the regex interpreter to use the literal meaning. Let’s use the first example:
text = 'Alan is the coolest man. Ever!$$$'
pattern = re.compile('$')
pattern.findall(text)
['']
Nothing. Let’s try to escape it:
pattern = re.compile('\$')
pattern.findall(text)
['$', '$', '$']
Now, what do you think this regular expression do?
pattern = re.compile('\$$')
pattern.findall(text)
['$']
Similarly, ‘^’ (caret, but not the kind that rabbits eat) matches the beginning of a line:
# IGNORECASE tells the intepreter match the meaning of the character, not character and case
pattern = re.compile('a', re.IGNORECASE)
pattern.findall(text)
['A', 'a', 'a']
And now with the caret:
pattern = re.compile('^a', re.IGNORECASE)
pattern.findall(text)
['A']
There are also boundary metacharacters. For example, ‘\b’ matches a word that ends with ‘ing’. Inversely, ‘\B’ matches a non-boundary word, so it would match the ‘ing’ in ‘things’ but not ‘thing’. This is useful for specifying substrings or whole words exclusively.
NOTE: some characters are treated as literals even when they are backslached. See here for more deltails:
https://stackoverflow.com/questions/2241600/python-regex-r-prefix
https://stackoverflow.com/questions/21104476/what-does-the-r-in-pythons-re-compiler-pattern-flags-mean
text = 'pistol'
# 'is' is surrounded by non-blank characters, not boundaries
pattern = re.compile(r'\bis\b')
print('word boundary',pattern.findall(text))
# However, it is surrounded by non-boundary characters, so it is found by non-word boundary searches
pattern = re.compile('\Bis\B')
print('non-word boundary',pattern.findall(text))
word boundary []
non-word boundary ['is']
text = 'is'
pattern = re.compile(r'\bis\b')
print('word boundary', pattern.findall(text))
pattern = re.compile('\Bis\B')
print('non-word boundary', pattern.findall(text))
word boundary ['is']
non-word boundary []
Single Metacharacters
These metacharacters match specific types of charactes. For example, you can match all alphanumeric characters with ‘\w’ or any whitespace character with ‘\s’
text = '123 abc !@#'
# any number
pattern = re.compile('\d')
pattern.findall(text)
['1', '2', '3']
# any alphanumeric character
pattern = re.compile('\w')
pattern.findall(text)
['1', '2', '3', 'a', 'b', 'c']
# any non-word character
pattern = re.compile('\W')
pattern.findall(text)
[' ', ' ', '!', '@', '#']
# any non-newline character
pattern = re.compile('.')
pattern.findall(text)
['1', '2', '3', ' ', 'a', 'b', 'c', ' ', '!', '@', '#']
Quantifiers
All examples before have been searching for individual characters. Quanitfiers allow you to match repeated patterns.
text = 'aa bb cdef 123'
# this looks for every instance of a word character
pattern = re.compile('\w')
pattern.findall(text)
['a', 'a', 'b', 'b', 'c', 'd', 'e', 'f', '1', '2', '3']
# the '+' tells the interpreter to look for one or more consecutive characters
pattern = re.compile('\w+')
pattern.findall(text)
['aa', 'bb', 'cdef', '123']
# '?' is looking for characters that appear once or not at all. It basically makes the preceding character optional.
text = 'colour'
pattern = re.compile('colou?r')
print('colour',pattern.findall(text))
text = 'color'
print('color', pattern.findall(text))
colour ['colour']
color ['color']
# Asterisk is doesn't require the preceding character to be there, but if it is it will match it
# will repeat the pattern as many times as is can.
text = 'b'
pattern = re.compile('bo*')
print('b',pattern.findall(text))
text = 'boo'
print('boo',pattern.findall(text))
text = 'boooo!'
print('boooo!', pattern.findall(text))
b ['b']
boo ['boo']
boooo! ['boooo']
# curly brackets define a number of times in which the preceding pattern will be repeated
pattern = re.compile('bo{2}')
text = 'boo'
print('boo',pattern.findall(text))
text = 'boooo!'
print('boooo!', pattern.findall(text))
boo ['boo']
boooo! ['boo']
Character Classes
A character class allows you to match a particular set of user defined characters as oppsed to the predefined metacharacters we went over previously. Think of searching for any vowel.
text = 'I like chocolate'
pattern = re.compile('[AEIOUY]', re.IGNORECASE)
print('vowels', pattern.findall(text))
vowels ['I', 'i', 'e', 'o', 'o', 'a', 'e']
Alternatively, when you use a caret ^
within the square brackets it will match the inverse of those defined in the character class. In this case, it will match all consonants.
pattern = re.compile('[^AEIOUY]', re.IGNORECASE)
print('consonants', pattern.findall(text))
consonants [' ', 'l', 'k', ' ', 'c', 'h', 'c', 'l', 't']
You can also specify ranges of characters if they are naturally consecutive by using a hyphen -
to separate the beginning and the end:
pattern = re.compile('[a-d]', re.IGNORECASE)
print('pattern', pattern.findall(text))
pattern ['c', 'c', 'a']
Alterations
This is essentially just an ‘or’ statement. An example would be if you are looking for whether a sentence says ‘we have ten dollars.’ or ‘I have ten dollars.’ Since the only varying piece of that sentence are the pronouns, you can try to match either.
text = 'I have ten dollars'
pattern = re.compile('we|i|they', re.IGNORECASE)
print('I', pattern.findall(text))
text = 'They have ten dollars'
print('They', pattern.findall(text))
text = 'We have ten dollars'
print('We', pattern.findall(text))
I ['I']
They ['They']
We ['We']
Backreferences
Back references/captures allow you to reuse regular expressions and/or patterns that match that regular expression.
Let’s give a biological example:
You’re looking for a motif that has flanking restriction sites ACTG. The motif can be of any length and any composition of nucleotides but is always flanked by those cuts sites:
text = 'ACTGTTTTTTTTTACTG'
# the '\1' is the refence to the first captured pattern.
# All patterns are ennumerated but can also be named.
print('matches:',re.search(r'(ACTG)([GTAC]+)\1', text).groups())
text = 'ATCGCAGCTACGACTGAAAAAAAAAAAAAAACTG'
print('matches:',re.search(r'(ACTG)([ACTG]+)\1', text).groups())
matches: ('ACTG', 'TTTTTTTTT')
matches: ('ACTG', 'AAAAAAAAAAAAAA')
Substitution Mode
There are several ways that one can use regular expressions, though the two main modes are searching and substituting.
Searching/matching will only look for whether or not a pattern is matched. Substituting will replace any pattern that is matched with another pattern. Above we have used only searching, so below will only showcase an example of a substitution.
text = 'ACTGTTTTTTTTTACTG'
re.sub(r'ACTG', 'AAAA', text)
'AAAATTTTTTTTTAAAA'
Command Line Examples
Constructing a Regular Expression
Arguably the most difficult task with regular expressions is being able to construct one. Often in cases I am trying extract information that match multiple different patterns or the same pattern but in different lines of text. In order to create a regular expression, you first need to identify the pattern. This generally requires some brief understanding of the file format and the relevant information as well as finding a pattern that applies to information that you’re interested in.
To give you some insight on how to do this, the examples below are scenarios that I often find myself in that are a perfect fit for regular expressions.
Parsing a GFF file
There are a few tools, such as sed, awk, and grep, that are used for text munging but I happen to use Perl, as it provides a lot of flexibility when doing complex regular expressions and other data munging tasks.
grep can only match patterns (or the inverse of patterns) and cannot be used to replace or transform text. Though limited, it is a nice tool to have for quick searches in files or across filesystems. In this example, all I need is to match a pattern, so grep will suffice.
A GFF file is a standard tab-delimited file format for genomic annotations. Most gff files contain every type of annotated feature for that organism (mRNA, exons, UTRs, motifs, etc.) which are not always relevant to the analysis and may sometimes may actuall interfere with the analysis.
Let’s say I want to extract all annotated mRNAs from this file. First thing is to understand the gff file format. Reading the documentation will tell you that it is tab delimited and the third column contains the feature type. So looking at the file will sho you:
! head -n 20 caenorhabditis_elegans.PRJNA13758.WBPS9.annotations.gff3
##gff-version 3
##sequence-region I 1 15072434
##sequence-region II 1 15279421
##sequence-region III 1 13783801
##sequence-region IV 1 17493829
##sequence-region V 1 20924180
##sequence-region X 1 17718942
##sequence-region MtDNA 1 13794
I BLAT_EST_OTHER expressed_sequence_match 1 50 12.8 - . ID=yk585b5.5.6;Target=yk585b5.5 119 168 +
I BLAT_Trinity_OTHER expressed_sequence_match 1 52 20.4 + . ID=elegans_PE_SS_GG6116|c0_g1_i1.2;Target=elegans_PE_SS_GG6116|c0_g1_i1 174 225 +
I inverted inverted_repeat 1 212 66 . . Note=loop 426
I Genbank assembly_component 1 2679 . + . genbank=FO080985
I Genomic_canonical assembly_component 1 2679 . + . Name=cTel33B;Note=Clone:cTel33B,GenBank:FO080985
I Variation_project_Polymorphism tandem_duplication 1 11000 . + . variation=WBVar02123961;public_name=WBVar02123961;other_name=cewivar00854884;strain=JU533;polymorphism=1;consequence=Coding_exon
I interpolated_pmap_position gene 1 559784 . . . ID=gmap:spe-13;gmap=spe-13;status=uncloned;Note=-21.3602 cM (+/- 1.84 cM)
I Balanced_by_balancer biological_region 1 5263413 . . . balancer=Rearrangement:hT3;balancer_type=Translocation;Note=Summary: Translocation (rigorous proof of reciprocity lacking)%252C moderately well characterized%252C very stable. Very effective balancer for left portion of LG I from left end to around let-363%252C and the right portion of LG X from the right end to between dpy-7 and unc-3. hT3(I)%252C which disjoins from normal LG I%252C is probably LG X (right) translocated to LG I (right). hT3(X)%252Cwhich disjoins from normal LG X%252C is probably LG I (left) translocated to LG X (left).,Growth characteristics: Original isolate marked with dpy-5 and unc-29. Homozygous inviable%252C probably breaks in let-363 (I). Heterozygotes exhibit reduced viability%252C low level of X chromosome nondisjunction (1.2%25).,Handling: Easy to manipulate. Rare exceptional progeny carry one half-translocation as a free duplication. Recombination frequency in unbalanced intervals increased on both LG I and LG X.,Recommended use: General balancing%252C strain maintenance
I Balanced_by_balancer biological_region 1 7383197 . . . balancer=Rearrangement:sDp2;balancer_type=Duplication;Note=Summary: Free duplication%252C well characterized%252C does not recombine with normal homologues. Very effective balancer for the left portion of LG I from the left end through unc-15 (just left of unc-13).,Handling: sDp2-bearing males mate and give some progeny%252C but are slow growing and do not compete well with non-Dp males in mating.,Recommended use: General balancing%252C strain maintenance%252C mutant screens.
I Balanced_by_balancer biological_region 1 7454088 . . . balancer=Rearrangement:szDp1;balancer_type=Duplication;Note=Summary: Complex free duplication%252C well characterized%252C does not recombine with normal LG I. Very effective balancer for the left portion of LG I from the left end through unc-13. Consists of one half-translocation [szT1(X)] from szT1 maintained in addition to a normal chromosome complement.,Growth characteristics: Animals carrying two copies of szDp1 apparently inviable. Duplication strains give rise to spontaneous males through meiotic nondisjunction of the X chromosome.,Handling: szDp1-bearing males either do not mate or are infertile.,Recommended use:
I Balanced_by_balancer biological_region 1 7454088 . . . balancer=Rearrangement:szT1;balancer_type=Translocation;Note=Summary: Reciprocal translocation%252C well characterized%252C very stable. Effective balancer for left portion of LG I through unc-13%252C nearly all of LG X from right end to around dpy-3. szT1(I) is large segment of LG X (right) translocated to LG I%252C disjoins from normal LG I. szT1(X) is LG I (left) translocated to fragment of LG X (left)%252C disjoins from normal LG X.,Handling: Easy to manipulate. Lon-2 szT1 males mate well. Rare exceptional progeny carry one half-translocation as a complex free duplication. Gives rise spontaneously to rare apparent lethal mutations that may represent fusion of szT1(X) and the normal X. Shows threefold enhanced recombination frequency immediately adjacent to right of LG I breakpoint and about twofold enhanced frequency in the unc-101 - unc-54 interval.,Recommended use: General balancing%252C strain construction%252C strain maintenance.
I Balanced_by_balancer biological_region 1 8244513 . . . balancer=Rearrangement:hT1;balancer_type=Translocation;Note=Summary: Reciprocal translocation%252C well characterized%252C very stable. Very effective balancer for left portion of LG I from the left end through let-80%252C and the left portion of LG V from the left end through dpy-11. hT1(I) is LG V (left) translocated to LG I (right)%252C disjoins from normal LG I. hT1(V) is LG I (left) translocated to LG V (right)%252C disjoins from normal LG V.,Growth characteristics: Homozygous inviable%252C cause unknown. Arrests at L3. Brood size in heterozygotes ~75. Easy to manipulate. Heterozygous males mate well. Rare exceptional progeny carry one half-translocation as a complex free duplication. Recombination frequency in the unbalanced unc-101 - unc-54 interval on LG I is increased twofold.,Handling: Easy to manipulate. Heterozygous males mate well. Rare exceptional progeny carry one half-translocation as a complex free duplication. Recombination frequency in the unbalanced unc-101 - unc-54 interval on LG I is increased twofold.,Recommended use: General balancing%252C strain maintenance. mutant screens.
Looking at the entire file is daunting and doesn’t provide a list of features found within this file. So, we can use a few commands to get a full representation of each feature type described in the file.
! cut -f3 caenorhabditis_elegans.PRJNA13758.WBPS9.annotations.gff3 | sort | uniq
antisense_RNA
assembly_component
base_call_error_correction
binding_site
biological_region
CDS
complex_substitution
conserved_region
deletion
DNAseI_hypersensitive_site
duplication
enhancer
exon
experimental_result_region
expressed_sequence_match
five_prime_UTR
gene
##gff-version 3
G_quartet
histone_binding_site
insertion_site
intron
inverted_repeat
lincRNA
low_complexity_region
miRNA
miRNA_primary_transcript
mRNA
mRNA_region
nc_primary_transcript
ncRNA
nucleotide_match
operon
PCR_product
piRNA
point_mutation
polyA_signal_sequence
polyA_site
polypeptide_motif
possible_base_call_error
pre_miRNA
promoter
protein_coding_primary_transcript
protein_match
pseudogenic_rRNA
pseudogenic_transcript
pseudogenic_tRNA
reagent
regulatory_region
repeat_region
RNAi_reagent
rRNA
SAGE_tag
scRNA
##sequence-region I 1 15072434
##sequence-region II 1 15279421
##sequence-region III 1 13783801
##sequence-region IV 1 17493829
##sequence-region MtDNA 1 13794
##sequence-region V 1 20924180
##sequence-region X 1 17718942
SL1_acceptor_site
SL2_acceptor_site
snoRNA
SNP
snRNA
substitution
tandem_duplication
tandem_repeat
TF_binding_site
three_prime_UTR
transcribed_fragment
transcription_end_site
transcript_region
translated_nucleotide_match
transposable_element
transposable_element_insertion_site
tRNA
TSS_region
We see that there are a lot of RNAs here, and there happen to be two different mRNA feature types. So, now that we know the feature type and the format we can construct our regular expression:
# The -P tells grep to use perl regular expressions which make things a bit easier otherwise you'll have
# to escape some characters!
# The pattern we're looking for is mRNA surrounded by tabs/white space
! grep -P '\tmRNA\t' caenorhabditis_elegans.PRJNA13758.WBPS9.annotations.gff3 | head -n 20
I WormBase mRNA 4116 10230 . - . ID=Transcript:Y74C9A.3;Parent=Gene:WBGene00022277;Name=Y74C9A.3;wormpep=WP:CE28146;locus=homt-1
I WormBase mRNA 11495 16793 . + . ID=Transcript:Y74C9A.2a.1;Parent=Gene:WBGene00022276;Name=Y74C9A.2a.1;wormpep=WP:CE24660;locus=nlp-40
I WormBase mRNA 11495 16837 . + . ID=Transcript:Y74C9A.2a.2;Parent=Gene:WBGene00022276;Name=Y74C9A.2a.2;wormpep=WP:CE24660;locus=nlp-40
I WormBase mRNA 11499 16837 . + . ID=Transcript:Y74C9A.2a.3;Parent=Gene:WBGene00022276;Name=Y74C9A.2a.3;wormpep=WP:CE24660;locus=nlp-40
I WormBase mRNA 11505 16837 . + . ID=Transcript:Y74C9A.2a.4;Parent=Gene:WBGene00022276;Name=Y74C9A.2a.4;wormpep=WP:CE24660;locus=nlp-40
I WormBase mRNA 11618 16837 . + . ID=Transcript:Y74C9A.2a.5;Parent=Gene:WBGene00022276;Name=Y74C9A.2a.5;wormpep=WP:CE24660;locus=nlp-40
I WormBase mRNA 11623 16837 . + . ID=Transcript:Y74C9A.2b;Parent=Gene:WBGene00022276;Name=Y74C9A.2b;wormpep=WP:CE49228;locus=nlp-40
I WormBase mRNA 17487 26781 . - . ID=Transcript:Y74C9A.4b;Parent=Gene:WBGene00022278;Name=Y74C9A.4b;wormpep=WP:CE28147;locus=rcor-1
I WormBase mRNA 17497 24796 . - . ID=Transcript:Y74C9A.4d;Parent=Gene:WBGene00022278;Name=Y74C9A.4d;wormpep=WP:CE49439;locus=rcor-1
I WormBase mRNA 17497 26643 . - . ID=Transcript:Y74C9A.4c;Parent=Gene:WBGene00022278;Name=Y74C9A.4c;wormpep=WP:CE49153;locus=rcor-1
I WormBase mRNA 17497 26781 . - . ID=Transcript:Y74C9A.4a;Parent=Gene:WBGene00022278;Name=Y74C9A.4a;wormpep=WP:CE24662;locus=rcor-1
I WormBase mRNA 27591 32544 . - . ID=Transcript:Y74C9A.5;Parent=Gene:WBGene00022279;Name=Y74C9A.5;wormpep=WP:CE40291;locus=sesn-1
I WormBase mRNA 43733 44677 . + . ID=Transcript:Y74C9A.1;Parent=Gene:WBGene00022275;Name=Y74C9A.1;wormpep=WP:CE34428
I WormBase mRNA 47467 49857 . + . ID=Transcript:Y48G1C.12;Parent=Gene:WBGene00044345;Name=Y48G1C.12;wormpep=WP:CE38647
I WormBase mRNA 49919 54426 . + . ID=Transcript:Y48G1C.4a;Parent=Gene:WBGene00021677;Name=Y48G1C.4a;wormpep=WP:CE30021;locus=pgs-1
I WormBase mRNA 52292 54360 . + . ID=Transcript:Y48G1C.4b;Parent=Gene:WBGene00021677;Name=Y48G1C.4b;wormpep=WP:CE49150;locus=pgs-1
I WormBase mRNA 55293 64066 . - . ID=Transcript:Y48G1C.5;Parent=Gene:WBGene00021678;Name=Y48G1C.5;wormpep=WP:CE39437
I WormBase mRNA 71425 81071 . + . ID=Transcript:Y48G1C.2b.2;Parent=Gene:WBGene00000812;Name=Y48G1C.2b.2;wormpep=WP:CE49183;locus=csk-1
I WormBase mRNA 71425 81063 . + . ID=Transcript:Y48G1C.2b.1;Parent=Gene:WBGene00000812;Name=Y48G1C.2b.1;wormpep=WP:CE49183;locus=csk-1
I WormBase mRNA 71845 80633 . + . ID=Transcript:Y48G1C.2a.1;Parent=Gene:WBGene00000812;Name=Y48G1C.2a.1;wormpep=WP:CE34405;locus=csk-1
grep: write error
Renaming FastQ Files
The filenames for sequences are generally too many characters an too much information, which can be annoying when working with those files. To extract only the relevant information and shorten the filename I often construct a regular expression, coupled with some bash commands, to rename the files how I deem fit.
ls /home/at120/badas/2015-03-11_AE52Y-redo
[0m[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a1.341000000083f5.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a2.3410000000847d.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a3.341000000084f4.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a4.3410000000857c.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a5.341000000085f3.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a6.3410000000867b.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a7.341000000086f2.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated a8.3410000000877a.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated b1.34100000008403.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated b2.3410000000848a.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated b3.34100000008502.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated b4.34100000008589.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated b5.34100000008601.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated b6.34100000008688.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated b7.34100000008700.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated c1.34100000008410.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated c2.34100000008497.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated c3.3410000000851f.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated c4.34100000008596.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated c5.3410000000861e.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated c6.34100000008695.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated c7.3410000000871d.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated d1.3410000000842d.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated d2.341000000084a4.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated d3.3410000000852c.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated d4.341000000085a3.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated d5.3410000000862b.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated d6.341000000086a2.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated d7.3410000000872a.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated e1.3410000000843a.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated e2.341000000084b0.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated e3.34100000008539.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated e4.341000000085bf.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated e5.34100000008638.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated e6.341000000086be.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated e7.34100000008737.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated f1.34100000008447.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated f2.341000000084cd.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated f3.34100000008546.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated f4.341000000085cc.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated f5.34100000008645.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated f6.341000000086cb.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated f7.34100000008744.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated g1.34100000008454.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated g2.341000000084da.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated g3.34100000008553.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated g4.341000000085d9.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated g5.34100000008652.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated g6.341000000086d8.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated g7.34100000008751.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated h1.34100000008460.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated h2.341000000084e7.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated h3.3410000000856f.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated h4.341000000085e6.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated h5.3410000000866e.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated h6.341000000086e5.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n01 flu mrt-pcr updated h7.3410000000876d.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a1.342000000083f2.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a2.3420000000847a.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a3.342000000084f1.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a4.34200000008579.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a5.342000000085f0.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a6.34200000008678.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a7.342000000086ff.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated a8.34200000008777.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated b1.34200000008400.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated b2.34200000008487.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated b3.3420000000850f.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated b4.34200000008586.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated b5.3420000000860e.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated b6.34200000008685.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated b7.3420000000870d.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated c1.3420000000841d.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated c2.34200000008494.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated c3.3420000000851c.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated c4.34200000008593.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated c5.3420000000861b.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated c6.34200000008692.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated c7.3420000000871a.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated d1.3420000000842a.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated d2.342000000084a1.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated d3.34200000008529.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated d4.342000000085a0.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated d5.34200000008628.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated d6.342000000086af.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated d7.34200000008727.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated e1.34200000008437.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated e2.342000000084bd.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated e3.34200000008536.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated e4.342000000085bc.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated e5.34200000008635.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated e6.342000000086bb.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated e7.34200000008734.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated f1.34200000008444.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated f2.342000000084ca.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated f3.34200000008543.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated f4.342000000085c9.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated f5.34200000008642.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated f6.342000000086c8.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated f7.34200000008741.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated g1.34200000008451.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated g2.342000000084d7.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated g3.34200000008550.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated g4.342000000085d6.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated g5.3420000000865f.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated g6.342000000086d5.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated g7.3420000000875e.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated h1.3420000000846d.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated h2.342000000084e4.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated h3.3420000000856c.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated h4.342000000085e3.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated h5.3420000000866b.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated h6.342000000086e2.fastq.gz[0m*
[38;5;34m000000000-AE52Y l01n02 flu mrt-pcr updated h7.3420000000876a.fastq.gz[0m*
There is a lot of information here, some of which is intelligible only to Tubo. The only information that I would like to retain are the sample names, consisting of one lowercase letter followed by one number and the pairing information, which is n0 and then a 1 or a 2.
What I usually do to get started is to work with one filename and construct the pattern from that. Regular expressions are read from left to right, so we should start by grabbing the first bit of information we want, which is the pair information:
%%bash
echo "000000000-AE52Y l01n02 flu mrt-pcr updated f3.34200000008543.fastq.gz" | perl -pe 's/^.+n0([12]).+/$1/g'
2
The syntax for a perl substitution regular expression is as follows:
perl -pe '<mode>/<pattern>/<replacement text>/<modifier>'
mode can be:
– substitution s
– match m
– translate tr
pattern is your regular expression
replacement text is the text to replace text that matches your regular expression
modifier can be:
– ignore case i
– global g
: will find all patterns, not just the first
and some others. Take a look at the quick reference for perl regular expressions for more information.
Next would be to grab the sample name:
%%bash
# It's good to be very specific with regular expressions for multiple reasons.
# 1) It limits the scope of the regular expression
# 2) It makes it easier to read later.
# So I do not NEED to specify 'flu' but it makes for fewer instructions and is more human readable.
echo "000000000-AE52Y l01n02 flu mrt-pcr updated f3.34200000008543.fastq.gz" \
| perl -pe 's/^.+n0([12])\sflu.+\s([a-z]\d)\..+/$1$2/g'
2f3
Lastly, the sample name and file extension and rearrange the caputred patterns:
%%bash
echo "000000000-AE52Y l01n02 flu mrt-pcr updated f3.34200000008543.fastq.gz" \
| perl -pe 's/^.+n0([12])\sflu.+\s([a-z]\d)\..+(fastq.gz)$/$2.r$1.$3/g'
f3.r2.fastq.gz
Now to wrap it up in a for loop and invoke a copy command:
%%bash
cd /home/at120/badas/2015-03-11_AE52Y-redo
for i in *gz; do cp "$i" `echo $i | perl -pe 's/^.+n0([12])\sflu.+\s([a-z]\d)\..+(fastq.gz)$/$2.r$1.$3/g'` ; done
ls
000000000-AE52Y l01n01 flu mrt-pcr updated a1.341000000083f5.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated a2.3410000000847d.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated a3.341000000084f4.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated a4.3410000000857c.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated a5.341000000085f3.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated a6.3410000000867b.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated a7.341000000086f2.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated a8.3410000000877a.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated b1.34100000008403.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated b2.3410000000848a.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated b3.34100000008502.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated b4.34100000008589.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated b5.34100000008601.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated b6.34100000008688.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated b7.34100000008700.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated c1.34100000008410.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated c2.34100000008497.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated c3.3410000000851f.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated c4.34100000008596.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated c5.3410000000861e.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated c6.34100000008695.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated c7.3410000000871d.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated d1.3410000000842d.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated d2.341000000084a4.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated d3.3410000000852c.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated d4.341000000085a3.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated d5.3410000000862b.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated d6.341000000086a2.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated d7.3410000000872a.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated e1.3410000000843a.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated e2.341000000084b0.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated e3.34100000008539.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated e4.341000000085bf.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated e5.34100000008638.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated e6.341000000086be.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated e7.34100000008737.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated f1.34100000008447.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated f2.341000000084cd.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated f3.34100000008546.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated f4.341000000085cc.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated f5.34100000008645.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated f6.341000000086cb.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated f7.34100000008744.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated g1.34100000008454.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated g2.341000000084da.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated g3.34100000008553.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated g4.341000000085d9.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated g5.34100000008652.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated g6.341000000086d8.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated g7.34100000008751.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated h1.34100000008460.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated h2.341000000084e7.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated h3.3410000000856f.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated h4.341000000085e6.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated h5.3410000000866e.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated h6.341000000086e5.fastq.gz
000000000-AE52Y l01n01 flu mrt-pcr updated h7.3410000000876d.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a1.342000000083f2.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a2.3420000000847a.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a3.342000000084f1.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a4.34200000008579.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a5.342000000085f0.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a6.34200000008678.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a7.342000000086ff.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated a8.34200000008777.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated b1.34200000008400.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated b2.34200000008487.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated b3.3420000000850f.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated b4.34200000008586.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated b5.3420000000860e.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated b6.34200000008685.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated b7.3420000000870d.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated c1.3420000000841d.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated c2.34200000008494.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated c3.3420000000851c.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated c4.34200000008593.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated c5.3420000000861b.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated c6.34200000008692.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated c7.3420000000871a.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated d1.3420000000842a.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated d2.342000000084a1.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated d3.34200000008529.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated d4.342000000085a0.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated d5.34200000008628.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated d6.342000000086af.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated d7.34200000008727.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated e1.34200000008437.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated e2.342000000084bd.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated e3.34200000008536.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated e4.342000000085bc.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated e5.34200000008635.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated e6.342000000086bb.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated e7.34200000008734.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated f1.34200000008444.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated f2.342000000084ca.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated f3.34200000008543.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated f4.342000000085c9.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated f5.34200000008642.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated f6.342000000086c8.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated f7.34200000008741.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated g1.34200000008451.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated g2.342000000084d7.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated g3.34200000008550.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated g4.342000000085d6.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated g5.3420000000865f.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated g6.342000000086d5.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated g7.3420000000875e.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated h1.3420000000846d.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated h2.342000000084e4.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated h3.3420000000856c.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated h4.342000000085e3.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated h5.3420000000866b.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated h6.342000000086e2.fastq.gz
000000000-AE52Y l01n02 flu mrt-pcr updated h7.3420000000876a.fastq.gz
a1.r1.fastq.gz
a1.r2.fastq.gz
a2.r1.fastq.gz
a2.r2.fastq.gz
a3.r1.fastq.gz
a3.r2.fastq.gz
a4.r1.fastq.gz
a4.r2.fastq.gz
a5.r1.fastq.gz
a5.r2.fastq.gz
a6.r1.fastq.gz
a6.r2.fastq.gz
a7.r1.fastq.gz
a7.r2.fastq.gz
a8.r1.fastq.gz
a8.r2.fastq.gz
b1.r1.fastq.gz
b1.r2.fastq.gz
b2.r1.fastq.gz
b2.r2.fastq.gz
b3.r1.fastq.gz
b3.r2.fastq.gz
b4.r1.fastq.gz
b4.r2.fastq.gz
b5.r1.fastq.gz
b5.r2.fastq.gz
b6.r1.fastq.gz
b6.r2.fastq.gz
b7.r1.fastq.gz
b7.r2.fastq.gz
c1.r1.fastq.gz
c1.r2.fastq.gz
c2.r1.fastq.gz
c2.r2.fastq.gz
c3.r1.fastq.gz
c3.r2.fastq.gz
c4.r1.fastq.gz
c4.r2.fastq.gz
c5.r1.fastq.gz
c5.r2.fastq.gz
c6.r1.fastq.gz
c6.r2.fastq.gz
c7.r1.fastq.gz
c7.r2.fastq.gz
d1.r1.fastq.gz
d1.r2.fastq.gz
d2.r1.fastq.gz
d2.r2.fastq.gz
d3.r1.fastq.gz
d3.r2.fastq.gz
d4.r1.fastq.gz
d4.r2.fastq.gz
d5.r1.fastq.gz
d5.r2.fastq.gz
d6.r1.fastq.gz
d6.r2.fastq.gz
d7.r1.fastq.gz
d7.r2.fastq.gz
e1.r1.fastq.gz
e1.r2.fastq.gz
e2.r1.fastq.gz
e2.r2.fastq.gz
e3.r1.fastq.gz
e3.r2.fastq.gz
e4.r1.fastq.gz
e4.r2.fastq.gz
e5.r1.fastq.gz
e5.r2.fastq.gz
e6.r1.fastq.gz
e6.r2.fastq.gz
e7.r1.fastq.gz
e7.r2.fastq.gz
f1.r1.fastq.gz
f1.r2.fastq.gz
f2.r1.fastq.gz
f2.r2.fastq.gz
f3.r1.fastq.gz
f3.r2.fastq.gz
f4.r1.fastq.gz
f4.r2.fastq.gz
f5.r1.fastq.gz
f5.r2.fastq.gz
f6.r1.fastq.gz
f6.r2.fastq.gz
f7.r1.fastq.gz
f7.r2.fastq.gz
g1.r1.fastq.gz
g1.r2.fastq.gz
g2.r1.fastq.gz
g2.r2.fastq.gz
g3.r1.fastq.gz
g3.r2.fastq.gz
g4.r1.fastq.gz
g4.r2.fastq.gz
g5.r1.fastq.gz
g5.r2.fastq.gz
g6.r1.fastq.gz
g6.r2.fastq.gz
g7.r1.fastq.gz
g7.r2.fastq.gz
h1.r1.fastq.gz
h1.r2.fastq.gz
h2.r1.fastq.gz
h2.r2.fastq.gz
h3.r1.fastq.gz
h3.r2.fastq.gz
h4.r1.fastq.gz
h4.r2.fastq.gz
h5.r1.fastq.gz
h5.r2.fastq.gz
h6.r1.fastq.gz
h6.r2.fastq.gz
h7.r1.fastq.gz
h7.r2.fastq.gz
! echo "TAAAAAA" | perl -pe 's/([ACTG]){3}//g'
TAAAAAA
Customizing Your Unix Environment
This walkthrough is designed to show users some interesting ways to use the command line and set it up in a way that makes it most comfortable.
Getting Started
In order to follow the steps exactly as described in this tutorial you will need the following:
- A personal computer with an ssh client
- An ssh connection to a server (HPC, standalone, etc)
- optional: a VPN connection to the network where the remote server is hosted.
What is a Unix Environment?
A Unix environment is composed of three components:
1) The kernel: this is the master control program of the operating system. This handles everything that make a computer a computer, such as I/O streams, booting, peripherals, etc.
2) File system: this is a hierarchical file system. This is the system in which the operating system keeps track of all the files and folders it needs in order to function. An example filesystem is seen in the figure below:
3) The shell aka. terminal, command prompt: This is the interface between the user and the kernel. The main purpose of the shell is to take a command or series of commands and sends them to the kernel to be processed into a set of instructions. This is place where you will spend most of your time analyzing data, so may as well make it comfortable for you to work in, which is the main objective in this tutorial.
.bashrc, .bash_profile, and .profile files
Each if these files could be considered a config file – they all help in making your terminal environment the way you want, though each has a particular purpose within the shell. Each of these are found in your home directory (/home/$USER/
) which we will access via the shortcut ~
or ~/
, which just saves you from having to type the full path to your home directory.
A note about hidden files: as you may have noticed, there is a preceding .
in front of the filenames. This is Unix/Linux notation to hide a file by default when looking into a directory. This is because you most likely won’t want to see these files every time you ls
your home directory (they can start to clutter things) but also because these are generally very important files that are critical to the way your account functions, so tampering with these by mistake could be very serious. In order to view them, you can run ls -a
which will list everything in the directory. To access them, use the complete filename, including the .
.
.bashrc
This is the main file where you’re add most of your configurations, such as aliases, PATH variable, color schemes, etc. Unlike the other configuration files, this affects every terminal instance as opposed to only the login shell, which is handled by `.bash_profile’.
A few things that are defined in this file are:
* set the $PS1 variable, which displays hostname and current directory
* set the $PATH variable (discussed below)
* aliases
* history settings
IMPORTANT: this file should never output anything! You will run into some very frustrating situations if it does.
.bash_profile
This handles the login nodes, i.e. as soon as you ssh
or log into a computer, this is the file that gets loaded. The reason being is that sometimes you want to view diagnostics of the machine that you’re logging into (how long has it been running, are there any updates that need to be installed, etc.) which you wouldn’t want to see in every other terminal instance. Unfortunately this does not have the same settings as the .bashrc
file, so your path will not work unless you load your .bashrc
file from within. To do so, you can add the following lines to your .bash_profile
file (it may already be there, so check before you add this):
if [ -f $HOME/.bashrc ]; then
source $HOME/.bashrc
fi
# likewise for .profile
if [ -f $HOME/.profile]; then
source $HOME/.profile
fi
.profile
This file isn’t used very often, however one thing to note: anything that should be available to graphical applications or sh MUST go here.
TMUX Session
Tmux is a terminal multiplexer that enables a number of terminals (or windows) to be created inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command-line interface, and for separating programs from the Unix shell that started the program. For the full set of features please check the man page or this handy cheatsheet.
To get started, ssh into your server:
ssh user@server
IMPORTANT: If you are using Dalma, please note that tmux is not installed on this system. Please skip to the next section of this tutorial or find another server to test this on.
To start a tmux sessions:
tmux
Or, you can name the tmux session to help keep things organized. In this example, the tmux session we will use is named ‘test’:
tmux new -s test
To detach from a tmux session (log out of the session but keep it running) press ctrl+b
then d
To see existing sessions on you account
tmux ls # or tmux list-sessions
NOTE: your tmux sessions will not appear if you are not on the same login node they were created on. For example, prince has log-0
and log-1
. A tmux session created on log-0
will not be accessible to that on log-1 and vice versa. Please see the session on setting up your ssh alias below.
To log back into your tmux session:
tmux attach
Or, of you have multiple sessions:
tmux attach -t # or test, in the case of this example
Script Command
The script
command is a way to log and replay the commands that were entered during a script session. This is useful for sharing workflows and keeping track of what has been done.
While in a tmux
session, start a script
session to begin logging you commands and name it test_commands
:
script test_commands
To end a script
session and dump the commands into a file type exit
or ctrl+d
. The results will be saved into a folder named test_commands
within your working directory. If no filename was entered provided they will be saved to a file name typescript
.
To append to an existing script file:
# in this example, filename is test_commands
script -a
Downloading, Compiling, and Adding Software to the $PATH
Download and Compile
There are often times when us users do not have sufficient permissions to install software on a server so then we have to wait for someone with permissions to do it for me. As a workaround, you can download and compile (NOT install), and run packages yourself.
The first step is to grab the package you’re interested in. We can use this using wget
, curl
, or git clone
. In this example, we will grab BWA using git clone
. This will download the BWA source code into the current directory (it is good practice to compile all of your software into one directory, say /scratch/$USER/software). Once it is done, we can compile the software using make
. There are other ways to compile software, but this is one of the more common ways so we will stick with this.
# Download the repository using git
git clone https://github.com/lh3/bwa.git
# change into the bwa directory
cd bwa
# compile using mak
make
# once it is done, you can try to run it bwa
./bwa
# You should see the help output from bwa.
# If you do then it works! If not, you may need to make the `bwa` binary file executable using `chmod +x ./bwa`
PATH Variable
Now that we have it running, we can continue in our analysis. However, it’s pretty annoying that you have to write /scratch/$USER/software/bwa/bwa
every time you want to run BWA; why can’t we just type bwa
like all the other commands (ls
, pwd
, cp
)? Afterall, it is a command, isn’t it? Well, in fact you can! This is where the PATH variable comes into play. For normal, distributed commands (such as those above) the path variable points to a directory that holds the binary executable files in it, telling your user account where to find them. So, if we print the $PATH variable:
echo $PATH
/opt/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
You might see something like this, a list of directory paths, separated by :
. If we ls
one of those directories, say /bin
:
ls /bin
bash bzmore dir fuser kill lsblk nc.openbsd ntfsfix pwd sh systemd-inhibit uname zfgrep
bunzip2 cat dmesg fusermount kmod lsmod netcat ntfsinfo rbash sh.distrib systemd-machine-id-setup uncompress zforce
busybox chacl dnsdomainname getfacl less mkdir netstat ntfsls readlink sleep systemd-notify unicode_start zgrep
bzcat chgrp domainname grep lessecho mknod networkctl ntfsmove red ss systemd-tmpfiles vdir zless
bzcmp chmod dumpkeys gunzip lessfile mktemp nisdomainname ntfstruncate rm static-sh systemd-tty-ask-password-agent vmmouse_detect zmore
bzdiff chown echo gzexe lesskey more ntfs-3g ntfswipe rmdir stty tailf wdctl znew
bzegrep chvt ed gzip lesspipe mount ntfs-3g.probe open rnano su tar which zsh
bzexe cp efibootmgr hciconfig ln mountpoint ntfs-3g.secaudit openvt run-parts sync tempfile whiptail zsh5
bzfgrep cpio egrep hostname loadkeys mt ntfs-3g.usermap pidof rzsh systemctl touch ypdomainname
bzgrep dash false ip login mt-gnu ntfscat ping sed systemd true zcat
bzip2 date fgconsole journalctl loginctl mv ntfscluster ping6 setfacl systemd-ask-password udevadm zcmp
bzip2recover dd fgrep kbd_mode lowntfs-3g nano ntfscmp plymouth setfont systemd-escape ulockmgr_server zdiff
bzless df findmnt keyctl ls nc ntfsfallocate ps setupcon systemd-hwdb umount zegrep
These are a lot of the commands you use on a daily basis in the terminal (ls
, pwd
, cp
). The $PATH variable is how your account can keep track of what commands to provide your user as different users have different setups, privileges, software, etc.. So now that we have a folder called software where we’re going to house all of our compiled software, we can tell the $PATH variable to look into our software
directory for binaries that we would like in our path so that we may access them from anywhere within the filesystem. If a file is executable then that and it resides in one of the paths in our $PATH variable, it can be executed from anywhere.
To do this, we need to append to the path (either the full or relative are fine) to the $PATH variable. IMPORTANT: Make sure that you follow the commands exactly and not overwrite the $PATH variable but append to it. If you overwrite it, the operating system will not be able to find any of the necessary commands since their paths will not be found in the $PATH variable.
There are two ways to add the new path to the $PATH variable:
# To append to the path variable for the current session only (until you log out)
export PATH=$PATH:/scratch/$USER/software/bwa
# To verify, you can use print your $PATH variable to see if it now contains the new path
echo $PATH
# To permanently add a path to your you will need to make this edit in your `.bashrc` file
vim ~/.bashrc
# Then at the top add the same command we used above and then save and quit
export PATH=$PATH:/scratch/$USER/software/bwa
# To reload your terminal environment with these new changes we will need to `source` the `.bashrc` file
source ~/.bashrc
# Now if you try to run `bwa` it should work. If not, then try logging out of the of your session and log back in
bwa
# If you get the same output as ./bwa from the previous step, then congratulations!
Command History
Now that you know how to edit and reload the .bashrc
file, we should go ahead and make some more changes that may be convenient for you.
It is common practice to search your command history to see all the commands that were executed previously on your account (you can do so with the history
command or search with ctrl+r
). However, what if you enter 100,000 commands? Most accounts won’t remember that many by default, they will only remember the last 1000, even if some commands are redundant. To change this to store all commands (or nearly all), you can add these lines to the .bashrc
file:
vim ~/.bashrc
# Removes duplicated commands and doesn’t store commands that start with a space
HISTCONTROL=ignoreboth
Sets the filesize (chances are you won’t fill this up for several years)
HISTFILESIZE=10000000
Number of lines that are store in memory while your session is ongoing
HISTSIZE=100000
# save and quit and then source the file
source ~/.bashrc
Aliases
Aliases are a way to rename commands or strings of commands. A common example is ssh; many analysts log into a server on a daily basis:
ssh -X at120@prince.hpc.nyu.edu
This is something I use on a daily basis. It may seem like not too much to type, but it does get annoying doing it every day. To make this more succinct, we can create an alias named something shorter which, when executed, will execute the ssh command. This is done in the .bashrc
file as well. So open it up and add these lines:
# NOTE: you can change the server to any other server you'd like, you do not have to use prince.hpc.nyu.edu
# Also NOTE: you can change $USER to whatever username you use for that server.
# For example, my laptop user is alan, but my hpc user is at120. So for this example I would use at120 instead of $USER
alias prince='ssh -X $USER@prince.hpc.nyu.edu'
source ~/.bashrc
Now you should be able to ssh into the server with just the command prince
.
BUT I STILL NEED TO ENTER A PASSWORD! Is there a way to skip that part, too? Yes.
Password-less SSH
Believe it or not, this is a significantly more secure way to access other computers. However, that’s not the point of this section. In this section we’ll simply show briefly how it works and how to set it up.
The first step is to create a signature for your account, so that when you ssh into a computer it knows who you are and will let you in without prompting for a password. This is called the RSA key. You may already have one; they are stored in the ~/.ssh
folder. If that folder doesn’t exist then create it:
# To make the .ssh directory
mkdir ~/.ssh # only do this if you do not have an .ssh directory in your home directory already!
# Then ls the .ssh directory to check to see if you have an RSA key already. It will be in the id_rsa.pub file. Your directory, if it exists, may look something like this
ls ~/.ssh
config id_rsa id_rsa.orig id_rsa.pem id_rsa.pub id_rsa.pub.orig keras-workshop.pem known_hosts
# If id_rsa.pub exists, then skip the command immediately below.
# If it does not exist, we can create an and RSA key with the following commands
ssh-keygen -t rsa
# Press enter all the way through, even when it prompts for a password!
# To verify, ls the directory and see if id_rsa.pub is now there
ls ~/.ssh
Now what we want to do is give our public RSA key to the server you’re going to access (presumably the one your use to create your alias for!). We can do that with a slightly sophisticated command (but don’t worry, you’ll learn what it means in one of our later posts 😉
# This command appends your signature to the authorized keys file on the server, which is a list of signatures that it recognizes.
# NOTE: You will have to enter your password at this step.
cat ~/.ssh/id_rsa.pub | ssh at120@prince.hpc.nyu.edu ‘cat >> .ssh/authorized_keys’
# Once the transfer is complete, try running your alias
hpc
Hopefully now you can login without being prompted for a password!