2626 */
2727public class SeqLoadMain {
2828 static int projIdx = 0 ;
29+ protected static final int maxPrtErr =2 ;
2930 private static final int CHUNK_SIZE = Constants .CHUNK_SIZE ;
3031 private static final int MAX_GRPS = BlockViewFrame .MAX_GRPS ;
3132 private static final int MAX_COLORS = BlockViewFrame .maxColors ;
33+ private static final int maxLenName = 20 ; // in input/index.html secondary name
34+
3235 private Mproject mProj ;
3336 private DBconn2 tdbc2 =null ;
3437 private ProgressDialog plog ;
@@ -38,6 +41,7 @@ public class SeqLoadMain {
3841 private long modDirDate =0 ;
3942 private int totalnSeqs =0 , cntFile =0 , totalSeqIgnore =0 , totalBasesWritten =0 ;
4043 private Vector <String > grpList = new Vector <String >();
44+ private int fileType =0 , iNCBI =1 , iEns =2 ;
4145
4246 protected boolean run (DBconn2 dbc2 , ProgressDialog plog , Mproject mProj ) throws Exception {
4347 try {
@@ -174,6 +178,8 @@ else if (f.isDirectory()) {
174178 */
175179 private boolean loadSeqFiles () {
176180 try {
181+ tdbc2 .tableCheckAddColumn ("xgroups" , "labname" , "VARCHAR(40) NOT NULL" , "fullname" ); // CAS582 backwards
182+
177183 String prefix = mProj .getGrpPrefix ();
178184 if (prefix .contentEquals ("" )) plog .msg (" No sequence prefix supplied (See Parameters - Group prefix)" );
179185 else plog .msg (" Load sequences with '" + prefix + "' prefix (See Parameters - Group prefix)" );
@@ -193,13 +199,19 @@ private boolean loadSeqFiles() {
193199 plog .msg ("Reading " + f .getName ());
194200
195201 StringBuffer curSeq = new StringBuffer ();
196- String grpName = null , grpFullName = null , firstTok = null , line ;
202+ String grpName = null , grpFullName = null , grpLabName = null , line ;
197203
198204 BufferedReader fh = Utils .openGZIP (f .getAbsolutePath ());
199-
205+
200206 while ((line = fh .readLine ()) != null ) {
201207
202- if (line .startsWith ("#" ) || line .isEmpty ()) continue ;
208+ if (line .startsWith ("#" ) || line .isEmpty ()) {
209+ if (line .contains ("Written by SyMAP" )) {
210+ if (line .contains ("ConvertNCBI" )) fileType =iNCBI ;
211+ else if (line .contains ("ConvertEnsembl" )) fileType =iEns ;
212+ }
213+ continue ;
214+ }
203215
204216 if (line .startsWith (">" )) {
205217 if (grpName != null && curSeq .length () >= minSize ) {
@@ -215,36 +227,36 @@ private boolean loadSeqFiles() {
215227 String chrSeq = curSeq .toString (); // prevent two copies in memory at once
216228 curSeq .setLength (0 );
217229
218- uploadSequence (grpName ,grpFullName ,chrSeq ,f .getName (),totalnSeqs +1 );
230+ uploadSequence (grpName ,grpFullName , grpLabName , chrSeq ,f .getName (),totalnSeqs +1 );
219231
220232 chrSeq = null ;
221233 System .gc ();
222234 }
223235 else {
224- plog .msgToFile ("+++ Dup seqid: " + grpName + " (" + firstTok + ") skipping..." );
236+ plog .msgToFile ("+++ Dup seqid: " + grpName + " skipping..." );
225237 }
226238 } else seqIgnore ++;
227239
228240 readSeq ++;
229241 if (readSeq %100 == 0 ) Globals .rprt ("Read sequences " + readSeq );
230242
231- grpName = null ; firstTok = grpFullName = "" ;
243+ grpName = null ; grpFullName = "" ;
232244 curSeq .setLength (0 );
233245
234- if (!parseHasPrefix (line , prefix )){
246+ grpName = parse1stId (line , prefix );
247+ if (grpName == null ){
235248 seqPrefixIgnore ++;
236- if (seqPrefixIgnore <=3 ) plog .msgToFile ("+++ Invalid prefix, ignore: " + line );
237- if (seqPrefixIgnore ==3 ) plog .msgToFile ("+++ Surpressing further invalid prefix " );
249+ if (seqPrefixIgnore <=maxPrtErr ) plog .msgToFile ("+++ Invalid prefix, ignore: " + line );
250+ if (seqPrefixIgnore ==maxPrtErr ) plog .msgToFile ("+++ Surpressing further invalid prefix " );
238251 continue ;
239252 }
240253
241- grpName = Utils .parseGrpName (line ,prefix );
242- grpFullName = Utils .parseGrpFullName (line );
254+ grpFullName = parse1stId (line , "" );
255+ grpLabName = parse2ndName (line , grpFullName );
256+
243257 if (grpName ==null || grpFullName ==null || grpName .equals ("" ) || grpFullName .equals ("" )){
244258 return rtError ("Unable to parse group name from:" + line );
245259 }
246- String [] tl = line .split (" " );
247- firstTok = (tl .length >0 ) ? tl [0 ] : Utils .parseGrpFullName (line );
248260 }
249261 else if (grpName !=null ) { // sequence for valid grpName
250262 line = line .replaceAll ("\\ s+" ,"" );
@@ -271,7 +283,7 @@ else if (grpName!=null) { // sequence for valid grpName
271283
272284 String chrSeq = curSeq .toString ();
273285 curSeq .setLength (0 );
274- uploadSequence (grpName , grpFullName , chrSeq , f .getName (), totalnSeqs +1 );
286+ uploadSequence (grpName , grpFullName , grpLabName , chrSeq , f .getName (), totalnSeqs +1 );
275287 chrSeq ="" ;
276288 }
277289 else if (curSeq .length ()>0 ) seqIgnore ++;
@@ -301,24 +313,97 @@ else if (grpName!=null) { // sequence for valid grpName
301313 return false ;
302314 }
303315 }
304- private boolean parseHasPrefix (String name , String prefix ){
305- if (prefix .equals ("" )) return true ;
306316
307- String regx = "\\ s*(" + prefix + ")(\\ w+)\\ s?.*" ;
308- Pattern pat = Pattern .compile (regx ,Pattern .CASE_INSENSITIVE );
317+ // Prefix - if no prefix, return null
318+ // Prefix - remove and return what is after it
319+ // No prefix - return 1st word after >
320+ private String parse1stId (String lineIn , String prefix ) {
321+ String line = lineIn ;
322+ if (line .startsWith (">" )) line = line .substring (1 , line .length ());
323+
324+ line = line .trim ();
325+ if (line .isBlank ()) return null ;
309326
310- String n = name + " " ; // hack, otherwise we need two cases in the regex
311- if (n .startsWith (">" )) n = n .substring (1 );
312- Matcher m = pat .matcher (n );
313- if (m .matches ()) return true ;
314- return false ;
327+ if (prefix !=null && !prefix .isBlank ()) {
328+ if (line .toLowerCase ().startsWith (prefix .toLowerCase ())) {
329+ line = line .substring (prefix .length (), line .length ());
330+ }
331+ else return null ;
332+
333+ }
334+ String id = line .split ("\\ s+" )[0 ];
335+ if (id .length ()>maxLenName ) {
336+ plog .msgToFile ("+++ Invalid Sequence Id; must be <= " + maxLenName + " characters, ignore: " + line );
337+ return null ;
338+ }
339+
340+ String regex = "^[a-zA-Z0-9-_.]*$" ;
341+ Pattern pat = Pattern .compile (regex ,Pattern .CASE_INSENSITIVE );
342+ Matcher m = pat .matcher (id );
343+ if (m .matches ()) return id ;
344+ return null ;
345+ }
346+ // CAS582 find secondary name (labname)
347+ // >Chr1 NC0000.1 chromosome; xToSymap produces headers with 3 words
348+ // if not 3 words, check for NCBI header
349+ private String parse2ndName (String line , String mainId ) {
350+ String [] words = line .split ("\\ s+" );
351+ int len = words .length ;
352+
353+ if (len >1 && len <4 && words [1 ].length ()<=maxLenName ) {
354+ if (fileType ==iEns && mainId .endsWith (words [1 ])) return "" ;
355+
356+ if (words [1 ].equals (mainId )) return "" ;
357+
358+ return words [1 ];
359+ }
360+
361+ // NCBI Only Make the name the chromosome number
362+ // >BK006935.2 TPA_inf: Saccharomyces cerevisiae S288C chromosome I, complete sequence
363+ for (int i =0 ; i <words .length ; i ++) {
364+ String prefix =null ;
365+ if (words [i ].equalsIgnoreCase ("chromosome" )) prefix = "Chr" ;
366+ else if (words [i ].equalsIgnoreCase ("scaffold" )) prefix = "Scaf" ;
367+ else if (words [i ].equalsIgnoreCase ("contig" )) prefix = "Ctg" ;
368+ else if (words [i ].equalsIgnoreCase ("linkage" )) prefix = "Lg" ;
369+
370+ if (prefix !=null ) {
371+ if (i +1 < words .length && words [i +1 ].length ()<=maxLenName ) {
372+ String n = words [i +1 ];
373+ if (n .contains ("," )) n = n .replace ("," , "" );
374+
375+ try {
376+ int d = Integer .parseInt (n );
377+ if (d <10 ) return String .format ("%s%02d" , prefix , d );
378+ else return prefix + n ;
379+ }
380+ catch (Exception e ) {}
381+
382+ if (n .matches ("^[XYIV]*$" )) return prefix + n ;
383+
384+ if (n .matches ("^[a-zA-Z0-9-_.]*$" )) return n ;
385+ }
386+ return prefix ;
387+ }
388+ else {
389+ String w = words [i ];
390+ if (w .contains ("," )) w = w .replace ("," , "" );
391+ if (w .equalsIgnoreCase ("mitochondrion" ) || w .equalsIgnoreCase ("mitochondrial" )) return "Mt" ;
392+ if (w .equalsIgnoreCase ("chloroplast" )) return "Cp" ;
393+ if (w .equals ("plastid" )) return "Pt" ;
394+ }
395+ }
396+ return "" ;
315397 }
398+
316399 /***********************************************************************/
317- private void uploadSequence (String grp , String fullname , String seq , String file , int order ) {
400+ private void uploadSequence (String grpname , String fullname , String labname , String seq , String file , int order ) {
318401 try {
319402 // First, create the group; FIXME check for duplicates
320- tdbc2 .executeUpdate ("INSERT INTO xgroups VALUES('0','" + projIdx + "','" +
321- grp + "','" + fullname + "'," + order + ",'0')" );
403+ tdbc2 .executeUpdate ("INSERT INTO xgroups VALUES('0','" +
404+ projIdx + "','" + grpname + "','" + fullname + "','" +
405+ labname + "'," + order + ",'0')" ); // CAS582 add labname
406+
322407 String sql = "select max(idx) as maxidx from xgroups where proj_idx=" + projIdx ;
323408 ResultSet rs = tdbc2 .executeQuery (sql );
324409 rs .next ();
@@ -347,7 +432,7 @@ private void uploadSequence(String grp, String fullname, String seq, String file
347432 }
348433 }
349434 catch (Exception e ) {
350- ErrorReport .print (e , "Fail load sequence: " + projIdx + "','" + grp + "','" + fullname + "'," + order );
435+ ErrorReport .print (e , "Fail load sequence: " + projIdx + "','" + grpname + "','" + fullname + "'," + order );
351436 Globals .prt (" This can happen if a project was just removed and another added immediately." );
352437 }
353438 }
0 commit comments