Changeset 55


Ignore:
Timestamp:
Apr 21, 2011, 4:04:44 PM (12 years ago)
Author:
robert@…
Message:

Adjusted artificial tag creation in order to avoid homopolymers (#37)

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/nl/tno/massSequencing/SampleController.groovy

    r52 r55  
    9999                tokens.each { token ->
    100100                        sample = Sample.findBySampleToken( token );
    101                         if( sample.study.canRead( session.user ) ) {
     101                        if( sample?.study?.canRead( session.user ) ) {
    102102                                if( sample && sample.assaySamples )
    103103                                        assaySamples += sample.assaySamples;
     
    106106                ids.each { id ->
    107107                        sample = Sample.get( id );
    108                         if( sample.study.canRead( session.user ) ) {
     108                        if( sample?.study?.canRead( session.user ) ) {
    109109                                if( sample && sample.assaySamples )
    110110                                        assaySamples += sample.assaySamples;
  • trunk/grails-app/services/nl/tno/massSequencing/FastaService.groovy

    r54 r55  
    624624                // The minimum number of characters used is 10, to ensure the correct working of the other
    625625                // programs.
    626                 int tagLength = Math.max( 10, Math.ceil( Math.log( assaySamples.size() ) / Math.log( 4 ) ) );
     626                int tagLength = computeTagLength( assaySamples.size() )
    627627                int tagNumber = 0;
    628628
     
    800800                def numChars = chars.size();
    801801
    802                 if( tagNumber > numChars ** length )
     802                // Determine whether the tagNumber is not too high
     803                if( tagNumber > numChars * ( ( numChars - 1 ) ** ( length - 1 ) ) )
    803804                        throw new Exception( "Given tag number (" + tagNumber + ") is too large for the specified length (" + length + ")")
    804805
    805806                String tag = "";
    806807
     808                // Loop through all characters
    807809                for( def i = 0; i < length; i++ ) {
    808                         int currentChar = tagNumber % numChars
    809 
    810                         // Append the new character to the end of the tag, to ensure that the first part of the tag is
    811                         // the most volatile. This way it is easy to find the end of the tag and the beginning of the real
    812                         // sequence on first sight.
    813                         tag = tag + chars[ currentChar ];
    814 
    815                         tagNumber = Math.floor( tagNumber / numChars );
     810                        def currentChars
     811                       
     812                        // Make sure the characters to choose from are different from
     813                        // the previous character
     814                        if( i == 0 )
     815                                currentChars = chars;
     816                        else
     817                                currentChars = chars - tag[ 0 ]
     818                               
     819                        // Determine which character to append by taking the tagNumber modulo the number of characters
     820                        int currentChar = tagNumber % currentChars.size()
     821                        tag = currentChars[ currentChar ] + tag;
     822                       
     823                        tagNumber = Math.floor( tagNumber / currentChars.size() );
    816824                }
    817825
    818826                return tag;
     827        }
     828       
     829        /**
     830         * Computes the number of characters needed for a tag to be unique for the given number of samples
     831         * @param numSamples    Number of samples to generate tags for
     832         * @return                              Number of characters needed for a unique tag
     833         */
     834        public int computeTagLength( int numSamples ) {
     835                // No homopolymers are allowed in the tag. For that reason we have
     836                // 4 possibilities for the first character, and 3 for every next character
     837               
     838                // In order to allow enough characters, we take the 3log(numSamples), with a
     839                // minimum of 10
     840                return Math.max( 10, Math.ceil( Math.log( numSamples ) / Math.log( 3 ) ) );
    819841        }
    820842
  • trunk/test/unit/nl/tno/massSequencing/FastaServiceTests.groovy

    r42 r55  
    2121    void testCreateTag() {
    2222                // Simple check for uniqueness on different lengths
    23                 for( int length = 1; length <= 5; length++ ) {
     23                for( int length = 1; length <= 6; length++ ) {
    2424                        println "Checking uniqueness on length " + length
    2525                       
    26                         int maxTags = 4 ** length;
     26                        int maxTags = 4 * 3 ** ( length - 1);
    2727                        List tags = []
    2828                        for( int tagNum = 0; tagNum < maxTags; tagNum++ ) {
    2929                                String tag = fastaService.createTag( length, tagNum );
    30                                 assert tag.size() == length;
    3130                                tags << tag;
    3231                        }
Note: See TracChangeset for help on using the changeset viewer.