<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 This section shows how to build a supermatrix by providing minimal requirements for gene content per taxon (OTU). This approach is more suited for small scale analysis, because it relies on manual decisions, whereas large scale suprematrices are better constructed with the parameter space and data explorations tools of ReproPhylo. However, these are not addressed in this section. First, lets load our Project with the trimmed alignments
Step1:  3.8.1 Sorting out the metadata
The main decision to make when building a supermatrix is what metadata will be used to indicate that sequences of several genes belong to the same OTU in the tree. Obvious candidates would be the species name (stored as 'source_organism' if we read a GenBank file), or sample ID, voucher specimen and so on. Often, we would be required to modify the metadata in our Project, in a way that will correctly reflect the relationship between sequences that emerged from the same sample.  
In the case of the Tetillidae.gb example file, sample IDs are stored either under 'source_specimen_voucher' or 'source_isolate'. In addition, identical voucher numbers are sometimes formatted differently for different genes.  
In the file 'data/Tetillida_otus_corrected.csv', I have unified the columns 'source_specimen_voucher' and 'source_isolate' in a single column called 'source_otu' and also made sure to uniformly format all the voucher specimens
Step2:  Our Project has to be updated with the recent changes to the spreadsheet
Step3:  Such fixes can also be done programmatically (see section 3.4)
3.8.2 Designing the supermatrix
Supermatrices are configured with objects of the class Concatenation. In a Concatenation object we can indicate the following
Step4:  If we print this Concatenation object we get this message
Step5:  3.8.3 Building the supermatrix
Building the suprematrix has two steps. First we need to mount the Concatenation object onto the Project where it will be stored in the list pj.concatenations. Second, we need to construct the MultipleSeqAlignment object, which will be stored in the pj.trimmed_alignments dictionary, under the key 'large_concat' in this case
Step6:  Now that this supermatrix is stored as a trimmed alignment in the pj.trimmed_alignments dictionary, we can write it to a file or fetch the MultipleSeqAlignment object, as shown in section 3.7.
3.8.4 Quick reference
<ASSISTANT_TASK:>
Python Code:
from reprophylo import *
pj = unpickle_pj('outputs/my_project.pkpj', git=False)
Explanation: This section shows how to build a supermatrix by providing minimal requirements for gene content per taxon (OTU). This approach is more suited for small scale analysis, because it relies on manual decisions, whereas large scale suprematrices are better constructed with the parameter space and data explorations tools of ReproPhylo. However, these are not addressed in this section. First, lets load our Project with the trimmed alignments:
End of explanation
from IPython.display import Image
Image('images/fix_otus.png', width = 400)
Explanation: 3.8.1 Sorting out the metadata
The main decision to make when building a supermatrix is what metadata will be used to indicate that sequences of several genes belong to the same OTU in the tree. Obvious candidates would be the species name (stored as 'source_organism' if we read a GenBank file), or sample ID, voucher specimen and so on. Often, we would be required to modify the metadata in our Project, in a way that will correctly reflect the relationship between sequences that emerged from the same sample.  
In the case of the Tetillidae.gb example file, sample IDs are stored either under 'source_specimen_voucher' or 'source_isolate'. In addition, identical voucher numbers are sometimes formatted differently for different genes.  
In the file 'data/Tetillida_otus_corrected.csv', I have unified the columns 'source_specimen_voucher' and 'source_isolate' in a single column called 'source_otu' and also made sure to uniformly format all the voucher specimens:
End of explanation
pj.correct_metadata_from_file('data/Tetillida_otus_corrected.csv')
Explanation: Our Project has to be updated with the recent changes to the spreadsheet:
End of explanation
concat = Concatenation('large_concat',                       # Any unique string
                       
                       pj.loci,                              # This is a list of Locus objects
                       
                       'source_otu',                         # The values of this qualifier 
                                                             # flag sequences the belong to the same
                                                             # sample
                                                    
                       otu_must_have_all_of=['MT-CO1'],      # All the OTUS must have a cox1 sequence
                       
                       otu_must_have_one_of=[['18s','28s']], # All the OTUs must have either 18s or 28s or both
                       
                       define_trimmed_alns=[]                # We only have one alignment per gene
                                                             # so the list is empty (default value)
                      )
Explanation: Such fixes can also be done programmatically (see section 3.4)
3.8.2 Designing the supermatrix
Supermatrices are configured with objects of the class Concatenation. In a Concatenation object we can indicate the following:
The name of the concatenation
The loci it includes (here we pass locus objects rather than just Locus names)
The qualifier or metadata that stores the relationships among the records
What loci all the OTUs must have
Groups of loci from which each OTU must have at least one
Which trimmed alignment to use, if we have more than one for each locus in our Project
Here is an example:
End of explanation
print concat
Explanation: If we print this Concatenation object we get this message:
End of explanation
pj.add_concatenation(concat)
pj.make_concatenation_alignments()
pickle_pj(pj, 'outputs/my_project.pkpj')
Explanation: 3.8.3 Building the supermatrix
Building the suprematrix has two steps. First we need to mount the Concatenation object onto the Project where it will be stored in the list pj.concatenations. Second, we need to construct the MultipleSeqAlignment object, which will be stored in the pj.trimmed_alignments dictionary, under the key 'large_concat' in this case:
End of explanation
# Design a supermatrix
concat = Concatenation('concat_name', loci_list, 'otu_qualifier' **kwargs)
# Add it to a project
pj.add_concatenation(concat)
# Build supermatrices based on the Concatenation
# objects in pj.concatenations
pj.make_concatenation_alignments()
Explanation: Now that this supermatrix is stored as a trimmed alignment in the pj.trimmed_alignments dictionary, we can write it to a file or fetch the MultipleSeqAlignment object, as shown in section 3.7.
3.8.4 Quick reference
End of explanation
<END_TASK>